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 (eval-when-compile (require 'cl))
32 (if (not (fboundp 'char-int))
33 (defalias 'char-int 'identity))
35 (defcustom uudecode-decoder-program "uudecode"
36 "*Non-nil value should be a string that names a uu decoder.
37 The program should expect to read uu data on its standard
38 input and write the converted data to its standard output."
42 (defcustom uudecode-decoder-switches nil
43 "*List of command line flags passed to `uudecode-decoder-program'."
45 :type '(repeat string))
47 (defconst uudecode-alphabet "\040-\140")
49 (defconst uudecode-begin-line "^begin[ \t]+[0-7][0-7][0-7][ \t]+\\(.*\\)$")
50 (defconst uudecode-end-line "^end[ \t]*$")
52 (defconst uudecode-body-line
53 (let ((i 61) (str "^M"))
54 (while (> (setq i (1- i)) 0)
55 (setq str (concat str "[^a-z]")))
58 (defvar uudecode-temporary-file-directory
59 (cond ((fboundp 'temp-directory) (temp-directory))
60 ((boundp 'temporary-file-directory) temporary-file-directory)
64 (defun uudecode-decode-region-external (start end &optional file-name)
65 "Uudecode region between START and END with external decoder.
67 If FILE-NAME is non-nil, save the result to FILE-NAME."
69 (let ((cbuf (current-buffer)) tempfile firstline work-buffer status)
72 (when (re-search-forward uudecode-begin-line nil t)
74 (setq firstline (point))
75 (cond ((null file-name))
78 (setq file-name (read-file-name "File to Name:"
81 (setq tempfile (if file-name
82 (expand-file-name file-name)
86 "uu" uudecode-temporary-file-directory))))
87 (let ((cdir default-directory) default-process-coding-system)
90 (set-buffer (setq work-buffer
91 (generate-new-buffer " *uudecode-work*")))
92 (buffer-disable-undo work-buffer)
93 (insert "begin 600 " (file-name-nondirectory tempfile) "\n")
94 (insert-buffer-substring cbuf firstline end)
95 (cd (file-name-directory tempfile))
96 (apply 'call-process-region
99 uudecode-decoder-program
103 uudecode-decoder-switches))
104 (cd cdir) (set-buffer cbuf)))
105 (if (file-exists-p tempfile)
108 (delete-region start end)
110 (insert-file-contents-literally tempfile)))
111 (message "Can not uudecode")))
112 (and work-buffer (kill-buffer work-buffer))
113 (ignore-errors (or file-name (delete-file tempfile))))))
115 (if (featurep 'xemacs)
116 (defalias 'uudecode-insert-char 'insert-char)
117 (defun uudecode-insert-char (char &optional count ignored buffer)
118 (if (or (null buffer) (eq buffer (current-buffer)))
119 (insert-char char count)
120 (with-current-buffer buffer
121 (insert-char char count)))))
125 (defun uudecode-decode-region (start end &optional file-name)
126 "Uudecode region between START and END.
127 If FILE-NAME is non-nil, save the result to FILE-NAME."
129 (let ((work-buffer nil)
135 (non-data-chars (concat "^" uudecode-alphabet)))
139 (when (re-search-forward uudecode-begin-line nil t)
140 (cond ((null file-name))
141 ((stringp file-name))
143 (setq file-name (expand-file-name
144 (read-file-name "File to Name:"
146 (match-string 1))))))
147 (setq work-buffer (generate-new-buffer " *uudecode-work*"))
148 (buffer-disable-undo work-buffer)
150 (skip-chars-forward non-data-chars end)
152 (setq inputpos (point))
153 (setq remain 0 bits 0 counter 0)
155 ((> (skip-chars-forward uudecode-alphabet end) 0)
158 (logand (- (char-int (char-after inputpos)) 32) 63))
159 (setq inputpos (1+ inputpos))
160 (if (= remain 0) (setq done t))
161 (while (and (< inputpos lim) (> remain 0))
165 (char-int (char-after inputpos)) 32) 63)))
166 (if (/= counter 0) (setq remain (1- remain)))
167 (setq counter (1+ counter)
168 inputpos (1+ inputpos))
170 (uudecode-insert-char
171 (lsh bits -16) 1 nil work-buffer)
172 (uudecode-insert-char
173 (logand (lsh bits -8) 255) 1 nil work-buffer)
174 (uudecode-insert-char (logand bits 255) 1 nil
176 (setq bits 0 counter 0))
177 (t (setq bits (lsh bits 6)))))))
181 (error "uucode line ends unexpectly")
183 ((and (= (point) end) (not done))
184 ;;(error "uucode ends unexpectly")
187 (uudecode-insert-char (logand (lsh bits -16) 255) 1 nil
189 (uudecode-insert-char (logand (lsh bits -8) 255) 1 nil
192 (uudecode-insert-char (logand (lsh bits -10) 255) 1 nil
194 (skip-chars-forward non-data-chars end))
197 (set-buffer work-buffer)
198 (write-file file-name))
199 (or (markerp end) (setq end (set-marker (make-marker) end)))
201 (insert-buffer-substring work-buffer)
202 (delete-region (point) end))))
203 (and work-buffer (kill-buffer work-buffer)))))
207 ;;; uudecode.el ends here