1 ;;; uudecode.el -- elisp native uudecode
2 ;; Copyright (c) 1998,99 Free Software Foundation, Inc.
4 ;; Author: Shenghuo Zhu <zsh@cs.rochester.edu>
5 ;; Keywords: uudecode news
7 ;; This file is a part of GNU Emacs.
9 ;; GNU Emacs is free software; you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation; either version 2, or (at your option)
14 ;; GNU Emacs is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 ;; GNU General Public License for more details.
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs; see the file COPYING. If not, write to the
21 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22 ;; Boston, MA 02111-1307, USA.
26 ;; Lots of codes are stolen from mm-decode.el, gnus-uu.el and
31 (if (not (fboundp 'char-int))
32 (defalias 'char-int 'identity))
34 (defcustom uudecode-decoder-program "uudecode"
35 "*Non-nil value should be a string that names a uu decoder.
36 The program should expect to read uu data on its standard
37 input and write the converted data to its standard output."
41 (defcustom uudecode-decoder-switches nil
42 "*List of command line flags passed to `uudecode-decoder-program'."
44 :type '(repeat string))
46 (defconst uudecode-alphabet "\040-\140")
48 (defconst uudecode-begin-line "^begin[ \t]+[0-7][0-7][0-7][ \t]+\\(.*\\)$")
49 (defconst uudecode-end-line "^end[ \t]*$")
51 (defconst uudecode-body-line
52 (let ((i 61) (str "^M"))
53 (while (> (setq i (1- i)) 0)
54 (setq str (concat str "[^a-z]")))
57 (defvar uudecode-temporary-file-directory
58 (cond ((fboundp 'temp-directory) (temp-directory))
59 ((boundp 'temporary-file-directory) temporary-file-directory)
63 (defun uudecode-decode-region-external (start end &optional file-name)
64 "Uudecode region between START and END with external decoder.
66 If FILE-NAME is non-nil, save the result to FILE-NAME."
68 (let ((cbuf (current-buffer)) tempfile firstline work-buffer status)
71 (when (re-search-forward uudecode-begin-line nil t)
73 (setq firstline (point))
74 (cond ((null file-name))
77 (setq file-name (read-file-name "File to Name:"
80 (setq tempfile (if file-name
81 (expand-file-name file-name)
85 "uu" uudecode-temporary-file-directory))))
86 (let ((cdir default-directory) default-process-coding-system)
89 (set-buffer (setq work-buffer
90 (generate-new-buffer " *uudecode-work*")))
91 (buffer-disable-undo work-buffer)
92 (insert "begin 600 " (file-name-nondirectory tempfile) "\n")
93 (insert-buffer-substring cbuf firstline end)
94 (cd (file-name-directory tempfile))
95 (apply 'call-process-region
98 uudecode-decoder-program
102 uudecode-decoder-switches))
103 (cd cdir) (set-buffer cbuf)))
104 (if (file-exists-p tempfile)
107 (delete-region start end)
109 (insert-file-contents-literally tempfile)))
110 (message "Can not uudecode")))
111 (and work-buffer (kill-buffer work-buffer))
112 (ignore-errors (or file-name (delete-file tempfile))))))
114 (if (string-match "XEmacs" emacs-version)
115 (defalias 'uudecode-insert-char 'insert-char)
116 (defun uudecode-insert-char (char &optional count ignored buffer)
117 (if (or (null buffer) (eq buffer (current-buffer)))
118 (insert-char char count)
119 (with-current-buffer buffer
120 (insert-char char count)))))
124 (defun uudecode-decode-region (start end &optional file-name)
125 "Uudecode region between START and END.
126 If FILE-NAME is non-nil, save the result to FILE-NAME."
128 (let ((work-buffer nil)
134 (non-data-chars (concat "^" uudecode-alphabet)))
138 (when (re-search-forward uudecode-begin-line nil t)
139 (cond ((null file-name))
140 ((stringp file-name))
142 (setq file-name (expand-file-name
143 (read-file-name "File to Name:"
145 (match-string 1))))))
146 (setq work-buffer (generate-new-buffer " *uudecode-work*"))
147 (buffer-disable-undo work-buffer)
149 (skip-chars-forward non-data-chars end)
151 (setq inputpos (point))
152 (setq remain 0 bits 0 counter 0)
154 ((> (skip-chars-forward uudecode-alphabet end) 0)
157 (logand (- (char-int (char-after inputpos)) 32) 63))
158 (setq inputpos (1+ inputpos))
159 (if (= remain 0) (setq done t))
160 (while (and (< inputpos lim) (> remain 0))
164 (char-int (char-after inputpos)) 32) 63)))
165 (if (/= counter 0) (setq remain (1- remain)))
166 (setq counter (1+ counter)
167 inputpos (1+ inputpos))
169 (uudecode-insert-char
170 (lsh bits -16) 1 nil work-buffer)
171 (uudecode-insert-char
172 (logand (lsh bits -8) 255) 1 nil work-buffer)
173 (uudecode-insert-char (logand bits 255) 1 nil
175 (setq bits 0 counter 0))
176 (t (setq bits (lsh bits 6)))))))
180 (error "uucode line ends unexpectly")
182 ((and (= (point) end) (not done))
183 ;;(error "uucode ends unexpectly")
186 (uudecode-insert-char (logand (lsh bits -16) 255) 1 nil
188 (uudecode-insert-char (logand (lsh bits -8) 255) 1 nil
191 (uudecode-insert-char (logand (lsh bits -10) 255) 1 nil
193 (skip-chars-forward non-data-chars end))
196 (set-buffer work-buffer)
197 (write-file file-name))
198 (or (markerp end) (setq end (set-marker (make-marker) end)))
200 (insert-buffer-substring work-buffer)
201 (delete-region (point) end))))
202 (and work-buffer (kill-buffer work-buffer)))))
206 ;;; uudecode.el ends here