Contents in 1999-06-04-13 of release-21-2.
[chise/xemacs-chise.git.1] / src / console-tty.c
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 "faces.h"
33 #include "frame.h"
34 #include "lstream.h"
35 #include "sysdep.h"
36 #include "sysfile.h"
37 #ifdef FILE_CODING
38 #include "file-coding.h"
39 #endif
40 #ifdef HAVE_GPM
41 #include "gpmevent.h"
42 #endif
43
44 DEFINE_CONSOLE_TYPE (tty);
45
46 Lisp_Object Qterminal_type;
47 Lisp_Object Qcontrolling_process;
48
49 \f
50 static void
51 allocate_tty_console_struct (struct console *con)
52 {
53   /* zero out all slots except the lisp ones ... */
54   CONSOLE_TTY_DATA (con) = xnew_and_zero (struct tty_console);
55   CONSOLE_TTY_DATA (con)->terminal_type = Qnil;
56   CONSOLE_TTY_DATA (con)->instream = Qnil;
57   CONSOLE_TTY_DATA (con)->outstream = Qnil;
58 }
59
60 static void
61 tty_init_console (struct console *con, Lisp_Object props)
62 {
63   Lisp_Object tty = CONSOLE_CONNECTION (con);
64   Lisp_Object terminal_type = Qnil;
65   Lisp_Object controlling_process = Qnil;
66   struct tty_console *tty_con;
67   struct gcpro gcpro1, gcpro2;
68
69   GCPRO2 (terminal_type, controlling_process);
70
71   terminal_type = Fplist_get (props, Qterminal_type, Qnil);
72   controlling_process = Fplist_get (props, Qcontrolling_process, 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 = 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   /* Determine the controlling process */
91   if (!NILP (controlling_process))
92     CHECK_INT (controlling_process);
93
94   /* Open the specified console */
95
96   allocate_tty_console_struct (con);
97   tty_con = CONSOLE_TTY_DATA (con);
98
99   if (internal_equal (tty, Vstdio_str, 0))
100     {
101       tty_con->infd  = fileno (stdin);
102       tty_con->outfd = fileno (stdout);
103       tty_con->is_stdio = 1;
104     }
105   else
106     {
107       tty_con->infd = tty_con->outfd =
108         open ((char *) XSTRING_DATA (tty), O_RDWR);
109       if (tty_con->infd < 0)
110         error ("Unable to open tty %s", XSTRING_DATA (tty));
111       tty_con->is_stdio = 0;
112     }
113
114   tty_con->instream  = make_filedesc_input_stream  (tty_con->infd,  0, -1, 0);
115   tty_con->outstream = make_filedesc_output_stream (tty_con->outfd, 0, -1, 0);
116 #ifdef MULE
117   tty_con->instream =
118     make_decoding_input_stream (XLSTREAM (tty_con->instream),
119                                 Fget_coding_system (Vkeyboard_coding_system));
120   Lstream_set_character_mode (XLSTREAM (tty_con->instream));
121   tty_con->outstream =
122     make_encoding_output_stream (XLSTREAM (tty_con->outstream),
123                                  Fget_coding_system (Vterminal_coding_system));
124 #endif /* MULE */
125   tty_con->terminal_type = terminal_type;
126   tty_con->controlling_process = controlling_process;
127
128 #ifdef HAVE_GPM
129   connect_to_gpm (con);
130 #endif
131
132   if (NILP (CONSOLE_NAME (con)))
133     CONSOLE_NAME (con) = Ffile_name_nondirectory (tty);
134   {
135     int tty_pg;
136     int controlling_tty_pg;
137     int cfd;
138
139     /* OK, the only sure-fire way I can think of to determine
140        whether a particular TTY is our controlling TTY is to check
141        if it has the same foreground process group as our controlling
142        TTY.  This is OK because a process group can never simultaneously
143        be the foreground process group of two TTY's (in that case it
144        would have two controlling TTY's, which is not allowed). */
145
146     EMACS_GET_TTY_PROCESS_GROUP (tty_con->infd, &tty_pg);
147     cfd = open ("/dev/tty", O_RDWR, 0);
148     EMACS_GET_TTY_PROCESS_GROUP (cfd, &controlling_tty_pg);
149     close (cfd);
150     if (tty_pg == controlling_tty_pg)
151       {
152         tty_con->controlling_terminal = 1;
153         XSETCONSOLE (Vcontrolling_terminal, con);
154         munge_tty_process_group ();
155       }
156     else
157       tty_con->controlling_terminal = 0;
158   }
159
160   UNGCPRO;
161 }
162
163 static void
164 tty_mark_console (struct console *con, void (*markobj) (Lisp_Object))
165 {
166   struct tty_console *tty_con = CONSOLE_TTY_DATA (con);
167   markobj (tty_con->terminal_type);
168   markobj (tty_con->instream);
169   markobj (tty_con->outstream);
170 }
171
172 static int
173 tty_initially_selected_for_input (struct console *con)
174 {
175   return 1;
176 }
177
178 static void
179 free_tty_console_struct (struct console *con)
180 {
181   struct tty_console *tty_con = CONSOLE_TTY_DATA (con);
182   if (tty_con)
183     {
184       if (tty_con->term_entry_buffer) /* allocated in term_init () */
185         {
186           xfree (tty_con->term_entry_buffer);
187           tty_con->term_entry_buffer = NULL;
188         }
189       xfree (tty_con);
190       CONSOLE_TTY_DATA (con) = NULL;
191     }
192 }
193
194 static void
195 tty_delete_console (struct console *con)
196 {
197   Lstream_close (XLSTREAM (CONSOLE_TTY_DATA (con)->instream));
198   Lstream_close (XLSTREAM (CONSOLE_TTY_DATA (con)->outstream));
199   if (!CONSOLE_TTY_DATA (con)->is_stdio)
200     close (CONSOLE_TTY_DATA (con)->infd);
201   if (CONSOLE_TTY_DATA (con)->controlling_terminal)
202     {
203       Vcontrolling_terminal = Qnil;
204       unmunge_tty_process_group ();
205     }
206   free_tty_console_struct (con);
207 }
208
209 \f
210 static struct console *
211 decode_tty_console (Lisp_Object console)
212 {
213   XSETCONSOLE (console, decode_console (console));
214   CHECK_TTY_CONSOLE (console);
215   return XCONSOLE (console);
216 }
217
218 DEFUN ("console-tty-terminal-type", Fconsole_tty_terminal_type,
219        0, 1, 0, /*
220 Return the terminal type of TTY console CONSOLE.
221 */
222        (console))
223 {
224   return CONSOLE_TTY_DATA (decode_tty_console (console))->terminal_type;
225 }
226
227 DEFUN ("console-tty-controlling-process", Fconsole_tty_controlling_process,
228        0, 1, 0, /*
229 Return the controlling process of tty console CONSOLE.
230 */
231        (console))
232 {
233   return CONSOLE_TTY_DATA (decode_tty_console (console))->controlling_process;
234 }
235
236 #ifdef FILE_CODING
237 \f
238 DEFUN ("console-tty-input-coding-system", Fconsole_tty_input_coding_system,
239        0, 1, 0, /*
240 Return the input coding system of tty console CONSOLE.
241 */
242        (console))
243 {
244   return decoding_stream_coding_system
245     (XLSTREAM (CONSOLE_TTY_DATA (decode_tty_console (console))->instream));
246 }
247
248 DEFUN ("set-console-tty-input-coding-system", Fset_console_tty_input_coding_system,
249        0, 2, 0, /*
250 Set the input coding system of tty console CONSOLE to CODESYS.
251 CONSOLE defaults to the selected console.
252 CODESYS defaults to the value of `keyboard-coding-system'.
253 */
254         (console, codesys))
255 {
256   set_decoding_stream_coding_system
257     (XLSTREAM (CONSOLE_TTY_DATA (decode_tty_console (console))->instream),
258      Fget_coding_system (NILP (codesys) ? Vkeyboard_coding_system : codesys));
259   return Qnil;
260 }
261
262 DEFUN ("console-tty-output-coding-system", Fconsole_tty_output_coding_system,
263        0, 1, 0, /*
264 Return TTY CONSOLE's output coding system.
265 */
266        (console))
267 {
268   return encoding_stream_coding_system
269     (XLSTREAM (CONSOLE_TTY_DATA (decode_tty_console (console))->outstream));
270 }
271
272 DEFUN ("set-console-tty-output-coding-system", Fset_console_tty_output_coding_system,
273        0, 2, 0, /*
274 Set the coding system of tty output of console CONSOLE to CODESYS.
275 CONSOLE defaults to the selected console.
276 CODESYS defaults to the value of `terminal-coding-system'.
277 */
278         (console, codesys))
279 {
280   set_encoding_stream_coding_system
281     (XLSTREAM (CONSOLE_TTY_DATA (decode_tty_console (console))->outstream),
282      Fget_coding_system (NILP (codesys) ? Vterminal_coding_system : codesys));
283   return Qnil;
284 }
285
286 /* ### Move this function to lisp */
287 DEFUN ("set-console-tty-coding-system", Fset_console_tty_coding_system,
288        0, 2, 0, /*
289 Set the input and output coding systems of tty console CONSOLE to CODESYS.
290 CONSOLE defaults to the selected console.
291 If CODESYS is nil, the values of `keyboard-coding-system' and
292 `terminal-coding-system' will be used for the input and
293 output coding systems of CONSOLE.
294 */
295         (console, codesys))
296 {
297   Fset_console_tty_input_coding_system (console, codesys);
298   Fset_console_tty_output_coding_system (console, codesys);
299   return Qnil;
300 }
301 #endif /* FILE_CODING */
302 \f
303
304 Lisp_Object
305 tty_semi_canonicalize_console_connection (Lisp_Object connection,
306                                           Error_behavior errb)
307 {
308   return stream_semi_canonicalize_console_connection (connection, errb);
309 }
310
311 Lisp_Object
312 tty_canonicalize_console_connection (Lisp_Object connection,
313                                      Error_behavior errb)
314 {
315   return stream_canonicalize_console_connection (connection, errb);
316 }
317
318 Lisp_Object
319 tty_semi_canonicalize_device_connection (Lisp_Object connection,
320                                          Error_behavior errb)
321 {
322   return stream_semi_canonicalize_console_connection (connection, errb);
323 }
324
325 Lisp_Object
326 tty_canonicalize_device_connection (Lisp_Object connection,
327                                     Error_behavior errb)
328 {
329   return stream_canonicalize_console_connection (connection, errb);
330 }
331
332 \f
333 /************************************************************************/
334 /*                            initialization                            */
335 /************************************************************************/
336
337 void
338 syms_of_console_tty (void)
339 {
340   DEFSUBR (Fconsole_tty_terminal_type);
341   DEFSUBR (Fconsole_tty_controlling_process);
342   defsymbol (&Qterminal_type, "terminal-type");
343   defsymbol (&Qcontrolling_process, "controlling-process");
344 #ifdef FILE_CODING
345   DEFSUBR (Fconsole_tty_output_coding_system);
346   DEFSUBR (Fset_console_tty_output_coding_system);
347   DEFSUBR (Fconsole_tty_input_coding_system);
348   DEFSUBR (Fset_console_tty_input_coding_system);
349   DEFSUBR (Fset_console_tty_coding_system);
350 #endif /* FILE_CODING */
351 }
352
353 void
354 console_type_create_tty (void)
355 {
356   INITIALIZE_CONSOLE_TYPE (tty, "tty", "console-tty-p");
357
358   /* console methods */
359   CONSOLE_HAS_METHOD (tty, init_console);
360   CONSOLE_HAS_METHOD (tty, mark_console);
361   CONSOLE_HAS_METHOD (tty, initially_selected_for_input);
362   CONSOLE_HAS_METHOD (tty, delete_console);
363   CONSOLE_HAS_METHOD (tty, canonicalize_console_connection);
364   CONSOLE_HAS_METHOD (tty, canonicalize_device_connection);
365   CONSOLE_HAS_METHOD (tty, semi_canonicalize_console_connection);
366   CONSOLE_HAS_METHOD (tty, semi_canonicalize_device_connection);
367 }
368
369 void
370 vars_of_console_tty (void)
371 {
372   Fprovide (Qtty);
373 }