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 "/tmp/")
58 (defun uudecode-decode-region-external (start end &optional file-name)
59 "uudecode region between START and END with external decoder.
61 If FILE-NAME is non-nil, save the result to FILE-NAME."
63 (let ((cbuf (current-buffer)) tempfile firstline work-buffer status)
66 (when (re-search-forward uudecode-begin-line nil t)
68 (setq firstline (point))
69 (cond ((null file-name))
72 (setq file-name (read-file-name "File to Name:"
75 (setq tempfile (expand-file-name
76 (or file-name (concat uudecode-temporary-file-directory
77 (make-temp-name "uu")))))
78 (let ((cdir default-directory) default-process-coding-system)
81 (set-buffer (setq work-buffer
82 (generate-new-buffer " *uudecode-work*")))
83 (buffer-disable-undo work-buffer)
84 (insert "begin 600 " (file-name-nondirectory tempfile) "\n")
85 (insert-buffer-substring cbuf firstline end)
86 (cd (file-name-directory tempfile))
87 (apply 'call-process-region
90 uudecode-decoder-program
94 uudecode-decoder-switches))
95 (cd cdir) (set-buffer cbuf)))
96 (if (file-exists-p tempfile)
99 (delete-region start end)
101 (insert-file-contents-literally tempfile)))
102 (message "Can not uudecode")))
103 (and work-buffer (kill-buffer work-buffer))
105 (or file-name (delete-file tempfile))
109 (defun uudecode-insert-char (char &optional count ignored buffer)
112 (insert-char char count ignored buffer)
113 (fset 'uudecode-insert-char 'insert-char))
114 (wrong-number-of-arguments
115 (fset 'uudecode-insert-char 'uudecode-xemacs-insert-char)
116 (uudecode-insert-char char count ignored buffer))))
118 (defun uudecode-xemacs-insert-char (char &optional count ignored buffer)
119 (if (or (null buffer) (eq buffer (current-buffer)))
120 (insert-char char count)
123 (insert-char char count))))
127 (defun uudecode-decode-region (start end &optional file-name)
128 "uudecode region between START and END.
129 If FILE-NAME is non-nil, save the result to FILE-NAME."
131 (let ((work-buffer nil)
137 (non-data-chars (concat "^" uudecode-alphabet)))
141 (when (re-search-forward uudecode-begin-line nil t)
142 (cond ((null file-name))
143 ((stringp file-name))
145 (setq file-name (expand-file-name
146 (read-file-name "File to Name:"
148 (match-string 1))))))
149 (setq work-buffer (generate-new-buffer " *uudecode-work*"))
150 (buffer-disable-undo work-buffer)
152 (skip-chars-forward non-data-chars end)
154 (setq inputpos (point))
155 (setq remain 0 bits 0 counter 0)
157 ((> (skip-chars-forward uudecode-alphabet end) 0)
160 (logand (- (char-int (char-after inputpos)) 32) 63))
161 (setq inputpos (1+ inputpos))
162 (if (= remain 0) (setq done t))
163 (while (and (< inputpos lim) (> remain 0))
167 (char-int (char-after inputpos)) 32) 63)))
168 (if (/= counter 0) (setq remain (1- remain)))
169 (setq counter (1+ counter)
170 inputpos (1+ inputpos))
172 (uudecode-insert-char
173 (lsh bits -16) 1 nil work-buffer)
174 (uudecode-insert-char
175 (logand (lsh bits -8) 255) 1 nil work-buffer)
176 (uudecode-insert-char (logand bits 255) 1 nil
178 (setq bits 0 counter 0))
179 (t (setq bits (lsh bits 6)))))))
183 (error "uucode line ends unexpectly")
185 ((and (= (point) end) (not done))
186 ;(error "uucode ends unexpectly")
189 (uudecode-insert-char (logand (lsh bits -16) 255) 1 nil
191 (uudecode-insert-char (logand (lsh bits -8) 255) 1 nil
194 (uudecode-insert-char (logand (lsh bits -10) 255) 1 nil
196 (skip-chars-forward non-data-chars end))
199 (set-buffer work-buffer)
200 (write-file file-name))
201 (or (markerp end) (setq end (set-marker (make-marker) end)))
203 (insert-buffer-substring work-buffer)
204 (delete-region (point) end))))
205 (and work-buffer (kill-buffer work-buffer)))))
209 ;;; uudecode.el ends here