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 "events.h" /* for Vcontrolling_terminal */
|
|
33 #include "faces.h"
|
|
34 #include "frame.h"
|
|
35 #include "lstream.h"
|
|
36 #include "redisplay.h"
|
|
37 #include "sysdep.h"
|
|
38 #ifdef MULE
|
|
39 #include "mule-coding.h"
|
|
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
|
78
|
50 extern Lisp_Object Vstdio_str; /* in console-stream.c */
|
0
|
51
|
|
52 static void
|
|
53 allocate_tty_console_struct (struct console *con)
|
|
54 {
|
185
|
55 /* zero out all slots except the lisp ones ... */
|
|
56 con->console_data = xnew_and_zero (struct tty_console);
|
0
|
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 {
|
108
|
65 Lisp_Object tty = CONSOLE_CONNECTION (con);
|
|
66 Lisp_Object terminal_type = Qnil, controlling_process = Qnil;
|
0
|
67 int infd, outfd;
|
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);
|
108
|
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 {
|
|
81 char *temp_type = (char *) getenv ("TERM");
|
|
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);
|
195
|
98 if (internal_equal (tty, Vstdio_str, 0))
|
0
|
99 {
|
|
100 infd = fileno (stdin);
|
|
101 outfd = fileno (stdout);
|
|
102 CONSOLE_TTY_DATA (con)->is_stdio = 1;
|
|
103 }
|
|
104 else
|
|
105 {
|
16
|
106 infd = outfd = open ((char *) XSTRING_DATA (tty), O_RDWR);
|
0
|
107 if (infd < 0)
|
16
|
108 error ("Unable to open tty %s", XSTRING_DATA (tty));
|
0
|
109 CONSOLE_TTY_DATA (con)->is_stdio = 0;
|
|
110 }
|
185
|
111
|
0
|
112 CONSOLE_TTY_DATA (con)->infd = infd;
|
|
113 CONSOLE_TTY_DATA (con)->outfd = outfd;
|
|
114 CONSOLE_TTY_DATA (con)->instream = make_filedesc_input_stream (infd, 0,
|
|
115 -1, 0);
|
|
116 CONSOLE_TTY_DATA (con)->outstream = make_filedesc_output_stream (outfd, 0,
|
|
117 -1, 0);
|
70
|
118 #ifdef MULE
|
|
119 CONSOLE_TTY_DATA (con)->instream =
|
|
120 make_decoding_input_stream (XLSTREAM (CONSOLE_TTY_DATA (con)->instream),
|
|
121 Fget_coding_system (Vkeyboard_coding_system));
|
|
122 Lstream_set_character_mode (XLSTREAM (CONSOLE_TTY_DATA (con)->instream));
|
|
123 CONSOLE_TTY_DATA (con)->outstream =
|
|
124 make_encoding_output_stream (XLSTREAM (CONSOLE_TTY_DATA (con)->outstream),
|
|
125 Fget_coding_system (Vterminal_coding_system));
|
|
126 #endif /* MULE */
|
0
|
127 CONSOLE_TTY_DATA (con)->terminal_type = terminal_type;
|
108
|
128 CONSOLE_TTY_DATA (con)->controlling_process = controlling_process;
|
153
|
129
|
|
130 #ifdef HAVE_GPM
|
|
131 connect_to_gpm(con);
|
|
132 #endif
|
|
133
|
0
|
134 if (NILP (CONSOLE_NAME (con)))
|
|
135 CONSOLE_NAME (con) = Ffile_name_nondirectory (tty);
|
|
136 {
|
|
137 int tty_pg;
|
|
138 int controlling_tty_pg;
|
|
139 int cfd;
|
|
140
|
|
141 /* OK, the only sure-fire way I can think of to determine
|
|
142 whether a particular TTY is our controlling TTY is to check
|
|
143 if it has the same foreground process group as our controlling
|
|
144 TTY. This is OK because a process group can never simultaneously
|
|
145 be the foreground process group of two TTY's (in that case it
|
|
146 would have two controlling TTY's, which is not allowed). */
|
|
147
|
|
148 EMACS_GET_TTY_PROCESS_GROUP (infd, &tty_pg);
|
|
149 cfd = open ("/dev/tty", O_RDWR, 0);
|
|
150 EMACS_GET_TTY_PROCESS_GROUP (cfd, &controlling_tty_pg);
|
|
151 close (cfd);
|
|
152 if (tty_pg == controlling_tty_pg)
|
|
153 {
|
|
154 CONSOLE_TTY_DATA (con)->controlling_terminal = 1;
|
|
155 XSETCONSOLE (Vcontrolling_terminal, con);
|
|
156 munge_tty_process_group ();
|
|
157 }
|
|
158 else
|
|
159 CONSOLE_TTY_DATA (con)->controlling_terminal = 0;
|
|
160 }
|
|
161
|
|
162 UNGCPRO;
|
|
163 }
|
|
164
|
|
165 static void
|
|
166 tty_mark_console (struct console *con, void (*markobj) (Lisp_Object))
|
|
167 {
|
|
168 ((markobj) (CONSOLE_TTY_DATA (con)->terminal_type));
|
|
169 ((markobj) (CONSOLE_TTY_DATA (con)->instream));
|
|
170 ((markobj) (CONSOLE_TTY_DATA (con)->outstream));
|
|
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 {
|
|
182 struct tty_console *tcon = (struct tty_console *) con->console_data;
|
|
183 if (tcon && tcon->term_entry_buffer) /* allocated in term_init () */
|
|
184 xfree (tcon->term_entry_buffer);
|
|
185 if (tcon)
|
|
186 xfree (tcon);
|
|
187 }
|
|
188
|
|
189 static void
|
|
190 tty_delete_console (struct console *con)
|
|
191 {
|
|
192 Lstream_close (XLSTREAM (CONSOLE_TTY_DATA (con)->instream));
|
|
193 Lstream_close (XLSTREAM (CONSOLE_TTY_DATA (con)->outstream));
|
|
194 if (!CONSOLE_TTY_DATA (con)->is_stdio)
|
|
195 close (CONSOLE_TTY_DATA (con)->infd);
|
|
196 if (CONSOLE_TTY_DATA (con)->controlling_terminal)
|
|
197 {
|
|
198 Vcontrolling_terminal = Qnil;
|
|
199 unmunge_tty_process_group ();
|
|
200 }
|
|
201 free_tty_console_struct (con);
|
|
202 }
|
|
203
|
|
204
|
|
205 static struct console *
|
|
206 decode_tty_console (Lisp_Object console)
|
|
207 {
|
|
208 XSETCONSOLE (console, decode_console (console));
|
|
209 CHECK_TTY_CONSOLE (console);
|
|
210 return XCONSOLE (console);
|
|
211 }
|
|
212
|
20
|
213 DEFUN ("console-tty-terminal-type", Fconsole_tty_terminal_type, 0, 1, 0, /*
|
0
|
214 Return the terminal type of TTY console CONSOLE.
|
20
|
215 */
|
|
216 (console))
|
0
|
217 {
|
|
218 return CONSOLE_TTY_DATA (decode_tty_console (console))->terminal_type;
|
|
219 }
|
|
220
|
108
|
221 DEFUN ("console-tty-controlling-process", Fconsole_tty_controlling_process, 0, 1, 0, /*
|
|
222 Return the controlling process of TTY console CONSOLE.
|
|
223 */
|
|
224 (console))
|
|
225 {
|
|
226 return CONSOLE_TTY_DATA (decode_tty_console (console))->controlling_process;
|
|
227 }
|
|
228
|
0
|
229 Lisp_Object
|
|
230 tty_semi_canonicalize_console_connection (Lisp_Object connection,
|
|
231 Error_behavior errb)
|
|
232 {
|
78
|
233 return stream_semi_canonicalize_console_connection (connection, errb);
|
0
|
234 }
|
|
235
|
|
236 Lisp_Object
|
|
237 tty_canonicalize_console_connection (Lisp_Object connection,
|
|
238 Error_behavior errb)
|
|
239 {
|
78
|
240 return stream_canonicalize_console_connection (connection, errb);
|
0
|
241 }
|
|
242
|
|
243 Lisp_Object
|
|
244 tty_semi_canonicalize_device_connection (Lisp_Object connection,
|
|
245 Error_behavior errb)
|
|
246 {
|
78
|
247 return stream_semi_canonicalize_console_connection (connection, errb);
|
0
|
248 }
|
|
249
|
|
250 Lisp_Object
|
|
251 tty_canonicalize_device_connection (Lisp_Object connection,
|
|
252 Error_behavior errb)
|
|
253 {
|
78
|
254 return stream_canonicalize_console_connection (connection, errb);
|
0
|
255 }
|
|
256
|
|
257
|
|
258 /************************************************************************/
|
|
259 /* initialization */
|
|
260 /************************************************************************/
|
|
261
|
|
262 void
|
|
263 syms_of_console_tty (void)
|
|
264 {
|
20
|
265 DEFSUBR (Fconsole_tty_terminal_type);
|
108
|
266 DEFSUBR (Fconsole_tty_controlling_process);
|
0
|
267 defsymbol (&Qterminal_type, "terminal-type");
|
108
|
268 defsymbol (&Qcontrolling_process, "controlling-process");
|
0
|
269 }
|
|
270
|
|
271 void
|
|
272 console_type_create_tty (void)
|
|
273 {
|
|
274 INITIALIZE_CONSOLE_TYPE (tty, "tty", "console-tty-p");
|
|
275
|
|
276 /* console methods */
|
|
277 CONSOLE_HAS_METHOD (tty, init_console);
|
|
278 CONSOLE_HAS_METHOD (tty, mark_console);
|
|
279 CONSOLE_HAS_METHOD (tty, initially_selected_for_input);
|
|
280 CONSOLE_HAS_METHOD (tty, delete_console);
|
|
281 CONSOLE_HAS_METHOD (tty, canonicalize_console_connection);
|
|
282 CONSOLE_HAS_METHOD (tty, canonicalize_device_connection);
|
|
283 CONSOLE_HAS_METHOD (tty, semi_canonicalize_console_connection);
|
|
284 CONSOLE_HAS_METHOD (tty, semi_canonicalize_device_connection);
|
|
285 }
|
|
286
|
|
287 void
|
|
288 vars_of_console_tty (void)
|
|
289 {
|
|
290 Fprovide (Qtty);
|
|
291 }
|