1 ;;; code-process.el --- Process coding functions for XEmacs.
3 ;; Copyright (C) 1985-1987, 1993, 1994, 1997 Free Software Foundation, Inc.
4 ;; Copyright (C) 1995 Ben Wing
5 ;; Copyright (C) 1997 MORIOKA Tomohiko
9 ;; Maintainer: XEmacs Development Team
10 ;; Keywords: mule, multilingual, coding system, process
12 ;; This file is part of XEmacs.
14 ;; This file is very similar to code-process.el
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)
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.
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
34 (defvar buffer-file-type)
35 (defvar binary-process-output))
37 (defvar process-coding-system-alist nil
38 "Alist to decide a coding system to use for a process I/O operation.
39 The format is ((PATTERN . VAL) ...),
40 where PATTERN is a regular expression matching a program name,
41 VAL is a coding system, a cons of coding systems, or a function symbol.
42 If VAL is a coding system, it is used for both decoding what received
43 from the program and encoding what sent to the program.
44 If VAL is a cons of coding systems, the car part is used for decoding,
45 and the cdr part is used for encoding.
46 If VAL is a function symbol, the function must return a coding system
47 or a cons of coding systems which are used as above.")
49 (defun call-process (program &optional infile buffer displayp &rest args)
50 "Call PROGRAM synchronously in separate process.
51 The program's input comes from file INFILE (nil means `/dev/null').
52 Insert output in BUFFER before point; t means current buffer;
53 nil for BUFFER means discard it; 0 means discard and don't wait.
54 BUFFER can also have the form (REAL-BUFFER STDERR-FILE); in that case,
55 REAL-BUFFER says what to do with standard output, as above,
56 while STDERR-FILE says what to do with standard error in the child.
57 STDERR-FILE may be nil (discard standard error output),
58 t (mix it with ordinary output), or a file name string.
60 Fourth arg DISPLAYP non-nil means redisplay buffer as output is inserted.
61 Remaining arguments are strings passed as command arguments to PROGRAM.
63 If BUFFER is 0, `call-process' returns immediately with value nil.
64 Otherwise it waits for PROGRAM to terminate and returns a numeric exit status
65 or a signal description string.
66 If you quit, the process is killed with SIGINT, or SIGKILL if you
68 (let* ((coding-system-for-read
69 (or coding-system-for-read
72 (let ((alist process-coding-system-alist)
73 (case-fold-search nil))
75 (if (string-match (car (car alist)) program)
76 (throw 'found (setq ret (cdr (car alist))))
78 (setq alist (cdr alist))
81 (setq ret (funcall ret 'call-process program))
83 (cond ((consp ret) (car ret))
84 ((not ret) 'undecided)
85 ((find-coding-system ret) ret)
88 (apply 'call-process-internal program infile buffer displayp args)
91 (defun call-process-region (start end program
92 &optional deletep buffer displayp
94 "Send text from START to END to a synchronous process running PROGRAM.
95 Delete the text if fourth arg DELETEP is non-nil.
97 Insert output in BUFFER before point; t means current buffer;
98 nil for BUFFER means discard it; 0 means discard and don't wait.
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.
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.
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."
115 (concat (file-name-as-directory (temp-directory))
116 (if (memq system-type '(ms-dos windows-nt)) "em" "emacs")))))
121 (let ((alist process-coding-system-alist)
122 (case-fold-search nil))
124 (if (string-match (car (car alist)) program)
125 (throw 'found (setq ret (cdr (car alist)))))
126 (setq alist (cdr alist))
129 (setq ret (funcall ret 'call-process-region program)))
133 ((find-coding-system ret)
136 (let ((coding-system-for-read
137 (or coding-system-for-read cs-r))
138 (coding-system-for-write
139 (or coding-system-for-write cs-w)))
140 (if (memq system-type '(ms-dos windows-nt))
141 (let ((buffer-file-type binary-process-output))
142 (write-region start end temp nil 'silent))
143 (write-region start end temp nil 'silent))
144 (if deletep (delete-region start end))
145 (apply #'call-process program temp buffer displayp args)))
146 (ignore-file-errors (delete-file temp)))))
148 (defun start-process (name buffer program &rest program-args)
149 "Start a program in a subprocess. Return the process object for it.
150 Args are NAME BUFFER PROGRAM &rest PROGRAM-ARGS
151 NAME is name for process. It is modified if necessary to make it unique.
152 BUFFER is the buffer or (buffer-name) to associate with the process.
153 Process output goes at end of that buffer, unless you specify
154 an output stream or filter function to handle the output.
155 BUFFER may be also nil, meaning that this process is not associated
157 Third arg is program file name. It is searched for as in the shell.
158 Remaining arguments are strings to give program as arguments.
159 INCODE and OUTCODE specify the coding-system objects used in input/output
160 from/to the process."
164 (let ((alist process-coding-system-alist)
165 (case-fold-search nil))
167 (if (string-match (car (car alist)) program)
168 (throw 'found (setq ret (cdr (car alist)))))
169 (setq alist (cdr alist))
172 (setq ret (funcall ret 'start-process program)))
176 ((find-coding-system ret)
179 (let ((coding-system-for-read
180 (or coding-system-for-read cs-r 'undecided))
181 (coding-system-for-write
182 (or coding-system-for-write cs-w)))
183 (apply 'start-process-internal name buffer program program-args)
186 (defvar network-coding-system-alist nil
187 "Alist to decide a coding system to use for a network I/O operation.
188 The format is ((PATTERN . VAL) ...),
189 where PATTERN is a regular expression matching a network service name
190 or is a port number to connect to,
191 VAL is a coding system, a cons of coding systems, or a function symbol.
192 If VAL is a coding system, it is used for both decoding what received
193 from the network stream and encoding what sent to the network stream.
194 If VAL is a cons of coding systems, the car part is used for decoding,
195 and the cdr part is used for encoding.
196 If VAL is a function symbol, the function must return a coding system
197 or a cons of coding systems which are used as above.
199 See also the function `find-operation-coding-system'.")
201 (defun open-network-stream (name buffer host service)
202 "Open a TCP connection for a service to a host.
203 Returns a subprocess-object to represent the connection.
204 Input and output work as for subprocesses; `delete-process' closes it.
205 Args are NAME BUFFER HOST SERVICE.
206 NAME is name for process. It is modified if necessary to make it unique.
207 BUFFER is the buffer (or buffer-name) to associate with the process.
208 Process output goes at end of that buffer, unless you specify
209 an output stream or filter function to handle the output.
210 BUFFER may be also nil, meaning that this process is not associated
212 Third arg is name of the host to connect to, or its IP address.
213 Fourth arg SERVICE is name of the service desired, or an integer
214 specifying a port number to connect to."
218 (let ((alist network-coding-system-alist)
219 (case-fold-search nil)
222 (setq pattern (car (car alist)))
224 (cond ((numberp pattern)
225 (and (numberp service)
226 (eq pattern service)))
228 (or (and (stringp service)
229 (string-match pattern service))
230 (and (numberp service)
231 (string-match pattern
232 (number-to-string service))))))
233 (throw 'found (setq ret (cdr (car alist)))))
234 (setq alist (cdr alist))
237 (setq ret (funcall ret 'open-network-stream service)))
241 ((find-coding-system ret)
244 (let ((coding-system-for-read
245 (or coding-system-for-read cs-r))
246 (coding-system-for-write
247 (or coding-system-for-write cs-w)))
248 (open-network-stream-internal name buffer host service))))
250 ;;; mule-process.el ends here