Synch to No Gnus 200406292138.
[elisp/gnus.git-] / lisp / uudecode.el
1 ;;; uudecode.el -- elisp native uudecode
2
3 ;; Copyright (c) 1998, 1999, 2000, 2001, 2003, 2004
4 ;;        Free Software Foundation, Inc.
5
6 ;; Author: Shenghuo Zhu <zsh@cs.rochester.edu>
7 ;; Keywords: uudecode news
8
9 ;; This file is part of GNU Emacs.
10
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 2, or (at your option)
14 ;; any later version.
15
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19 ;; GNU General Public License for more details.
20
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
23 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 ;; Boston, MA 02111-1307, USA.
25
26 ;;; Commentary:
27
28 ;;; Code:
29
30 (eval-when-compile (require 'cl))
31
32 (require 'path-util)
33
34 (eval-and-compile
35   (defalias 'uudecode-char-int
36     (if (fboundp 'char-int)
37         'char-int
38       'identity)))
39
40 (defcustom uudecode-decoder-program "uudecode"
41   "*Non-nil value should be a string that names a uu decoder.
42 The program should expect to read uu data on its standard
43 input and write the converted data to its standard output."
44   :type 'string
45   :group 'gnus-extract)
46
47 (defcustom uudecode-decoder-switches nil
48   "*List of command line flags passed to `uudecode-decoder-program'."
49   :group 'gnus-extract
50   :type '(repeat string))
51
52 (defcustom uudecode-use-external
53   (exec-installed-p uudecode-decoder-program)
54   "*Use external uudecode program."
55   :group 'gnus-extract
56   :type 'boolean)
57
58 (defconst uudecode-alphabet "\040-\140")
59
60 (defconst uudecode-begin-line "^begin[ \t]+[0-7][0-7][0-7][ \t]+\\(.*\\)$")
61 (defconst uudecode-end-line "^end[ \t]*$")
62
63 (defconst uudecode-body-line
64   (let ((i 61) (str "^M"))
65     (while (> (setq i (1- i)) 0)
66       (setq str (concat str "[^a-z]")))
67     (concat str ".?$")))
68
69 (defvar uudecode-temporary-file-directory
70   (cond ((fboundp 'temp-directory) (temp-directory))
71         ((boundp 'temporary-file-directory) temporary-file-directory)
72         ("/tmp")))
73
74 ;;;###autoload
75 (defun uudecode-decode-region-external (start end &optional file-name)
76   "Uudecode region between START and END using external program.
77 If FILE-NAME is non-nil, save the result to FILE-NAME.  The program
78 used is specified by `uudecode-decoder-program'."
79   (interactive "r\nP")
80   (let ((cbuf (current-buffer)) tempfile firstline status)
81     (save-excursion
82       (goto-char start)
83       (when (re-search-forward uudecode-begin-line nil t)
84         (forward-line 1)
85         (setq firstline (point))
86         (cond ((null file-name))
87               ((stringp file-name))
88               (t
89                (setq file-name (read-file-name "File to Name:"
90                                                nil nil nil
91                                                (match-string 1)))))
92         (setq tempfile (if file-name
93                            (expand-file-name file-name)
94                          (if (fboundp 'make-temp-file)
95                              (let ((temporary-file-directory
96                                     uudecode-temporary-file-directory))
97                                (make-temp-file "uu"))
98                            (expand-file-name
99                             (make-temp-name "uu")
100                             uudecode-temporary-file-directory))))
101         (let ((cdir default-directory)
102               default-process-coding-system)
103           (unwind-protect
104               (with-temp-buffer
105                 (insert "begin 600 " (file-name-nondirectory tempfile) "\n")
106                 (insert-buffer-substring cbuf firstline end)
107                 (cd (file-name-directory tempfile))
108                 (apply 'call-process-region
109                        (point-min)
110                        (point-max)
111                        uudecode-decoder-program
112                        nil
113                        nil
114                        nil
115                        uudecode-decoder-switches))
116             (cd cdir) (set-buffer cbuf)))
117         (if (file-exists-p tempfile)
118             (unless file-name
119               (goto-char start)
120               (delete-region start end)
121               (let (format-alist)
122                 (insert-file-contents-literally tempfile)))
123           (message "Can not uudecode")))
124       (ignore-errors (or file-name (delete-file tempfile))))))
125
126 ;;;###autoload
127 (defun uudecode-decode-region-internal (start end &optional file-name)
128   "Uudecode region between START and END without using an external program.
129 If FILE-NAME is non-nil, save the result to FILE-NAME."
130   (interactive "r\nP")
131   (let ((done nil)
132         (counter 0)
133         (remain 0)
134         (bits 0)
135         (lim 0) inputpos result
136         (non-data-chars (concat "^" uudecode-alphabet)))
137     (save-excursion
138       (goto-char start)
139       (when (re-search-forward uudecode-begin-line nil t)
140         (cond ((null file-name))
141               ((stringp file-name))
142               (t
143                (setq file-name (expand-file-name
144                                 (read-file-name "File to Name:"
145                                                 nil nil nil
146                                                 (match-string 1))))))
147         (forward-line 1)
148         (skip-chars-forward non-data-chars end)
149         (while (not done)
150           (setq inputpos (point))
151           (setq remain 0 bits 0 counter 0)
152           (cond
153            ((> (skip-chars-forward uudecode-alphabet end) 0)
154             (setq lim (point))
155             (setq remain
156                   (logand (- (uudecode-char-int (char-after inputpos)) 32)
157                           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                               (uudecode-char-int (char-after inputpos)) 32)
165                              63)))
166               (if (/= counter 0) (setq remain (1- remain)))
167               (setq counter (1+ counter)
168                     inputpos (1+ inputpos))
169               (cond ((= counter 4)
170                      (setq result (cons
171                                    (concat
172                                     (char-to-string (lsh bits -16))
173                                     (char-to-string (logand (lsh bits -8) 255))
174                                     (char-to-string (logand bits 255)))
175                                    result))
176                      (setq bits 0 counter 0))
177                     (t (setq bits (lsh bits 6)))))))
178           (cond
179            (done)
180            ((> 0 remain)
181             (error "uucode line ends unexpectly")
182             (setq done t))
183            ((and (= (point) end) (not done))
184             ;;(error "uucode ends unexpectly")
185             (setq done t))
186            ((= counter 3)
187             (setq result (cons
188                           (concat
189                            (char-to-string (logand (lsh bits -16) 255))
190                            (char-to-string (logand (lsh bits -8) 255)))
191                           result)))
192            ((= counter 2)
193             (setq result (cons
194                           (char-to-string (logand (lsh bits -10) 255))
195                           result))))
196           (skip-chars-forward non-data-chars end))
197         (if file-name
198             (let (default-enable-multibyte-characters)
199               (with-temp-file file-name
200                 (insert (apply 'concat (nreverse result)))))
201           (or (markerp end) (setq end (set-marker (make-marker) end)))
202           (goto-char start)
203           (insert (apply 'concat (nreverse result)))
204           (delete-region (point) end))))))
205
206 ;;;###autoload
207 (defun uudecode-decode-region (start end &optional file-name)
208   "Uudecode region between START and END.
209 If FILE-NAME is non-nil, save the result to FILE-NAME."
210   (if uudecode-use-external
211       (uudecode-decode-region-external start end file-name)
212     (uudecode-decode-region-internal start end file-name)))
213
214 (provide 'uudecode)
215
216 ;;; uudecode.el ends here