]> code.delx.au - gnu-emacs/blob - lisp/gnus/uudecode.el
*** empty log message ***
[gnu-emacs] / lisp / gnus / uudecode.el
1 ;;; uudecode.el -- elisp native uudecode
2
3 ;; Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004,
4 ;; 2005, 2006 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., 51 Franklin Street, Fifth Floor,
24 ;; Boston, MA 02110-1301, USA.
25
26 ;;; Commentary:
27
28 ;;; Code:
29
30 (autoload 'executable-find "executable")
31
32 (eval-when-compile (require 'cl))
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 (executable-find uudecode-decoder-program)
54 "*Use external uudecode program."
55 :version "22.1"
56 :group 'gnus-extract
57 :type 'boolean)
58
59 (defconst uudecode-alphabet "\040-\140")
60
61 (defconst uudecode-begin-line "^begin[ \t]+[0-7][0-7][0-7][ \t]+\\(.*\\)$")
62 (defconst uudecode-end-line "^end[ \t]*$")
63
64 (defconst uudecode-body-line
65 (let ((i 61) (str "^M"))
66 (while (> (setq i (1- i)) 0)
67 (setq str (concat str "[^a-z]")))
68 (concat str ".?$")))
69
70 (defvar uudecode-temporary-file-directory
71 (cond ((fboundp 'temp-directory) (temp-directory))
72 ((boundp 'temporary-file-directory) temporary-file-directory)
73 ("/tmp")))
74
75 ;;;###autoload
76 (defun uudecode-decode-region-external (start end &optional file-name)
77 "Uudecode region between START and END using external program.
78 If FILE-NAME is non-nil, save the result to FILE-NAME. The program
79 used is specified by `uudecode-decoder-program'."
80 (interactive "r\nP")
81 (let ((cbuf (current-buffer)) tempfile firstline status)
82 (save-excursion
83 (goto-char start)
84 (when (re-search-forward uudecode-begin-line nil t)
85 (forward-line 1)
86 (setq firstline (point))
87 (cond ((null file-name))
88 ((stringp file-name))
89 (t
90 (setq file-name (read-file-name "File to Name:"
91 nil nil nil
92 (match-string 1)))))
93 (setq tempfile (if file-name
94 (expand-file-name file-name)
95 (if (fboundp 'make-temp-file)
96 (let ((temporary-file-directory
97 uudecode-temporary-file-directory))
98 (make-temp-file "uu"))
99 (expand-file-name
100 (make-temp-name "uu")
101 uudecode-temporary-file-directory))))
102 (let ((cdir default-directory)
103 default-process-coding-system)
104 (unwind-protect
105 (with-temp-buffer
106 (insert "begin 600 " (file-name-nondirectory tempfile) "\n")
107 (insert-buffer-substring cbuf firstline end)
108 (cd (file-name-directory tempfile))
109 (apply 'call-process-region
110 (point-min)
111 (point-max)
112 uudecode-decoder-program
113 nil
114 nil
115 nil
116 uudecode-decoder-switches))
117 (cd cdir) (set-buffer cbuf)))
118 (if (file-exists-p tempfile)
119 (unless file-name
120 (goto-char start)
121 (delete-region start end)
122 (let (format-alist)
123 (insert-file-contents-literally tempfile)))
124 (message "Can not uudecode")))
125 (ignore-errors (or file-name (delete-file tempfile))))))
126
127 ;;;###autoload
128 (defun uudecode-decode-region-internal (start end &optional file-name)
129 "Uudecode region between START and END without using an external program.
130 If FILE-NAME is non-nil, save the result to FILE-NAME."
131 (interactive "r\nP")
132 (let ((done nil)
133 (counter 0)
134 (remain 0)
135 (bits 0)
136 (lim 0) inputpos result
137 (non-data-chars (concat "^" uudecode-alphabet)))
138 (save-excursion
139 (goto-char start)
140 (when (re-search-forward uudecode-begin-line nil t)
141 (cond ((null file-name))
142 ((stringp file-name))
143 (t
144 (setq file-name (expand-file-name
145 (read-file-name "File to Name:"
146 nil nil nil
147 (match-string 1))))))
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 (- (uudecode-char-int (char-after inputpos)) 32)
158 63))
159 (setq inputpos (1+ inputpos))
160 (if (= remain 0) (setq done t))
161 (while (and (< inputpos lim) (> remain 0))
162 (setq bits (+ bits
163 (logand
164 (-
165 (uudecode-char-int (char-after inputpos)) 32)
166 63)))
167 (if (/= counter 0) (setq remain (1- remain)))
168 (setq counter (1+ counter)
169 inputpos (1+ inputpos))
170 (cond ((= counter 4)
171 (setq result (cons
172 (concat
173 (char-to-string (lsh bits -16))
174 (char-to-string (logand (lsh bits -8) 255))
175 (char-to-string (logand bits 255)))
176 result))
177 (setq bits 0 counter 0))
178 (t (setq bits (lsh bits 6)))))))
179 (cond
180 (done)
181 ((> 0 remain)
182 (error "uucode line ends unexpectly")
183 (setq done t))
184 ((and (= (point) end) (not done))
185 ;;(error "uucode ends unexpectly")
186 (setq done t))
187 ((= counter 3)
188 (setq result (cons
189 (concat
190 (char-to-string (logand (lsh bits -16) 255))
191 (char-to-string (logand (lsh bits -8) 255)))
192 result)))
193 ((= counter 2)
194 (setq result (cons
195 (char-to-string (logand (lsh bits -10) 255))
196 result))))
197 (skip-chars-forward non-data-chars end))
198 (if file-name
199 (let (default-enable-multibyte-characters)
200 (with-temp-file file-name
201 (insert (apply 'concat (nreverse result)))))
202 (or (markerp end) (setq end (set-marker (make-marker) end)))
203 (goto-char start)
204 (insert (apply 'concat (nreverse result)))
205 (delete-region (point) end))))))
206
207 ;;;###autoload
208 (defun uudecode-decode-region (start end &optional file-name)
209 "Uudecode region between START and END.
210 If FILE-NAME is non-nil, save the result to FILE-NAME."
211 (if uudecode-use-external
212 (uudecode-decode-region-external start end file-name)
213 (uudecode-decode-region-internal start end file-name)))
214
215 (provide 'uudecode)
216
217 ;;; arch-tag: e1f09ed5-62b4-4677-9f13-4e81c4fe8ce3
218 ;;; uudecode.el ends here