Mercurial > hg > xemacs-beta
annotate src/console-tty.c @ 5118:e0db3c197671 ben-lisp-object
merge up to latest default branch, doesn't compile yet
author | Ben Wing <ben@xemacs.org> |
---|---|
date | Sat, 26 Dec 2009 21:18:49 -0600 |
parents | 229bd619740a |
children | d1247f3cc363 |
rev | line source |
---|---|
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 |
5118
e0db3c197671
merge up to latest default branch, doesn't compile yet
Ben Wing <ben@xemacs.org>
parents:
4117
diff
changeset
|
63 DEFINE_DUMPABLE_INTERNAL_LISP_OBJECT ("tty-console", tty_console, |
e0db3c197671
merge up to latest default branch, doesn't compile yet
Ben Wing <ben@xemacs.org>
parents:
4117
diff
changeset
|
64 0, tty_console_data_description_1, |
e0db3c197671
merge up to latest default branch, doesn't compile yet
Ben Wing <ben@xemacs.org>
parents:
4117
diff
changeset
|
65 Lisp_Tty_Console); |
3092 | 66 #else /* not NEW_GC */ |
1204 | 67 const struct sized_memory_description tty_console_data_description = { |
68 sizeof (struct tty_console), tty_console_data_description_1 | |
69 }; | |
3092 | 70 #endif /* not NEW_GC */ |
1204 | 71 |
428 | 72 |
73 static void | |
74 allocate_tty_console_struct (struct console *con) | |
75 { | |
76 /* zero out all slots except the lisp ones ... */ | |
3092 | 77 #ifdef NEW_GC |
78 CONSOLE_TTY_DATA (con) = alloc_lrecord_type (struct tty_console, | |
79 &lrecord_tty_console); | |
80 #else /* not NEW_GC */ | |
428 | 81 CONSOLE_TTY_DATA (con) = xnew_and_zero (struct tty_console); |
3092 | 82 #endif /* not NEW_GC */ |
428 | 83 CONSOLE_TTY_DATA (con)->terminal_type = Qnil; |
84 CONSOLE_TTY_DATA (con)->instream = Qnil; | |
85 CONSOLE_TTY_DATA (con)->outstream = Qnil; | |
86 } | |
87 | |
88 static void | |
89 tty_init_console (struct console *con, Lisp_Object props) | |
90 { | |
91 Lisp_Object tty = CONSOLE_CONNECTION (con); | |
92 Lisp_Object terminal_type = Qnil; | |
93 Lisp_Object controlling_process = Qnil; | |
94 struct tty_console *tty_con; | |
95 struct gcpro gcpro1, gcpro2; | |
96 | |
97 GCPRO2 (terminal_type, controlling_process); | |
98 | |
99 terminal_type = Fplist_get (props, Qterminal_type, Qnil); | |
100 controlling_process = Fplist_get (props, Qcontrolling_process, Qnil); | |
101 | |
102 /* Determine the terminal type */ | |
103 | |
104 if (!NILP (terminal_type)) | |
105 CHECK_STRING (terminal_type); | |
106 else | |
107 { | |
867 | 108 Ibyte *temp_type = egetenv ("TERM"); |
428 | 109 |
110 if (!temp_type) | |
111 { | |
563 | 112 invalid_state ("Cannot determine terminal type", Qunbound); |
428 | 113 } |
114 else | |
771 | 115 terminal_type = build_intstring (temp_type); |
428 | 116 } |
117 | |
118 /* Determine the controlling process */ | |
119 if (!NILP (controlling_process)) | |
120 CHECK_INT (controlling_process); | |
121 | |
122 /* Open the specified console */ | |
123 | |
124 allocate_tty_console_struct (con); | |
125 tty_con = CONSOLE_TTY_DATA (con); | |
126 | |
127 if (internal_equal (tty, Vstdio_str, 0)) | |
128 { | |
129 tty_con->infd = fileno (stdin); | |
130 tty_con->outfd = fileno (stdout); | |
131 tty_con->is_stdio = 1; | |
132 } | |
133 else | |
134 { | |
135 tty_con->infd = tty_con->outfd = | |
771 | 136 qxe_open (XSTRING_DATA (tty), O_RDWR); |
428 | 137 if (tty_con->infd < 0) |
563 | 138 signal_error (Qio_error, "Unable to open tty", tty); |
428 | 139 tty_con->is_stdio = 0; |
140 } | |
141 | |
802 | 142 /* set_descriptor_non_blocking (tty_con->infd); */ |
428 | 143 tty_con->instream = make_filedesc_input_stream (tty_con->infd, 0, -1, 0); |
771 | 144 Lstream_set_buffering (XLSTREAM (tty_con->instream), LSTREAM_UNBUFFERED, 0); |
428 | 145 tty_con->instream = |
771 | 146 make_coding_input_stream (XLSTREAM (tty_con->instream), |
147 get_coding_system_for_text_file (Qkeyboard, 0), | |
814 | 148 CODING_DECODE, |
149 LSTREAM_FL_READ_ONE_BYTE_AT_A_TIME); | |
771 | 150 Lstream_set_buffering (XLSTREAM (tty_con->instream), LSTREAM_UNBUFFERED, 0); |
151 tty_con->outstream = make_filedesc_output_stream (tty_con->outfd, 0, -1, 0); | |
428 | 152 tty_con->outstream = |
771 | 153 make_coding_output_stream (XLSTREAM (tty_con->outstream), |
154 get_coding_system_for_text_file (Qterminal, 0), | |
800 | 155 CODING_ENCODE, 0); |
428 | 156 tty_con->terminal_type = terminal_type; |
157 tty_con->controlling_process = controlling_process; | |
158 | |
3571 | 159 /* Defaults to 1 with Mule, 0 without. In the latter case the attribute is |
160 read-only from Lisp. */ | |
161 tty_con->multiple_width = CONSOLE_TTY_SUPPORTS_MULTIPLE_WIDTH(c); | |
162 | |
428 | 163 if (NILP (CONSOLE_NAME (con))) |
164 CONSOLE_NAME (con) = Ffile_name_nondirectory (tty); | |
165 { | |
442 | 166 pid_t tty_pg; |
167 pid_t controlling_tty_pg; | |
428 | 168 int cfd; |
169 | |
170 /* OK, the only sure-fire way I can think of to determine | |
171 whether a particular TTY is our controlling TTY is to check | |
172 if it has the same foreground process group as our controlling | |
173 TTY. This is OK because a process group can never simultaneously | |
174 be the foreground process group of two TTY's (in that case it | |
175 would have two controlling TTY's, which is not allowed). */ | |
176 | |
177 EMACS_GET_TTY_PROCESS_GROUP (tty_con->infd, &tty_pg); | |
867 | 178 cfd = qxe_open ((Ibyte *) "/dev/tty", O_RDWR, 0); |
428 | 179 EMACS_GET_TTY_PROCESS_GROUP (cfd, &controlling_tty_pg); |
771 | 180 retry_close (cfd); |
428 | 181 if (tty_pg == controlling_tty_pg) |
182 { | |
183 tty_con->controlling_terminal = 1; | |
793 | 184 Vcontrolling_terminal = wrap_console (con); |
428 | 185 munge_tty_process_group (); |
186 } | |
187 else | |
188 tty_con->controlling_terminal = 0; | |
189 } | |
190 | |
191 UNGCPRO; | |
192 } | |
193 | |
194 static void | |
195 tty_mark_console (struct console *con) | |
196 { | |
197 struct tty_console *tty_con = CONSOLE_TTY_DATA (con); | |
198 mark_object (tty_con->terminal_type); | |
199 mark_object (tty_con->instream); | |
200 mark_object (tty_con->outstream); | |
201 } | |
202 | |
203 static int | |
2286 | 204 tty_initially_selected_for_input (struct console *UNUSED (con)) |
428 | 205 { |
206 return 1; | |
207 } | |
208 | |
209 static void | |
210 free_tty_console_struct (struct console *con) | |
211 { | |
212 struct tty_console *tty_con = CONSOLE_TTY_DATA (con); | |
213 if (tty_con) | |
214 { | |
215 if (tty_con->term_entry_buffer) /* allocated in term_init () */ | |
216 { | |
1726 | 217 xfree (tty_con->term_entry_buffer, char *); |
428 | 218 tty_con->term_entry_buffer = NULL; |
219 } | |
4117 | 220 #ifndef 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 | |
3571 | 322 DEFUN ("console-tty-multiple-width", Fconsole_tty_multiple_width, |
323 0, 1, 0, /* | |
324 Return whether CONSOLE treats East Asian double-width chars as such. | |
325 | |
326 CONSOLE defaults to the selected console. Without XEmacs support for | |
327 double-width characters, this always gives nil. | |
328 */ | |
329 (console)) | |
330 { | |
331 return CONSOLE_TTY_MULTIPLE_WIDTH (decode_tty_console(console)) | |
332 ? Qt : Qnil; | |
333 } | |
334 | |
335 DEFUN ("set-console-tty-multiple-width", Fset_console_tty_multiple_width, | |
336 0, 2, 0, /* | |
337 Set whether CONSOLE treats East Asian double-width characters as such. | |
338 | |
339 CONSOLE defaults to the selected console, and VALUE defaults to nil. | |
340 Without XEmacs support for double-width characters, this throws an error if | |
341 VALUE is non-nil. | |
342 */ | |
343 (console, value)) | |
344 { | |
345 struct console *c = decode_tty_console (console); | |
346 | |
347 /* So people outside of East Asia can put (set-console-tty-multiple-width | |
348 (selected-console) nil) in their init files, independent of whether | |
349 Mule is enabled. */ | |
350 if (!CONSOLE_TTY_MULTIPLE_WIDTH (c) && NILP(value)) | |
351 { | |
352 return Qnil; | |
353 } | |
354 | |
355 if (!CONSOLE_TTY_SUPPORTS_MULTIPLE_WIDTH (c)) | |
356 { | |
357 invalid_change | |
358 ("No console support for double-width chars", | |
359 Fmake_symbol(CONSOLE_NAME(c))); | |
360 } | |
361 | |
362 CONSOLE_TTY_DATA(c)->multiple_width = NILP(value) ? 0 : 1; | |
363 | |
364 return Qnil; | |
365 } | |
366 | |
440 | 367 /* #### Move this function to lisp */ |
428 | 368 DEFUN ("set-console-tty-coding-system", Fset_console_tty_coding_system, |
369 0, 2, 0, /* | |
370 Set the input and output coding systems of tty console CONSOLE to CODESYS. | |
371 CONSOLE defaults to the selected console. | |
372 If CODESYS is nil, the values of `keyboard-coding-system' and | |
373 `terminal-coding-system' will be used for the input and | |
374 output coding systems of CONSOLE. | |
375 */ | |
376 (console, codesys)) | |
377 { | |
378 Fset_console_tty_input_coding_system (console, codesys); | |
379 Fset_console_tty_output_coding_system (console, codesys); | |
380 return Qnil; | |
381 } | |
382 | |
383 | |
384 Lisp_Object | |
385 tty_semi_canonicalize_console_connection (Lisp_Object connection, | |
578 | 386 Error_Behavior errb) |
428 | 387 { |
388 return stream_semi_canonicalize_console_connection (connection, errb); | |
389 } | |
390 | |
391 Lisp_Object | |
392 tty_canonicalize_console_connection (Lisp_Object connection, | |
578 | 393 Error_Behavior errb) |
428 | 394 { |
395 return stream_canonicalize_console_connection (connection, errb); | |
396 } | |
397 | |
398 Lisp_Object | |
399 tty_semi_canonicalize_device_connection (Lisp_Object connection, | |
578 | 400 Error_Behavior errb) |
428 | 401 { |
402 return stream_semi_canonicalize_console_connection (connection, errb); | |
403 } | |
404 | |
405 Lisp_Object | |
406 tty_canonicalize_device_connection (Lisp_Object connection, | |
578 | 407 Error_Behavior errb) |
428 | 408 { |
409 return stream_canonicalize_console_connection (connection, errb); | |
410 } | |
411 | |
2828 | 412 static Lisp_Object |
413 tty_perhaps_init_unseen_key_defaults (struct console *UNUSED(con), | |
414 Lisp_Object key) | |
415 { | |
416 Ichar val; | |
417 extern Lisp_Object Vcurrent_global_map; | |
418 | |
419 if (SYMBOLP(key)) | |
420 { | |
421 /* We've no idea what to default an unknown symbol to on the TTY. */ | |
422 return Qnil; | |
423 } | |
424 | |
425 CHECK_CHAR(key); | |
426 | |
2850 | 427 if (!(HASH_TABLEP(Vtty_seen_characters))) |
2828 | 428 { |
429 /* All the keysyms we deal with are character objects; therefore, we | |
430 can use eq as the test without worrying. */ | |
2850 | 431 Vtty_seen_characters = make_lisp_hash_table (128, HASH_TABLE_NON_WEAK, |
2828 | 432 HASH_TABLE_EQ); |
433 } | |
434 | |
435 /* Might give the user an opaque error if make_lisp_hash_table fails, | |
436 but it won't crash. */ | |
2850 | 437 CHECK_HASH_TABLE(Vtty_seen_characters); |
2828 | 438 |
439 val = XCHAR(key); | |
440 | |
441 /* Same logic as in x_has_keysym; I'm not convinced it's always sane. */ | |
442 if (val < 0x80) | |
443 { | |
444 return Qnil; | |
445 } | |
446 | |
2850 | 447 if (!NILP(Fgethash(key, Vtty_seen_characters, Qnil))) |
2828 | 448 { |
449 return Qnil; | |
450 } | |
451 | |
452 if (NILP (Flookup_key (Vcurrent_global_map, key, Qnil))) | |
453 { | |
2850 | 454 Fputhash(key, Qt, Vtty_seen_characters); |
2828 | 455 Fdefine_key (Vcurrent_global_map, key, Qself_insert_command); |
456 return Qt; | |
457 } | |
458 | |
459 return Qnil; | |
460 } | |
461 | |
428 | 462 |
463 /************************************************************************/ | |
464 /* initialization */ | |
465 /************************************************************************/ | |
466 | |
467 void | |
468 syms_of_console_tty (void) | |
469 { | |
470 DEFSUBR (Fconsole_tty_terminal_type); | |
471 DEFSUBR (Fconsole_tty_controlling_process); | |
563 | 472 DEFSYMBOL (Qterminal_type); |
473 DEFSYMBOL (Qcontrolling_process); | |
428 | 474 DEFSUBR (Fconsole_tty_output_coding_system); |
475 DEFSUBR (Fset_console_tty_output_coding_system); | |
476 DEFSUBR (Fconsole_tty_input_coding_system); | |
477 DEFSUBR (Fset_console_tty_input_coding_system); | |
478 DEFSUBR (Fset_console_tty_coding_system); | |
3571 | 479 DEFSUBR (Fconsole_tty_multiple_width); |
480 DEFSUBR (Fset_console_tty_multiple_width); | |
428 | 481 } |
482 | |
483 void | |
484 console_type_create_tty (void) | |
485 { | |
486 INITIALIZE_CONSOLE_TYPE (tty, "tty", "console-tty-p"); | |
487 | |
488 /* console methods */ | |
489 CONSOLE_HAS_METHOD (tty, init_console); | |
490 CONSOLE_HAS_METHOD (tty, mark_console); | |
491 CONSOLE_HAS_METHOD (tty, initially_selected_for_input); | |
492 CONSOLE_HAS_METHOD (tty, delete_console); | |
493 CONSOLE_HAS_METHOD (tty, canonicalize_console_connection); | |
494 CONSOLE_HAS_METHOD (tty, canonicalize_device_connection); | |
495 CONSOLE_HAS_METHOD (tty, semi_canonicalize_console_connection); | |
496 CONSOLE_HAS_METHOD (tty, semi_canonicalize_device_connection); | |
2828 | 497 CONSOLE_HAS_METHOD (tty, perhaps_init_unseen_key_defaults); |
428 | 498 } |
499 | |
500 void | |
501 reinit_console_type_create_tty (void) | |
502 { | |
503 REINITIALIZE_CONSOLE_TYPE (tty); | |
504 } | |
505 | |
506 void | |
507 image_instantiator_format_create_glyphs_tty (void) | |
508 { | |
509 IIFORMAT_VALID_CONSOLE (tty, nothing); | |
510 IIFORMAT_VALID_CONSOLE (tty, string); | |
511 IIFORMAT_VALID_CONSOLE (tty, formatted_string); | |
512 IIFORMAT_VALID_CONSOLE (tty, inherit); | |
513 } | |
514 | |
515 void | |
516 vars_of_console_tty (void) | |
517 { | |
2850 | 518 DEFVAR_LISP ("tty-seen-characters", &Vtty_seen_characters /* |
519 Hash table of non-ASCII characters the TTY subsystem has seen. | |
520 */ ); | |
521 Vtty_seen_characters = Qnil; | |
428 | 522 Fprovide (Qtty); |
523 } |