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