1 ;;; uudecode.el -- elisp native uudecode
3 ;; Copyright (c) 1998, 1999, 2000 Free Software Foundation, Inc.
5 ;; Author: Shenghuo Zhu <zsh@cs.rochester.edu>
6 ;; Keywords: uudecode news
8 ;; This file is a part of GNU Emacs.
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING. If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
27 ;; Lots of codes are stolen from mm-decode.el, gnus-uu.el and
30 ;; This looks as though it could be made rather more efficient for
31 ;; internal working. Encoding could use a lookup table and decoding
32 ;; should presumably use a vector or list buffer for partial results
33 ;; rather than with-current-buffer. -- fx
35 ;; Only `uudecode-decode-region' should be advertised, and whether or
36 ;; not that uses a program should be customizable, but I guess it's
37 ;; too late now. -- fx
41 (eval-when-compile (require 'cl))
44 (defalias 'uudecode-char-int
45 (if (fboundp 'char-int)
49 (if (featurep 'xemacs)
50 (defalias 'uudecode-insert-char 'insert-char)
51 (defun uudecode-insert-char (char &optional count ignored buffer)
52 (if (or (null buffer) (eq buffer (current-buffer)))
53 (insert-char char count)
54 (with-current-buffer buffer
55 (insert-char char count))))))
57 (defcustom uudecode-decoder-program "uudecode"
58 "*Non-nil value should be a string that names a uu decoder.
59 The program should expect to read uu data on its standard
60 input and write the converted data to its standard output."
64 (defcustom uudecode-decoder-switches nil
65 "*List of command line flags passed to `uudecode-decoder-program'."
67 :type '(repeat string))
69 (defconst uudecode-alphabet "\040-\140")
71 (defconst uudecode-begin-line "^begin[ \t]+[0-7][0-7][0-7][ \t]+\\(.*\\)$")
72 (defconst uudecode-end-line "^end[ \t]*$")
74 (defconst uudecode-body-line
75 (let ((i 61) (str "^M"))
76 (while (> (setq i (1- i)) 0)
77 (setq str (concat str "[^a-z]")))
80 (defvar uudecode-temporary-file-directory
81 (cond ((fboundp 'temp-directory) (temp-directory))
82 ((boundp 'temporary-file-directory) temporary-file-directory)
86 (defun uudecode-decode-region-external (start end &optional file-name)
87 "Uudecode region between START and END using external program.
88 If FILE-NAME is non-nil, save the result to FILE-NAME. The program
89 used is specified by `uudecode-decoder-program'."
91 (let ((cbuf (current-buffer)) tempfile firstline status)
94 (when (re-search-forward uudecode-begin-line nil t)
96 (setq firstline (point))
97 (cond ((null file-name))
100 (setq file-name (read-file-name "File to Name:"
103 (setq tempfile (if file-name
104 (expand-file-name file-name)
105 (if (fboundp 'make-temp-file)
106 (let ((temporary-file-directory
107 uudecode-temporary-file-directory))
108 (make-temp-file "uu"))
110 (make-temp-name "uu")
111 uudecode-temporary-file-directory))))
112 (let ((cdir default-directory)
113 default-process-coding-system)
116 (insert "begin 600 " (file-name-nondirectory tempfile) "\n")
117 (insert-buffer-substring cbuf firstline end)
118 (cd (file-name-directory tempfile))
119 (apply 'call-process-region
122 uudecode-decoder-program
126 uudecode-decoder-switches))
127 (cd cdir) (set-buffer cbuf)))
128 (if (file-exists-p tempfile)
131 (delete-region start end)
133 (insert-file-contents-literally tempfile)))
134 (message "Can not uudecode")))
135 (ignore-errors (or file-name (delete-file tempfile))))))
138 (defun uudecode-decode-region (start end &optional file-name)
139 "Uudecode region between START and END without using an external program.
140 If FILE-NAME is non-nil, save the result to FILE-NAME."
142 (let ((work-buffer nil)
148 (non-data-chars (concat "^" uudecode-alphabet)))
152 (when (re-search-forward uudecode-begin-line nil t)
153 (cond ((null file-name))
154 ((stringp file-name))
156 (setq file-name (expand-file-name
157 (read-file-name "File to Name:"
159 (match-string 1))))))
160 (setq work-buffer (generate-new-buffer " *uudecode-work*"))
162 (skip-chars-forward non-data-chars end)
164 (setq inputpos (point))
165 (setq remain 0 bits 0 counter 0)
167 ((> (skip-chars-forward uudecode-alphabet end) 0)
170 (logand (- (uudecode-char-int (char-after inputpos)) 32)
172 (setq inputpos (1+ inputpos))
173 (if (= remain 0) (setq done t))
174 (while (and (< inputpos lim) (> remain 0))
178 (uudecode-char-int (char-after inputpos)) 32)
180 (if (/= counter 0) (setq remain (1- remain)))
181 (setq counter (1+ counter)
182 inputpos (1+ inputpos))
184 (uudecode-insert-char
185 (lsh bits -16) 1 nil work-buffer)
186 (uudecode-insert-char
187 (logand (lsh bits -8) 255) 1 nil work-buffer)
188 (uudecode-insert-char (logand bits 255) 1 nil
190 (setq bits 0 counter 0))
191 (t (setq bits (lsh bits 6)))))))
195 (error "uucode line ends unexpectly")
197 ((and (= (point) end) (not done))
198 ;;(error "uucode ends unexpectly")
201 (uudecode-insert-char (logand (lsh bits -16) 255) 1 nil
203 (uudecode-insert-char (logand (lsh bits -8) 255) 1 nil
206 (uudecode-insert-char (logand (lsh bits -10) 255) 1 nil
208 (skip-chars-forward non-data-chars end))
211 (set-buffer work-buffer)
212 (write-file file-name))
213 (or (markerp end) (setq end (set-marker (make-marker) end)))
215 (insert-buffer-substring work-buffer)
216 (delete-region (point) end))))
217 (and work-buffer (kill-buffer work-buffer)))))
221 ;;; uudecode.el ends here