XEmacs 21.2.34 "Molpe".
[chise/xemacs-chise.git-] / 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 stream_init_console (struct console *con, Lisp_Object params)
50 {
51   Lisp_Object tty = CONSOLE_CONNECTION (con);
52   struct stream_console *stream_con;
53
54   if (CONSOLE_STREAM_DATA (con) == NULL)
55     CONSOLE_STREAM_DATA (con) = xnew (struct stream_console);
56
57   stream_con = CONSOLE_STREAM_DATA (con);
58
59   stream_con->needs_newline = 0;
60
61   /* Open the specified console */
62   if (NILP (tty) || internal_equal (tty, Vstdio_str, 0))
63     {
64       stream_con->in  = stdin;
65       stream_con->out = stdout;
66       stream_con->err = stderr;
67     }
68   else
69     {
70       CHECK_STRING (tty);
71       stream_con->in = stream_con->out = stream_con->err =
72         fopen ((char *) XSTRING_DATA (tty), "r+");
73       if (!stream_con->in)
74         error ("Unable to open tty %s", XSTRING_DATA (tty));
75     }
76 }
77
78 static void
79 stream_init_device (struct device *d, Lisp_Object params)
80 {
81   struct console *con = XCONSOLE (DEVICE_CONSOLE (d));
82
83   DEVICE_INFD  (d) = fileno (CONSOLE_STREAM_DATA (con)->in);
84   DEVICE_OUTFD (d) = fileno (CONSOLE_STREAM_DATA (con)->out);
85   init_baud_rate (d);
86   init_one_device (d);
87 }
88
89 static int
90 stream_initially_selected_for_input (struct console *con)
91 {
92   return noninteractive && initialized;
93 }
94
95 extern int stdout_needs_newline;
96
97 static void
98 stream_delete_console (struct console *con)
99 {
100   struct stream_console *stream_con = CONSOLE_STREAM_DATA (con);
101   if (stream_con)
102     {
103       if (/* stream_con->needs_newline */
104           stdout_needs_newline) /* #### clean this up */
105         {
106           fputc ('\n', stream_con->out);
107           fflush (stream_con->out);
108         }
109       if (stream_con->in != stdin)
110         fclose (stream_con->in);
111
112       xfree (stream_con);
113       CONSOLE_STREAM_DATA (con) = NULL;
114     }
115 }
116
117 Lisp_Object
118 stream_semi_canonicalize_console_connection (Lisp_Object connection,
119                                              Error_behavior errb)
120 {
121   return NILP (connection) ? Vstdio_str : connection;
122 }
123
124 Lisp_Object
125 stream_canonicalize_console_connection (Lisp_Object connection,
126                                         Error_behavior errb)
127 {
128   if (NILP (connection) || internal_equal (connection, Vstdio_str, 0))
129     return Vstdio_str;
130
131   if (!ERRB_EQ (errb, ERROR_ME))
132     {
133       if (!STRINGP (connection))
134         return Qunbound;
135     }
136   else
137     CHECK_STRING (connection);
138
139   return Ffile_truename (connection, Qnil);
140 }
141
142 Lisp_Object
143 stream_semi_canonicalize_device_connection (Lisp_Object connection,
144                                             Error_behavior errb)
145 {
146   return stream_semi_canonicalize_console_connection (connection, errb);
147 }
148
149 Lisp_Object
150 stream_canonicalize_device_connection (Lisp_Object connection,
151                                        Error_behavior errb)
152 {
153   return stream_canonicalize_console_connection (connection, errb);
154 }
155
156 \f
157 static void
158 stream_init_frame_1 (struct frame *f, Lisp_Object props)
159 {
160 #if 0
161   struct device *d = XDEVICE (FRAME_DEVICE (f));
162   if (!NILP (DEVICE_FRAME_LIST (d)))
163     error ("Only one frame allowed on stream devices");
164 #endif
165   f->name = build_string ("stream");
166   f->height = 80;
167   f->width = 24;
168   f->visible = 0; /* so redisplay doesn't try to do anything */
169 }
170
171 \f
172 static int
173 stream_text_width (struct frame *f, struct face_cachel *cachel,
174                    const Emchar *str, Charcount len)
175 {
176   return len;
177 }
178
179 static int
180 stream_left_margin_width (struct window *w)
181 {
182   return 0;
183 }
184
185 static int
186 stream_right_margin_width (struct window *w)
187 {
188   return 0;
189 }
190
191 static int
192 stream_divider_height (void)
193 {
194   return 1;
195 }
196
197 static int
198 stream_eol_cursor_width (void)
199 {
200   return 1;
201 }
202
203 static void
204 stream_output_display_block (struct window *w, struct display_line *dl,
205                              int block, int start, int end,
206                              int start_pixpos, int cursor_start,
207                              int cursor_width, int cursor_height)
208 {
209 }
210
211 static void
212 stream_clear_region (Lisp_Object window, struct device* d, struct frame * f,
213                   face_index findex, int x, int y,
214                   int width, int height, Lisp_Object fcolor, Lisp_Object bcolor,
215                   Lisp_Object background_pixmap)
216 {
217 }
218
219 static int
220 stream_flash (struct device *d)
221 {
222   return 0; /* sorry can't do it */
223 }
224
225 static void
226 stream_ring_bell (struct device *d, int volume, int pitch, int duration)
227 {
228   struct console *c = XCONSOLE (DEVICE_CONSOLE (d));
229   fputc (07, CONSOLE_STREAM_DATA (c)->out);
230   fflush (CONSOLE_STREAM_DATA (c)->out);
231 }
232
233 \f
234 /************************************************************************/
235 /*                            initialization                            */
236 /************************************************************************/
237
238 void
239 console_type_create_stream (void)
240 {
241   INITIALIZE_CONSOLE_TYPE (stream, "stream", "console-stream-p");
242
243   /* console methods */
244   CONSOLE_HAS_METHOD (stream, init_console);
245   CONSOLE_HAS_METHOD (stream, initially_selected_for_input);
246   CONSOLE_HAS_METHOD (stream, delete_console);
247   CONSOLE_HAS_METHOD (stream, canonicalize_console_connection);
248   CONSOLE_HAS_METHOD (stream, canonicalize_device_connection);
249   CONSOLE_HAS_METHOD (stream, semi_canonicalize_console_connection);
250   CONSOLE_HAS_METHOD (stream, semi_canonicalize_device_connection);
251
252   /* device methods */
253   CONSOLE_HAS_METHOD (stream, init_device);
254
255   /* frame methods */
256   CONSOLE_HAS_METHOD (stream, init_frame_1);
257
258   /* redisplay methods */
259   CONSOLE_HAS_METHOD (stream, left_margin_width);
260   CONSOLE_HAS_METHOD (stream, right_margin_width);
261   CONSOLE_HAS_METHOD (stream, text_width);
262   CONSOLE_HAS_METHOD (stream, output_display_block);
263   CONSOLE_HAS_METHOD (stream, divider_height);
264   CONSOLE_HAS_METHOD (stream, eol_cursor_width);
265   CONSOLE_HAS_METHOD (stream, clear_region);
266   CONSOLE_HAS_METHOD (stream, flash);
267   CONSOLE_HAS_METHOD (stream, ring_bell);
268 }
269
270 void
271 reinit_console_type_create_stream (void)
272 {
273   REINITIALIZE_CONSOLE_TYPE (stream);
274 }
275
276 void
277 vars_of_console_stream (void)
278 {
279   DEFVAR_LISP ("terminal-console", &Vterminal_console /*
280 The initial console-object, which represents XEmacs' stdout.
281 */ );
282   Vterminal_console = Qnil;
283
284   DEFVAR_LISP ("terminal-device", &Vterminal_device /*
285 The initial device-object, which represents XEmacs' stdout.
286 */ );
287   Vterminal_device = Qnil;
288
289   DEFVAR_LISP ("terminal-frame", &Vterminal_frame /*
290 The initial frame-object, which represents XEmacs' stdout.
291 */ );
292   Vterminal_frame = Qnil;
293
294   /* Moved from console-tty.c */
295   Vstdio_str = build_string ("stdio");
296   staticpro (&Vstdio_str);
297 }
298
299 #ifndef PDUMP
300 void
301 init_console_stream (int reinit)
302 {
303   /* This function can GC */
304   if (!initialized)
305     {
306       Vterminal_device = Fmake_device (Qstream, Qnil, Qnil);
307       Vterminal_console = Fdevice_console (Vterminal_device);
308       Vterminal_frame = Fmake_frame (Qnil, Vterminal_device);
309       minibuf_window = XFRAME (Vterminal_frame)->minibuffer_window;
310     }
311   else
312     {
313       /* Re-initialize the FILE fields of the console. */
314       stream_init_console (XCONSOLE (Vterminal_console), Qnil);
315       if (noninteractive)
316         event_stream_select_console (XCONSOLE (Vterminal_console));
317     }
318 }
319
320 #else
321
322 void
323 init_console_stream (int reinit)
324 {
325   /* This function can GC */
326   if (!reinit)
327     {
328       Vterminal_device = Fmake_device (Qstream, Qnil, Qnil);
329       Vterminal_console = Fdevice_console (Vterminal_device);
330       Vterminal_frame = Fmake_frame (Qnil, Vterminal_device);
331       minibuf_window = XFRAME (Vterminal_frame)->minibuffer_window;
332     }
333   if (initialized)
334     {
335       stream_init_console (XCONSOLE (Vterminal_console), Qnil);
336       if (noninteractive)
337         event_stream_select_console (XCONSOLE (Vterminal_console));
338     }
339 }
340 #endif