Contents in 1999-06-04-13 of release-21-2.
[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 (eval-when-compile
34   (defvar buffer-file-type)
35   (defvar binary-process-output))
36
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.")
48
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.
59
60 Fourth arg DISPLAYP non-nil means redisplay buffer as output is inserted.
61 Remaining arguments are strings passed as command arguments to PROGRAM.
62
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
67  quit again."
68   (let* ((coding-system-for-read
69           (or coding-system-for-read
70               (let (ret)
71                 (catch 'found
72                   (let ((alist process-coding-system-alist)
73                         (case-fold-search nil))
74                     (while alist
75                       (if (string-match (car (car alist)) program)
76                           (throw 'found (setq ret (cdr (car alist))))
77                         )
78                       (setq alist (cdr alist))
79                       )))
80                 (if (functionp ret)
81                     (setq ret (funcall ret 'call-process program))
82                   )
83                 (cond ((consp ret) (car ret))
84                       ((not ret) 'undecided)
85                       ((find-coding-system ret) ret)
86                       )
87                 ))))
88     (apply 'call-process-internal program infile buffer displayp args)
89     ))
90
91 (defun call-process-region (start end program
92                                   &optional deletep buffer displayp
93                                   &rest args)
94   "Send text from START to END to a synchronous process running PROGRAM.
95 Delete the text if fourth arg DELETEP is non-nil.
96
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.
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
115           (concat (file-name-as-directory (temp-directory))
116                   (if (memq system-type '(ms-dos windows-nt)) "em" "emacs")))))
117     (unwind-protect
118         (let (cs-r cs-w)
119           (let (ret)
120             (catch 'found
121               (let ((alist process-coding-system-alist)
122                     (case-fold-search nil))
123                 (while alist
124                   (if (string-match (car (car alist)) program)
125                       (throw 'found (setq ret (cdr (car alist)))))
126                   (setq alist (cdr alist))
127                   )))
128             (if (functionp ret)
129                 (setq ret (funcall ret 'call-process-region program)))
130             (cond ((consp ret)
131                    (setq cs-r (car ret)
132                          cs-w (cdr ret)))
133                   ((find-coding-system ret)
134                    (setq cs-r ret
135                          cs-w 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)))))
147
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
156  with any buffer
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."
161   (let (cs-r cs-w)
162     (let (ret)
163       (catch 'found
164         (let ((alist process-coding-system-alist)
165               (case-fold-search nil))
166           (while alist
167             (if (string-match (car (car alist)) program)
168                 (throw 'found (setq ret (cdr (car alist)))))
169             (setq alist (cdr alist))
170             )))
171       (if (functionp ret)
172           (setq ret (funcall ret 'start-process program)))
173       (cond ((consp ret)
174              (setq cs-r (car ret)
175                    cs-w (cdr ret)))
176             ((find-coding-system ret)
177              (setq cs-r ret
178                    cs-w 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)
184       )))
185
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.
198
199 See also the function `find-operation-coding-system'.")
200
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
211  with any buffer
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."
215   (let (cs-r cs-w)
216     (let (ret)
217       (catch 'found
218         (let ((alist network-coding-system-alist)
219               (case-fold-search nil)
220               pattern)
221           (while alist
222             (setq pattern (car (car alist)))
223             (and
224              (cond ((numberp pattern)
225                     (and (numberp service)
226                          (eq pattern service)))
227                    ((stringp pattern)
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))
235             )))
236       (if (functionp ret)
237           (setq ret (funcall ret 'open-network-stream service)))
238       (cond ((consp ret)
239              (setq cs-r (car ret)
240                    cs-w (cdr ret)))
241             ((find-coding-system ret)
242              (setq cs-r ret
243                    cs-w 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))))
249
250 ;;; mule-process.el ends here