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