comparison src/console-tty.c @ 428:3ecd8885ac67 r21-2-22

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