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