1 ;;; code-process.el --- Process coding functions for XEmacs.
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
10 ;; Maintainer: XEmacs Development Team
11 ;; Keywords: mule, multilingual, coding system, process
13 ;; This file is part of SXEmacs.
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.
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.
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/>.
30 ;; This file has some similarities to code-files.el.
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.")
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.
57 Fourth arg DISPLAYP non-nil means redisplay buffer as output is inserted.
58 Remaining arguments are strings passed as command arguments to PROGRAM.
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
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
74 (let ((alist process-coding-system-alist)
75 (case-fold-search nil))
77 (if (string-match (car (car alist)) program)
78 (throw 'found (setq ret (cdr (car alist))))
80 (setq alist (cdr alist))
83 (setq ret (funcall ret 'call-process program))
85 (cond ((consp ret) (car ret))
86 ((not ret) 'undecided)
87 ((find-coding-system ret) ret)
90 (apply 'call-process-internal program infile buffer displayp args)
93 (defun call-process-region (start end program
94 &optional deletep buffer displayp
96 "Send text from START to END to a synchronous process running PROGRAM.
97 Delete the text if fourth arg DELETEP is non-nil.
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.
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.
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.
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."
122 (concat (file-name-as-directory (temp-directory)) "emacs"))))
127 (let ((alist process-coding-system-alist)
128 (case-fold-search nil))
130 (if (string-match (car (car alist)) program)
131 (throw 'found (setq ret (cdr (car alist)))))
132 (setq alist (cdr alist))
135 (setq ret (funcall ret 'call-process-region program)))
140 (setq cs-r buffer-file-coding-system
141 cs-w buffer-file-coding-system))
142 ((find-coding-system 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)))))
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
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.
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
174 (let ((alist process-coding-system-alist)
175 (case-fold-search nil))
177 (if (string-match (car (car alist)) program)
178 (throw 'found (setq ret (cdr (car alist)))))
179 (setq alist (cdr alist))
182 (setq ret (funcall ret 'start-process program)))
186 ((find-coding-system 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)
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.
209 See also the function `find-operation-coding-system'.")
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
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.
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
238 (let ((alist network-coding-system-alist)
239 (case-fold-search nil)
242 (setq pattern (car (car alist)))
244 (cond ((numberp pattern)
245 (and (numberp service)
246 (eq pattern service)))
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))
257 (setq ret (funcall ret 'open-network-stream service)))
261 ((find-coding-system 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))))
270 ;;; code-process.el ends here