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