comparison src/console-tty.c @ 0:376386a54a3c r19-14

Import from CVS: tag r19-14
author cvs
date Mon, 13 Aug 2007 08:45:50 +0200
parents
children bcdc7deadc19
comparison
equal deleted inserted replaced
-1:000000000000 0:376386a54a3c
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
46 Lisp_Object Vstdio_str;
47
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 {
66 Lisp_Object tty = CONSOLE_CONNECTION (con), terminal_type = Qnil;
67 int infd, outfd;
68 struct gcpro gcpro1;
69
70 GCPRO1 (terminal_type);
71
72 terminal_type = Fplist_get (props, Qterminal_type, Qnil);
73
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 {
101 infd = outfd = open ((char *) string_data (XSTRING (tty)), O_RDWR);
102 if (infd < 0)
103 error ("Unable to open tty %s", string_data (XSTRING (tty)));
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);
113 CONSOLE_TTY_DATA (con)->terminal_type = terminal_type;
114 if (NILP (CONSOLE_NAME (con)))
115 CONSOLE_NAME (con) = Ffile_name_nondirectory (tty);
116 {
117 int tty_pg;
118 int controlling_tty_pg;
119 int cfd;
120
121 /* OK, the only sure-fire way I can think of to determine
122 whether a particular TTY is our controlling TTY is to check
123 if it has the same foreground process group as our controlling
124 TTY. This is OK because a process group can never simultaneously
125 be the foreground process group of two TTY's (in that case it
126 would have two controlling TTY's, which is not allowed). */
127
128 EMACS_GET_TTY_PROCESS_GROUP (infd, &tty_pg);
129 cfd = open ("/dev/tty", O_RDWR, 0);
130 EMACS_GET_TTY_PROCESS_GROUP (cfd, &controlling_tty_pg);
131 close (cfd);
132 if (tty_pg == controlling_tty_pg)
133 {
134 CONSOLE_TTY_DATA (con)->controlling_terminal = 1;
135 XSETCONSOLE (Vcontrolling_terminal, con);
136 munge_tty_process_group ();
137 }
138 else
139 CONSOLE_TTY_DATA (con)->controlling_terminal = 0;
140 }
141
142 UNGCPRO;
143 }
144
145 static void
146 tty_mark_console (struct console *con, void (*markobj) (Lisp_Object))
147 {
148 ((markobj) (CONSOLE_TTY_DATA (con)->terminal_type));
149 ((markobj) (CONSOLE_TTY_DATA (con)->instream));
150 ((markobj) (CONSOLE_TTY_DATA (con)->outstream));
151 }
152
153 static int
154 tty_initially_selected_for_input (struct console *con)
155 {
156 return 1;
157 }
158
159 static void
160 free_tty_console_struct (struct console *con)
161 {
162 struct tty_console *tcon = (struct tty_console *) con->console_data;
163 if (tcon && tcon->term_entry_buffer) /* allocated in term_init () */
164 xfree (tcon->term_entry_buffer);
165 if (tcon)
166 xfree (tcon);
167 }
168
169 static void
170 tty_delete_console (struct console *con)
171 {
172 Lstream_close (XLSTREAM (CONSOLE_TTY_DATA (con)->instream));
173 Lstream_close (XLSTREAM (CONSOLE_TTY_DATA (con)->outstream));
174 if (!CONSOLE_TTY_DATA (con)->is_stdio)
175 close (CONSOLE_TTY_DATA (con)->infd);
176 if (CONSOLE_TTY_DATA (con)->controlling_terminal)
177 {
178 Vcontrolling_terminal = Qnil;
179 unmunge_tty_process_group ();
180 }
181 free_tty_console_struct (con);
182 }
183
184
185 static struct console *
186 decode_tty_console (Lisp_Object console)
187 {
188 XSETCONSOLE (console, decode_console (console));
189 CHECK_TTY_CONSOLE (console);
190 return XCONSOLE (console);
191 }
192
193 DEFUN ("console-tty-terminal-type", Fconsole_tty_terminal_type,
194 Sconsole_tty_terminal_type, 0, 1, 0 /*
195 Return the terminal type of TTY console CONSOLE.
196 */ )
197 (console)
198 Lisp_Object console;
199 {
200 return CONSOLE_TTY_DATA (decode_tty_console (console))->terminal_type;
201 }
202
203 Lisp_Object
204 tty_semi_canonicalize_console_connection (Lisp_Object connection,
205 Error_behavior errb)
206 {
207 if (NILP (connection))
208 return Vstdio_str;
209
210 return connection;
211 }
212
213 Lisp_Object
214 tty_canonicalize_console_connection (Lisp_Object connection,
215 Error_behavior errb)
216 {
217 if (NILP (connection) || !NILP (Fequal (connection, Vstdio_str)))
218 return Vstdio_str;
219
220 if (!ERRB_EQ (errb, ERROR_ME))
221 {
222 if (!STRINGP (connection))
223 return Qunbound;
224 }
225 else
226 CHECK_STRING (connection);
227
228 return Ffile_truename (connection, Qnil);
229 }
230
231 Lisp_Object
232 tty_semi_canonicalize_device_connection (Lisp_Object connection,
233 Error_behavior errb)
234 {
235 return tty_semi_canonicalize_console_connection (connection, errb);
236 }
237
238 Lisp_Object
239 tty_canonicalize_device_connection (Lisp_Object connection,
240 Error_behavior errb)
241 {
242 return tty_canonicalize_console_connection (connection, errb);
243 }
244
245
246 /************************************************************************/
247 /* initialization */
248 /************************************************************************/
249
250 void
251 syms_of_console_tty (void)
252 {
253 defsubr (&Sconsole_tty_terminal_type);
254 defsymbol (&Qterminal_type, "terminal-type");
255 }
256
257 void
258 console_type_create_tty (void)
259 {
260 INITIALIZE_CONSOLE_TYPE (tty, "tty", "console-tty-p");
261
262 /* console methods */
263 CONSOLE_HAS_METHOD (tty, init_console);
264 CONSOLE_HAS_METHOD (tty, mark_console);
265 CONSOLE_HAS_METHOD (tty, initially_selected_for_input);
266 CONSOLE_HAS_METHOD (tty, delete_console);
267 CONSOLE_HAS_METHOD (tty, canonicalize_console_connection);
268 CONSOLE_HAS_METHOD (tty, canonicalize_device_connection);
269 CONSOLE_HAS_METHOD (tty, semi_canonicalize_console_connection);
270 CONSOLE_HAS_METHOD (tty, semi_canonicalize_device_connection);
271 }
272
273 void
274 vars_of_console_tty (void)
275 {
276 Fprovide (Qtty);
277
278 Vstdio_str = build_string ("stdio");
279 staticpro (&Vstdio_str);
280 }