21.4.14 "Reasonable Discussion".
[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, 2003
4 ;;               Free Software Foundation, Inc.
5 ;; Copyright (C) 1995 Ben Wing
6 ;; Copyright (C) 1997 MORIOKA Tomohiko
7
8 ;; Author: Ben Wing
9 ;;         MORIOKA Tomohiko
10 ;; Maintainer: XEmacs Development Team
11 ;; Keywords: mule, multilingual, coding system, process
12
13 ;; This file is part of XEmacs.
14
15 ;; XEmacs is free software; you can redistribute it and/or modify it
16 ;; under the terms of the GNU General Public License as published by
17 ;; the Free Software Foundation; either version 2, or (at your option)
18 ;; any later version.
19
20 ;; XEmacs is distributed in the hope that it will be useful, but
21 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
22 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
23 ;; General Public License for more details.
24
25 ;; You should have received a copy of the GNU General Public License
26 ;; along with XEmacs; see the file COPYING.  If not, write to the Free
27 ;; Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
28 ;; 02111-1307, USA.
29
30 ;;; Commentary:
31
32 ;; This file has some similarities to code-files.el.
33
34 ;;; Code:
35
36 (defvar process-coding-system-alist nil
37   "Alist to decide a coding system to use for a process I/O operation.
38 The format is ((PATTERN . VAL) ...),
39 where PATTERN is a regular expression matching a program name,
40 VAL is a coding system, a cons of coding systems, or a function symbol.
41 If VAL is a coding system, it is used for both decoding what received
42 from the program and encoding what sent to the program.
43 If VAL is a cons of coding systems, the car part is used for decoding,
44 and the cdr part is used for encoding.
45 If VAL is a function symbol, the function must return a coding system
46 or a cons of coding systems which are used as above.")
47
48 (defun call-process (program &optional infile buffer displayp &rest args)
49   "Call PROGRAM synchronously in separate process.
50 The program's input comes from file INFILE (nil means `/dev/null').
51 Insert output in BUFFER before point; t means current buffer;
52  nil for BUFFER means discard it; 0 means discard and don't wait.
53 BUFFER can also have the form (REAL-BUFFER STDERR-FILE); in that case,
54 REAL-BUFFER says what to do with standard output, as above,
55 while STDERR-FILE says what to do with standard error in the child.
56 STDERR-FILE may be nil (discard standard error output),
57 t (mix it with ordinary output), or a file name string.
58
59 Fourth arg DISPLAYP non-nil means redisplay buffer as output is inserted.
60 Remaining arguments are strings passed as command arguments to PROGRAM.
61
62 If BUFFER is 0, `call-process' returns immediately with value nil.
63 Otherwise it waits for PROGRAM to terminate and returns a numeric exit status
64  or a signal description string.
65 If you quit, the process is killed with SIGINT, or SIGKILL if you
66  quit again.
67
68 Coding systems are taken from `coding-system-for-read' for input and
69 `coding-system-for-write' for output if those variables are bound.
70 Otherwise they are looked up in `process-coding-system-alist'.  If not
71 found, they default to `nil' for both input and output."
72   (let* ((coding-system-for-read
73           (or coding-system-for-read
74               (let (ret)
75                 (catch 'found
76                   (let ((alist process-coding-system-alist)
77                         (case-fold-search nil))
78                     (while alist
79                       (if (string-match (car (car alist)) program)
80                           (throw 'found (setq ret (cdr (car alist))))
81                         )
82                       (setq alist (cdr alist))
83                       )))
84                 (if (functionp ret)
85                     (setq ret (funcall ret 'call-process program))
86                   )
87                 (cond ((consp ret) (car ret))
88                       ((not ret) 'undecided)
89                       ((find-coding-system ret) ret)
90                       )
91                 ))))
92     (apply 'call-process-internal program infile buffer displayp args)
93     ))
94
95 (defun call-process-region (start end program
96                                   &optional deletep buffer displayp
97                                   &rest args)
98   "Send text from START to END to a synchronous process running PROGRAM.
99 Delete the text if fourth arg DELETEP is non-nil.
100
101 Insert output in BUFFER before point; t means current buffer;
102  nil for BUFFER means discard it; 0 means discard and don't wait.
103 BUFFER can also have the form (REAL-BUFFER STDERR-FILE); in that case,
104 REAL-BUFFER says what to do with standard output, as above,
105 while STDERR-FILE says what to do with standard error in the child.
106 STDERR-FILE may be nil (discard standard error output),
107 t (mix it with ordinary output), or a file name string.
108
109 Sixth arg DISPLAYP non-nil means redisplay buffer as output is inserted.
110 Remaining args are passed to PROGRAM at startup as command args.
111
112 If BUFFER is 0, returns immediately with value nil.
113 Otherwise waits for PROGRAM to terminate
114 and returns a numeric exit status or a signal description string.
115 If you quit, the process is first killed with SIGINT, then with SIGKILL if
116 you quit again before the process exits.
117
118 Coding systems are taken from `coding-system-for-read' for input and
119 `coding-system-for-write' for output if those variables are bound.
120 Otherwise they are looked up in `process-coding-system-alist'.  If not
121 found, they default to `nil' for both input and output."
122   (let ((temp
123          (make-temp-name
124           (concat (file-name-as-directory (temp-directory)) "emacs"))))
125     (unwind-protect
126         (let (cs-r cs-w)
127           (let (ret)
128             (catch 'found
129               (let ((alist process-coding-system-alist)
130                     (case-fold-search nil))
131                 (while alist
132                   (if (string-match (car (car alist)) program)
133                       (throw 'found (setq ret (cdr (car alist)))))
134                   (setq alist (cdr alist))
135                   )))
136             (if (functionp ret)
137                 (setq ret (funcall ret 'call-process-region program)))
138             (cond ((consp ret)
139                    (setq cs-r (car ret)
140                          cs-w (cdr ret)))
141                   ((null ret)
142                    (setq cs-r buffer-file-coding-system
143                          cs-w buffer-file-coding-system))
144                   ((find-coding-system ret)
145                    (setq cs-r ret
146                          cs-w ret))))
147           (let ((coding-system-for-read
148                  (or coding-system-for-read cs-r))
149                 (coding-system-for-write
150                  (or coding-system-for-write cs-w)))
151             (write-region start end temp nil 'silent)
152             (if deletep (delete-region start end))
153             (apply #'call-process program temp buffer displayp args)))
154       (ignore-file-errors (delete-file temp)))))
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
168 Coding systems are taken from `coding-system-for-read' for input and
169 `coding-system-for-write' for output if those variables are bound.
170 Otherwise they are looked up in `process-coding-system-alist'.  If not
171 found, they default to `undecided' for input and `nil' (binary) for
172 output."
173   (let (cs-r cs-w)
174     (let (ret)
175       (catch 'found
176         (let ((alist process-coding-system-alist)
177               (case-fold-search nil))
178           (while alist
179             (if (string-match (car (car alist)) program)
180                 (throw 'found (setq ret (cdr (car alist)))))
181             (setq alist (cdr alist))
182             )))
183       (if (functionp ret)
184           (setq ret (funcall ret 'start-process program)))
185       (cond ((consp ret)
186              (setq cs-r (car ret)
187                    cs-w (cdr ret)))
188             ((find-coding-system ret)
189              (setq cs-r ret
190                    cs-w ret))))
191     (let ((coding-system-for-read
192            (or coding-system-for-read cs-r 'undecided))
193           (coding-system-for-write
194            (or coding-system-for-write cs-w)))
195       (apply 'start-process-internal name buffer program program-args)
196       )))
197
198 (defvar network-coding-system-alist nil
199   "Alist to decide a coding system to use for a network I/O operation.
200 The format is ((PATTERN . VAL) ...),
201 where PATTERN is a regular expression matching a network service name
202 or is a port number to connect to,
203 VAL is a coding system, a cons of coding systems, or a function symbol.
204 If VAL is a coding system, it is used for both decoding what received
205 from the network stream and encoding what sent to the network stream.
206 If VAL is a cons of coding systems, the car part is used for decoding,
207 and the cdr part is used for encoding.
208 If VAL is a function symbol, the function must return a coding system
209 or a cons of coding systems which are used as above.
210
211 See also the function `find-operation-coding-system'.")
212
213 (defun open-network-stream (name buffer host service &optional protocol)
214   "Open a TCP connection for a service to a host.
215 Return a process object to represent the connection.
216 Input and output work as for subprocesses; `delete-process' closes it.
217 Args are NAME BUFFER HOST SERVICE.
218 NAME is name for process.  It is modified if necessary to make it unique.
219 BUFFER is the buffer (or buffer-name) to associate with the process.
220  Process output goes at end of that buffer, unless you specify
221  an output stream or filter function to handle the output.
222  BUFFER may be also nil, meaning that this process is not associated
223  with any buffer
224 Third arg is name of the host to connect to, or its IP address.
225 Fourth arg SERVICE is name of the service desired, or an integer
226  specifying a port number to connect to.
227 Fifth argument PROTOCOL is a network protocol.  Currently 'tcp
228  (Transmission Control Protocol) and 'udp (User Datagram Protocol) are
229  supported.  When omitted, 'tcp is assumed.
230
231 Output via `process-send-string' and input via buffer or filter (see
232 `set-process-filter') are stream-oriented.  That means UDP datagrams are
233 not guaranteed to be sent and received in discrete packets. (But small
234 datagrams around 500 bytes that are not truncated by `process-send-string'
235 are usually fine.)  Note further that UDP protocol does not guard against 
236 lost packets."
237   (let (cs-r cs-w)
238     (let (ret)
239       (catch 'found
240         (let ((alist network-coding-system-alist)
241               (case-fold-search nil)
242               pattern)
243           (while alist
244             (setq pattern (car (car alist)))
245             (and
246              (cond ((numberp pattern)
247                     (and (numberp service)
248                          (eq pattern service)))
249                    ((stringp pattern)
250                     (or (and (stringp service)
251                              (string-match pattern service))
252                         (and (numberp service)
253                              (string-match pattern
254                                            (number-to-string service))))))
255              (throw 'found (setq ret (cdr (car alist)))))
256             (setq alist (cdr alist))
257             )))
258       (if (functionp ret)
259           (setq ret (funcall ret 'open-network-stream service)))
260       (cond ((consp ret)
261              (setq cs-r (car ret)
262                    cs-w (cdr ret)))
263             ((find-coding-system ret)
264              (setq cs-r ret
265                    cs-w ret))))
266     (let ((coding-system-for-read
267            (or coding-system-for-read cs-r))
268           (coding-system-for-write
269            (or coding-system-for-write cs-w)))
270       (open-network-stream-internal name buffer host service protocol))))
271
272 ;;; code-process.el ends here