1 ;;; uudecode.el -- elisp native uudecode
2 ;; Copyright (c) 1998 by Shenghuo Zhu <zsh@cs.rochester.edu>
4 ;; Author: Shenghuo Zhu <zsh@cs.rochester.edu>
8 ;; This file is not part of GNU Emacs, but the same permissions
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 2, or (at your option)
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 ;; Boston, MA 02111-1307, USA.
28 ;; Lots of codes are stolen from mm-decode.el, gnus-uu.el and
33 (if (not (fboundp 'char-int))
34 (fset 'char-int 'identity))
36 (defvar uudecode-decoder-program "uudecode"
37 "*Non-nil value should be a string that names a uu decoder.
38 The program should expect to read uu data on its standard
39 input and write the converted data to its standard output.")
41 (defvar uudecode-decoder-switches nil
42 "*List of command line flags passed to the command named by uudecode-decoder-program.")
44 (defconst uudecode-alphabet "\040-\140")
46 (defconst uudecode-begin-line "^begin[ \t]+[0-7][0-7][0-7][ \t]+\\(.*\\)$")
47 (defconst uudecode-end-line "^end[ \t]*$")
49 (defconst uudecode-body-line
50 (let ((i 61) (str "^M"))
51 (while (> (setq i (1- i)) 0)
52 (setq str (concat str "[^a-z]")))
55 (defvar uudecode-temporary-file-directory
56 (cond ((fboundp 'temp-directory) (temp-directory))
57 ((boundp 'temporary-file-directory) temporary-file-directory)
61 (defun uudecode-decode-region-external (start end &optional file-name)
62 "uudecode region between START and END with external decoder.
64 If FILE-NAME is non-nil, save the result to FILE-NAME."
66 (let ((cbuf (current-buffer)) tempfile firstline work-buffer status)
69 (when (re-search-forward uudecode-begin-line nil t)
71 (setq firstline (point))
72 (cond ((null file-name))
75 (setq file-name (read-file-name "File to Name:"
78 (setq tempfile (expand-file-name
79 (or file-name (concat uudecode-temporary-file-directory
80 (make-temp-name "uu")))))
81 (let ((cdir default-directory) default-process-coding-system)
84 (set-buffer (setq work-buffer
85 (generate-new-buffer " *uudecode-work*")))
86 (buffer-disable-undo work-buffer)
87 (insert "begin 600 " (file-name-nondirectory tempfile) "\n")
88 (insert-buffer-substring cbuf firstline end)
89 (cd (file-name-directory tempfile))
90 (apply 'call-process-region
93 uudecode-decoder-program
97 uudecode-decoder-switches))
98 (cd cdir) (set-buffer cbuf)))
99 (if (file-exists-p tempfile)
102 (delete-region start end)
104 (insert-file-contents-literally tempfile)))
105 (message "Can not uudecode")))
106 (and work-buffer (kill-buffer work-buffer))
107 (ignore-errors (or file-name (delete-file tempfile))))))
109 (if (string-match "XEmacs" emacs-version)
110 (defalias 'uudecode-insert-char 'insert-char)
111 (defun uudecode-insert-char (char &optional count ignored buffer)
112 (if (or (null buffer) (eq buffer (current-buffer)))
113 (insert-char char count)
114 (with-current-buffer buffer
115 (insert-char char count)))))
119 (defun uudecode-decode-region (start end &optional file-name)
120 "uudecode region between START and END.
121 If FILE-NAME is non-nil, save the result to FILE-NAME."
123 (let ((work-buffer nil)
129 (non-data-chars (concat "^" uudecode-alphabet)))
133 (when (re-search-forward uudecode-begin-line nil t)
134 (cond ((null file-name))
135 ((stringp file-name))
137 (setq file-name (expand-file-name
138 (read-file-name "File to Name:"
140 (match-string 1))))))
141 (setq work-buffer (generate-new-buffer " *uudecode-work*"))
142 (buffer-disable-undo work-buffer)
144 (skip-chars-forward non-data-chars end)
146 (setq inputpos (point))
147 (setq remain 0 bits 0 counter 0)
149 ((> (skip-chars-forward uudecode-alphabet end) 0)
152 (logand (- (char-int (char-after inputpos)) 32) 63))
153 (setq inputpos (1+ inputpos))
154 (if (= remain 0) (setq done t))
155 (while (and (< inputpos lim) (> remain 0))
159 (char-int (char-after inputpos)) 32) 63)))
160 (if (/= counter 0) (setq remain (1- remain)))
161 (setq counter (1+ counter)
162 inputpos (1+ inputpos))
164 (uudecode-insert-char
165 (lsh bits -16) 1 nil work-buffer)
166 (uudecode-insert-char
167 (logand (lsh bits -8) 255) 1 nil work-buffer)
168 (uudecode-insert-char (logand bits 255) 1 nil
170 (setq bits 0 counter 0))
171 (t (setq bits (lsh bits 6)))))))
175 (error "uucode line ends unexpectly")
177 ((and (= (point) end) (not done))
178 ;(error "uucode ends unexpectly")
181 (uudecode-insert-char (logand (lsh bits -16) 255) 1 nil
183 (uudecode-insert-char (logand (lsh bits -8) 255) 1 nil
186 (uudecode-insert-char (logand (lsh bits -10) 255) 1 nil
188 (skip-chars-forward non-data-chars end))
191 (set-buffer work-buffer)
192 (write-file file-name))
193 (or (markerp end) (setq end (set-marker (make-marker) end)))
195 (insert-buffer-substring work-buffer)
196 (delete-region (point) end))))
197 (and work-buffer (kill-buffer work-buffer)))))
201 ;;; uudecode.el ends here