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