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
|
2828
|
33 #include "elhash.h"
|
428
|
34 #include "faces.h"
|
872
|
35 #include "file-coding.h"
|
428
|
36 #include "frame.h"
|
872
|
37 #include "glyphs.h"
|
428
|
38 #include "lstream.h"
|
872
|
39 #include "process.h"
|
|
40
|
428
|
41 #include "sysdep.h"
|
|
42 #include "sysfile.h"
|
|
43
|
|
44 DEFINE_CONSOLE_TYPE (tty);
|
|
45 DECLARE_IMAGE_INSTANTIATOR_FORMAT (nothing);
|
|
46 DECLARE_IMAGE_INSTANTIATOR_FORMAT (string);
|
|
47 DECLARE_IMAGE_INSTANTIATOR_FORMAT (formatted_string);
|
|
48 DECLARE_IMAGE_INSTANTIATOR_FORMAT (inherit);
|
|
49
|
|
50 Lisp_Object Qterminal_type;
|
|
51 Lisp_Object Qcontrolling_process;
|
|
52
|
2850
|
53 Lisp_Object Vtty_seen_characters;
|
2828
|
54
|
1204
|
55 static const struct memory_description tty_console_data_description_1 [] = {
|
|
56 { XD_LISP_OBJECT, offsetof (struct tty_console, terminal_type) },
|
|
57 { XD_LISP_OBJECT, offsetof (struct tty_console, instream) },
|
|
58 { XD_LISP_OBJECT, offsetof (struct tty_console, outstream) },
|
|
59 { XD_END }
|
|
60 };
|
|
61
|
3092
|
62 #ifdef NEW_GC
|
|
63 DEFINE_LRECORD_IMPLEMENTATION ("tty-console", tty_console,
|
|
64 1, /*dumpable-flag*/
|
|
65 0, 0, 0, 0, 0,
|
|
66 tty_console_data_description_1,
|
|
67 Lisp_Tty_Console);
|
|
68 #else /* not NEW_GC */
|
1204
|
69 const struct sized_memory_description tty_console_data_description = {
|
|
70 sizeof (struct tty_console), tty_console_data_description_1
|
|
71 };
|
3092
|
72 #endif /* not NEW_GC */
|
1204
|
73
|
428
|
74
|
|
75 static void
|
|
76 allocate_tty_console_struct (struct console *con)
|
|
77 {
|
|
78 /* zero out all slots except the lisp ones ... */
|
3092
|
79 #ifdef NEW_GC
|
|
80 CONSOLE_TTY_DATA (con) = alloc_lrecord_type (struct tty_console,
|
|
81 &lrecord_tty_console);
|
|
82 #else /* not NEW_GC */
|
428
|
83 CONSOLE_TTY_DATA (con) = xnew_and_zero (struct tty_console);
|
3092
|
84 #endif /* not NEW_GC */
|
428
|
85 CONSOLE_TTY_DATA (con)->terminal_type = Qnil;
|
|
86 CONSOLE_TTY_DATA (con)->instream = Qnil;
|
|
87 CONSOLE_TTY_DATA (con)->outstream = Qnil;
|
|
88 }
|
|
89
|
|
90 static void
|
|
91 tty_init_console (struct console *con, Lisp_Object props)
|
|
92 {
|
|
93 Lisp_Object tty = CONSOLE_CONNECTION (con);
|
|
94 Lisp_Object terminal_type = Qnil;
|
|
95 Lisp_Object controlling_process = Qnil;
|
|
96 struct tty_console *tty_con;
|
|
97 struct gcpro gcpro1, gcpro2;
|
|
98
|
|
99 GCPRO2 (terminal_type, controlling_process);
|
|
100
|
|
101 terminal_type = Fplist_get (props, Qterminal_type, Qnil);
|
|
102 controlling_process = Fplist_get (props, Qcontrolling_process, Qnil);
|
|
103
|
|
104 /* Determine the terminal type */
|
|
105
|
|
106 if (!NILP (terminal_type))
|
|
107 CHECK_STRING (terminal_type);
|
|
108 else
|
|
109 {
|
867
|
110 Ibyte *temp_type = egetenv ("TERM");
|
428
|
111
|
|
112 if (!temp_type)
|
|
113 {
|
563
|
114 invalid_state ("Cannot determine terminal type", Qunbound);
|
428
|
115 }
|
|
116 else
|
771
|
117 terminal_type = build_intstring (temp_type);
|
428
|
118 }
|
|
119
|
|
120 /* Determine the controlling process */
|
|
121 if (!NILP (controlling_process))
|
|
122 CHECK_INT (controlling_process);
|
|
123
|
|
124 /* Open the specified console */
|
|
125
|
|
126 allocate_tty_console_struct (con);
|
|
127 tty_con = CONSOLE_TTY_DATA (con);
|
|
128
|
|
129 if (internal_equal (tty, Vstdio_str, 0))
|
|
130 {
|
|
131 tty_con->infd = fileno (stdin);
|
|
132 tty_con->outfd = fileno (stdout);
|
|
133 tty_con->is_stdio = 1;
|
|
134 }
|
|
135 else
|
|
136 {
|
|
137 tty_con->infd = tty_con->outfd =
|
771
|
138 qxe_open (XSTRING_DATA (tty), O_RDWR);
|
428
|
139 if (tty_con->infd < 0)
|
563
|
140 signal_error (Qio_error, "Unable to open tty", tty);
|
428
|
141 tty_con->is_stdio = 0;
|
|
142 }
|
|
143
|
802
|
144 /* set_descriptor_non_blocking (tty_con->infd); */
|
428
|
145 tty_con->instream = make_filedesc_input_stream (tty_con->infd, 0, -1, 0);
|
771
|
146 Lstream_set_buffering (XLSTREAM (tty_con->instream), LSTREAM_UNBUFFERED, 0);
|
428
|
147 tty_con->instream =
|
771
|
148 make_coding_input_stream (XLSTREAM (tty_con->instream),
|
|
149 get_coding_system_for_text_file (Qkeyboard, 0),
|
814
|
150 CODING_DECODE,
|
|
151 LSTREAM_FL_READ_ONE_BYTE_AT_A_TIME);
|
771
|
152 Lstream_set_buffering (XLSTREAM (tty_con->instream), LSTREAM_UNBUFFERED, 0);
|
|
153 tty_con->outstream = make_filedesc_output_stream (tty_con->outfd, 0, -1, 0);
|
428
|
154 tty_con->outstream =
|
771
|
155 make_coding_output_stream (XLSTREAM (tty_con->outstream),
|
|
156 get_coding_system_for_text_file (Qterminal, 0),
|
800
|
157 CODING_ENCODE, 0);
|
428
|
158 tty_con->terminal_type = terminal_type;
|
|
159 tty_con->controlling_process = controlling_process;
|
|
160
|
|
161 if (NILP (CONSOLE_NAME (con)))
|
|
162 CONSOLE_NAME (con) = Ffile_name_nondirectory (tty);
|
|
163 {
|
442
|
164 pid_t tty_pg;
|
|
165 pid_t controlling_tty_pg;
|
428
|
166 int cfd;
|
|
167
|
|
168 /* OK, the only sure-fire way I can think of to determine
|
|
169 whether a particular TTY is our controlling TTY is to check
|
|
170 if it has the same foreground process group as our controlling
|
|
171 TTY. This is OK because a process group can never simultaneously
|
|
172 be the foreground process group of two TTY's (in that case it
|
|
173 would have two controlling TTY's, which is not allowed). */
|
|
174
|
|
175 EMACS_GET_TTY_PROCESS_GROUP (tty_con->infd, &tty_pg);
|
867
|
176 cfd = qxe_open ((Ibyte *) "/dev/tty", O_RDWR, 0);
|
428
|
177 EMACS_GET_TTY_PROCESS_GROUP (cfd, &controlling_tty_pg);
|
771
|
178 retry_close (cfd);
|
428
|
179 if (tty_pg == controlling_tty_pg)
|
|
180 {
|
|
181 tty_con->controlling_terminal = 1;
|
793
|
182 Vcontrolling_terminal = wrap_console (con);
|
428
|
183 munge_tty_process_group ();
|
|
184 }
|
|
185 else
|
|
186 tty_con->controlling_terminal = 0;
|
|
187 }
|
|
188
|
|
189 UNGCPRO;
|
|
190 }
|
|
191
|
|
192 static void
|
|
193 tty_mark_console (struct console *con)
|
|
194 {
|
|
195 struct tty_console *tty_con = CONSOLE_TTY_DATA (con);
|
|
196 mark_object (tty_con->terminal_type);
|
|
197 mark_object (tty_con->instream);
|
|
198 mark_object (tty_con->outstream);
|
|
199 }
|
|
200
|
|
201 static int
|
2286
|
202 tty_initially_selected_for_input (struct console *UNUSED (con))
|
428
|
203 {
|
|
204 return 1;
|
|
205 }
|
|
206
|
|
207 static void
|
|
208 free_tty_console_struct (struct console *con)
|
|
209 {
|
|
210 struct tty_console *tty_con = CONSOLE_TTY_DATA (con);
|
|
211 if (tty_con)
|
|
212 {
|
|
213 if (tty_con->term_entry_buffer) /* allocated in term_init () */
|
|
214 {
|
1726
|
215 xfree (tty_con->term_entry_buffer, char *);
|
428
|
216 tty_con->term_entry_buffer = NULL;
|
|
217 }
|
3092
|
218 #ifdef NEW_GC
|
|
219 mc_free (tty_con);
|
|
220 #else /* not NEW_GC */
|
1726
|
221 xfree (tty_con, struct tty_console *);
|
3092
|
222 #endif /* not NEW_GC */
|
428
|
223 CONSOLE_TTY_DATA (con) = NULL;
|
|
224 }
|
|
225 }
|
|
226
|
|
227 static void
|
|
228 tty_delete_console (struct console *con)
|
|
229 {
|
|
230 Lstream_close (XLSTREAM (CONSOLE_TTY_DATA (con)->instream));
|
|
231 Lstream_close (XLSTREAM (CONSOLE_TTY_DATA (con)->outstream));
|
|
232 if (!CONSOLE_TTY_DATA (con)->is_stdio)
|
771
|
233 retry_close (CONSOLE_TTY_DATA (con)->infd);
|
428
|
234 if (CONSOLE_TTY_DATA (con)->controlling_terminal)
|
|
235 {
|
|
236 Vcontrolling_terminal = Qnil;
|
|
237 unmunge_tty_process_group ();
|
|
238 }
|
|
239 free_tty_console_struct (con);
|
|
240 }
|
|
241
|
|
242
|
|
243 static struct console *
|
|
244 decode_tty_console (Lisp_Object console)
|
|
245 {
|
793
|
246 console = wrap_console (decode_console (console));
|
428
|
247 CHECK_TTY_CONSOLE (console);
|
|
248 return XCONSOLE (console);
|
|
249 }
|
|
250
|
|
251 DEFUN ("console-tty-terminal-type", Fconsole_tty_terminal_type,
|
|
252 0, 1, 0, /*
|
|
253 Return the terminal type of TTY console CONSOLE.
|
|
254 */
|
|
255 (console))
|
|
256 {
|
|
257 return CONSOLE_TTY_DATA (decode_tty_console (console))->terminal_type;
|
|
258 }
|
|
259
|
|
260 DEFUN ("console-tty-controlling-process", Fconsole_tty_controlling_process,
|
|
261 0, 1, 0, /*
|
|
262 Return the controlling process of tty console CONSOLE.
|
|
263 */
|
|
264 (console))
|
|
265 {
|
|
266 return CONSOLE_TTY_DATA (decode_tty_console (console))->controlling_process;
|
|
267 }
|
|
268
|
|
269
|
|
270 DEFUN ("console-tty-input-coding-system", Fconsole_tty_input_coding_system,
|
|
271 0, 1, 0, /*
|
|
272 Return the input coding system of tty console CONSOLE.
|
|
273 */
|
|
274 (console))
|
|
275 {
|
771
|
276 return coding_stream_detected_coding_system
|
428
|
277 (XLSTREAM (CONSOLE_TTY_DATA (decode_tty_console (console))->instream));
|
|
278 }
|
|
279
|
|
280 DEFUN ("set-console-tty-input-coding-system", Fset_console_tty_input_coding_system,
|
|
281 0, 2, 0, /*
|
|
282 Set the input coding system of tty console CONSOLE to CODESYS.
|
|
283 CONSOLE defaults to the selected console.
|
|
284 CODESYS defaults to the value of `keyboard-coding-system'.
|
|
285 */
|
|
286 (console, codesys))
|
|
287 {
|
771
|
288 set_coding_stream_coding_system
|
428
|
289 (XLSTREAM (CONSOLE_TTY_DATA (decode_tty_console (console))->instream),
|
771
|
290 get_coding_system_for_text_file (NILP (codesys) ? Qkeyboard : codesys,
|
|
291 0));
|
428
|
292 return Qnil;
|
|
293 }
|
|
294
|
|
295 DEFUN ("console-tty-output-coding-system", Fconsole_tty_output_coding_system,
|
|
296 0, 1, 0, /*
|
|
297 Return TTY CONSOLE's output coding system.
|
|
298 */
|
|
299 (console))
|
|
300 {
|
771
|
301 return coding_stream_coding_system
|
428
|
302 (XLSTREAM (CONSOLE_TTY_DATA (decode_tty_console (console))->outstream));
|
|
303 }
|
|
304
|
|
305 DEFUN ("set-console-tty-output-coding-system", Fset_console_tty_output_coding_system,
|
|
306 0, 2, 0, /*
|
|
307 Set the coding system of tty output of console CONSOLE to CODESYS.
|
|
308 CONSOLE defaults to the selected console.
|
|
309 CODESYS defaults to the value of `terminal-coding-system'.
|
|
310 */
|
|
311 (console, codesys))
|
|
312 {
|
771
|
313 set_coding_stream_coding_system
|
428
|
314 (XLSTREAM (CONSOLE_TTY_DATA (decode_tty_console (console))->outstream),
|
771
|
315 get_coding_system_for_text_file (NILP (codesys) ? Qterminal : codesys,
|
|
316 0));
|
438
|
317 /* Redraw tty */
|
|
318 face_property_was_changed (Vdefault_face, Qfont, Qtty);
|
428
|
319 return Qnil;
|
|
320 }
|
|
321
|
440
|
322 /* #### Move this function to lisp */
|
428
|
323 DEFUN ("set-console-tty-coding-system", Fset_console_tty_coding_system,
|
|
324 0, 2, 0, /*
|
|
325 Set the input and output coding systems of tty console CONSOLE to CODESYS.
|
|
326 CONSOLE defaults to the selected console.
|
|
327 If CODESYS is nil, the values of `keyboard-coding-system' and
|
|
328 `terminal-coding-system' will be used for the input and
|
|
329 output coding systems of CONSOLE.
|
|
330 */
|
|
331 (console, codesys))
|
|
332 {
|
|
333 Fset_console_tty_input_coding_system (console, codesys);
|
|
334 Fset_console_tty_output_coding_system (console, codesys);
|
|
335 return Qnil;
|
|
336 }
|
|
337
|
|
338
|
|
339 Lisp_Object
|
|
340 tty_semi_canonicalize_console_connection (Lisp_Object connection,
|
578
|
341 Error_Behavior errb)
|
428
|
342 {
|
|
343 return stream_semi_canonicalize_console_connection (connection, errb);
|
|
344 }
|
|
345
|
|
346 Lisp_Object
|
|
347 tty_canonicalize_console_connection (Lisp_Object connection,
|
578
|
348 Error_Behavior errb)
|
428
|
349 {
|
|
350 return stream_canonicalize_console_connection (connection, errb);
|
|
351 }
|
|
352
|
|
353 Lisp_Object
|
|
354 tty_semi_canonicalize_device_connection (Lisp_Object connection,
|
578
|
355 Error_Behavior errb)
|
428
|
356 {
|
|
357 return stream_semi_canonicalize_console_connection (connection, errb);
|
|
358 }
|
|
359
|
|
360 Lisp_Object
|
|
361 tty_canonicalize_device_connection (Lisp_Object connection,
|
578
|
362 Error_Behavior errb)
|
428
|
363 {
|
|
364 return stream_canonicalize_console_connection (connection, errb);
|
|
365 }
|
|
366
|
2828
|
367 static Lisp_Object
|
|
368 tty_perhaps_init_unseen_key_defaults (struct console *UNUSED(con),
|
|
369 Lisp_Object key)
|
|
370 {
|
|
371 Ichar val;
|
|
372 extern Lisp_Object Vcurrent_global_map;
|
|
373
|
|
374 if (SYMBOLP(key))
|
|
375 {
|
|
376 /* We've no idea what to default an unknown symbol to on the TTY. */
|
|
377 return Qnil;
|
|
378 }
|
|
379
|
|
380 CHECK_CHAR(key);
|
|
381
|
2850
|
382 if (!(HASH_TABLEP(Vtty_seen_characters)))
|
2828
|
383 {
|
|
384 /* All the keysyms we deal with are character objects; therefore, we
|
|
385 can use eq as the test without worrying. */
|
2850
|
386 Vtty_seen_characters = make_lisp_hash_table (128, HASH_TABLE_NON_WEAK,
|
2828
|
387 HASH_TABLE_EQ);
|
|
388 }
|
|
389
|
|
390 /* Might give the user an opaque error if make_lisp_hash_table fails,
|
|
391 but it won't crash. */
|
2850
|
392 CHECK_HASH_TABLE(Vtty_seen_characters);
|
2828
|
393
|
|
394 val = XCHAR(key);
|
|
395
|
|
396 /* Same logic as in x_has_keysym; I'm not convinced it's always sane. */
|
|
397 if (val < 0x80)
|
|
398 {
|
|
399 return Qnil;
|
|
400 }
|
|
401
|
2850
|
402 if (!NILP(Fgethash(key, Vtty_seen_characters, Qnil)))
|
2828
|
403 {
|
|
404 return Qnil;
|
|
405 }
|
|
406
|
|
407 if (NILP (Flookup_key (Vcurrent_global_map, key, Qnil)))
|
|
408 {
|
2850
|
409 Fputhash(key, Qt, Vtty_seen_characters);
|
2828
|
410 Fdefine_key (Vcurrent_global_map, key, Qself_insert_command);
|
|
411 return Qt;
|
|
412 }
|
|
413
|
|
414 return Qnil;
|
|
415 }
|
|
416
|
428
|
417
|
|
418 /************************************************************************/
|
|
419 /* initialization */
|
|
420 /************************************************************************/
|
|
421
|
|
422 void
|
|
423 syms_of_console_tty (void)
|
|
424 {
|
|
425 DEFSUBR (Fconsole_tty_terminal_type);
|
|
426 DEFSUBR (Fconsole_tty_controlling_process);
|
563
|
427 DEFSYMBOL (Qterminal_type);
|
|
428 DEFSYMBOL (Qcontrolling_process);
|
428
|
429 DEFSUBR (Fconsole_tty_output_coding_system);
|
|
430 DEFSUBR (Fset_console_tty_output_coding_system);
|
|
431 DEFSUBR (Fconsole_tty_input_coding_system);
|
|
432 DEFSUBR (Fset_console_tty_input_coding_system);
|
|
433 DEFSUBR (Fset_console_tty_coding_system);
|
|
434 }
|
|
435
|
|
436 void
|
|
437 console_type_create_tty (void)
|
|
438 {
|
|
439 INITIALIZE_CONSOLE_TYPE (tty, "tty", "console-tty-p");
|
|
440
|
|
441 /* console methods */
|
|
442 CONSOLE_HAS_METHOD (tty, init_console);
|
|
443 CONSOLE_HAS_METHOD (tty, mark_console);
|
|
444 CONSOLE_HAS_METHOD (tty, initially_selected_for_input);
|
|
445 CONSOLE_HAS_METHOD (tty, delete_console);
|
|
446 CONSOLE_HAS_METHOD (tty, canonicalize_console_connection);
|
|
447 CONSOLE_HAS_METHOD (tty, canonicalize_device_connection);
|
|
448 CONSOLE_HAS_METHOD (tty, semi_canonicalize_console_connection);
|
|
449 CONSOLE_HAS_METHOD (tty, semi_canonicalize_device_connection);
|
2828
|
450 CONSOLE_HAS_METHOD (tty, perhaps_init_unseen_key_defaults);
|
428
|
451 }
|
|
452
|
|
453 void
|
|
454 reinit_console_type_create_tty (void)
|
|
455 {
|
|
456 REINITIALIZE_CONSOLE_TYPE (tty);
|
|
457 }
|
|
458
|
|
459 void
|
|
460 image_instantiator_format_create_glyphs_tty (void)
|
|
461 {
|
|
462 IIFORMAT_VALID_CONSOLE (tty, nothing);
|
|
463 IIFORMAT_VALID_CONSOLE (tty, string);
|
|
464 IIFORMAT_VALID_CONSOLE (tty, formatted_string);
|
|
465 IIFORMAT_VALID_CONSOLE (tty, inherit);
|
|
466 }
|
|
467
|
|
468 void
|
|
469 vars_of_console_tty (void)
|
|
470 {
|
2850
|
471 DEFVAR_LISP ("tty-seen-characters", &Vtty_seen_characters /*
|
|
472 Hash table of non-ASCII characters the TTY subsystem has seen.
|
|
473 */ );
|
|
474 Vtty_seen_characters = Qnil;
|
428
|
475 Fprovide (Qtty);
|
|
476 }
|