Initial revision
[chise/xemacs-chise.git.1] / src / console-stream.c
1 /* Stream device functions.
2    Copyright (C) 1995 Free Software Foundation, Inc.
3    Copyright (C) 1996 Ben Wing.
4
5 This file is part of XEmacs.
6
7 XEmacs is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by the
9 Free Software Foundation; either version 2, or (at your option) any
10 later version.
11
12 XEmacs is distributed in the hope that it will be useful, but WITHOUT
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
15 for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with XEmacs; see the file COPYING.  If not, write to
19 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA.  */
21
22 /* Synched up with: Not in FSF. */
23
24 /* This file has been Mule-ized. */
25
26 /* Written by Ben Wing. */
27
28 #include <config.h>
29 #include "lisp.h"
30
31 #include "console-stream.h"
32 #include "console-tty.h"
33 #include "events.h"
34 #include "frame.h"
35 #include "redisplay.h"
36 #include "sysdep.h"
37 #include "sysfile.h"
38 #include "window.h"
39
40 DEFINE_CONSOLE_TYPE (stream);
41
42 Lisp_Object Vterminal_console;
43 Lisp_Object Vterminal_device;
44 Lisp_Object Vterminal_frame;
45
46 Lisp_Object Vstdio_str;
47
48 static void
49 allocate_stream_console_struct (struct console *con)
50 {
51   if (!CONSOLE_STREAM_DATA (con))
52     CONSOLE_STREAM_DATA (con) = xnew_and_zero (struct stream_console);
53   else
54     xzero (*CONSOLE_STREAM_DATA (con));
55 }
56
57 static void
58 stream_init_console (struct console *con, Lisp_Object params)
59 {
60   Lisp_Object tty = CONSOLE_CONNECTION (con);
61   FILE *infd, *outfd, *errfd;
62
63   /* Open the specified console */
64
65   if (NILP (tty) || internal_equal (tty, Vstdio_str, 0))
66     {
67       infd  = stdin;
68       outfd = stdout;
69       errfd = stderr;
70     }
71   else
72     {
73       CHECK_STRING (tty);
74       infd = outfd = errfd =
75         fopen ((char *) XSTRING_DATA (tty), "r+");
76       if (!infd)
77         error ("Unable to open tty %s", XSTRING_DATA (tty));
78     }
79
80   allocate_stream_console_struct (con);
81   CONSOLE_STREAM_DATA (con)->infd  = infd;
82   CONSOLE_STREAM_DATA (con)->outfd = outfd;
83   CONSOLE_STREAM_DATA (con)->errfd = errfd;
84 }
85
86 static void
87 stream_init_device (struct device *d, Lisp_Object params)
88 {
89   struct console *con = XCONSOLE (DEVICE_CONSOLE (d));
90
91   DEVICE_INFD  (d) = fileno (CONSOLE_STREAM_DATA (con)->infd);
92   DEVICE_OUTFD (d) = fileno (CONSOLE_STREAM_DATA (con)->outfd);
93   init_baud_rate (d);
94   init_one_device (d);
95 }
96
97 static int
98 stream_initially_selected_for_input (struct console *con)
99 {
100   return noninteractive && initialized;
101 }
102
103 static void
104 free_stream_console_struct (struct console *con)
105 {
106   if (CONSOLE_STREAM_DATA (con))
107     {
108       xfree (CONSOLE_STREAM_DATA (con));
109       CONSOLE_STREAM_DATA (con) = NULL;
110     }
111 }
112
113 extern int stdout_needs_newline;
114
115 static void
116 stream_delete_console (struct console *con)
117 {
118   if (/* CONSOLE_STREAM_DATA (con)->needs_newline */
119       stdout_needs_newline) /* #### clean this up */
120     {
121       fputc ('\n', CONSOLE_STREAM_DATA (con)->outfd);
122       fflush (CONSOLE_STREAM_DATA (con)->outfd);
123     }
124   if (CONSOLE_STREAM_DATA (con)->infd != stdin)
125     fclose (CONSOLE_STREAM_DATA (con)->infd);
126   free_stream_console_struct (con);
127 }
128
129 Lisp_Object
130 stream_semi_canonicalize_console_connection (Lisp_Object connection,
131                                              Error_behavior errb)
132 {
133   return NILP (connection) ? Vstdio_str : connection;
134 }
135
136 Lisp_Object
137 stream_canonicalize_console_connection (Lisp_Object connection,
138                                         Error_behavior errb)
139 {
140   if (NILP (connection) || internal_equal (connection, Vstdio_str, 0))
141     return Vstdio_str;
142
143   if (!ERRB_EQ (errb, ERROR_ME))
144     {
145       if (!STRINGP (connection))
146         return Qunbound;
147     }
148   else
149     CHECK_STRING (connection);
150
151   return Ffile_truename (connection, Qnil);
152 }
153
154 Lisp_Object
155 stream_semi_canonicalize_device_connection (Lisp_Object connection,
156                                             Error_behavior errb)
157 {
158   return stream_semi_canonicalize_console_connection (connection, errb);
159 }
160
161 Lisp_Object
162 stream_canonicalize_device_connection (Lisp_Object connection,
163                                        Error_behavior errb)
164 {
165   return stream_canonicalize_console_connection (connection, errb);
166 }
167
168 \f
169 static void
170 stream_init_frame_1 (struct frame *f, Lisp_Object props)
171 {
172 #if 0
173   struct device *d = XDEVICE (FRAME_DEVICE (f));
174   if (!NILP (DEVICE_FRAME_LIST (d)))
175     error ("Only one frame allowed on stream devices");
176 #endif
177   f->name = build_string ("stream");
178   f->height = 80;
179   f->width = 24;
180   f->visible = 0; /* so redisplay doesn't try to do anything */
181 }
182
183 \f
184 static int
185 stream_text_width (struct frame *f, struct face_cachel *cachel,
186                    CONST Emchar *str, Charcount len)
187 {
188   return len;
189 }
190
191 static int
192 stream_left_margin_width (struct window *w)
193 {
194   return 0;
195 }
196
197 static int
198 stream_right_margin_width (struct window *w)
199 {
200   return 0;
201 }
202
203 static int
204 stream_divider_height (void)
205 {
206   return 1;
207 }
208
209 static int
210 stream_eol_cursor_width (void)
211 {
212   return 1;
213 }
214
215 static void
216 stream_output_begin (struct device *d)
217 {
218 }
219
220 static void
221 stream_output_end (struct device *d)
222 {
223 }
224
225 static void
226 stream_output_display_block (struct window *w, struct display_line *dl,
227                              int block, int start, int end,
228                              int start_pixpos, int cursor_start,
229                              int cursor_width, int cursor_height)
230 {
231 }
232
233 static void
234 stream_output_vertical_divider (struct window *w, int clear)
235 {
236 }
237
238 static void
239 stream_clear_to_window_end (struct window *w, int ypos1, int ypos2)
240 {
241 }
242
243 static void
244 stream_clear_region (Lisp_Object locale, face_index findex, int x, int y,
245                        int width, int height)
246 {
247 }
248
249 static void
250 stream_clear_frame (struct frame *f)
251 {
252 }
253
254 static int
255 stream_flash (struct device *d)
256 {
257   return 0; /* sorry can't do it */
258 }
259
260 static void
261 stream_ring_bell (struct device *d, int volume, int pitch, int duration)
262 {
263   struct console *c = XCONSOLE (DEVICE_CONSOLE (d));
264   fputc (07, CONSOLE_STREAM_DATA (c)->outfd);
265   fflush (CONSOLE_STREAM_DATA (c)->outfd);
266 }
267
268 \f
269 /************************************************************************/
270 /*                            initialization                            */
271 /************************************************************************/
272
273 void
274 console_type_create_stream (void)
275 {
276   INITIALIZE_CONSOLE_TYPE (stream, "stream", "console-stream-p");
277
278   /* console methods */
279   CONSOLE_HAS_METHOD (stream, init_console);
280   CONSOLE_HAS_METHOD (stream, initially_selected_for_input);
281   CONSOLE_HAS_METHOD (stream, delete_console);
282   CONSOLE_HAS_METHOD (stream, canonicalize_console_connection);
283   CONSOLE_HAS_METHOD (stream, canonicalize_device_connection);
284   CONSOLE_HAS_METHOD (stream, semi_canonicalize_console_connection);
285   CONSOLE_HAS_METHOD (stream, semi_canonicalize_device_connection);
286
287   /* device methods */
288   CONSOLE_HAS_METHOD (stream, init_device);
289
290   /* frame methods */
291   CONSOLE_HAS_METHOD (stream, init_frame_1);
292
293   /* redisplay methods */
294   CONSOLE_HAS_METHOD (stream, left_margin_width);
295   CONSOLE_HAS_METHOD (stream, right_margin_width);
296   CONSOLE_HAS_METHOD (stream, text_width);
297   CONSOLE_HAS_METHOD (stream, output_display_block);
298   CONSOLE_HAS_METHOD (stream, output_vertical_divider);
299   CONSOLE_HAS_METHOD (stream, divider_height);
300   CONSOLE_HAS_METHOD (stream, eol_cursor_width);
301   CONSOLE_HAS_METHOD (stream, clear_to_window_end);
302   CONSOLE_HAS_METHOD (stream, clear_region);
303   CONSOLE_HAS_METHOD (stream, clear_frame);
304   CONSOLE_HAS_METHOD (stream, output_begin);
305   CONSOLE_HAS_METHOD (stream, output_end);
306   CONSOLE_HAS_METHOD (stream, flash);
307   CONSOLE_HAS_METHOD (stream, ring_bell);
308 }
309
310 void
311 vars_of_console_stream (void)
312 {
313   DEFVAR_LISP ("terminal-console", &Vterminal_console /*
314 The initial console-object, which represents XEmacs' stdout.
315 */ );
316   Vterminal_console = Qnil;
317
318   DEFVAR_LISP ("terminal-device", &Vterminal_device /*
319 The initial device-object, which represents XEmacs' stdout.
320 */ );
321   Vterminal_device = Qnil;
322
323   DEFVAR_LISP ("terminal-frame", &Vterminal_frame /*
324 The initial frame-object, which represents XEmacs' stdout.
325 */ );
326   Vterminal_frame = Qnil;
327
328   /* Moved from console-tty.c */
329   Vstdio_str = build_string ("stdio");
330   staticpro (&Vstdio_str);
331 }
332
333 void
334 init_console_stream (void)
335 {
336   /* This function can GC */
337   if (!initialized)
338     {
339       Vterminal_device = Fmake_device (Qstream, Qnil, Qnil);
340       Vterminal_console = Fdevice_console (Vterminal_device);
341       Vterminal_frame = Fmake_frame (Qnil, Vterminal_device);
342       minibuf_window = XFRAME (Vterminal_frame)->minibuffer_window;
343     }
344   else
345     {
346       /* Re-initialize the FILE fields of the console. */
347       stream_init_console (XCONSOLE (Vterminal_console), Qnil);
348       if (noninteractive)
349         event_stream_select_console (XCONSOLE (Vterminal_console));
350     }
351 }