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