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
|
78
|
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 {
|
70
|
65 Lisp_Object tty = CONSOLE_CONNECTION (con), terminal_type = Qnil;
|
0
|
66 int infd, outfd;
|
70
|
67 struct gcpro gcpro1;
|
0
|
68
|
70
|
69 GCPRO1 (terminal_type);
|
0
|
70
|
|
71 terminal_type = Fplist_get (props, Qterminal_type, Qnil);
|
70
|
72
|
0
|
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);
|
70
|
112 #ifdef MULE
|
|
113 CONSOLE_TTY_DATA (con)->instream =
|
|
114 make_decoding_input_stream (XLSTREAM (CONSOLE_TTY_DATA (con)->instream),
|
|
115 Fget_coding_system (Vkeyboard_coding_system));
|
|
116 Lstream_set_character_mode (XLSTREAM (CONSOLE_TTY_DATA (con)->instream));
|
|
117 CONSOLE_TTY_DATA (con)->outstream =
|
|
118 make_encoding_output_stream (XLSTREAM (CONSOLE_TTY_DATA (con)->outstream),
|
|
119 Fget_coding_system (Vterminal_coding_system));
|
|
120 #endif /* MULE */
|
0
|
121 CONSOLE_TTY_DATA (con)->terminal_type = terminal_type;
|
|
122 if (NILP (CONSOLE_NAME (con)))
|
|
123 CONSOLE_NAME (con) = Ffile_name_nondirectory (tty);
|
|
124 {
|
|
125 int tty_pg;
|
|
126 int controlling_tty_pg;
|
|
127 int cfd;
|
|
128
|
|
129 /* OK, the only sure-fire way I can think of to determine
|
|
130 whether a particular TTY is our controlling TTY is to check
|
|
131 if it has the same foreground process group as our controlling
|
|
132 TTY. This is OK because a process group can never simultaneously
|
|
133 be the foreground process group of two TTY's (in that case it
|
|
134 would have two controlling TTY's, which is not allowed). */
|
|
135
|
|
136 EMACS_GET_TTY_PROCESS_GROUP (infd, &tty_pg);
|
|
137 cfd = open ("/dev/tty", O_RDWR, 0);
|
|
138 EMACS_GET_TTY_PROCESS_GROUP (cfd, &controlling_tty_pg);
|
|
139 close (cfd);
|
|
140 if (tty_pg == controlling_tty_pg)
|
|
141 {
|
|
142 CONSOLE_TTY_DATA (con)->controlling_terminal = 1;
|
|
143 XSETCONSOLE (Vcontrolling_terminal, con);
|
|
144 munge_tty_process_group ();
|
|
145 }
|
|
146 else
|
|
147 CONSOLE_TTY_DATA (con)->controlling_terminal = 0;
|
|
148 }
|
|
149
|
|
150 UNGCPRO;
|
|
151 }
|
|
152
|
|
153 static void
|
|
154 tty_mark_console (struct console *con, void (*markobj) (Lisp_Object))
|
|
155 {
|
|
156 ((markobj) (CONSOLE_TTY_DATA (con)->terminal_type));
|
|
157 ((markobj) (CONSOLE_TTY_DATA (con)->instream));
|
|
158 ((markobj) (CONSOLE_TTY_DATA (con)->outstream));
|
|
159 }
|
|
160
|
|
161 static int
|
|
162 tty_initially_selected_for_input (struct console *con)
|
|
163 {
|
|
164 return 1;
|
|
165 }
|
|
166
|
|
167 static void
|
|
168 free_tty_console_struct (struct console *con)
|
|
169 {
|
|
170 struct tty_console *tcon = (struct tty_console *) con->console_data;
|
|
171 if (tcon && tcon->term_entry_buffer) /* allocated in term_init () */
|
|
172 xfree (tcon->term_entry_buffer);
|
|
173 if (tcon)
|
|
174 xfree (tcon);
|
|
175 }
|
|
176
|
|
177 static void
|
|
178 tty_delete_console (struct console *con)
|
|
179 {
|
|
180 Lstream_close (XLSTREAM (CONSOLE_TTY_DATA (con)->instream));
|
|
181 Lstream_close (XLSTREAM (CONSOLE_TTY_DATA (con)->outstream));
|
|
182 if (!CONSOLE_TTY_DATA (con)->is_stdio)
|
|
183 close (CONSOLE_TTY_DATA (con)->infd);
|
|
184 if (CONSOLE_TTY_DATA (con)->controlling_terminal)
|
|
185 {
|
|
186 Vcontrolling_terminal = Qnil;
|
|
187 unmunge_tty_process_group ();
|
|
188 }
|
|
189 free_tty_console_struct (con);
|
|
190 }
|
|
191
|
|
192
|
|
193 static struct console *
|
|
194 decode_tty_console (Lisp_Object console)
|
|
195 {
|
|
196 XSETCONSOLE (console, decode_console (console));
|
|
197 CHECK_TTY_CONSOLE (console);
|
|
198 return XCONSOLE (console);
|
|
199 }
|
|
200
|
20
|
201 DEFUN ("console-tty-terminal-type", Fconsole_tty_terminal_type, 0, 1, 0, /*
|
0
|
202 Return the terminal type of TTY console CONSOLE.
|
20
|
203 */
|
|
204 (console))
|
0
|
205 {
|
|
206 return CONSOLE_TTY_DATA (decode_tty_console (console))->terminal_type;
|
|
207 }
|
|
208
|
|
209 Lisp_Object
|
|
210 tty_semi_canonicalize_console_connection (Lisp_Object connection,
|
|
211 Error_behavior errb)
|
|
212 {
|
78
|
213 return stream_semi_canonicalize_console_connection (connection, errb);
|
0
|
214 }
|
|
215
|
|
216 Lisp_Object
|
|
217 tty_canonicalize_console_connection (Lisp_Object connection,
|
|
218 Error_behavior errb)
|
|
219 {
|
78
|
220 return stream_canonicalize_console_connection (connection, errb);
|
0
|
221 }
|
|
222
|
|
223 Lisp_Object
|
|
224 tty_semi_canonicalize_device_connection (Lisp_Object connection,
|
|
225 Error_behavior errb)
|
|
226 {
|
78
|
227 return stream_semi_canonicalize_console_connection (connection, errb);
|
0
|
228 }
|
|
229
|
|
230 Lisp_Object
|
|
231 tty_canonicalize_device_connection (Lisp_Object connection,
|
|
232 Error_behavior errb)
|
|
233 {
|
78
|
234 return stream_canonicalize_console_connection (connection, errb);
|
0
|
235 }
|
|
236
|
|
237
|
|
238 /************************************************************************/
|
|
239 /* initialization */
|
|
240 /************************************************************************/
|
|
241
|
|
242 void
|
|
243 syms_of_console_tty (void)
|
|
244 {
|
20
|
245 DEFSUBR (Fconsole_tty_terminal_type);
|
0
|
246 defsymbol (&Qterminal_type, "terminal-type");
|
|
247 }
|
|
248
|
|
249 void
|
|
250 console_type_create_tty (void)
|
|
251 {
|
|
252 INITIALIZE_CONSOLE_TYPE (tty, "tty", "console-tty-p");
|
|
253
|
|
254 /* console methods */
|
|
255 CONSOLE_HAS_METHOD (tty, init_console);
|
|
256 CONSOLE_HAS_METHOD (tty, mark_console);
|
|
257 CONSOLE_HAS_METHOD (tty, initially_selected_for_input);
|
|
258 CONSOLE_HAS_METHOD (tty, delete_console);
|
|
259 CONSOLE_HAS_METHOD (tty, canonicalize_console_connection);
|
|
260 CONSOLE_HAS_METHOD (tty, canonicalize_device_connection);
|
|
261 CONSOLE_HAS_METHOD (tty, semi_canonicalize_console_connection);
|
|
262 CONSOLE_HAS_METHOD (tty, semi_canonicalize_device_connection);
|
|
263 }
|
|
264
|
|
265 void
|
|
266 vars_of_console_tty (void)
|
|
267 {
|
|
268 Fprovide (Qtty);
|
|
269 }
|