1 /* TTY console functions.
2 Copyright (C) 1994, 1995 Board of Trustees, University of Illinois.
3 Copyright (C) 1994, 1995 Free Software Foundation, Inc.
4 Copyright (C) 1996 Ben Wing.
6 This file is part of XEmacs.
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
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
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. */
23 /* Synched up with: Not in FSF. */
25 /* Authors: Ben Wing and Chuck Thompson. */
30 #include "console-tty.h"
31 #include "console-stream.h"
39 #include "file-coding.h"
45 DEFINE_CONSOLE_TYPE (tty);
46 DECLARE_IMAGE_INSTANTIATOR_FORMAT (nothing);
47 DECLARE_IMAGE_INSTANTIATOR_FORMAT (string);
48 DECLARE_IMAGE_INSTANTIATOR_FORMAT (formatted_string);
49 DECLARE_IMAGE_INSTANTIATOR_FORMAT (inherit);
51 Lisp_Object Qterminal_type;
52 Lisp_Object Qcontrolling_process;
56 allocate_tty_console_struct (struct console *con)
58 /* zero out all slots except the lisp ones ... */
59 CONSOLE_TTY_DATA (con) = xnew_and_zero (struct tty_console);
60 CONSOLE_TTY_DATA (con)->terminal_type = Qnil;
61 CONSOLE_TTY_DATA (con)->instream = Qnil;
62 CONSOLE_TTY_DATA (con)->outstream = Qnil;
66 tty_init_console (struct console *con, Lisp_Object props)
68 Lisp_Object tty = CONSOLE_CONNECTION (con);
69 Lisp_Object terminal_type = Qnil;
70 Lisp_Object controlling_process = Qnil;
71 struct tty_console *tty_con;
72 struct gcpro gcpro1, gcpro2;
74 GCPRO2 (terminal_type, controlling_process);
76 terminal_type = Fplist_get (props, Qterminal_type, Qnil);
77 controlling_process = Fplist_get (props, Qcontrolling_process, Qnil);
79 /* Determine the terminal type */
81 if (!NILP (terminal_type))
82 CHECK_STRING (terminal_type);
85 char *temp_type = getenv ("TERM");
89 error ("Cannot determine terminal type");
92 terminal_type = build_string (temp_type);
95 /* Determine the controlling process */
96 if (!NILP (controlling_process))
97 CHECK_INT (controlling_process);
99 /* Open the specified console */
101 allocate_tty_console_struct (con);
102 tty_con = CONSOLE_TTY_DATA (con);
104 if (internal_equal (tty, Vstdio_str, 0))
106 tty_con->infd = fileno (stdin);
107 tty_con->outfd = fileno (stdout);
108 tty_con->is_stdio = 1;
112 tty_con->infd = tty_con->outfd =
113 open ((char *) XSTRING_DATA (tty), O_RDWR);
114 if (tty_con->infd < 0)
115 error ("Unable to open tty %s", XSTRING_DATA (tty));
116 tty_con->is_stdio = 0;
119 tty_con->instream = make_filedesc_input_stream (tty_con->infd, 0, -1, 0);
120 tty_con->outstream = make_filedesc_output_stream (tty_con->outfd, 0, -1, 0);
123 make_decoding_input_stream (XLSTREAM (tty_con->instream),
124 Fget_coding_system (Vkeyboard_coding_system));
125 Lstream_set_character_mode (XLSTREAM (tty_con->instream));
127 make_encoding_output_stream (XLSTREAM (tty_con->outstream),
128 Fget_coding_system (Vterminal_coding_system));
130 tty_con->terminal_type = terminal_type;
131 tty_con->controlling_process = controlling_process;
134 connect_to_gpm (con);
137 if (NILP (CONSOLE_NAME (con)))
138 CONSOLE_NAME (con) = Ffile_name_nondirectory (tty);
141 int controlling_tty_pg;
144 /* OK, the only sure-fire way I can think of to determine
145 whether a particular TTY is our controlling TTY is to check
146 if it has the same foreground process group as our controlling
147 TTY. This is OK because a process group can never simultaneously
148 be the foreground process group of two TTY's (in that case it
149 would have two controlling TTY's, which is not allowed). */
151 EMACS_GET_TTY_PROCESS_GROUP (tty_con->infd, &tty_pg);
152 cfd = open ("/dev/tty", O_RDWR, 0);
153 EMACS_GET_TTY_PROCESS_GROUP (cfd, &controlling_tty_pg);
155 if (tty_pg == controlling_tty_pg)
157 tty_con->controlling_terminal = 1;
158 XSETCONSOLE (Vcontrolling_terminal, con);
159 munge_tty_process_group ();
162 tty_con->controlling_terminal = 0;
169 tty_mark_console (struct console *con)
171 struct tty_console *tty_con = CONSOLE_TTY_DATA (con);
172 mark_object (tty_con->terminal_type);
173 mark_object (tty_con->instream);
174 mark_object (tty_con->outstream);
178 tty_initially_selected_for_input (struct console *con)
184 free_tty_console_struct (struct console *con)
186 struct tty_console *tty_con = CONSOLE_TTY_DATA (con);
189 if (tty_con->term_entry_buffer) /* allocated in term_init () */
191 xfree (tty_con->term_entry_buffer);
192 tty_con->term_entry_buffer = NULL;
195 CONSOLE_TTY_DATA (con) = NULL;
200 tty_delete_console (struct console *con)
202 Lstream_close (XLSTREAM (CONSOLE_TTY_DATA (con)->instream));
203 Lstream_close (XLSTREAM (CONSOLE_TTY_DATA (con)->outstream));
204 if (!CONSOLE_TTY_DATA (con)->is_stdio)
205 close (CONSOLE_TTY_DATA (con)->infd);
206 if (CONSOLE_TTY_DATA (con)->controlling_terminal)
208 Vcontrolling_terminal = Qnil;
209 unmunge_tty_process_group ();
211 free_tty_console_struct (con);
215 static struct console *
216 decode_tty_console (Lisp_Object console)
218 XSETCONSOLE (console, decode_console (console));
219 CHECK_TTY_CONSOLE (console);
220 return XCONSOLE (console);
223 DEFUN ("console-tty-terminal-type", Fconsole_tty_terminal_type,
225 Return the terminal type of TTY console CONSOLE.
229 return CONSOLE_TTY_DATA (decode_tty_console (console))->terminal_type;
232 DEFUN ("console-tty-controlling-process", Fconsole_tty_controlling_process,
234 Return the controlling process of tty console CONSOLE.
238 return CONSOLE_TTY_DATA (decode_tty_console (console))->controlling_process;
243 DEFUN ("console-tty-input-coding-system", Fconsole_tty_input_coding_system,
245 Return the input coding system of tty console CONSOLE.
249 return decoding_stream_coding_system
250 (XLSTREAM (CONSOLE_TTY_DATA (decode_tty_console (console))->instream));
253 DEFUN ("set-console-tty-input-coding-system", Fset_console_tty_input_coding_system,
255 Set the input coding system of tty console CONSOLE to CODESYS.
256 CONSOLE defaults to the selected console.
257 CODESYS defaults to the value of `keyboard-coding-system'.
261 set_decoding_stream_coding_system
262 (XLSTREAM (CONSOLE_TTY_DATA (decode_tty_console (console))->instream),
263 Fget_coding_system (NILP (codesys) ? Vkeyboard_coding_system : codesys));
267 DEFUN ("console-tty-output-coding-system", Fconsole_tty_output_coding_system,
269 Return TTY CONSOLE's output coding system.
273 return encoding_stream_coding_system
274 (XLSTREAM (CONSOLE_TTY_DATA (decode_tty_console (console))->outstream));
277 DEFUN ("set-console-tty-output-coding-system", Fset_console_tty_output_coding_system,
279 Set the coding system of tty output of console CONSOLE to CODESYS.
280 CONSOLE defaults to the selected console.
281 CODESYS defaults to the value of `terminal-coding-system'.
285 set_encoding_stream_coding_system
286 (XLSTREAM (CONSOLE_TTY_DATA (decode_tty_console (console))->outstream),
287 Fget_coding_system (NILP (codesys) ? Vterminal_coding_system : codesys));
291 /* ### Move this function to lisp */
292 DEFUN ("set-console-tty-coding-system", Fset_console_tty_coding_system,
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.
302 Fset_console_tty_input_coding_system (console, codesys);
303 Fset_console_tty_output_coding_system (console, codesys);
306 #endif /* FILE_CODING */
310 tty_semi_canonicalize_console_connection (Lisp_Object connection,
313 return stream_semi_canonicalize_console_connection (connection, errb);
317 tty_canonicalize_console_connection (Lisp_Object connection,
320 return stream_canonicalize_console_connection (connection, errb);
324 tty_semi_canonicalize_device_connection (Lisp_Object connection,
327 return stream_semi_canonicalize_console_connection (connection, errb);
331 tty_canonicalize_device_connection (Lisp_Object connection,
334 return stream_canonicalize_console_connection (connection, errb);
338 /************************************************************************/
340 /************************************************************************/
343 syms_of_console_tty (void)
345 DEFSUBR (Fconsole_tty_terminal_type);
346 DEFSUBR (Fconsole_tty_controlling_process);
347 defsymbol (&Qterminal_type, "terminal-type");
348 defsymbol (&Qcontrolling_process, "controlling-process");
350 DEFSUBR (Fconsole_tty_output_coding_system);
351 DEFSUBR (Fset_console_tty_output_coding_system);
352 DEFSUBR (Fconsole_tty_input_coding_system);
353 DEFSUBR (Fset_console_tty_input_coding_system);
354 DEFSUBR (Fset_console_tty_coding_system);
355 #endif /* FILE_CODING */
359 console_type_create_tty (void)
361 INITIALIZE_CONSOLE_TYPE (tty, "tty", "console-tty-p");
363 /* console methods */
364 CONSOLE_HAS_METHOD (tty, init_console);
365 CONSOLE_HAS_METHOD (tty, mark_console);
366 CONSOLE_HAS_METHOD (tty, initially_selected_for_input);
367 CONSOLE_HAS_METHOD (tty, delete_console);
368 CONSOLE_HAS_METHOD (tty, canonicalize_console_connection);
369 CONSOLE_HAS_METHOD (tty, canonicalize_device_connection);
370 CONSOLE_HAS_METHOD (tty, semi_canonicalize_console_connection);
371 CONSOLE_HAS_METHOD (tty, semi_canonicalize_device_connection);
375 reinit_console_type_create_tty (void)
377 REINITIALIZE_CONSOLE_TYPE (tty);
381 image_instantiator_format_create_glyphs_tty (void)
383 IIFORMAT_VALID_CONSOLE (tty, nothing);
384 IIFORMAT_VALID_CONSOLE (tty, string);
385 IIFORMAT_VALID_CONSOLE (tty, formatted_string);
386 IIFORMAT_VALID_CONSOLE (tty, inherit);
390 vars_of_console_tty (void)