* gnus.el: Fixed all the doc strings to match the FSF convetions.
[gnus] / lisp / uudecode.el
1 ;;; uudecode.el -- elisp native uudecode
2 ;; Copyright (c) 1998 by Shenghuo Zhu <zsh@cs.rochester.edu>
3
4 ;; Author: Shenghuo Zhu <zsh@cs.rochester.edu>
5 ;; $Revision: 5.7 $
6 ;; Keywords: uudecode
7
8 ;; This file is not part of GNU Emacs, but the same permissions
9 ;; apply.
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 ;;     Lots of codes are stolen from mm-decode.el, gnus-uu.el and
29 ;;     base64.el
30
31 ;;; Code:
32
33 (if (not (fboundp 'char-int))
34     (fset 'char-int 'identity))
35
36 (defvar uudecode-decoder-program "uudecode"
37   "*Non-nil value should be a string that names a uu decoder.
38 The program should expect to read uu data on its standard
39 input and write the converted data to its standard output.")
40
41 (defvar uudecode-decoder-switches nil
42   "*List of command line flags passed to the command named by uudecode-decoder-program.")
43
44 (defconst uudecode-alphabet "\040-\140")
45
46 (defconst uudecode-begin-line "^begin[ \t]+[0-7][0-7][0-7][ \t]+\\(.*\\)$")
47 (defconst uudecode-end-line "^end[ \t]*$")
48
49 (defconst uudecode-body-line
50   (let ((i 61) (str "^M"))
51     (while (> (setq i (1- i)) 0)
52       (setq str (concat str "[^a-z]")))
53     (concat str ".?$")))
54
55 (defvar uudecode-temporary-file-directory
56   (cond ((fboundp 'temp-directory) (temp-directory))
57         ((boundp 'temporary-file-directory) temporary-file-directory)
58         ("/tmp")))
59
60 ;;;###autoload
61 (defun uudecode-decode-region-external (start end &optional file-name)
62   "uudecode region between START and END with external decoder.
63
64 If FILE-NAME is non-nil, save the result to FILE-NAME."
65   (interactive "r\nP")
66   (let ((cbuf (current-buffer)) tempfile firstline work-buffer status)
67     (save-excursion
68       (goto-char start)
69       (when (re-search-forward uudecode-begin-line nil t)
70         (forward-line 1)
71         (setq firstline (point))
72         (cond ((null file-name))
73               ((stringp file-name))
74               (t
75                (setq file-name (read-file-name "File to Name:"
76                                                nil nil nil
77                                                (match-string 1)))))
78         (setq tempfile (if file-name
79                            (expand-file-name file-name)
80                          (make-temp-name
81                           ;; /tmp/uu...
82                           (expand-file-name
83                            "uu" uudecode-temporary-file-directory))))
84         (let ((cdir default-directory) default-process-coding-system)
85           (unwind-protect
86               (progn
87                 (set-buffer (setq work-buffer
88                                   (generate-new-buffer " *uudecode-work*")))
89                 (buffer-disable-undo work-buffer)
90                 (insert "begin 600 " (file-name-nondirectory tempfile) "\n")
91                 (insert-buffer-substring cbuf firstline end)
92                 (cd (file-name-directory tempfile))
93                 (apply 'call-process-region
94                        (point-min)
95                        (point-max)
96                        uudecode-decoder-program
97                        nil
98                        nil
99                        nil
100                        uudecode-decoder-switches))
101             (cd cdir) (set-buffer cbuf)))
102         (if (file-exists-p tempfile)
103             (unless file-name
104               (goto-char start)
105               (delete-region start end)
106               (let (format-alist)
107                 (insert-file-contents-literally tempfile)))
108           (message "Can not uudecode")))
109       (and work-buffer (kill-buffer work-buffer))
110       (ignore-errors (or file-name (delete-file tempfile))))))
111
112 (if (string-match "XEmacs" emacs-version)
113     (defalias 'uudecode-insert-char 'insert-char)
114   (defun uudecode-insert-char (char &optional count ignored buffer)
115     (if (or (null buffer) (eq buffer (current-buffer)))
116         (insert-char char count)
117       (with-current-buffer buffer
118         (insert-char char count)))))
119
120 ;;;###autoload
121
122 (defun uudecode-decode-region (start end &optional file-name)
123   "uudecode region between START and END.
124 If FILE-NAME is non-nil, save the result to FILE-NAME."
125   (interactive "r\nP")
126   (let ((work-buffer nil)
127         (done nil)
128         (counter 0)
129         (remain 0)
130         (bits 0)
131         (lim 0) inputpos
132         (non-data-chars (concat "^" uudecode-alphabet)))
133     (unwind-protect
134         (save-excursion
135           (goto-char start)
136           (when (re-search-forward uudecode-begin-line nil t)
137             (cond ((null file-name))
138                   ((stringp file-name))
139                   (t
140                    (setq file-name (expand-file-name
141                                     (read-file-name "File to Name:"
142                                                     nil nil nil
143                                                     (match-string 1))))))
144             (setq work-buffer (generate-new-buffer " *uudecode-work*"))
145             (buffer-disable-undo work-buffer)
146             (forward-line 1)
147             (skip-chars-forward non-data-chars end)
148             (while (not done)
149               (setq inputpos (point))
150               (setq remain 0 bits 0 counter 0)
151               (cond
152                ((> (skip-chars-forward uudecode-alphabet end) 0)
153                 (setq lim (point))
154                 (setq remain
155                       (logand (- (char-int (char-after inputpos)) 32) 63))
156                 (setq inputpos (1+ inputpos))
157                 (if (= remain 0) (setq done t))
158                 (while (and (< inputpos lim) (> remain 0))
159                   (setq bits (+ bits
160                                 (logand
161                                  (-
162                                   (char-int (char-after inputpos)) 32) 63)))
163                   (if (/= counter 0) (setq remain (1- remain)))
164                   (setq counter (1+ counter)
165                         inputpos (1+ inputpos))
166                   (cond ((= counter 4)
167                          (uudecode-insert-char
168                           (lsh bits -16) 1 nil work-buffer)
169                          (uudecode-insert-char
170                           (logand (lsh bits -8) 255) 1 nil work-buffer)
171                          (uudecode-insert-char (logand bits 255) 1 nil
172                                                work-buffer)
173                          (setq bits 0 counter 0))
174                         (t (setq bits (lsh bits 6)))))))
175               (cond
176                (done)
177                ((> 0 remain)
178                 (error "uucode line ends unexpectly")
179                 (setq done t))
180                ((and (= (point) end) (not done))
181                 ;;(error "uucode ends unexpectly")
182                 (setq done t))
183                ((= counter 3)
184                 (uudecode-insert-char (logand (lsh bits -16) 255) 1 nil
185                                       work-buffer)
186                 (uudecode-insert-char (logand (lsh bits -8) 255) 1 nil
187                                       work-buffer))
188                ((= counter 2)
189                 (uudecode-insert-char (logand (lsh bits -10) 255) 1 nil
190                                       work-buffer)))
191               (skip-chars-forward non-data-chars end))
192             (if file-name
193                 (save-excursion
194                   (set-buffer work-buffer)
195                   (write-file file-name))
196               (or (markerp end) (setq end (set-marker (make-marker) end)))
197               (goto-char start)
198               (insert-buffer-substring work-buffer)
199               (delete-region (point) end))))
200       (and work-buffer (kill-buffer work-buffer)))))
201
202 (provide 'uudecode)
203
204 ;;; uudecode.el ends here