comparison lisp/code-process.el @ 259:11cf20601dec r20-5b28

Import from CVS: tag r20-5b28
author cvs
date Mon, 13 Aug 2007 10:23:02 +0200
parents
children 727739f917cb
comparison
equal deleted inserted replaced
258:58424f6abf56 259:11cf20601dec
1 ;;; code-process.el --- Process coding functions for XEmacs.
2
3 ;; Copyright (C) 1985-1987, 1993, 1994, 1997 Free Software Foundation, Inc.
4 ;; Copyright (C) 1995 Ben Wing
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.
50 BUFFER can also have the form (REAL-BUFFER STDERR-FILE); in that case,
51 REAL-BUFFER says what to do with standard output, as above,
52 while STDERR-FILE says what to do with standard error in the child.
53 STDERR-FILE may be nil (discard standard error output),
54 t (mix it with ordinary output), or a file name string.
55
56 Fourth arg DISPLAYP non-nil means redisplay buffer as output is inserted.
57 Remaining arguments are strings passed as command arguments to PROGRAM.
58
59 If BUFFER is 0, `call-process' returns immediately with value nil.
60 Otherwise it waits for PROGRAM to terminate and returns a numeric exit status
61 or a signal description string.
62 If you quit, the process is killed with SIGINT, or SIGKILL if you
63 quit again."
64 (let* ((coding-system-for-read
65 (or coding-system-for-read
66 (let (ret)
67 (catch 'found
68 (let ((alist process-coding-system-alist)
69 (case-fold-search (eq system-type 'vax-vms)))
70 (while alist
71 (if (string-match (car (car alist)) program)
72 (throw 'found (setq ret (cdr (car alist))))
73 )
74 (setq alist (cdr alist))
75 )))
76 (if (functionp ret)
77 (setq ret (funcall ret 'call-process program))
78 )
79 (cond ((consp ret) (car ret))
80 ((find-coding-system ret) ret)
81 )
82 ))))
83 (apply 'call-process-internal program infile buffer displayp args)
84 ))
85
86 (defun call-process-region (start end program
87 &optional deletep buffer displayp
88 &rest args)
89 "Send text from START to END to a synchronous process running PROGRAM.
90 Delete the text if fourth arg DELETEP is non-nil.
91
92 Insert output in BUFFER before point; t means current buffer;
93 nil for BUFFER means discard it; 0 means discard and don't wait.
94 BUFFER can also have the form (REAL-BUFFER STDERR-FILE); in that case,
95 REAL-BUFFER says what to do with standard output, as above,
96 while STDERR-FILE says what to do with standard error in the child.
97 STDERR-FILE may be nil (discard standard error output),
98 t (mix it with ordinary output), or a file name string.
99
100 Sixth arg DISPLAYP non-nil means redisplay buffer as output is inserted.
101 Remaining args are passed to PROGRAM at startup as command args.
102
103 If BUFFER is 0, returns immediately with value nil.
104 Otherwise waits for PROGRAM to terminate
105 and returns a numeric exit status or a signal description string.
106 If you quit, the process is first killed with SIGINT, then with SIGKILL if
107 you quit again before the process exits."
108 (let ((temp (cond ((eq system-type 'vax-vms)
109 (make-temp-name "tmp:emacs"))
110 ((or (eq system-type 'ms-dos)
111 (eq system-type 'windows-nt))
112 (make-temp-name
113 (concat (file-name-as-directory
114 (or (getenv "TMP")
115 (getenv "TEMP")
116 ""))
117 "em")))
118 (t
119 (make-temp-name "/tmp/emacs")))))
120 (unwind-protect
121 (let (cs-r cs-w)
122 (let (ret)
123 (catch 'found
124 (let ((alist process-coding-system-alist)
125 (case-fold-search (eq system-type 'vax-vms)))
126 (while alist
127 (if (string-match (car (car alist)) program)
128 (throw 'found (setq ret (cdr (car alist)))))
129 (setq alist (cdr alist))
130 )))
131 (if (functionp ret)
132 (setq ret (funcall ret 'call-process-region program)))
133 (cond ((consp ret)
134 (setq cs-r (car ret)
135 cs-w (cdr ret)))
136 ((find-coding-system ret)
137 (setq cs-r ret
138 cs-w ret))))
139 (let ((coding-system-for-read
140 (or coding-system-for-read cs-r))
141 (coding-system-for-write
142 (or coding-system-for-write cs-w)))
143 (if (or (eq system-type 'ms-dos)
144 (eq system-type 'windows-nt))
145 (let ((buffer-file-type binary-process-output))
146 (write-region start end temp nil 'silent))
147 (write-region start end temp nil 'silent))
148 (if deletep (delete-region start end))
149 (apply #'call-process program temp buffer displayp args)))
150 (condition-case ()
151 (delete-file temp)
152 (file-error nil)))))
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 INCODE and OUTCODE specify the coding-system objects used in input/output
166 from/to the process."
167 (let (cs-r cs-w)
168 (let (ret)
169 (catch 'found
170 (let ((alist process-coding-system-alist)
171 (case-fold-search (eq system-type 'vax-vms)))
172 (while alist
173 (if (string-match (car (car alist)) program)
174 (throw 'found (setq ret (cdr (car alist)))))
175 (setq alist (cdr alist))
176 )))
177 (if (functionp ret)
178 (setq ret (funcall ret 'start-process program)))
179 (cond ((consp ret)
180 (setq cs-r (car ret)
181 cs-w (cdr ret)))
182 ((find-coding-system ret)
183 (setq cs-r ret
184 cs-w ret))))
185 (let ((coding-system-for-read
186 (or coding-system-for-read cs-r))
187 (coding-system-for-write
188 (or coding-system-for-write cs-w)))
189 (apply 'start-process-internal name buffer program program-args)
190 )))
191
192 (defvar network-coding-system-alist nil
193 "Alist to decide a coding system to use for a network I/O operation.
194 The format is ((PATTERN . VAL) ...),
195 where PATTERN is a regular expression matching a network service name
196 or is a port number to connect to,
197 VAL is a coding system, a cons of coding systems, or a function symbol.
198 If VAL is a coding system, it is used for both decoding what received
199 from the network stream and encoding what sent to the network stream.
200 If VAL is a cons of coding systems, the car part is used for decoding,
201 and the cdr part is used for encoding.
202 If VAL is a function symbol, the function must return a coding system
203 or a cons of coding systems which are used as above.
204
205 See also the function `find-operation-coding-system'.")
206
207 (defun open-network-stream (name buffer host service)
208 "Open a TCP connection for a service to a host.
209 Returns a subprocess-object to represent the connection.
210 Input and output work as for subprocesses; `delete-process' closes it.
211 Args are NAME BUFFER HOST SERVICE.
212 NAME is name for process. It is modified if necessary to make it unique.
213 BUFFER is the buffer (or buffer-name) to associate with the process.
214 Process output goes at end of that buffer, unless you specify
215 an output stream or filter function to handle the output.
216 BUFFER may be also nil, meaning that this process is not associated
217 with any buffer
218 Third arg is name of the host to connect to, or its IP address.
219 Fourth arg SERVICE is name of the service desired, or an integer
220 specifying a port number to connect to."
221 (let (cs-r cs-w)
222 (let (ret)
223 (catch 'found
224 (let ((alist network-coding-system-alist)
225 (case-fold-search (eq system-type 'vax-vms))
226 pattern)
227 (while alist
228 (setq pattern (car (car alist)))
229 (and
230 (cond ((numberp pattern)
231 (and (numberp service)
232 (eq pattern service)))
233 ((stringp pattern)
234 (or (and (stringp service)
235 (string-match pattern service))
236 (and (numberp service)
237 (string-match pattern
238 (number-to-string service))))))
239 (throw 'found (setq ret (cdr (car alist)))))
240 (setq alist (cdr alist))
241 )))
242 (if (functionp ret)
243 (setq ret (funcall ret 'open-network-stream service)))
244 (cond ((consp ret)
245 (setq cs-r (car ret)
246 cs-w (cdr ret)))
247 ((find-coding-system ret)
248 (setq cs-r ret
249 cs-w ret))))
250 (let ((coding-system-for-read
251 (or coding-system-for-read cs-r))
252 (coding-system-for-write
253 (or coding-system-for-write cs-w)))
254 (open-network-stream-internal name buffer host service))))
255
256 ;;; mule-process.el ends here