Initial git import
[sxemacs] / lisp / code-process.el
1 ;;; code-process.el --- Process coding functions for XEmacs.
2
3 ;; Copyright (C) 1985-1987, 1993, 1994, 1997, 2003
4 ;;               Free Software Foundation, Inc.
5 ;; Copyright (C) 1995 Ben Wing
6 ;; Copyright (C) 1997 MORIOKA Tomohiko
7
8 ;; Author: Ben Wing
9 ;;         MORIOKA Tomohiko
10 ;; Maintainer: XEmacs Development Team
11 ;; Keywords: mule, multilingual, coding system, process
12
13 ;; This file is part of SXEmacs.
14
15 ;; SXEmacs is free software: you can redistribute it and/or modify
16 ;; it under the terms of the GNU General Public License as published by
17 ;; the Free Software Foundation, either version 3 of the License, or
18 ;; (at your option) any later version.
19
20 ;; SXEmacs is distributed in the hope that it will be useful,
21 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
22 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23 ;; GNU General Public License for more details.
24
25 ;; You should have received a copy of the GNU General Public License
26 ;; along with this program.  If not, see <http://www.gnu.org/licenses/>.
27
28 ;;; Commentary:
29
30 ;; This file has some similarities to code-files.el.
31
32 ;;; Code:
33
34 (defvar process-coding-system-alist nil
35   "Alist to decide a coding system to use for a process I/O operation.
36 The format is ((PATTERN . VAL) ...),
37 where PATTERN is a regular expression matching a program name,
38 VAL is a coding system, a cons of coding systems, or a function symbol.
39 If VAL is a coding system, it is used for both decoding what received
40 from the program and encoding what sent to the program.
41 If VAL is a cons of coding systems, the car part is used for decoding,
42 and the cdr part is used for encoding.
43 If VAL is a function symbol, the function must return a coding system
44 or a cons of coding systems which are used as above.")
45
46 (defun call-process (program &optional infile buffer displayp &rest args)
47   "Call PROGRAM synchronously in separate process.
48 The program's input comes from file INFILE (nil means `/dev/null').
49 Insert output in BUFFER before point; t means current buffer;
50  nil for BUFFER means discard it; 0 means discard and don't wait.
51 BUFFER can also have the form (REAL-BUFFER STDERR-FILE); in that case,
52 REAL-BUFFER says what to do with standard output, as above,
53 while STDERR-FILE says what to do with standard error in the child.
54 STDERR-FILE may be nil (discard standard error output),
55 t (mix it with ordinary output), or a file name string.
56
57 Fourth arg DISPLAYP non-nil means redisplay buffer as output is inserted.
58 Remaining arguments are strings passed as command arguments to PROGRAM.
59
60 If BUFFER is 0, `call-process' returns immediately with value nil.
61 Otherwise it waits for PROGRAM to terminate and returns a numeric exit status
62  or a signal description string.
63 If you quit, the process is killed with SIGINT, or SIGKILL if you
64  quit again.
65
66 Coding systems are taken from `coding-system-for-read' for input and
67 `coding-system-for-write' for output if those variables are bound.
68 Otherwise they are looked up in `process-coding-system-alist'.  If not
69 found, they default to `nil' for both input and output."
70   (let* ((coding-system-for-read
71           (or coding-system-for-read
72               (let (ret)
73                 (catch 'found
74                   (let ((alist process-coding-system-alist)
75                         (case-fold-search nil))
76                     (while alist
77                       (if (string-match (car (car alist)) program)
78                           (throw 'found (setq ret (cdr (car alist))))
79                         )
80                       (setq alist (cdr alist))
81                       )))
82                 (if (functionp ret)
83                     (setq ret (funcall ret 'call-process program))
84                   )
85                 (cond ((consp ret) (car ret))
86                       ((not ret) 'undecided)
87                       ((find-coding-system ret) ret)
88                       )
89                 ))))
90     (apply 'call-process-internal program infile buffer displayp args)
91     ))
92
93 (defun call-process-region (start end program
94                                   &optional deletep buffer displayp
95                                   &rest args)
96   "Send text from START to END to a synchronous process running PROGRAM.
97 Delete the text if fourth arg DELETEP is non-nil.
98
99 Insert output in BUFFER before point; t means current buffer;
100  nil for BUFFER means discard it; 0 means discard and don't wait.
101 BUFFER can also have the form (REAL-BUFFER STDERR-FILE); in that case,
102 REAL-BUFFER says what to do with standard output, as above,
103 while STDERR-FILE says what to do with standard error in the child.
104 STDERR-FILE may be nil (discard standard error output),
105 t (mix it with ordinary output), or a file name string.
106
107 Sixth arg DISPLAYP non-nil means redisplay buffer as output is inserted.
108 Remaining args are passed to PROGRAM at startup as command args.
109
110 If BUFFER is 0, returns immediately with value nil.
111 Otherwise waits for PROGRAM to terminate
112 and returns a numeric exit status or a signal description string.
113 If you quit, the process is first killed with SIGINT, then with SIGKILL if
114 you quit again before the process exits.
115
116 Coding systems are taken from `coding-system-for-read' for input and
117 `coding-system-for-write' for output if those variables are bound.
118 Otherwise they are looked up in `process-coding-system-alist'.  If not
119 found, they default to `nil' for both input and output."
120   (let ((temp
121          (make-temp-name
122           (concat (file-name-as-directory (temp-directory)) "emacs"))))
123     (unwind-protect
124         (let (cs-r cs-w)
125           (let (ret)
126             (catch 'found
127               (let ((alist process-coding-system-alist)
128                     (case-fold-search nil))
129                 (while alist
130                   (if (string-match (car (car alist)) program)
131                       (throw 'found (setq ret (cdr (car alist)))))
132                   (setq alist (cdr alist))
133                   )))
134             (if (functionp ret)
135                 (setq ret (funcall ret 'call-process-region program)))
136             (cond ((consp ret)
137                    (setq cs-r (car ret)
138                          cs-w (cdr ret)))
139                   ((null ret)
140                    (setq cs-r buffer-file-coding-system
141                          cs-w buffer-file-coding-system))
142                   ((find-coding-system ret)
143                    (setq cs-r ret
144                          cs-w ret))))
145           (let ((coding-system-for-read
146                  (or coding-system-for-read cs-r))
147                 (coding-system-for-write
148                  (or coding-system-for-write cs-w)))
149             (write-region start end temp nil 'silent)
150             (if deletep (delete-region start end))
151             (apply #'call-process program temp buffer displayp args)))
152       (ignore-file-errors (delete-file temp)))))
153
154 (defun start-process (name buffer program &rest program-args)
155   "Start a program in a subprocess.  Return the process object for it.
156 Args are NAME BUFFER PROGRAM &rest PROGRAM-ARGS
157 NAME is name for process.  It is modified if necessary to make it unique.
158 BUFFER is the buffer or (buffer-name) to associate with the process.
159  Process output goes at end of that buffer, unless you specify
160  an output stream or filter function to handle the output.
161  BUFFER may be also nil, meaning that this process is not associated
162  with any buffer
163 Third arg is program file name.  It is searched for as in the shell.
164 Remaining arguments are strings to give program as arguments.
165
166 Coding systems are taken from `coding-system-for-read' for input and
167 `coding-system-for-write' for output if those variables are bound.
168 Otherwise they are looked up in `process-coding-system-alist'.  If not
169 found, they default to `undecided' for input and `nil' (binary) for
170 output."
171   (let (cs-r cs-w)
172     (let (ret)
173       (catch 'found
174         (let ((alist process-coding-system-alist)
175               (case-fold-search nil))
176           (while alist
177             (if (string-match (car (car alist)) program)
178                 (throw 'found (setq ret (cdr (car alist)))))
179             (setq alist (cdr alist))
180             )))
181       (if (functionp ret)
182           (setq ret (funcall ret 'start-process program)))
183       (cond ((consp ret)
184              (setq cs-r (car ret)
185                    cs-w (cdr ret)))
186             ((find-coding-system ret)
187              (setq cs-r ret
188                    cs-w ret))))
189     (let ((coding-system-for-read
190            (or coding-system-for-read cs-r 'undecided))
191           (coding-system-for-write
192            (or coding-system-for-write cs-w)))
193       (apply 'start-process-internal name buffer program program-args)
194       )))
195
196 (defvar network-coding-system-alist nil
197   "Alist to decide a coding system to use for a network I/O operation.
198 The format is ((PATTERN . VAL) ...),
199 where PATTERN is a regular expression matching a network service name
200 or is a port number to connect to,
201 VAL is a coding system, a cons of coding systems, or a function symbol.
202 If VAL is a coding system, it is used for both decoding what received
203 from the network stream and encoding what sent to the network stream.
204 If VAL is a cons of coding systems, the car part is used for decoding,
205 and the cdr part is used for encoding.
206 If VAL is a function symbol, the function must return a coding system
207 or a cons of coding systems which are used as above.
208
209 See also the function `find-operation-coding-system'.")
210
211 (defun open-network-stream (name buffer host service &optional protocol)
212   "Open a TCP connection for a service to a host.
213 Return a process object to represent the connection.
214 Input and output work as for subprocesses; `delete-process' closes it.
215 Args are NAME BUFFER HOST SERVICE.
216 NAME is name for process.  It is modified if necessary to make it unique.
217 BUFFER is the buffer (or buffer-name) to associate with the process.
218  Process output goes at end of that buffer, unless you specify
219  an output stream or filter function to handle the output.
220  BUFFER may be also nil, meaning that this process is not associated
221  with any buffer
222 Third arg is name of the host to connect to, or its IP address.
223 Fourth arg SERVICE is name of the service desired, or an integer
224  specifying a port number to connect to.
225 Fifth argument PROTOCOL is a network protocol.  Currently 'tcp
226  (Transmission Control Protocol) and 'udp (User Datagram Protocol) are
227  supported.  When omitted, 'tcp is assumed.
228
229 Output via `process-send-string' and input via buffer or filter (see
230 `set-process-filter') are stream-oriented.  That means UDP datagrams are
231 not guaranteed to be sent and received in discrete packets. (But small
232 datagrams around 500 bytes that are not truncated by `process-send-string'
233 are usually fine.)  Note further that UDP protocol does not guard against 
234 lost packets."
235   (let (cs-r cs-w)
236     (let (ret)
237       (catch 'found
238         (let ((alist network-coding-system-alist)
239               (case-fold-search nil)
240               pattern)
241           (while alist
242             (setq pattern (car (car alist)))
243             (and
244              (cond ((numberp pattern)
245                     (and (numberp service)
246                          (eq pattern service)))
247                    ((stringp pattern)
248                     (or (and (stringp service)
249                              (string-match pattern service))
250                         (and (numberp service)
251                              (string-match pattern
252                                            (number-to-string service))))))
253              (throw 'found (setq ret (cdr (car alist)))))
254             (setq alist (cdr alist))
255             )))
256       (if (functionp ret)
257           (setq ret (funcall ret 'open-network-stream service)))
258       (cond ((consp ret)
259              (setq cs-r (car ret)
260                    cs-w (cdr ret)))
261             ((find-coding-system ret)
262              (setq cs-r ret
263                    cs-w ret))))
264     (let ((coding-system-for-read
265            (or coding-system-for-read cs-r))
266           (coding-system-for-write
267            (or coding-system-for-write cs-w)))
268       (open-network-stream-internal name buffer host service protocol))))
269
270 ;;; code-process.el ends here