428
|
1 /* TTY console functions.
|
|
2 Copyright (C) 1994, 1995 Board of Trustees, University of Illinois.
|
|
3 Copyright (C) 1994, 1995 Free Software Foundation, Inc.
|
800
|
4 Copyright (C) 1996, 2001, 2002 Ben Wing.
|
428
|
5
|
|
6 This file is part of XEmacs.
|
|
7
|
|
8 XEmacs is free software; you can redistribute it and/or modify it
|
|
9 under the terms of the GNU General Public License as published by the
|
|
10 Free Software Foundation; either version 2, or (at your option) any
|
|
11 later version.
|
|
12
|
|
13 XEmacs is distributed in the hope that it will be useful, but WITHOUT
|
|
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
16 for more details.
|
|
17
|
|
18 You should have received a copy of the GNU General Public License
|
|
19 along with XEmacs; see the file COPYING. If not, write to
|
|
20 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|
21 Boston, MA 02111-1307, USA. */
|
|
22
|
|
23 /* Synched up with: Not in FSF. */
|
|
24
|
|
25 /* Authors: Ben Wing and Chuck Thompson. */
|
|
26
|
|
27 #include <config.h>
|
|
28 #include "lisp.h"
|
|
29
|
872
|
30 #include "console-tty-impl.h"
|
428
|
31 #include "console-stream.h"
|
872
|
32
|
428
|
33 #include "faces.h"
|
872
|
34 #include "file-coding.h"
|
428
|
35 #include "frame.h"
|
872
|
36 #include "glyphs.h"
|
428
|
37 #include "lstream.h"
|
872
|
38 #include "process.h"
|
|
39
|
428
|
40 #include "sysdep.h"
|
|
41 #include "sysfile.h"
|
|
42
|
|
43 DEFINE_CONSOLE_TYPE (tty);
|
|
44 DECLARE_IMAGE_INSTANTIATOR_FORMAT (nothing);
|
|
45 DECLARE_IMAGE_INSTANTIATOR_FORMAT (string);
|
|
46 DECLARE_IMAGE_INSTANTIATOR_FORMAT (formatted_string);
|
|
47 DECLARE_IMAGE_INSTANTIATOR_FORMAT (inherit);
|
|
48
|
|
49 Lisp_Object Qterminal_type;
|
|
50 Lisp_Object Qcontrolling_process;
|
|
51
|
|
52
|
|
53 static void
|
|
54 allocate_tty_console_struct (struct console *con)
|
|
55 {
|
|
56 /* zero out all slots except the lisp ones ... */
|
|
57 CONSOLE_TTY_DATA (con) = xnew_and_zero (struct tty_console);
|
|
58 CONSOLE_TTY_DATA (con)->terminal_type = Qnil;
|
|
59 CONSOLE_TTY_DATA (con)->instream = Qnil;
|
|
60 CONSOLE_TTY_DATA (con)->outstream = Qnil;
|
|
61 }
|
|
62
|
|
63 static void
|
|
64 tty_init_console (struct console *con, Lisp_Object props)
|
|
65 {
|
|
66 Lisp_Object tty = CONSOLE_CONNECTION (con);
|
|
67 Lisp_Object terminal_type = Qnil;
|
|
68 Lisp_Object controlling_process = Qnil;
|
|
69 struct tty_console *tty_con;
|
|
70 struct gcpro gcpro1, gcpro2;
|
|
71
|
|
72 GCPRO2 (terminal_type, controlling_process);
|
|
73
|
|
74 terminal_type = Fplist_get (props, Qterminal_type, Qnil);
|
|
75 controlling_process = Fplist_get (props, Qcontrolling_process, Qnil);
|
|
76
|
|
77 /* Determine the terminal type */
|
|
78
|
|
79 if (!NILP (terminal_type))
|
|
80 CHECK_STRING (terminal_type);
|
|
81 else
|
|
82 {
|
867
|
83 Ibyte *temp_type = egetenv ("TERM");
|
428
|
84
|
|
85 if (!temp_type)
|
|
86 {
|
563
|
87 invalid_state ("Cannot determine terminal type", Qunbound);
|
428
|
88 }
|
|
89 else
|
771
|
90 terminal_type = build_intstring (temp_type);
|
428
|
91 }
|
|
92
|
|
93 /* Determine the controlling process */
|
|
94 if (!NILP (controlling_process))
|
|
95 CHECK_INT (controlling_process);
|
|
96
|
|
97 /* Open the specified console */
|
|
98
|
|
99 allocate_tty_console_struct (con);
|
|
100 tty_con = CONSOLE_TTY_DATA (con);
|
|
101
|
|
102 if (internal_equal (tty, Vstdio_str, 0))
|
|
103 {
|
|
104 tty_con->infd = fileno (stdin);
|
|
105 tty_con->outfd = fileno (stdout);
|
|
106 tty_con->is_stdio = 1;
|
|
107 }
|
|
108 else
|
|
109 {
|
|
110 tty_con->infd = tty_con->outfd =
|
771
|
111 qxe_open (XSTRING_DATA (tty), O_RDWR);
|
428
|
112 if (tty_con->infd < 0)
|
563
|
113 signal_error (Qio_error, "Unable to open tty", tty);
|
428
|
114 tty_con->is_stdio = 0;
|
|
115 }
|
|
116
|
802
|
117 /* set_descriptor_non_blocking (tty_con->infd); */
|
428
|
118 tty_con->instream = make_filedesc_input_stream (tty_con->infd, 0, -1, 0);
|
771
|
119 Lstream_set_buffering (XLSTREAM (tty_con->instream), LSTREAM_UNBUFFERED, 0);
|
428
|
120 tty_con->instream =
|
771
|
121 make_coding_input_stream (XLSTREAM (tty_con->instream),
|
|
122 get_coding_system_for_text_file (Qkeyboard, 0),
|
814
|
123 CODING_DECODE,
|
|
124 LSTREAM_FL_READ_ONE_BYTE_AT_A_TIME);
|
771
|
125 Lstream_set_buffering (XLSTREAM (tty_con->instream), LSTREAM_UNBUFFERED, 0);
|
|
126 tty_con->outstream = make_filedesc_output_stream (tty_con->outfd, 0, -1, 0);
|
428
|
127 tty_con->outstream =
|
771
|
128 make_coding_output_stream (XLSTREAM (tty_con->outstream),
|
|
129 get_coding_system_for_text_file (Qterminal, 0),
|
800
|
130 CODING_ENCODE, 0);
|
428
|
131 tty_con->terminal_type = terminal_type;
|
|
132 tty_con->controlling_process = controlling_process;
|
|
133
|
|
134 if (NILP (CONSOLE_NAME (con)))
|
|
135 CONSOLE_NAME (con) = Ffile_name_nondirectory (tty);
|
|
136 {
|
442
|
137 pid_t tty_pg;
|
|
138 pid_t controlling_tty_pg;
|
428
|
139 int cfd;
|
|
140
|
|
141 /* OK, the only sure-fire way I can think of to determine
|
|
142 whether a particular TTY is our controlling TTY is to check
|
|
143 if it has the same foreground process group as our controlling
|
|
144 TTY. This is OK because a process group can never simultaneously
|
|
145 be the foreground process group of two TTY's (in that case it
|
|
146 would have two controlling TTY's, which is not allowed). */
|
|
147
|
|
148 EMACS_GET_TTY_PROCESS_GROUP (tty_con->infd, &tty_pg);
|
867
|
149 cfd = qxe_open ((Ibyte *) "/dev/tty", O_RDWR, 0);
|
428
|
150 EMACS_GET_TTY_PROCESS_GROUP (cfd, &controlling_tty_pg);
|
771
|
151 retry_close (cfd);
|
428
|
152 if (tty_pg == controlling_tty_pg)
|
|
153 {
|
|
154 tty_con->controlling_terminal = 1;
|
793
|
155 Vcontrolling_terminal = wrap_console (con);
|
428
|
156 munge_tty_process_group ();
|
|
157 }
|
|
158 else
|
|
159 tty_con->controlling_terminal = 0;
|
|
160 }
|
|
161
|
|
162 UNGCPRO;
|
|
163 }
|
|
164
|
|
165 static void
|
|
166 tty_mark_console (struct console *con)
|
|
167 {
|
|
168 struct tty_console *tty_con = CONSOLE_TTY_DATA (con);
|
|
169 mark_object (tty_con->terminal_type);
|
|
170 mark_object (tty_con->instream);
|
|
171 mark_object (tty_con->outstream);
|
|
172 }
|
|
173
|
|
174 static int
|
|
175 tty_initially_selected_for_input (struct console *con)
|
|
176 {
|
|
177 return 1;
|
|
178 }
|
|
179
|
|
180 static void
|
|
181 free_tty_console_struct (struct console *con)
|
|
182 {
|
|
183 struct tty_console *tty_con = CONSOLE_TTY_DATA (con);
|
|
184 if (tty_con)
|
|
185 {
|
|
186 if (tty_con->term_entry_buffer) /* allocated in term_init () */
|
|
187 {
|
|
188 xfree (tty_con->term_entry_buffer);
|
|
189 tty_con->term_entry_buffer = NULL;
|
|
190 }
|
|
191 xfree (tty_con);
|
|
192 CONSOLE_TTY_DATA (con) = NULL;
|
|
193 }
|
|
194 }
|
|
195
|
|
196 static void
|
|
197 tty_delete_console (struct console *con)
|
|
198 {
|
|
199 Lstream_close (XLSTREAM (CONSOLE_TTY_DATA (con)->instream));
|
|
200 Lstream_close (XLSTREAM (CONSOLE_TTY_DATA (con)->outstream));
|
|
201 if (!CONSOLE_TTY_DATA (con)->is_stdio)
|
771
|
202 retry_close (CONSOLE_TTY_DATA (con)->infd);
|
428
|
203 if (CONSOLE_TTY_DATA (con)->controlling_terminal)
|
|
204 {
|
|
205 Vcontrolling_terminal = Qnil;
|
|
206 unmunge_tty_process_group ();
|
|
207 }
|
|
208 free_tty_console_struct (con);
|
|
209 }
|
|
210
|
|
211
|
|
212 static struct console *
|
|
213 decode_tty_console (Lisp_Object console)
|
|
214 {
|
793
|
215 console = wrap_console (decode_console (console));
|
428
|
216 CHECK_TTY_CONSOLE (console);
|
|
217 return XCONSOLE (console);
|
|
218 }
|
|
219
|
|
220 DEFUN ("console-tty-terminal-type", Fconsole_tty_terminal_type,
|
|
221 0, 1, 0, /*
|
|
222 Return the terminal type of TTY console CONSOLE.
|
|
223 */
|
|
224 (console))
|
|
225 {
|
|
226 return CONSOLE_TTY_DATA (decode_tty_console (console))->terminal_type;
|
|
227 }
|
|
228
|
|
229 DEFUN ("console-tty-controlling-process", Fconsole_tty_controlling_process,
|
|
230 0, 1, 0, /*
|
|
231 Return the controlling process of tty console CONSOLE.
|
|
232 */
|
|
233 (console))
|
|
234 {
|
|
235 return CONSOLE_TTY_DATA (decode_tty_console (console))->controlling_process;
|
|
236 }
|
|
237
|
|
238
|
|
239 DEFUN ("console-tty-input-coding-system", Fconsole_tty_input_coding_system,
|
|
240 0, 1, 0, /*
|
|
241 Return the input coding system of tty console CONSOLE.
|
|
242 */
|
|
243 (console))
|
|
244 {
|
771
|
245 return coding_stream_detected_coding_system
|
428
|
246 (XLSTREAM (CONSOLE_TTY_DATA (decode_tty_console (console))->instream));
|
|
247 }
|
|
248
|
|
249 DEFUN ("set-console-tty-input-coding-system", Fset_console_tty_input_coding_system,
|
|
250 0, 2, 0, /*
|
|
251 Set the input coding system of tty console CONSOLE to CODESYS.
|
|
252 CONSOLE defaults to the selected console.
|
|
253 CODESYS defaults to the value of `keyboard-coding-system'.
|
|
254 */
|
|
255 (console, codesys))
|
|
256 {
|
771
|
257 set_coding_stream_coding_system
|
428
|
258 (XLSTREAM (CONSOLE_TTY_DATA (decode_tty_console (console))->instream),
|
771
|
259 get_coding_system_for_text_file (NILP (codesys) ? Qkeyboard : codesys,
|
|
260 0));
|
428
|
261 return Qnil;
|
|
262 }
|
|
263
|
|
264 DEFUN ("console-tty-output-coding-system", Fconsole_tty_output_coding_system,
|
|
265 0, 1, 0, /*
|
|
266 Return TTY CONSOLE's output coding system.
|
|
267 */
|
|
268 (console))
|
|
269 {
|
771
|
270 return coding_stream_coding_system
|
428
|
271 (XLSTREAM (CONSOLE_TTY_DATA (decode_tty_console (console))->outstream));
|
|
272 }
|
|
273
|
|
274 DEFUN ("set-console-tty-output-coding-system", Fset_console_tty_output_coding_system,
|
|
275 0, 2, 0, /*
|
|
276 Set the coding system of tty output of console CONSOLE to CODESYS.
|
|
277 CONSOLE defaults to the selected console.
|
|
278 CODESYS defaults to the value of `terminal-coding-system'.
|
|
279 */
|
|
280 (console, codesys))
|
|
281 {
|
771
|
282 set_coding_stream_coding_system
|
428
|
283 (XLSTREAM (CONSOLE_TTY_DATA (decode_tty_console (console))->outstream),
|
771
|
284 get_coding_system_for_text_file (NILP (codesys) ? Qterminal : codesys,
|
|
285 0));
|
438
|
286 /* Redraw tty */
|
|
287 face_property_was_changed (Vdefault_face, Qfont, Qtty);
|
428
|
288 return Qnil;
|
|
289 }
|
|
290
|
440
|
291 /* #### Move this function to lisp */
|
428
|
292 DEFUN ("set-console-tty-coding-system", Fset_console_tty_coding_system,
|
|
293 0, 2, 0, /*
|
|
294 Set the input and output coding systems of tty console CONSOLE to CODESYS.
|
|
295 CONSOLE defaults to the selected console.
|
|
296 If CODESYS is nil, the values of `keyboard-coding-system' and
|
|
297 `terminal-coding-system' will be used for the input and
|
|
298 output coding systems of CONSOLE.
|
|
299 */
|
|
300 (console, codesys))
|
|
301 {
|
|
302 Fset_console_tty_input_coding_system (console, codesys);
|
|
303 Fset_console_tty_output_coding_system (console, codesys);
|
|
304 return Qnil;
|
|
305 }
|
|
306
|
|
307
|
|
308 Lisp_Object
|
|
309 tty_semi_canonicalize_console_connection (Lisp_Object connection,
|
578
|
310 Error_Behavior errb)
|
428
|
311 {
|
|
312 return stream_semi_canonicalize_console_connection (connection, errb);
|
|
313 }
|
|
314
|
|
315 Lisp_Object
|
|
316 tty_canonicalize_console_connection (Lisp_Object connection,
|
578
|
317 Error_Behavior errb)
|
428
|
318 {
|
|
319 return stream_canonicalize_console_connection (connection, errb);
|
|
320 }
|
|
321
|
|
322 Lisp_Object
|
|
323 tty_semi_canonicalize_device_connection (Lisp_Object connection,
|
578
|
324 Error_Behavior errb)
|
428
|
325 {
|
|
326 return stream_semi_canonicalize_console_connection (connection, errb);
|
|
327 }
|
|
328
|
|
329 Lisp_Object
|
|
330 tty_canonicalize_device_connection (Lisp_Object connection,
|
578
|
331 Error_Behavior errb)
|
428
|
332 {
|
|
333 return stream_canonicalize_console_connection (connection, errb);
|
|
334 }
|
|
335
|
|
336
|
|
337 /************************************************************************/
|
|
338 /* initialization */
|
|
339 /************************************************************************/
|
|
340
|
|
341 void
|
|
342 syms_of_console_tty (void)
|
|
343 {
|
|
344 DEFSUBR (Fconsole_tty_terminal_type);
|
|
345 DEFSUBR (Fconsole_tty_controlling_process);
|
563
|
346 DEFSYMBOL (Qterminal_type);
|
|
347 DEFSYMBOL (Qcontrolling_process);
|
428
|
348 DEFSUBR (Fconsole_tty_output_coding_system);
|
|
349 DEFSUBR (Fset_console_tty_output_coding_system);
|
|
350 DEFSUBR (Fconsole_tty_input_coding_system);
|
|
351 DEFSUBR (Fset_console_tty_input_coding_system);
|
|
352 DEFSUBR (Fset_console_tty_coding_system);
|
|
353 }
|
|
354
|
|
355 void
|
|
356 console_type_create_tty (void)
|
|
357 {
|
|
358 INITIALIZE_CONSOLE_TYPE (tty, "tty", "console-tty-p");
|
|
359
|
|
360 /* console methods */
|
|
361 CONSOLE_HAS_METHOD (tty, init_console);
|
|
362 CONSOLE_HAS_METHOD (tty, mark_console);
|
|
363 CONSOLE_HAS_METHOD (tty, initially_selected_for_input);
|
|
364 CONSOLE_HAS_METHOD (tty, delete_console);
|
|
365 CONSOLE_HAS_METHOD (tty, canonicalize_console_connection);
|
|
366 CONSOLE_HAS_METHOD (tty, canonicalize_device_connection);
|
|
367 CONSOLE_HAS_METHOD (tty, semi_canonicalize_console_connection);
|
|
368 CONSOLE_HAS_METHOD (tty, semi_canonicalize_device_connection);
|
|
369 }
|
|
370
|
|
371 void
|
|
372 reinit_console_type_create_tty (void)
|
|
373 {
|
|
374 REINITIALIZE_CONSOLE_TYPE (tty);
|
|
375 }
|
|
376
|
|
377 void
|
|
378 image_instantiator_format_create_glyphs_tty (void)
|
|
379 {
|
|
380 IIFORMAT_VALID_CONSOLE (tty, nothing);
|
|
381 IIFORMAT_VALID_CONSOLE (tty, string);
|
|
382 IIFORMAT_VALID_CONSOLE (tty, formatted_string);
|
|
383 IIFORMAT_VALID_CONSOLE (tty, inherit);
|
|
384 }
|
|
385
|
|
386 void
|
|
387 vars_of_console_tty (void)
|
|
388 {
|
|
389 Fprovide (Qtty);
|
|
390 }
|