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