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 XEmacs.
15 ;; XEmacs is free software; you can redistribute it and/or modify it
16 ;; under the terms of the GNU General Public License as published by
17 ;; the Free Software Foundation; either version 2, or (at your option)
20 ;; XEmacs is distributed in the hope that it will be useful, but
21 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
22 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
23 ;; General Public License for more details.
25 ;; You should have received a copy of the GNU General Public License
26 ;; along with XEmacs; see the file COPYING. If not, write to the Free
27 ;; Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
32 ;; This file has some similarities to code-files.el.
36 (defvar process-coding-system-alist nil
37 "Alist to decide a coding system to use for a process I/O operation.
38 The format is ((PATTERN . VAL) ...),
39 where PATTERN is a regular expression matching a program name,
40 VAL is a coding system, a cons of coding systems, or a function symbol.
41 If VAL is a coding system, it is used for both decoding what received
42 from the program and encoding what sent to the program.
43 If VAL is a cons of coding systems, the car part is used for decoding,
44 and the cdr part is used for encoding.
45 If VAL is a function symbol, the function must return a coding system
46 or a cons of coding systems which are used as above.")
48 (defun call-process (program &optional infile buffer displayp &rest args)
49 "Call PROGRAM synchronously in separate process.
50 The program's input comes from file INFILE (nil means `/dev/null').
51 Insert output in BUFFER before point; t means current buffer;
52 nil for BUFFER means discard it; 0 means discard and don't wait.
53 BUFFER can also have the form (REAL-BUFFER STDERR-FILE); in that case,
54 REAL-BUFFER says what to do with standard output, as above,
55 while STDERR-FILE says what to do with standard error in the child.
56 STDERR-FILE may be nil (discard standard error output),
57 t (mix it with ordinary output), or a file name string.
59 Fourth arg DISPLAYP non-nil means redisplay buffer as output is inserted.
60 Remaining arguments are strings passed as command arguments to PROGRAM.
62 If BUFFER is 0, `call-process' returns immediately with value nil.
63 Otherwise it waits for PROGRAM to terminate and returns a numeric exit status
64 or a signal description string.
65 If you quit, the process is killed with SIGINT, or SIGKILL if you
68 Coding systems are taken from `coding-system-for-read' for input and
69 `coding-system-for-write' for output if those variables are bound.
70 Otherwise they are looked up in `process-coding-system-alist'. If not
71 found, they default to `nil' for both input and output."
72 (let* ((coding-system-for-read
73 (or coding-system-for-read
76 (let ((alist process-coding-system-alist)
77 (case-fold-search nil))
79 (if (string-match (car (car alist)) program)
80 (throw 'found (setq ret (cdr (car alist))))
82 (setq alist (cdr alist))
85 (setq ret (funcall ret 'call-process program))
87 (cond ((consp ret) (car ret))
88 ((not ret) 'undecided)
89 ((find-coding-system ret) ret)
92 (apply 'call-process-internal program infile buffer displayp args)
95 (defun call-process-region (start end program
96 &optional deletep buffer displayp
98 "Send text from START to END to a synchronous process running PROGRAM.
99 Delete the text if fourth arg DELETEP is non-nil.
101 Insert output in BUFFER before point; t means current buffer;
102 nil for BUFFER means discard it; 0 means discard and don't wait.
103 BUFFER can also have the form (REAL-BUFFER STDERR-FILE); in that case,
104 REAL-BUFFER says what to do with standard output, as above,
105 while STDERR-FILE says what to do with standard error in the child.
106 STDERR-FILE may be nil (discard standard error output),
107 t (mix it with ordinary output), or a file name string.
109 Sixth arg DISPLAYP non-nil means redisplay buffer as output is inserted.
110 Remaining args are passed to PROGRAM at startup as command args.
112 If BUFFER is 0, returns immediately with value nil.
113 Otherwise waits for PROGRAM to terminate
114 and returns a numeric exit status or a signal description string.
115 If you quit, the process is first killed with SIGINT, then with SIGKILL if
116 you quit again before the process exits.
118 Coding systems are taken from `coding-system-for-read' for input and
119 `coding-system-for-write' for output if those variables are bound.
120 Otherwise they are looked up in `process-coding-system-alist'. If not
121 found, they default to `nil' for both input and output."
124 (concat (file-name-as-directory (temp-directory)) "emacs"))))
129 (let ((alist process-coding-system-alist)
130 (case-fold-search nil))
132 (if (string-match (car (car alist)) program)
133 (throw 'found (setq ret (cdr (car alist)))))
134 (setq alist (cdr alist))
137 (setq ret (funcall ret 'call-process-region program)))
142 (setq cs-r buffer-file-coding-system
143 cs-w buffer-file-coding-system))
144 ((find-coding-system ret)
147 (let ((coding-system-for-read
148 (or coding-system-for-read cs-r))
149 (coding-system-for-write
150 (or coding-system-for-write cs-w)))
151 (write-region start end temp nil 'silent)
152 (if deletep (delete-region start end))
153 (apply #'call-process program temp buffer displayp args)))
154 (ignore-file-errors (delete-file temp)))))
156 (defun start-process (name buffer program &rest program-args)
157 "Start a program in a subprocess. Return the process object for it.
158 Args are NAME BUFFER PROGRAM &rest PROGRAM-ARGS
159 NAME is name for process. It is modified if necessary to make it unique.
160 BUFFER is the buffer or (buffer-name) to associate with the process.
161 Process output goes at end of that buffer, unless you specify
162 an output stream or filter function to handle the output.
163 BUFFER may be also nil, meaning that this process is not associated
165 Third arg is program file name. It is searched for as in the shell.
166 Remaining arguments are strings to give program as arguments.
168 Coding systems are taken from `coding-system-for-read' for input and
169 `coding-system-for-write' for output if those variables are bound.
170 Otherwise they are looked up in `process-coding-system-alist'. If not
171 found, they default to `undecided' for input and `nil' (binary) for
176 (let ((alist process-coding-system-alist)
177 (case-fold-search nil))
179 (if (string-match (car (car alist)) program)
180 (throw 'found (setq ret (cdr (car alist)))))
181 (setq alist (cdr alist))
184 (setq ret (funcall ret 'start-process program)))
188 ((find-coding-system ret)
191 (let ((coding-system-for-read
192 (or coding-system-for-read cs-r 'undecided))
193 (coding-system-for-write
194 (or coding-system-for-write cs-w)))
195 (apply 'start-process-internal name buffer program program-args)
198 (defvar network-coding-system-alist nil
199 "Alist to decide a coding system to use for a network I/O operation.
200 The format is ((PATTERN . VAL) ...),
201 where PATTERN is a regular expression matching a network service name
202 or is a port number to connect to,
203 VAL is a coding system, a cons of coding systems, or a function symbol.
204 If VAL is a coding system, it is used for both decoding what received
205 from the network stream and encoding what sent to the network stream.
206 If VAL is a cons of coding systems, the car part is used for decoding,
207 and the cdr part is used for encoding.
208 If VAL is a function symbol, the function must return a coding system
209 or a cons of coding systems which are used as above.
211 See also the function `find-operation-coding-system'.")
213 (defun open-network-stream (name buffer host service &optional protocol)
214 "Open a TCP connection for a service to a host.
215 Return a process object to represent the connection.
216 Input and output work as for subprocesses; `delete-process' closes it.
217 Args are NAME BUFFER HOST SERVICE.
218 NAME is name for process. It is modified if necessary to make it unique.
219 BUFFER is the buffer (or buffer-name) to associate with the process.
220 Process output goes at end of that buffer, unless you specify
221 an output stream or filter function to handle the output.
222 BUFFER may be also nil, meaning that this process is not associated
224 Third arg is name of the host to connect to, or its IP address.
225 Fourth arg SERVICE is name of the service desired, or an integer
226 specifying a port number to connect to.
227 Fifth argument PROTOCOL is a network protocol. Currently 'tcp
228 (Transmission Control Protocol) and 'udp (User Datagram Protocol) are
229 supported. When omitted, 'tcp is assumed.
231 Output via `process-send-string' and input via buffer or filter (see
232 `set-process-filter') are stream-oriented. That means UDP datagrams are
233 not guaranteed to be sent and received in discrete packets. (But small
234 datagrams around 500 bytes that are not truncated by `process-send-string'
235 are usually fine.) Note further that UDP protocol does not guard against
240 (let ((alist network-coding-system-alist)
241 (case-fold-search nil)
244 (setq pattern (car (car alist)))
246 (cond ((numberp pattern)
247 (and (numberp service)
248 (eq pattern service)))
250 (or (and (stringp service)
251 (string-match pattern service))
252 (and (numberp service)
253 (string-match pattern
254 (number-to-string service))))))
255 (throw 'found (setq ret (cdr (car alist)))))
256 (setq alist (cdr alist))
259 (setq ret (funcall ret 'open-network-stream service)))
263 ((find-coding-system ret)
266 (let ((coding-system-for-read
267 (or coding-system-for-read cs-r))
268 (coding-system-for-write
269 (or coding-system-for-write cs-w)))
270 (open-network-stream-internal name buffer host service protocol))))
272 ;;; code-process.el ends here