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 (fset 'char-int 'identity))
34 (defvar 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.")
39 (defvar uudecode-decoder-switches nil
40 "*List of command line flags passed to the command named by uudecode-decoder-program.")
42 (defconst uudecode-alphabet "\040-\140")
44 (defconst uudecode-begin-line "^begin[ \t]+[0-7][0-7][0-7][ \t]+\\(.*\\)$")
45 (defconst uudecode-end-line "^end[ \t]*$")
47 (defconst uudecode-body-line
48 (let ((i 61) (str "^M"))
49 (while (> (setq i (1- i)) 0)
50 (setq str (concat str "[^a-z]")))
53 (defvar uudecode-temporary-file-directory
54 (cond ((fboundp 'temp-directory) (temp-directory))
55 ((boundp 'temporary-file-directory) temporary-file-directory)
59 (defun uudecode-decode-region-external (start end &optional file-name)
60 "uudecode region between START and END with external decoder.
62 If FILE-NAME is non-nil, save the result to FILE-NAME."
64 (let ((cbuf (current-buffer)) tempfile firstline work-buffer status)
67 (when (re-search-forward uudecode-begin-line nil t)
69 (setq firstline (point))
70 (cond ((null file-name))
73 (setq file-name (read-file-name "File to Name:"
76 (setq tempfile (if file-name
77 (expand-file-name file-name)
81 "uu" uudecode-temporary-file-directory))))
82 (let ((cdir default-directory) default-process-coding-system)
85 (set-buffer (setq work-buffer
86 (generate-new-buffer " *uudecode-work*")))
87 (buffer-disable-undo work-buffer)
88 (insert "begin 600 " (file-name-nondirectory tempfile) "\n")
89 (insert-buffer-substring cbuf firstline end)
90 (cd (file-name-directory tempfile))
91 (apply 'call-process-region
94 uudecode-decoder-program
98 uudecode-decoder-switches))
99 (cd cdir) (set-buffer cbuf)))
100 (if (file-exists-p tempfile)
103 (delete-region start end)
105 (insert-file-contents-literally tempfile)))
106 (message "Can not uudecode")))
107 (and work-buffer (kill-buffer work-buffer))
108 (ignore-errors (or file-name (delete-file tempfile))))))
110 (if (string-match "XEmacs" emacs-version)
111 (defalias 'uudecode-insert-char 'insert-char)
112 (defun uudecode-insert-char (char &optional count ignored buffer)
113 (if (or (null buffer) (eq buffer (current-buffer)))
114 (insert-char char count)
115 (with-current-buffer buffer
116 (insert-char char count)))))
120 (defun uudecode-decode-region (start end &optional file-name)
121 "uudecode region between START and END.
122 If FILE-NAME is non-nil, save the result to FILE-NAME."
124 (let ((work-buffer nil)
130 (non-data-chars (concat "^" uudecode-alphabet)))
134 (when (re-search-forward uudecode-begin-line nil t)
135 (cond ((null file-name))
136 ((stringp file-name))
138 (setq file-name (expand-file-name
139 (read-file-name "File to Name:"
141 (match-string 1))))))
142 (setq work-buffer (generate-new-buffer " *uudecode-work*"))
143 (buffer-disable-undo work-buffer)
145 (skip-chars-forward non-data-chars end)
147 (setq inputpos (point))
148 (setq remain 0 bits 0 counter 0)
150 ((> (skip-chars-forward uudecode-alphabet end) 0)
153 (logand (- (char-int (char-after inputpos)) 32) 63))
154 (setq inputpos (1+ inputpos))
155 (if (= remain 0) (setq done t))
156 (while (and (< inputpos lim) (> remain 0))
160 (char-int (char-after inputpos)) 32) 63)))
161 (if (/= counter 0) (setq remain (1- remain)))
162 (setq counter (1+ counter)
163 inputpos (1+ inputpos))
165 (uudecode-insert-char
166 (lsh bits -16) 1 nil work-buffer)
167 (uudecode-insert-char
168 (logand (lsh bits -8) 255) 1 nil work-buffer)
169 (uudecode-insert-char (logand bits 255) 1 nil
171 (setq bits 0 counter 0))
172 (t (setq bits (lsh bits 6)))))))
176 (error "uucode line ends unexpectly")
178 ((and (= (point) end) (not done))
179 ;;(error "uucode ends unexpectly")
182 (uudecode-insert-char (logand (lsh bits -16) 255) 1 nil
184 (uudecode-insert-char (logand (lsh bits -8) 255) 1 nil
187 (uudecode-insert-char (logand (lsh bits -10) 255) 1 nil
189 (skip-chars-forward non-data-chars end))
192 (set-buffer work-buffer)
193 (write-file file-name))
194 (or (markerp end) (setq end (set-marker (make-marker) end)))
196 (insert-buffer-substring work-buffer)
197 (delete-region (point) end))))
198 (and work-buffer (kill-buffer work-buffer)))))
202 ;;; uudecode.el ends here