Fix my last change.
[gnus] / lisp / uudecode.el
1 ;;; uudecode.el -- elisp native uudecode
2 ;; Copyright (c) 1998,99 Free Software Foundation, Inc.
3
4 ;; Author: Shenghuo Zhu <zsh@cs.rochester.edu>
5 ;; Keywords: uudecode news
6
7 ;; This file is a part of GNU Emacs.
8
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)
12 ;; any later version.
13
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.
18
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.
23
24 ;;; Commentary:
25
26 ;;     Lots of codes are stolen from mm-decode.el, gnus-uu.el and
27 ;;     base64.el
28
29 ;;; Code:
30
31 (if (not (fboundp 'char-int))
32     (defalias 'char-int 'identity))
33
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."
38   :type 'string
39   :group 'gnus-extract)
40
41 (defcustom uudecode-decoder-switches nil
42   "*List of command line flags passed to `uudecode-decoder-program'."
43   :group 'gnus-extract
44   :type '(repeat string))
45
46 (defconst uudecode-alphabet "\040-\140")
47
48 (defconst uudecode-begin-line "^begin[ \t]+[0-7][0-7][0-7][ \t]+\\(.*\\)$")
49 (defconst uudecode-end-line "^end[ \t]*$")
50
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]")))
55     (concat str ".?$")))
56
57 (defvar uudecode-temporary-file-directory
58   (cond ((fboundp 'temp-directory) (temp-directory))
59         ((boundp 'temporary-file-directory) temporary-file-directory)
60         ("/tmp")))
61
62 ;;;###autoload
63 (defun uudecode-decode-region-external (start end &optional file-name)
64   "Uudecode region between START and END with external decoder.
65
66 If FILE-NAME is non-nil, save the result to FILE-NAME."
67   (interactive "r\nP")
68   (let ((cbuf (current-buffer)) tempfile firstline work-buffer status)
69     (save-excursion
70       (goto-char start)
71       (when (re-search-forward uudecode-begin-line nil t)
72         (forward-line 1)
73         (setq firstline (point))
74         (cond ((null file-name))
75               ((stringp file-name))
76               (t
77                (setq file-name (read-file-name "File to Name:"
78                                                nil nil nil
79                                                (match-string 1)))))
80         (setq tempfile (if file-name
81                            (expand-file-name file-name)
82                          (make-temp-name
83                           ;; /tmp/uu...
84                           (expand-file-name
85                            "uu" uudecode-temporary-file-directory))))
86         (let ((cdir default-directory) default-process-coding-system)
87           (unwind-protect
88               (progn
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
96                        (point-min)
97                        (point-max)
98                        uudecode-decoder-program
99                        nil
100                        nil
101                        nil
102                        uudecode-decoder-switches))
103             (cd cdir) (set-buffer cbuf)))
104         (if (file-exists-p tempfile)
105             (unless file-name
106               (goto-char start)
107               (delete-region start end)
108               (let (format-alist)
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))))))
113
114 (if (featurep 'xemacs)
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)))))
121
122 ;;;###autoload
123
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."
127   (interactive "r\nP")
128   (let ((work-buffer nil)
129         (done nil)
130         (counter 0)
131         (remain 0)
132         (bits 0)
133         (lim 0) inputpos
134         (non-data-chars (concat "^" uudecode-alphabet)))
135     (unwind-protect
136         (save-excursion
137           (goto-char start)
138           (when (re-search-forward uudecode-begin-line nil t)
139             (cond ((null file-name))
140                   ((stringp file-name))
141                   (t
142                    (setq file-name (expand-file-name
143                                     (read-file-name "File to Name:"
144                                                     nil nil nil
145                                                     (match-string 1))))))
146             (setq work-buffer (generate-new-buffer " *uudecode-work*"))
147             (buffer-disable-undo work-buffer)
148             (forward-line 1)
149             (skip-chars-forward non-data-chars end)
150             (while (not done)
151               (setq inputpos (point))
152               (setq remain 0 bits 0 counter 0)
153               (cond
154                ((> (skip-chars-forward uudecode-alphabet end) 0)
155                 (setq lim (point))
156                 (setq remain
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))
161                   (setq bits (+ bits
162                                 (logand
163                                  (-
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))
168                   (cond ((= counter 4)
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
174                                                work-buffer)
175                          (setq bits 0 counter 0))
176                         (t (setq bits (lsh bits 6)))))))
177               (cond
178                (done)
179                ((> 0 remain)
180                 (error "uucode line ends unexpectly")
181                 (setq done t))
182                ((and (= (point) end) (not done))
183                 ;;(error "uucode ends unexpectly")
184                 (setq done t))
185                ((= counter 3)
186                 (uudecode-insert-char (logand (lsh bits -16) 255) 1 nil
187                                       work-buffer)
188                 (uudecode-insert-char (logand (lsh bits -8) 255) 1 nil
189                                       work-buffer))
190                ((= counter 2)
191                 (uudecode-insert-char (logand (lsh bits -10) 255) 1 nil
192                                       work-buffer)))
193               (skip-chars-forward non-data-chars end))
194             (if file-name
195                 (save-excursion
196                   (set-buffer work-buffer)
197                   (write-file file-name))
198               (or (markerp end) (setq end (set-marker (make-marker) end)))
199               (goto-char start)
200               (insert-buffer-substring work-buffer)
201               (delete-region (point) end))))
202       (and work-buffer (kill-buffer work-buffer)))))
203
204 (provide 'uudecode)
205
206 ;;; uudecode.el ends here