Commit | Line | Data |
---|---|---|
23f87bed | 1 | ;;; uudecode.el -- elisp native uudecode |
c113de23 | 2 | |
e84b4b86 | 3 | ;; Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004, |
d7a0267c | 4 | ;; 2005, 2006, 2007 Free Software Foundation, Inc. |
c113de23 GM |
5 | |
6 | ;; Author: Shenghuo Zhu <zsh@cs.rochester.edu> | |
7 | ;; Keywords: uudecode news | |
8 | ||
715a2ca2 | 9 | ;; This file is part of GNU Emacs. |
c113de23 GM |
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 | |
5a9dffec | 13 | ;; the Free Software Foundation; either version 3, or (at your option) |
c113de23 GM |
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 | |
3a35cf56 LK |
23 | ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
24 | ;; Boston, MA 02110-1301, USA. | |
c113de23 GM |
25 | |
26 | ;;; Commentary: | |
27 | ||
c113de23 GM |
28 | ;;; Code: |
29 | ||
117b4b78 DL |
30 | (eval-when-compile (require 'cl)) |
31 | ||
05c2a83c DL |
32 | (eval-and-compile |
33 | (defalias 'uudecode-char-int | |
34 | (if (fboundp 'char-int) | |
35 | 'char-int | |
23f87bed | 36 | 'identity))) |
c113de23 GM |
37 | |
38 | (defcustom uudecode-decoder-program "uudecode" | |
39 | "*Non-nil value should be a string that names a uu decoder. | |
40 | The program should expect to read uu data on its standard | |
41 | input and write the converted data to its standard output." | |
42 | :type 'string | |
43 | :group 'gnus-extract) | |
44 | ||
45 | (defcustom uudecode-decoder-switches nil | |
46 | "*List of command line flags passed to `uudecode-decoder-program'." | |
47 | :group 'gnus-extract | |
48 | :type '(repeat string)) | |
49 | ||
23f87bed MB |
50 | (defcustom uudecode-use-external |
51 | (executable-find uudecode-decoder-program) | |
52 | "*Use external uudecode program." | |
bf247b6e | 53 | :version "22.1" |
23f87bed MB |
54 | :group 'gnus-extract |
55 | :type 'boolean) | |
56 | ||
c113de23 GM |
57 | (defconst uudecode-alphabet "\040-\140") |
58 | ||
59 | (defconst uudecode-begin-line "^begin[ \t]+[0-7][0-7][0-7][ \t]+\\(.*\\)$") | |
60 | (defconst uudecode-end-line "^end[ \t]*$") | |
61 | ||
62 | (defconst uudecode-body-line | |
63 | (let ((i 61) (str "^M")) | |
64 | (while (> (setq i (1- i)) 0) | |
65 | (setq str (concat str "[^a-z]"))) | |
66 | (concat str ".?$"))) | |
67 | ||
68 | (defvar uudecode-temporary-file-directory | |
69 | (cond ((fboundp 'temp-directory) (temp-directory)) | |
70 | ((boundp 'temporary-file-directory) temporary-file-directory) | |
71 | ("/tmp"))) | |
72 | ||
73 | ;;;###autoload | |
74 | (defun uudecode-decode-region-external (start end &optional file-name) | |
96403ac1 DL |
75 | "Uudecode region between START and END using external program. |
76 | If FILE-NAME is non-nil, save the result to FILE-NAME. The program | |
77 | used is specified by `uudecode-decoder-program'." | |
c113de23 | 78 | (interactive "r\nP") |
96403ac1 | 79 | (let ((cbuf (current-buffer)) tempfile firstline status) |
c113de23 GM |
80 | (save-excursion |
81 | (goto-char start) | |
82 | (when (re-search-forward uudecode-begin-line nil t) | |
83 | (forward-line 1) | |
84 | (setq firstline (point)) | |
85 | (cond ((null file-name)) | |
86 | ((stringp file-name)) | |
87 | (t | |
88 | (setq file-name (read-file-name "File to Name:" | |
89 | nil nil nil | |
90 | (match-string 1))))) | |
91 | (setq tempfile (if file-name | |
92 | (expand-file-name file-name) | |
23f87bed MB |
93 | (if (fboundp 'make-temp-file) |
94 | (let ((temporary-file-directory | |
95 | uudecode-temporary-file-directory)) | |
96 | (make-temp-file "uu")) | |
97 | (expand-file-name | |
98 | (make-temp-name "uu") | |
99 | uudecode-temporary-file-directory)))) | |
96403ac1 | 100 | (let ((cdir default-directory) |
26c9afc3 MB |
101 | (default-process-coding-system |
102 | (if (featurep 'xemacs) | |
103 | ;; In XEmacs, `nil' is not a valid coding system. | |
104 | '(binary . binary) | |
105 | nil))) | |
c113de23 | 106 | (unwind-protect |
96403ac1 | 107 | (with-temp-buffer |
c113de23 GM |
108 | (insert "begin 600 " (file-name-nondirectory tempfile) "\n") |
109 | (insert-buffer-substring cbuf firstline end) | |
110 | (cd (file-name-directory tempfile)) | |
111 | (apply 'call-process-region | |
112 | (point-min) | |
113 | (point-max) | |
114 | uudecode-decoder-program | |
115 | nil | |
116 | nil | |
117 | nil | |
118 | uudecode-decoder-switches)) | |
119 | (cd cdir) (set-buffer cbuf))) | |
120 | (if (file-exists-p tempfile) | |
121 | (unless file-name | |
122 | (goto-char start) | |
123 | (delete-region start end) | |
124 | (let (format-alist) | |
125 | (insert-file-contents-literally tempfile))) | |
126 | (message "Can not uudecode"))) | |
c113de23 GM |
127 | (ignore-errors (or file-name (delete-file tempfile)))))) |
128 | ||
41e49ce6 MB |
129 | (eval-and-compile |
130 | (defalias 'uudecode-string-to-multibyte | |
131 | (cond | |
132 | ((featurep 'xemacs) | |
133 | 'identity) | |
134 | ((fboundp 'string-to-multibyte) | |
135 | 'string-to-multibyte) | |
136 | (t | |
137 | (lambda (string) | |
138 | "Return a multibyte string with the same individual chars as string." | |
139 | (mapconcat | |
140 | (lambda (ch) (string-as-multibyte (char-to-string ch))) | |
141 | string "")))))) | |
142 | ||
c113de23 | 143 | ;;;###autoload |
23f87bed | 144 | (defun uudecode-decode-region-internal (start end &optional file-name) |
96403ac1 | 145 | "Uudecode region between START and END without using an external program. |
c113de23 GM |
146 | If FILE-NAME is non-nil, save the result to FILE-NAME." |
147 | (interactive "r\nP") | |
23f87bed | 148 | (let ((done nil) |
c113de23 GM |
149 | (counter 0) |
150 | (remain 0) | |
151 | (bits 0) | |
23f87bed | 152 | (lim 0) inputpos result |
c113de23 | 153 | (non-data-chars (concat "^" uudecode-alphabet))) |
23f87bed MB |
154 | (save-excursion |
155 | (goto-char start) | |
156 | (when (re-search-forward uudecode-begin-line nil t) | |
157 | (cond ((null file-name)) | |
158 | ((stringp file-name)) | |
159 | (t | |
160 | (setq file-name (expand-file-name | |
161 | (read-file-name "File to Name:" | |
162 | nil nil nil | |
163 | (match-string 1)))))) | |
164 | (forward-line 1) | |
165 | (skip-chars-forward non-data-chars end) | |
166 | (while (not done) | |
167 | (setq inputpos (point)) | |
168 | (setq remain 0 bits 0 counter 0) | |
169 | (cond | |
170 | ((> (skip-chars-forward uudecode-alphabet end) 0) | |
171 | (setq lim (point)) | |
172 | (setq remain | |
173 | (logand (- (uudecode-char-int (char-after inputpos)) 32) | |
174 | 63)) | |
175 | (setq inputpos (1+ inputpos)) | |
176 | (if (= remain 0) (setq done t)) | |
177 | (while (and (< inputpos lim) (> remain 0)) | |
178 | (setq bits (+ bits | |
179 | (logand | |
180 | (- | |
181 | (uudecode-char-int (char-after inputpos)) 32) | |
182 | 63))) | |
183 | (if (/= counter 0) (setq remain (1- remain))) | |
184 | (setq counter (1+ counter) | |
185 | inputpos (1+ inputpos)) | |
186 | (cond ((= counter 4) | |
187 | (setq result (cons | |
188 | (concat | |
189 | (char-to-string (lsh bits -16)) | |
190 | (char-to-string (logand (lsh bits -8) 255)) | |
191 | (char-to-string (logand bits 255))) | |
192 | result)) | |
193 | (setq bits 0 counter 0)) | |
194 | (t (setq bits (lsh bits 6))))))) | |
195 | (cond | |
196 | (done) | |
197 | ((> 0 remain) | |
198 | (error "uucode line ends unexpectly") | |
199 | (setq done t)) | |
200 | ((and (= (point) end) (not done)) | |
201 | ;;(error "uucode ends unexpectly") | |
202 | (setq done t)) | |
203 | ((= counter 3) | |
204 | (setq result (cons | |
205 | (concat | |
206 | (char-to-string (logand (lsh bits -16) 255)) | |
207 | (char-to-string (logand (lsh bits -8) 255))) | |
208 | result))) | |
209 | ((= counter 2) | |
210 | (setq result (cons | |
211 | (char-to-string (logand (lsh bits -10) 255)) | |
212 | result)))) | |
213 | (skip-chars-forward non-data-chars end)) | |
214 | (if file-name | |
215 | (let (default-enable-multibyte-characters) | |
216 | (with-temp-file file-name | |
217 | (insert (apply 'concat (nreverse result))))) | |
218 | (or (markerp end) (setq end (set-marker (make-marker) end))) | |
c113de23 | 219 | (goto-char start) |
0d7c8ac4 | 220 | (if enable-multibyte-characters |
41e49ce6 | 221 | (mapc #'(lambda (x) (insert (uudecode-string-to-multibyte x))) |
0d7c8ac4 KH |
222 | (nreverse result)) |
223 | (insert (apply 'concat (nreverse result)))) | |
23f87bed MB |
224 | (delete-region (point) end)))))) |
225 | ||
226 | ;;;###autoload | |
227 | (defun uudecode-decode-region (start end &optional file-name) | |
228 | "Uudecode region between START and END. | |
229 | If FILE-NAME is non-nil, save the result to FILE-NAME." | |
230 | (if uudecode-use-external | |
231 | (uudecode-decode-region-external start end file-name) | |
232 | (uudecode-decode-region-internal start end file-name))) | |
c113de23 GM |
233 | |
234 | (provide 'uudecode) | |
235 | ||
ab5796a9 | 236 | ;;; arch-tag: e1f09ed5-62b4-4677-9f13-4e81c4fe8ce3 |
c113de23 | 237 | ;;; uudecode.el ends here |