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