428
|
1 ;;; code-process.el --- Process coding functions for XEmacs.
|
|
2
|
|
3 ;; Copyright (C) 1985-1987, 1993, 1994, 1997 Free Software Foundation, Inc.
|
853
|
4 ;; Copyright (C) 1995, 2000, 2002 Ben Wing
|
428
|
5 ;; Copyright (C) 1997 MORIOKA Tomohiko
|
|
6
|
|
7 ;; Author: Ben Wing
|
|
8 ;; MORIOKA Tomohiko
|
|
9 ;; Maintainer: XEmacs Development Team
|
|
10 ;; Keywords: mule, multilingual, coding system, process
|
|
11
|
|
12 ;; This file is part of XEmacs.
|
|
13
|
|
14 ;; This file is very similar to code-process.el
|
|
15
|
|
16 ;; XEmacs is free software; you can redistribute it and/or modify it
|
|
17 ;; under the terms of the GNU General Public License as published by
|
|
18 ;; the Free Software Foundation; either version 2, or (at your option)
|
|
19 ;; any later version.
|
|
20
|
|
21 ;; XEmacs is distributed in the hope that it will be useful, but
|
|
22 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
23 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
24 ;; General Public License for more details.
|
|
25
|
|
26 ;; You should have received a copy of the GNU General Public License
|
|
27 ;; along with XEmacs; see the file COPYING. If not, write to the Free
|
|
28 ;; Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
|
|
29 ;; 02111-1307, USA.
|
|
30
|
|
31 ;;; Code:
|
|
32
|
|
33 (defvar process-coding-system-alist nil
|
|
34 "Alist to decide a coding system to use for a process I/O operation.
|
|
35 The format is ((PATTERN . VAL) ...),
|
|
36 where PATTERN is a regular expression matching a program name,
|
|
37 VAL is a coding system, a cons of coding systems, or a function symbol.
|
|
38 If VAL is a coding system, it is used for both decoding what received
|
|
39 from the program and encoding what sent to the program.
|
|
40 If VAL is a cons of coding systems, the car part is used for decoding,
|
|
41 and the cdr part is used for encoding.
|
853
|
42 If VAL is a function symbol, it is called with two arguments, a symbol
|
|
43 indicating the operation being performed (one of `start-process',
|
|
44 `call-process', `open-network-stream', or `open-multicast-group') and the
|
|
45 program name. The function must return a coding system or a cons of
|
|
46 coding systems which are used as above.")
|
428
|
47
|
|
48 (defun call-process (program &optional infile buffer displayp &rest args)
|
|
49 "Call PROGRAM synchronously in separate process.
|
853
|
50
|
428
|
51 The program's input comes from file INFILE (nil means `/dev/null').
|
853
|
52 XEmacs feature: INFILE can also be a list of (BUFFER [START [END]]), i.e.
|
|
53 a list of one to three elements, consisting of a buffer and optionally
|
|
54 a start position or start and end position. In this case, input comes
|
|
55 from the buffer, starting from START (defaults to the beginning of the
|
|
56 buffer) and ending at END (defaults to the end of the buffer).
|
|
57
|
428
|
58 Insert output in BUFFER before point; t means current buffer;
|
|
59 nil for BUFFER means discard it; 0 means discard and don't wait.
|
837
|
60 If BUFFER is a string, then find or create a buffer with that name,
|
|
61 then insert the output in that buffer, before point.
|
428
|
62 BUFFER can also have the form (REAL-BUFFER STDERR-FILE); in that case,
|
|
63 REAL-BUFFER says what to do with standard output, as above,
|
|
64 while STDERR-FILE says what to do with standard error in the child.
|
|
65 STDERR-FILE may be nil (discard standard error output),
|
853
|
66 t (mix it with ordinary output), a file name string, or (XEmacs feature)
|
|
67 a buffer object. If STDERR-FILE is a buffer object (but not the name of
|
|
68 a buffer, since that would be interpreted as a file), the standard error
|
|
69 output will be inserted into the buffer before point.
|
428
|
70
|
853
|
71 Fourth arg DISPLAY non-nil means redisplay buffer as output is inserted.
|
428
|
72 Remaining arguments are strings passed as command arguments to PROGRAM.
|
|
73
|
853
|
74 If BUFFER is 0, returns immediately with value nil.
|
|
75 Otherwise waits for PROGRAM to terminate and returns a numeric exit status
|
|
76 or a signal description string. If you quit, the process is first killed
|
|
77 with SIGINT, then with SIGKILL if you quit again before the process exits.
|
|
78
|
|
79 If INFILE is a file, we transfer its exact contents to the process without
|
|
80 any encoding/decoding. (#### This policy might change.)
|
|
81
|
|
82 Otherwise, the read/write coding systems used for process I/O on the
|
|
83 process are determined as follows:
|
|
84
|
|
85 1. `coding-system-for-read', `coding-system-for-write', if non-nil.
|
|
86 (Intended as a temporary overriding mechanism for use by Lisp
|
|
87 code.)
|
|
88 2. The matching value for the process name from `process-coding-system-alist',
|
|
89 if any, and if non-nil. The value may be either a single coding
|
|
90 system, used for both read and write; or a cons of read/write; or a
|
|
91 function, called to get one of the other two values.
|
|
92 3. For writing: If a buffer was given in INFILE, the value of
|
|
93 `buffer-file-coding-system' in that buffer.
|
|
94 For reading: if a buffer was given in BUFFER, the value of
|
|
95 `buffer-file-coding-system-for-read' in that buffer.
|
|
96 4. The value of `default-process-coding-system', which should be a cons
|
|
97 of read/write coding systems, if the values are non-nil.
|
|
98 5. The coding system `undecided' for read, and `raw-text' for write.
|
|
99
|
|
100 Note that the processes of determining the read and write coding systems
|
|
101 proceed essentially independently one from the other, as in `start-process'."
|
|
102 (let (cs-r cs-w)
|
|
103 (let (ret)
|
|
104 (catch 'found
|
|
105 (let ((alist process-coding-system-alist)
|
|
106 (case-fold-search nil))
|
|
107 (while alist
|
|
108 (if (string-match (car (car alist)) program)
|
|
109 (throw 'found (setq ret (cdr (car alist)))))
|
|
110 (setq alist (cdr alist))
|
|
111 )))
|
|
112 (if (functionp ret)
|
|
113 (setq ret (funcall ret 'call-process program)))
|
|
114 (cond ((consp ret)
|
|
115 (setq cs-r (car ret)
|
|
116 cs-w (cdr ret)))
|
|
117 ((and ret (find-coding-system ret))
|
|
118 (setq cs-r ret
|
|
119 cs-w ret))))
|
|
120 (let ((coding-system-for-read
|
|
121 (or coding-system-for-read cs-r
|
|
122 (let ((thebuf (if (consp buffer) (car buffer) buffer)))
|
|
123 (and (or (bufferp thebuf) (stringp thebuf))
|
|
124 (get-buffer thebuf)
|
|
125 (symbol-value-in-buffer
|
857
|
126 'buffer-file-coding-system-for-read (get-buffer thebuf))))
|
853
|
127 (car default-process-coding-system)
|
|
128 'undecided))
|
|
129 (coding-system-for-write
|
|
130 (or coding-system-for-write cs-w
|
|
131 (and (consp infile)
|
|
132 (symbol-value-in-buffer
|
|
133 'buffer-file-coding-system
|
|
134 (get-buffer (car infile))))
|
|
135 (cdr default-process-coding-system)
|
|
136 'raw-text)))
|
|
137 (apply 'call-process-internal program infile buffer displayp args))))
|
428
|
138
|
|
139 (defun call-process-region (start end program
|
|
140 &optional deletep buffer displayp
|
|
141 &rest args)
|
|
142 "Send text from START to END to a synchronous process running PROGRAM.
|
|
143 Delete the text if fourth arg DELETEP is non-nil.
|
|
144
|
|
145 Insert output in BUFFER before point; t means current buffer;
|
|
146 nil for BUFFER means discard it; 0 means discard and don't wait.
|
837
|
147 If BUFFER is a string, then find or create a buffer with that name,
|
|
148 then insert the output in that buffer, before point.
|
428
|
149 BUFFER can also have the form (REAL-BUFFER STDERR-FILE); in that case,
|
|
150 REAL-BUFFER says what to do with standard output, as above,
|
|
151 while STDERR-FILE says what to do with standard error in the child.
|
|
152 STDERR-FILE may be nil (discard standard error output),
|
853
|
153 t (mix it with ordinary output), a file name string, or (XEmacs feature)
|
|
154 a buffer object. If STDERR-FILE is a buffer object (but not the name of
|
|
155 a buffer, since that would be interpreted as a file), the standard error
|
|
156 output will be inserted into the buffer before point.
|
428
|
157
|
|
158 Sixth arg DISPLAYP non-nil means redisplay buffer as output is inserted.
|
|
159 Remaining args are passed to PROGRAM at startup as command args.
|
|
160
|
|
161 If BUFFER is 0, returns immediately with value nil.
|
853
|
162 Otherwise waits for PROGRAM to terminate and returns a numeric exit status
|
|
163 or a signal description string. If you quit, the process is first killed
|
|
164 with SIGINT, then with SIGKILL if you quit again before the process exits.
|
|
165
|
|
166 The read/write coding systems used for process I/O on the process are
|
|
167 the same as for `call-process'."
|
|
168 ;; We used to delete the text before calling call-process; that was when
|
|
169 ;; a temporary file was used to pass the text to call-process. Now that
|
|
170 ;; we don't do that, we delete the text afterward; if it's being inserted
|
|
171 ;; in the same buffer, make sure we track the insertion, and don't get
|
|
172 ;; any of it in the deleted region if insertion happens at either end
|
|
173 ;; of the region.
|
|
174 (let ((s (and deletep (copy-marker start t)))
|
|
175 (e (and deletep (copy-marker end))))
|
|
176 (apply #'call-process program (list (current-buffer) start end)
|
|
177 buffer displayp args)
|
922
|
178 ; If start and end were the same originally, s will be beyond e now
|
|
179 (if (and deletep (> e s))
|
|
180 (delete-region s e))))
|
428
|
181
|
|
182 (defun start-process (name buffer program &rest program-args)
|
|
183 "Start a program in a subprocess. Return the process object for it.
|
|
184 NAME is name for process. It is modified if necessary to make it unique.
|
|
185 BUFFER is the buffer or (buffer-name) to associate with the process.
|
853
|
186 Process output goes at end of that buffer, unless you specify
|
|
187 an output stream or filter function to handle the output.
|
|
188 BUFFER may be also nil, meaning that this process is not associated
|
|
189 with any buffer.
|
|
190 BUFFER can also have the form (REAL-BUFFER STDERR-BUFFER); in that case,
|
|
191 REAL-BUFFER says what to do with standard output, as above,
|
|
192 while STDERR-BUFFER says what to do with standard error in the child.
|
|
193 STDERR-BUFFER may be nil (discard standard error output, unless a stderr
|
|
194 filter is set). Note that if you do not use this form at process creation,
|
|
195 stdout and stderr will be mixed in the output buffer, and this cannot be
|
|
196 changed, even by setting a stderr filter.
|
428
|
197 Third arg is program file name. It is searched for as in the shell.
|
|
198 Remaining arguments are strings to give program as arguments.
|
853
|
199
|
|
200 The read/write coding systems used for process I/O on the process are
|
|
201 determined as follows:
|
|
202
|
|
203 1. `coding-system-for-read', `coding-system-for-write', if non-nil.
|
|
204 (Intended as a temporary overriding mechanism for use by Lisp
|
|
205 code.)
|
|
206 2. The matching value for the process name from `process-coding-system-alist',
|
|
207 if any, and if non-nil. The value may be either a single coding
|
|
208 system, used for both read and write; or a cons of read/write; or a
|
|
209 function, called to get one of the other two values.
|
|
210 3. The value of `default-process-coding-system', which should be a cons
|
|
211 of read/write coding systems, if the values are non-nil.
|
|
212 4. The coding system `undecided' for read, and `raw-text' for write.
|
|
213
|
|
214 Note that the processes of determining the read and write coding systems
|
|
215 proceed essentially independently one from the other. For example, a value
|
|
216 determined from `process-coding-system-alist' might specify a read coding
|
|
217 system but not a write coding system, in which the read coding system is as
|
|
218 specified and the write coding system comes from proceeding to step 3 (and
|
|
219 looking in `default-process-coding-system').
|
|
220
|
|
221 You can change the coding systems later on using
|
|
222 `set-process-coding-system', `set-process-input-coding-system', or
|
|
223 `set-process-output-coding-system'.
|
|
224
|
|
225 See also `set-process-filter' and `set-process-stderr-filter'."
|
428
|
226 (let (cs-r cs-w)
|
|
227 (let (ret)
|
|
228 (catch 'found
|
|
229 (let ((alist process-coding-system-alist)
|
|
230 (case-fold-search nil))
|
|
231 (while alist
|
|
232 (if (string-match (car (car alist)) program)
|
|
233 (throw 'found (setq ret (cdr (car alist)))))
|
|
234 (setq alist (cdr alist))
|
|
235 )))
|
|
236 (if (functionp ret)
|
|
237 (setq ret (funcall ret 'start-process program)))
|
|
238 (cond ((consp ret)
|
|
239 (setq cs-r (car ret)
|
|
240 cs-w (cdr ret)))
|
853
|
241 ((and ret (find-coding-system ret))
|
428
|
242 (setq cs-r ret
|
|
243 cs-w ret))))
|
|
244 (let ((coding-system-for-read
|
771
|
245 (or coding-system-for-read cs-r
|
853
|
246 (car default-process-coding-system) 'undecided))
|
428
|
247 (coding-system-for-write
|
771
|
248 (or coding-system-for-write cs-w
|
853
|
249 (cdr default-process-coding-system) 'raw-text)))
|
428
|
250 (apply 'start-process-internal name buffer program program-args)
|
|
251 )))
|
|
252
|
|
253 (defvar network-coding-system-alist nil
|
|
254 "Alist to decide a coding system to use for a network I/O operation.
|
|
255 The format is ((PATTERN . VAL) ...),
|
|
256 where PATTERN is a regular expression matching a network service name
|
|
257 or is a port number to connect to,
|
|
258 VAL is a coding system, a cons of coding systems, or a function symbol.
|
|
259 If VAL is a coding system, it is used for both decoding what received
|
|
260 from the network stream and encoding what sent to the network stream.
|
|
261 If VAL is a cons of coding systems, the car part is used for decoding,
|
|
262 and the cdr part is used for encoding.
|
|
263 If VAL is a function symbol, the function must return a coding system
|
|
264 or a cons of coding systems which are used as above.
|
|
265
|
|
266 See also the function `find-operation-coding-system'.")
|
|
267
|
|
268 (defun open-network-stream (name buffer host service &optional protocol)
|
|
269 "Open a TCP connection for a service to a host.
|
444
|
270 Return a process object to represent the connection.
|
428
|
271 Input and output work as for subprocesses; `delete-process' closes it.
|
|
272 NAME is name for process. It is modified if necessary to make it unique.
|
|
273 BUFFER is the buffer (or buffer-name) to associate with the process.
|
|
274 Process output goes at end of that buffer, unless you specify
|
|
275 an output stream or filter function to handle the output.
|
|
276 BUFFER may be also nil, meaning that this process is not associated
|
853
|
277 with any buffer.
|
428
|
278 Third arg is name of the host to connect to, or its IP address.
|
|
279 Fourth arg SERVICE is name of the service desired, or an integer
|
|
280 specifying a port number to connect to.
|
|
281 Fifth argument PROTOCOL is a network protocol. Currently 'tcp
|
|
282 (Transmission Control Protocol) and 'udp (User Datagram Protocol) are
|
|
283 supported. When omitted, 'tcp is assumed.
|
|
284
|
442
|
285 Output via `process-send-string' and input via buffer or filter (see
|
428
|
286 `set-process-filter') are stream-oriented. That means UDP datagrams are
|
|
287 not guaranteed to be sent and received in discrete packets. (But small
|
|
288 datagrams around 500 bytes that are not truncated by `process-send-string'
|
|
289 are usually fine.) Note further that UDP protocol does not guard against
|
853
|
290 lost packets.
|
|
291
|
|
292 The read/write coding systems used for process I/O on the process are
|
|
293 determined as follows:
|
|
294
|
|
295 1. `coding-system-for-read', `coding-system-for-write', if non-nil.
|
|
296 (Intended as a temporary overriding mechanism for use by Lisp
|
|
297 code.)
|
|
298 2. The matching value for the service from `network-coding-system-alist',
|
|
299 if any, and if non-nil. The value may be either a single coding
|
|
300 system, used for both read and write; or a cons of read/write; or a
|
|
301 function, called to get one of the other two values.
|
|
302 3. The value of `default-network-coding-system', which should be a cons
|
|
303 of read/write coding systems, if the values are non-nil.
|
|
304 4. The coding system `undecided' for read, and `raw-text' for write.
|
|
305
|
|
306 Note that the processes of determining the read and write coding systems
|
|
307 proceed essentially independently one from the other, as in `start-process'.
|
|
308
|
|
309 You can change the coding systems later on using
|
|
310 `set-process-coding-system', `set-process-input-coding-system', or
|
|
311 `set-process-output-coding-system'."
|
428
|
312 (let (cs-r cs-w)
|
|
313 (let (ret)
|
|
314 (catch 'found
|
|
315 (let ((alist network-coding-system-alist)
|
|
316 (case-fold-search nil)
|
|
317 pattern)
|
|
318 (while alist
|
|
319 (setq pattern (car (car alist)))
|
|
320 (and
|
|
321 (cond ((numberp pattern)
|
|
322 (and (numberp service)
|
|
323 (eq pattern service)))
|
|
324 ((stringp pattern)
|
|
325 (or (and (stringp service)
|
|
326 (string-match pattern service))
|
|
327 (and (numberp service)
|
|
328 (string-match pattern
|
|
329 (number-to-string service))))))
|
|
330 (throw 'found (setq ret (cdr (car alist)))))
|
|
331 (setq alist (cdr alist))
|
|
332 )))
|
|
333 (if (functionp ret)
|
|
334 (setq ret (funcall ret 'open-network-stream service)))
|
|
335 (cond ((consp ret)
|
|
336 (setq cs-r (car ret)
|
|
337 cs-w (cdr ret)))
|
853
|
338 ((and ret (find-coding-system ret))
|
428
|
339 (setq cs-r ret
|
|
340 cs-w ret))))
|
|
341 (let ((coding-system-for-read
|
853
|
342 (or coding-system-for-read cs-r
|
|
343 (car default-network-coding-system)
|
|
344 'undecided))
|
428
|
345 (coding-system-for-write
|
853
|
346 (or coding-system-for-write cs-w
|
|
347 (cdr default-network-coding-system)
|
|
348 'raw-text)))
|
428
|
349 (open-network-stream-internal name buffer host service protocol))))
|
|
350
|
771
|
351 (defun set-buffer-process-coding-system (decoding encoding)
|
|
352 "Set coding systems for the process associated with the current buffer.
|
|
353 DECODING is the coding system to be used to decode input from the process,
|
|
354 ENCODING is the coding system to be used to encode output to the process.
|
|
355
|
853
|
356 For a list of possible values of CODING-SYSTEM, use \\[coding-system-list]."
|
771
|
357 (interactive
|
|
358 "zCoding-system for process input: \nzCoding-system for process output: ")
|
|
359 (let ((proc (get-buffer-process (current-buffer))))
|
|
360 (if (null proc)
|
|
361 (error "no process")
|
|
362 (get-coding-system decoding)
|
|
363 (get-coding-system encoding)
|
|
364 (set-process-coding-system proc decoding encoding)))
|
|
365 (force-mode-line-update))
|
|
366
|
440
|
367 ;;; code-process.el ends here
|