Importing Gnus v5.8.3.
[elisp/gnus.git-] / 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     (fset 'char-int 'identity))
33
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.")
38
39 (defvar uudecode-decoder-switches nil
40   "*List of command line flags passed to the command named by uudecode-decoder-program.")
41
42 (defconst uudecode-alphabet "\040-\140")
43
44 (defconst uudecode-begin-line "^begin[ \t]+[0-7][0-7][0-7][ \t]+\\(.*\\)$")
45 (defconst uudecode-end-line "^end[ \t]*$")
46
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]")))
51     (concat str ".?$")))
52
53 (defvar uudecode-temporary-file-directory
54   (cond ((fboundp 'temp-directory) (temp-directory))
55         ((boundp 'temporary-file-directory) temporary-file-directory)
56         ("/tmp")))
57
58 ;;;###autoload
59 (defun uudecode-decode-region-external (start end &optional file-name)
60   "uudecode region between START and END with external decoder.
61
62 If FILE-NAME is non-nil, save the result to FILE-NAME."
63   (interactive "r\nP")
64   (let ((cbuf (current-buffer)) tempfile firstline work-buffer status)
65     (save-excursion
66       (goto-char start)
67       (when (re-search-forward uudecode-begin-line nil t)
68         (forward-line 1)
69         (setq firstline (point))
70         (cond ((null file-name))
71               ((stringp file-name))
72               (t
73                (setq file-name (read-file-name "File to Name:"
74                                                nil nil nil
75                                                (match-string 1)))))
76         (setq tempfile (if file-name
77                            (expand-file-name file-name)
78                          (make-temp-name
79                           ;; /tmp/uu...
80                           (expand-file-name
81                            "uu" uudecode-temporary-file-directory))))
82         (let ((cdir default-directory) default-process-coding-system)
83           (unwind-protect
84               (progn
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
92                        (point-min)
93                        (point-max)
94                        uudecode-decoder-program
95                        nil
96                        nil
97                        nil
98                        uudecode-decoder-switches))
99             (cd cdir) (set-buffer cbuf)))
100         (if (file-exists-p tempfile)
101             (unless file-name
102               (goto-char start)
103               (delete-region start end)
104               (let (format-alist)
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))))))
109
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)))))
117
118 ;;;###autoload
119
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."
123   (interactive "r\nP")
124   (let ((work-buffer nil)
125         (done nil)
126         (counter 0)
127         (remain 0)
128         (bits 0)
129         (lim 0) inputpos
130         (non-data-chars (concat "^" uudecode-alphabet)))
131     (unwind-protect
132         (save-excursion
133           (goto-char start)
134           (when (re-search-forward uudecode-begin-line nil t)
135             (cond ((null file-name))
136                   ((stringp file-name))
137                   (t
138                    (setq file-name (expand-file-name
139                                     (read-file-name "File to Name:"
140                                                     nil nil nil
141                                                     (match-string 1))))))
142             (setq work-buffer (generate-new-buffer " *uudecode-work*"))
143             (buffer-disable-undo work-buffer)
144             (forward-line 1)
145             (skip-chars-forward non-data-chars end)
146             (while (not done)
147               (setq inputpos (point))
148               (setq remain 0 bits 0 counter 0)
149               (cond
150                ((> (skip-chars-forward uudecode-alphabet end) 0)
151                 (setq lim (point))
152                 (setq remain
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))
157                   (setq bits (+ bits
158                                 (logand
159                                  (-
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))
164                   (cond ((= counter 4)
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
170                                                work-buffer)
171                          (setq bits 0 counter 0))
172                         (t (setq bits (lsh bits 6)))))))
173               (cond
174                (done)
175                ((> 0 remain)
176                 (error "uucode line ends unexpectly")
177                 (setq done t))
178                ((and (= (point) end) (not done))
179                 ;;(error "uucode ends unexpectly")
180                 (setq done t))
181                ((= counter 3)
182                 (uudecode-insert-char (logand (lsh bits -16) 255) 1 nil
183                                       work-buffer)
184                 (uudecode-insert-char (logand (lsh bits -8) 255) 1 nil
185                                       work-buffer))
186                ((= counter 2)
187                 (uudecode-insert-char (logand (lsh bits -10) 255) 1 nil
188                                       work-buffer)))
189               (skip-chars-forward non-data-chars end))
190             (if file-name
191                 (save-excursion
192                   (set-buffer work-buffer)
193                   (write-file file-name))
194               (or (markerp end) (setq end (set-marker (make-marker) end)))
195               (goto-char start)
196               (insert-buffer-substring work-buffer)
197               (delete-region (point) end))))
198       (and work-buffer (kill-buffer work-buffer)))))
199
200 (provide 'uudecode)
201
202 ;;; uudecode.el ends here