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