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