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;
|
0
|
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 (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 CONSOLE_TTY_DATA (con)->controlling_terminal = 1;
|
|
151 XSETCONSOLE (Vcontrolling_terminal, con);
|
|
152 munge_tty_process_group ();
|
|
153 }
|
|
154 else
|
|
155 CONSOLE_TTY_DATA (con)->controlling_terminal = 0;
|
|
156 }
|
|
157
|
|
158 UNGCPRO;
|
|
159 }
|
|
160
|
|
161 static void
|
|
162 tty_mark_console (struct console *con, void (*markobj) (Lisp_Object))
|
|
163 {
|
|
164 ((markobj) (CONSOLE_TTY_DATA (con)->terminal_type));
|
|
165 ((markobj) (CONSOLE_TTY_DATA (con)->instream));
|
|
166 ((markobj) (CONSOLE_TTY_DATA (con)->outstream));
|
|
167 }
|
|
168
|
|
169 static int
|
|
170 tty_initially_selected_for_input (struct console *con)
|
|
171 {
|
|
172 return 1;
|
|
173 }
|
|
174
|
|
175 static void
|
|
176 free_tty_console_struct (struct console *con)
|
|
177 {
|
|
178 struct tty_console *tcon = (struct tty_console *) con->console_data;
|
|
179 if (tcon && tcon->term_entry_buffer) /* allocated in term_init () */
|
|
180 xfree (tcon->term_entry_buffer);
|
|
181 if (tcon)
|
|
182 xfree (tcon);
|
|
183 }
|
|
184
|
|
185 static void
|
|
186 tty_delete_console (struct console *con)
|
|
187 {
|
|
188 Lstream_close (XLSTREAM (CONSOLE_TTY_DATA (con)->instream));
|
|
189 Lstream_close (XLSTREAM (CONSOLE_TTY_DATA (con)->outstream));
|
|
190 if (!CONSOLE_TTY_DATA (con)->is_stdio)
|
|
191 close (CONSOLE_TTY_DATA (con)->infd);
|
|
192 if (CONSOLE_TTY_DATA (con)->controlling_terminal)
|
|
193 {
|
|
194 Vcontrolling_terminal = Qnil;
|
|
195 unmunge_tty_process_group ();
|
|
196 }
|
|
197 free_tty_console_struct (con);
|
|
198 }
|
|
199
|
|
200
|
|
201 static struct console *
|
|
202 decode_tty_console (Lisp_Object console)
|
|
203 {
|
|
204 XSETCONSOLE (console, decode_console (console));
|
|
205 CHECK_TTY_CONSOLE (console);
|
|
206 return XCONSOLE (console);
|
|
207 }
|
|
208
|
20
|
209 DEFUN ("console-tty-terminal-type", Fconsole_tty_terminal_type, 0, 1, 0, /*
|
0
|
210 Return the terminal type of TTY console CONSOLE.
|
20
|
211 */
|
|
212 (console))
|
0
|
213 {
|
|
214 return CONSOLE_TTY_DATA (decode_tty_console (console))->terminal_type;
|
|
215 }
|
|
216
|
108
|
217 DEFUN ("console-tty-controlling-process", Fconsole_tty_controlling_process, 0, 1, 0, /*
|
|
218 Return the controlling process of TTY console CONSOLE.
|
|
219 */
|
|
220 (console))
|
|
221 {
|
|
222 return CONSOLE_TTY_DATA (decode_tty_console (console))->controlling_process;
|
|
223 }
|
|
224
|
80
|
225 extern Lisp_Object stream_semi_canonicalize_console_connection(Lisp_Object,
|
|
226 Error_behavior);
|
0
|
227 Lisp_Object
|
|
228 tty_semi_canonicalize_console_connection (Lisp_Object connection,
|
|
229 Error_behavior errb)
|
|
230 {
|
78
|
231 return stream_semi_canonicalize_console_connection (connection, errb);
|
0
|
232 }
|
|
233
|
80
|
234 extern Lisp_Object stream_canonicalize_console_connection(Lisp_Object,
|
|
235 Error_behavior);
|
0
|
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 }
|