428
|
1 ;;; code-process.el --- Process coding functions for XEmacs.
|
|
2
|
|
3 ;; Copyright (C) 1985-1987, 1993, 1994, 1997 Free Software Foundation, Inc.
|
771
|
4 ;; Copyright (C) 1995, 2000 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.
|
|
42 If VAL is a function symbol, the function must return a coding system
|
|
43 or a cons of coding systems which are used as above.")
|
|
44
|
|
45 (defun call-process (program &optional infile buffer displayp &rest args)
|
|
46 "Call PROGRAM synchronously in separate process.
|
|
47 The program's input comes from file INFILE (nil means `/dev/null').
|
|
48 Insert output in BUFFER before point; t means current buffer;
|
|
49 nil for BUFFER means discard it; 0 means discard and don't wait.
|
837
|
50 If BUFFER is a string, then find or create a buffer with that name,
|
|
51 then insert the output in that buffer, before point.
|
428
|
52 BUFFER can also have the form (REAL-BUFFER STDERR-FILE); in that case,
|
|
53 REAL-BUFFER says what to do with standard output, as above,
|
|
54 while STDERR-FILE says what to do with standard error in the child.
|
|
55 STDERR-FILE may be nil (discard standard error output),
|
|
56 t (mix it with ordinary output), or a file name string.
|
|
57
|
|
58 Fourth arg DISPLAYP non-nil means redisplay buffer as output is inserted.
|
|
59 Remaining arguments are strings passed as command arguments to PROGRAM.
|
|
60
|
|
61 If BUFFER is 0, `call-process' returns immediately with value nil.
|
|
62 Otherwise it waits for PROGRAM to terminate and returns a numeric exit status
|
|
63 or a signal description string.
|
|
64 If you quit, the process is killed with SIGINT, or SIGKILL if you
|
|
65 quit again."
|
|
66 (let* ((coding-system-for-read
|
|
67 (or coding-system-for-read
|
|
68 (let (ret)
|
|
69 (catch 'found
|
|
70 (let ((alist process-coding-system-alist)
|
|
71 (case-fold-search nil))
|
|
72 (while alist
|
|
73 (if (string-match (car (car alist)) program)
|
|
74 (throw 'found (setq ret (cdr (car alist))))
|
|
75 )
|
|
76 (setq alist (cdr alist))
|
|
77 )))
|
|
78 (if (functionp ret)
|
|
79 (setq ret (funcall ret 'call-process program))
|
|
80 )
|
|
81 (cond ((consp ret) (car ret))
|
|
82 ((not ret) 'undecided)
|
|
83 ((find-coding-system ret) ret)
|
|
84 )
|
|
85 ))))
|
|
86 (apply 'call-process-internal program infile buffer displayp args)
|
|
87 ))
|
|
88
|
|
89 (defun call-process-region (start end program
|
|
90 &optional deletep buffer displayp
|
|
91 &rest args)
|
|
92 "Send text from START to END to a synchronous process running PROGRAM.
|
|
93 Delete the text if fourth arg DELETEP is non-nil.
|
|
94
|
|
95 Insert output in BUFFER before point; t means current buffer;
|
|
96 nil for BUFFER means discard it; 0 means discard and don't wait.
|
837
|
97 If BUFFER is a string, then find or create a buffer with that name,
|
|
98 then insert the output in that buffer, before point.
|
428
|
99 BUFFER can also have the form (REAL-BUFFER STDERR-FILE); in that case,
|
|
100 REAL-BUFFER says what to do with standard output, as above,
|
|
101 while STDERR-FILE says what to do with standard error in the child.
|
|
102 STDERR-FILE may be nil (discard standard error output),
|
|
103 t (mix it with ordinary output), or a file name string.
|
|
104
|
|
105 Sixth arg DISPLAYP non-nil means redisplay buffer as output is inserted.
|
|
106 Remaining args are passed to PROGRAM at startup as command args.
|
|
107
|
|
108 If BUFFER is 0, returns immediately with value nil.
|
|
109 Otherwise waits for PROGRAM to terminate
|
|
110 and returns a numeric exit status or a signal description string.
|
|
111 If you quit, the process is first killed with SIGINT, then with SIGKILL if
|
|
112 you quit again before the process exits."
|
|
113 (let ((temp
|
|
114 (make-temp-name
|
440
|
115 (concat (file-name-as-directory (temp-directory)) "emacs"))))
|
428
|
116 (unwind-protect
|
|
117 (let (cs-r cs-w)
|
|
118 (let (ret)
|
|
119 (catch 'found
|
|
120 (let ((alist process-coding-system-alist)
|
|
121 (case-fold-search nil))
|
|
122 (while alist
|
|
123 (if (string-match (car (car alist)) program)
|
|
124 (throw 'found (setq ret (cdr (car alist)))))
|
|
125 (setq alist (cdr alist))
|
|
126 )))
|
|
127 (if (functionp ret)
|
|
128 (setq ret (funcall ret 'call-process-region program)))
|
|
129 (cond ((consp ret)
|
|
130 (setq cs-r (car ret)
|
|
131 cs-w (cdr ret)))
|
442
|
132 ((null ret)
|
|
133 (setq cs-r buffer-file-coding-system
|
|
134 cs-w buffer-file-coding-system))
|
428
|
135 ((find-coding-system ret)
|
|
136 (setq cs-r ret
|
|
137 cs-w ret))))
|
|
138 (let ((coding-system-for-read
|
|
139 (or coding-system-for-read cs-r))
|
|
140 (coding-system-for-write
|
|
141 (or coding-system-for-write cs-w)))
|
440
|
142 (write-region start end temp nil 'silent)
|
428
|
143 (if deletep (delete-region start end))
|
|
144 (apply #'call-process program temp buffer displayp args)))
|
|
145 (ignore-file-errors (delete-file temp)))))
|
|
146
|
|
147 (defun start-process (name buffer program &rest program-args)
|
|
148 "Start a program in a subprocess. Return the process object for it.
|
|
149 Args are NAME BUFFER PROGRAM &rest PROGRAM-ARGS
|
|
150 NAME is name for process. It is modified if necessary to make it unique.
|
|
151 BUFFER is the buffer or (buffer-name) to associate with the process.
|
837
|
152 Process output goes at end of that buffer, unless you specify
|
|
153 an output stream or filter function to handle the output.
|
|
154 BUFFER may also be nil, meaning that this process is not associated
|
|
155 with any buffer.
|
428
|
156 Third arg is program file name. It is searched for as in the shell.
|
|
157 Remaining arguments are strings to give program as arguments.
|
|
158 INCODE and OUTCODE specify the coding-system objects used in input/output
|
|
159 from/to the process."
|
|
160 (let (cs-r cs-w)
|
|
161 (let (ret)
|
|
162 (catch 'found
|
|
163 (let ((alist process-coding-system-alist)
|
|
164 (case-fold-search nil))
|
|
165 (while alist
|
|
166 (if (string-match (car (car alist)) program)
|
|
167 (throw 'found (setq ret (cdr (car alist)))))
|
|
168 (setq alist (cdr alist))
|
|
169 )))
|
|
170 (if (functionp ret)
|
|
171 (setq ret (funcall ret 'start-process program)))
|
|
172 (cond ((consp ret)
|
|
173 (setq cs-r (car ret)
|
|
174 cs-w (cdr ret)))
|
|
175 ((find-coding-system ret)
|
|
176 (setq cs-r ret
|
|
177 cs-w ret))))
|
|
178 (let ((coding-system-for-read
|
771
|
179 (or coding-system-for-read cs-r
|
|
180 (car default-process-coding-system)))
|
428
|
181 (coding-system-for-write
|
771
|
182 (or coding-system-for-write cs-w
|
|
183 (cdr default-process-coding-system))))
|
428
|
184 (apply 'start-process-internal name buffer program program-args)
|
|
185 )))
|
|
186
|
|
187 (defvar network-coding-system-alist nil
|
|
188 "Alist to decide a coding system to use for a network I/O operation.
|
|
189 The format is ((PATTERN . VAL) ...),
|
|
190 where PATTERN is a regular expression matching a network service name
|
|
191 or is a port number to connect to,
|
|
192 VAL is a coding system, a cons of coding systems, or a function symbol.
|
|
193 If VAL is a coding system, it is used for both decoding what received
|
|
194 from the network stream and encoding what sent to the network stream.
|
|
195 If VAL is a cons of coding systems, the car part is used for decoding,
|
|
196 and the cdr part is used for encoding.
|
|
197 If VAL is a function symbol, the function must return a coding system
|
|
198 or a cons of coding systems which are used as above.
|
|
199
|
|
200 See also the function `find-operation-coding-system'.")
|
|
201
|
|
202 (defun open-network-stream (name buffer host service &optional protocol)
|
|
203 "Open a TCP connection for a service to a host.
|
444
|
204 Return a process object to represent the connection.
|
428
|
205 Input and output work as for subprocesses; `delete-process' closes it.
|
|
206 Args are NAME BUFFER HOST SERVICE.
|
|
207 NAME is name for process. It is modified if necessary to make it unique.
|
|
208 BUFFER is the buffer (or buffer-name) to associate with the process.
|
|
209 Process output goes at end of that buffer, unless you specify
|
|
210 an output stream or filter function to handle the output.
|
|
211 BUFFER may be also nil, meaning that this process is not associated
|
|
212 with any buffer
|
|
213 Third arg is name of the host to connect to, or its IP address.
|
|
214 Fourth arg SERVICE is name of the service desired, or an integer
|
|
215 specifying a port number to connect to.
|
|
216 Fifth argument PROTOCOL is a network protocol. Currently 'tcp
|
|
217 (Transmission Control Protocol) and 'udp (User Datagram Protocol) are
|
|
218 supported. When omitted, 'tcp is assumed.
|
|
219
|
442
|
220 Output via `process-send-string' and input via buffer or filter (see
|
428
|
221 `set-process-filter') are stream-oriented. That means UDP datagrams are
|
|
222 not guaranteed to be sent and received in discrete packets. (But small
|
|
223 datagrams around 500 bytes that are not truncated by `process-send-string'
|
|
224 are usually fine.) Note further that UDP protocol does not guard against
|
|
225 lost packets."
|
|
226 (let (cs-r cs-w)
|
|
227 (let (ret)
|
|
228 (catch 'found
|
|
229 (let ((alist network-coding-system-alist)
|
|
230 (case-fold-search nil)
|
|
231 pattern)
|
|
232 (while alist
|
|
233 (setq pattern (car (car alist)))
|
|
234 (and
|
|
235 (cond ((numberp pattern)
|
|
236 (and (numberp service)
|
|
237 (eq pattern service)))
|
|
238 ((stringp pattern)
|
|
239 (or (and (stringp service)
|
|
240 (string-match pattern service))
|
|
241 (and (numberp service)
|
|
242 (string-match pattern
|
|
243 (number-to-string service))))))
|
|
244 (throw 'found (setq ret (cdr (car alist)))))
|
|
245 (setq alist (cdr alist))
|
|
246 )))
|
|
247 (if (functionp ret)
|
|
248 (setq ret (funcall ret 'open-network-stream service)))
|
|
249 (cond ((consp ret)
|
|
250 (setq cs-r (car ret)
|
|
251 cs-w (cdr ret)))
|
|
252 ((find-coding-system ret)
|
|
253 (setq cs-r ret
|
|
254 cs-w ret))))
|
|
255 (let ((coding-system-for-read
|
|
256 (or coding-system-for-read cs-r))
|
|
257 (coding-system-for-write
|
|
258 (or coding-system-for-write cs-w)))
|
|
259 (open-network-stream-internal name buffer host service protocol))))
|
|
260
|
771
|
261 (defun set-buffer-process-coding-system (decoding encoding)
|
|
262 "Set coding systems for the process associated with the current buffer.
|
|
263 DECODING is the coding system to be used to decode input from the process,
|
|
264 ENCODING is the coding system to be used to encode output to the process.
|
|
265
|
|
266 For a list of possible values of CODING-SYSTEM, use \\[list-coding-systems]."
|
|
267 (interactive
|
|
268 "zCoding-system for process input: \nzCoding-system for process output: ")
|
|
269 (let ((proc (get-buffer-process (current-buffer))))
|
|
270 (if (null proc)
|
|
271 (error "no process")
|
|
272 (get-coding-system decoding)
|
|
273 (get-coding-system encoding)
|
|
274 (set-process-coding-system proc decoding encoding)))
|
|
275 (force-mode-line-update))
|
|
276
|
440
|
277 ;;; code-process.el ends here
|