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 (if file-name
79 (expand-file-name file-name)
83 "uu" uudecode-temporary-file-directory))))
84 (let ((cdir default-directory) default-process-coding-system)
87 (set-buffer (setq work-buffer
88 (generate-new-buffer " *uudecode-work*")))
89 (buffer-disable-undo work-buffer)
90 (insert "begin 600 " (file-name-nondirectory tempfile) "\n")
91 (insert-buffer-substring cbuf firstline end)
92 (cd (file-name-directory tempfile))
93 (apply 'call-process-region
96 uudecode-decoder-program
100 uudecode-decoder-switches))
101 (cd cdir) (set-buffer cbuf)))
102 (if (file-exists-p tempfile)
105 (delete-region start end)
107 (insert-file-contents-literally tempfile)))
108 (message "Can not uudecode")))
109 (and work-buffer (kill-buffer work-buffer))
110 (ignore-errors (or file-name (delete-file tempfile))))))
112 (if (string-match "XEmacs" emacs-version)
113 (defalias 'uudecode-insert-char 'insert-char)
114 (defun uudecode-insert-char (char &optional count ignored buffer)
115 (if (or (null buffer) (eq buffer (current-buffer)))
116 (insert-char char count)
117 (with-current-buffer buffer
118 (insert-char char count)))))
122 (defun uudecode-decode-region (start end &optional file-name)
123 "uudecode region between START and END.
124 If FILE-NAME is non-nil, save the result to FILE-NAME."
126 (let ((work-buffer nil)
132 (non-data-chars (concat "^" uudecode-alphabet)))
136 (when (re-search-forward uudecode-begin-line nil t)
137 (cond ((null file-name))
138 ((stringp file-name))
140 (setq file-name (expand-file-name
141 (read-file-name "File to Name:"
143 (match-string 1))))))
144 (setq work-buffer (generate-new-buffer " *uudecode-work*"))
145 (buffer-disable-undo work-buffer)
147 (skip-chars-forward non-data-chars end)
149 (setq inputpos (point))
150 (setq remain 0 bits 0 counter 0)
152 ((> (skip-chars-forward uudecode-alphabet end) 0)
155 (logand (- (char-int (char-after inputpos)) 32) 63))
156 (setq inputpos (1+ inputpos))
157 (if (= remain 0) (setq done t))
158 (while (and (< inputpos lim) (> remain 0))
162 (char-int (char-after inputpos)) 32) 63)))
163 (if (/= counter 0) (setq remain (1- remain)))
164 (setq counter (1+ counter)
165 inputpos (1+ inputpos))
167 (uudecode-insert-char
168 (lsh bits -16) 1 nil work-buffer)
169 (uudecode-insert-char
170 (logand (lsh bits -8) 255) 1 nil work-buffer)
171 (uudecode-insert-char (logand bits 255) 1 nil
173 (setq bits 0 counter 0))
174 (t (setq bits (lsh bits 6)))))))
178 (error "uucode line ends unexpectly")
180 ((and (= (point) end) (not done))
181 ;(error "uucode ends unexpectly")
184 (uudecode-insert-char (logand (lsh bits -16) 255) 1 nil
186 (uudecode-insert-char (logand (lsh bits -8) 255) 1 nil
189 (uudecode-insert-char (logand (lsh bits -10) 255) 1 nil
191 (skip-chars-forward non-data-chars end))
194 (set-buffer work-buffer)
195 (write-file file-name))
196 (or (markerp end) (setq end (set-marker (make-marker) end)))
198 (insert-buffer-substring work-buffer)
199 (delete-region (point) end))))
200 (and work-buffer (kill-buffer work-buffer)))))
204 ;;; uudecode.el ends here