Mercurial > hg > xemacs-beta
comparison lisp/mule/mule-process.el @ 197:acd284d43ca1 r20-3b25
Import from CVS: tag r20-3b25
author | cvs |
---|---|
date | Mon, 13 Aug 2007 10:00:02 +0200 |
parents | |
children | e45d5e7c476e |
comparison
equal
deleted
inserted
replaced
196:58e0786448ca | 197:acd284d43ca1 |
---|---|
1 ;;; mule-process.el --- Process functions for XEmacs/mule. | |
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 ;; XEmacs is free software; you can redistribute it and/or modify it | |
15 ;; under the terms of the GNU General Public License as published by | |
16 ;; the Free Software Foundation; either version 2, or (at your option) | |
17 ;; any later version. | |
18 | |
19 ;; XEmacs is distributed in the hope that it will be useful, but | |
20 ;; WITHOUT ANY WARRANTY; without even the implied warranty of | |
21 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
22 ;; General Public License for more details. | |
23 | |
24 ;; You should have received a copy of the GNU General Public License | |
25 ;; along with XEmacs; see the file COPYING. If not, write to the Free | |
26 ;; Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA | |
27 ;; 02111-1307, USA. | |
28 | |
29 ;;; Code: | |
30 | |
31 (defvar process-coding-system-alist nil | |
32 "Alist to decide a coding system to use for a process I/O operation. | |
33 The format is ((PATTERN . VAL) ...), | |
34 where PATTERN is a regular expression matching a program name, | |
35 VAL is a coding system, a cons of coding systems, or a function symbol. | |
36 If VAL is a coding system, it is used for both decoding what received | |
37 from the program and encoding what sent to the program. | |
38 If VAL is a cons of coding systems, the car part is used for decoding, | |
39 and the cdr part is used for encoding. | |
40 If VAL is a function symbol, the function must return a coding system | |
41 or a cons of coding systems which are used as above.") | |
42 | |
43 (defun call-process (program &optional infile buffer displayp &rest args) | |
44 "Call PROGRAM synchronously in separate process. | |
45 The program's input comes from file INFILE (nil means `/dev/null'). | |
46 Insert output in BUFFER before point; t means current buffer; | |
47 nil for BUFFER means discard it; 0 means discard and don't wait. | |
48 BUFFER can also have the form (REAL-BUFFER STDERR-FILE); in that case, | |
49 REAL-BUFFER says what to do with standard output, as above, | |
50 while STDERR-FILE says what to do with standard error in the child. | |
51 STDERR-FILE may be nil (discard standard error output), | |
52 t (mix it with ordinary output), or a file name string. | |
53 | |
54 Fourth arg DISPLAYP non-nil means redisplay buffer as output is inserted. | |
55 Remaining arguments are strings passed as command arguments to PROGRAM. | |
56 | |
57 If BUFFER is 0, `call-process' returns immediately with value nil. | |
58 Otherwise it waits for PROGRAM to terminate and returns a numeric exit status | |
59 or a signal description string. | |
60 If you quit, the process is killed with SIGINT, or SIGKILL if you | |
61 quit again." | |
62 (let* ((coding-system-for-read | |
63 (or coding-system-for-read | |
64 (let (ret) | |
65 (catch 'found | |
66 (let ((alist process-coding-system-alist) | |
67 (case-fold-search (eq system-type 'vax-vms))) | |
68 (while alist | |
69 (if (string-match (car (car alist)) program) | |
70 (throw 'found (setq ret (cdr (car alist)))) | |
71 ) | |
72 (setq alist (cdr alist)) | |
73 ))) | |
74 (if (functionp ret) | |
75 (setq ret (funcall ret 'call-process filename)) | |
76 ) | |
77 (cond ((consp ret) (car ret)) | |
78 ((find-coding-system ret) ret) | |
79 ) | |
80 )))) | |
81 (apply 'call-process-internal program infile buffer displayp args) | |
82 )) | |
83 | |
84 (defun call-process-region (start end program | |
85 &optional deletep buffer displayp | |
86 &rest args) | |
87 "Send text from START to END to a synchronous process running PROGRAM. | |
88 Delete the text if fourth arg DELETEP is non-nil. | |
89 | |
90 Insert output in BUFFER before point; t means current buffer; | |
91 nil for BUFFER means discard it; 0 means discard and don't wait. | |
92 BUFFER can also have the form (REAL-BUFFER STDERR-FILE); in that case, | |
93 REAL-BUFFER says what to do with standard output, as above, | |
94 while STDERR-FILE says what to do with standard error in the child. | |
95 STDERR-FILE may be nil (discard standard error output), | |
96 t (mix it with ordinary output), or a file name string. | |
97 | |
98 Sixth arg DISPLAYP non-nil means redisplay buffer as output is inserted. | |
99 Remaining args are passed to PROGRAM at startup as command args. | |
100 | |
101 If BUFFER is 0, returns immediately with value nil. | |
102 Otherwise waits for PROGRAM to terminate | |
103 and returns a numeric exit status or a signal description string. | |
104 If you quit, the process is first killed with SIGINT, then with SIGKILL if | |
105 you quit again before the process exits." | |
106 (let ((temp (cond ((eq system-type 'vax-vms) | |
107 (make-temp-name "tmp:emacs")) | |
108 ((or (eq system-type 'ms-dos) | |
109 (eq system-type 'windows-nt)) | |
110 (make-temp-name | |
111 (concat (file-name-as-directory | |
112 (or (getenv "TMP") | |
113 (getenv "TEMP") | |
114 "")) | |
115 "em"))) | |
116 (t | |
117 (make-temp-name "/tmp/emacs"))))) | |
118 (unwind-protect | |
119 (let (cs-r cd-w) | |
120 (let (ret) | |
121 (catch 'found | |
122 (let ((alist process-coding-system-alist) | |
123 (case-fold-search (eq system-type 'vax-vms))) | |
124 (while alist | |
125 (if (string-match (car (car alist)) program) | |
126 (throw 'found (setq ret (cdr (car alist))))) | |
127 (setq alist (cdr alist)) | |
128 ))) | |
129 (if (functionp ret) | |
130 (setq ret (funcall ret 'call-process-region filename))) | |
131 (cond ((consp ret) | |
132 (setq cs-r (car ret) | |
133 cs-w (cdr ret))) | |
134 ((find-coding-system ret) | |
135 (setq cs-r ret | |
136 cs-w ret)))) | |
137 (let ((coding-system-for-read | |
138 (or coding-system-for-read cs-r)) | |
139 (coding-system-for-write | |
140 (or coding-system-for-write cs-w))) | |
141 (if (or (eq system-type 'ms-dos) | |
142 (eq system-type 'windows-nt)) | |
143 (let ((buffer-file-type binary-process-output)) | |
144 (write-region start end temp nil 'silent)) | |
145 (write-region start end temp nil 'silent)) | |
146 (if deletep (delete-region start end)) | |
147 (apply #'call-process program temp buffer displayp args))) | |
148 (condition-case () | |
149 (delete-file temp) | |
150 (file-error nil))))) | |
151 | |
152 (defun start-process (name buffer program &rest program-args) | |
153 "Start a program in a subprocess. Return the process object for it. | |
154 Args are NAME BUFFER PROGRAM &rest PROGRAM-ARGS | |
155 NAME is name for process. It is modified if necessary to make it unique. | |
156 BUFFER is the buffer or (buffer-name) to associate with the process. | |
157 Process output goes at end of that buffer, unless you specify | |
158 an output stream or filter function to handle the output. | |
159 BUFFER may be also nil, meaning that this process is not associated | |
160 with any buffer | |
161 Third arg is program file name. It is searched for as in the shell. | |
162 Remaining arguments are strings to give program as arguments. | |
163 INCODE and OUTCODE specify the coding-system objects used in input/output | |
164 from/to the process." | |
165 (let (cs-r cd-w) | |
166 (let (ret) | |
167 (catch 'found | |
168 (let ((alist process-coding-system-alist) | |
169 (case-fold-search (eq system-type 'vax-vms))) | |
170 (while alist | |
171 (if (string-match (car (car alist)) program) | |
172 (throw 'found (setq ret (cdr (car alist))))) | |
173 (setq alist (cdr alist)) | |
174 ))) | |
175 (if (functionp ret) | |
176 (setq ret (funcall ret 'start-process filename))) | |
177 (cond ((consp ret) | |
178 (setq cs-r (car ret) | |
179 cs-w (cdr ret))) | |
180 ((find-coding-system ret) | |
181 (setq cs-r ret | |
182 cs-w ret)))) | |
183 (let ((coding-system-for-read | |
184 (or coding-system-for-read cs-r)) | |
185 (coding-system-for-write | |
186 (or coding-system-for-write cs-w))) | |
187 (apply 'start-process-internal name buffer program program-args) | |
188 ))) | |
189 | |
190 (defvar network-coding-system-alist nil | |
191 "Alist to decide a coding system to use for a network I/O operation. | |
192 The format is ((PATTERN . VAL) ...), | |
193 where PATTERN is a regular expression matching a network service name | |
194 or is a port number to connect to, | |
195 VAL is a coding system, a cons of coding systems, or a function symbol. | |
196 If VAL is a coding system, it is used for both decoding what received | |
197 from the network stream and encoding what sent to the network stream. | |
198 If VAL is a cons of coding systems, the car part is used for decoding, | |
199 and the cdr part is used for encoding. | |
200 If VAL is a function symbol, the function must return a coding system | |
201 or a cons of coding systems which are used as above. | |
202 | |
203 See also the function `find-operation-coding-system'.") | |
204 | |
205 (defun open-network-stream (name buffer host service) | |
206 "Open a TCP connection for a service to a host. | |
207 Returns a subprocess-object to represent the connection. | |
208 Input and output work as for subprocesses; `delete-process' closes it. | |
209 Args are NAME BUFFER HOST SERVICE. | |
210 NAME is name for process. It is modified if necessary to make it unique. | |
211 BUFFER is the buffer (or buffer-name) to associate with the process. | |
212 Process output goes at end of that buffer, unless you specify | |
213 an output stream or filter function to handle the output. | |
214 BUFFER may be also nil, meaning that this process is not associated | |
215 with any buffer | |
216 Third arg is name of the host to connect to, or its IP address. | |
217 Fourth arg SERVICE is name of the service desired, or an integer | |
218 specifying a port number to connect to." | |
219 (let (cs-r cd-w) | |
220 (let (ret) | |
221 (catch 'found | |
222 (let ((alist network-coding-system-alist) | |
223 (case-fold-search (eq system-type 'vax-vms))) | |
224 (while alist | |
225 (if (string-match (car (car alist)) program) | |
226 (throw 'found (setq ret (cdr (car alist))))) | |
227 (setq alist (cdr alist)) | |
228 ))) | |
229 (if (functionp ret) | |
230 (setq ret (funcall ret 'open-network-stream filename))) | |
231 (cond ((consp ret) | |
232 (setq cs-r (car ret) | |
233 cs-w (cdr ret))) | |
234 ((find-coding-system ret) | |
235 (setq cs-r ret | |
236 cs-w ret)))) | |
237 (let ((coding-system-for-read | |
238 (or coding-system-for-read cs-r)) | |
239 (coding-system-for-write | |
240 (or coding-system-for-write cs-w))) | |
241 (open-network-stream-internal name buffer host service)))) | |
242 | |
243 ;;; mule-process.el ends here |