ANSI C fixes.
[wine] / server / console.c
1 /*
2  * Server-side console management
3  *
4  * Copyright (C) 1998 Alexandre Julliard
5  *
6  * FIXME: all this stuff is a hack to avoid breaking
7  *        the client-side console support.
8  */
9
10 #include <assert.h>
11 #include <fcntl.h>
12 #include <signal.h>
13 #include <string.h>
14 #include <stdio.h>
15 #include <stdlib.h>
16 #include <sys/errno.h>
17 #include <sys/stat.h>
18 #include <sys/time.h>
19 #include <sys/types.h>
20 #include <time.h>
21 #include <unistd.h>
22
23 #include "winerror.h"
24 #include "winnt.h"
25 #include "wincon.h"
26
27 #include "handle.h"
28 #include "process.h"
29 #include "thread.h"
30 #include "request.h"
31
32 struct screen_buffer;
33
34 struct console_input
35 {
36     struct object         obj;           /* object header */
37     struct select_user    select;        /* select user */
38     int                   mode;          /* input mode */
39     struct screen_buffer *output;        /* associated screen buffer */
40     int                   recnum;        /* number of input records */
41     INPUT_RECORD         *records;       /* input records */
42 };
43
44 struct screen_buffer
45 {
46     struct object         obj;           /* object header */
47     struct select_user    select;        /* select user */
48     int                   mode;          /* output mode */
49     struct console_input *input;         /* associated console input */
50     int                   cursor_size;   /* size of cursor (percentage filled) */
51     int                   cursor_visible;/* cursor visibility flag */
52     int                   pid;           /* xterm pid (hack) */
53     char                 *title;         /* console title */
54 };
55
56
57 static void console_input_dump( struct object *obj, int verbose );
58 static int console_input_add_queue( struct object *obj, struct wait_queue_entry *entry );
59 static void console_input_remove_queue( struct object *obj, struct wait_queue_entry *entry );
60 static int console_input_signaled( struct object *obj, struct thread *thread );
61 static int console_input_get_read_fd( struct object *obj );
62 static void console_input_destroy( struct object *obj );
63
64 static void screen_buffer_dump( struct object *obj, int verbose );
65 static int screen_buffer_add_queue( struct object *obj, struct wait_queue_entry *entry );
66 static void screen_buffer_remove_queue( struct object *obj, struct wait_queue_entry *entry );
67 static int screen_buffer_signaled( struct object *obj, struct thread *thread );
68 static int screen_buffer_get_write_fd( struct object *obj );
69 static void screen_buffer_destroy( struct object *obj );
70
71 /* common routine */
72 static int console_get_info( struct object *obj, struct get_file_info_request *req );
73
74 static const struct object_ops console_input_ops =
75 {
76     sizeof(struct console_input),
77     console_input_dump,
78     console_input_add_queue,
79     console_input_remove_queue,
80     console_input_signaled,
81     no_satisfied,
82     console_input_get_read_fd,
83     no_write_fd,
84     no_flush,
85     console_get_info,
86     console_input_destroy
87 };
88
89 static const struct object_ops screen_buffer_ops =
90 {
91     sizeof(struct screen_buffer),
92     screen_buffer_dump,
93     screen_buffer_add_queue,
94     screen_buffer_remove_queue,
95     screen_buffer_signaled,
96     no_satisfied,
97     no_read_fd,
98     screen_buffer_get_write_fd,
99     no_flush,
100     console_get_info,
101     screen_buffer_destroy
102 };
103
104
105 static struct object *create_console_input( int fd )
106 {
107     struct console_input *console_input;
108
109     if ((fd = (fd != -1) ? dup(fd) : dup(0)) == -1)
110     {
111         file_set_error();
112         return NULL;
113     }
114     if ((console_input = alloc_object( &console_input_ops )))
115     {
116         console_input->select.fd      = fd;
117         console_input->select.func    = default_select_event;
118         console_input->select.private = console_input;
119         console_input->mode           = ENABLE_PROCESSED_INPUT | ENABLE_LINE_INPUT |
120                                         ENABLE_ECHO_INPUT | ENABLE_MOUSE_INPUT;
121         console_input->output         = NULL;
122         console_input->recnum         = 0;
123         console_input->records        = NULL;
124         register_select_user( &console_input->select );
125         return &console_input->obj;
126     }
127     close( fd );
128     return NULL;
129 }
130
131 static struct object *create_console_output( int fd, struct object *input )
132 {
133     struct console_input *console_input = (struct console_input *)input;
134     struct screen_buffer *screen_buffer;
135
136     if ((fd = (fd != -1) ? dup(fd) : dup(1)) == -1)
137     {
138         file_set_error();
139         return NULL;
140     }
141     if ((screen_buffer = alloc_object( &screen_buffer_ops )))
142     {
143         screen_buffer->select.fd      = fd;
144         screen_buffer->select.func    = default_select_event;
145         screen_buffer->select.private = screen_buffer;
146         screen_buffer->mode           = ENABLE_PROCESSED_OUTPUT | ENABLE_WRAP_AT_EOL_OUTPUT;
147         screen_buffer->input          = console_input;
148         screen_buffer->cursor_size    = 100;
149         screen_buffer->cursor_visible = 1;
150         screen_buffer->pid            = 0;
151         screen_buffer->title          = strdup( "Wine console" );
152         register_select_user( &screen_buffer->select );
153         console_input->output = screen_buffer;
154         return &screen_buffer->obj;
155     }
156     close( fd );
157     return NULL;
158 }
159
160 /* allocate a console for this process */
161 int alloc_console( struct process *process )
162 {
163     if (process->console_in || process->console_out)
164     {
165         set_error( ERROR_ACCESS_DENIED );
166         return 0;
167     }
168     if ((process->console_in = create_console_input( -1 )))
169     {
170         if ((process->console_out = create_console_output( -1, process->console_in )))
171             return 1;
172         release_object( process->console_in );
173     }
174     return 0;
175 }
176
177 /* free the console for this process */
178 int free_console( struct process *process )
179 {
180     if (process->console_in) release_object( process->console_in );
181     if (process->console_out) release_object( process->console_out );
182     process->console_in = process->console_out = NULL;
183     return 1;
184 }
185
186 static int set_console_fd( int handle, int fd_in, int fd_out, int pid )
187 {
188     struct console_input *input;
189     struct screen_buffer *output;
190     struct object *obj;
191
192     if (!(obj = get_handle_obj( current->process, handle, 0, NULL )))
193         return 0;
194     if (obj->ops == &console_input_ops)
195     {
196         input = (struct console_input *)obj;
197         output = input->output;
198         grab_object( output );
199     }
200     else if (obj->ops == &screen_buffer_ops)
201     {
202         output = (struct screen_buffer *)obj;
203         input = output->input;
204         grab_object( input );
205     }
206     else
207     {
208         set_error( ERROR_INVALID_HANDLE );
209         release_object( obj );
210         return 0;
211     }
212
213     /* can't change the fd if someone is waiting on it */
214     assert( !input->obj.head );
215     assert( !output->obj.head );
216
217     unregister_select_user( &input->select );
218     unregister_select_user( &output->select );
219     close( input->select.fd );
220     close( output->select.fd );
221     input->select.fd  = fd_in;
222     output->select.fd = fd_out;
223     output->pid       = pid;
224     register_select_user( &input->select );
225     register_select_user( &output->select );
226     release_object( input );
227     release_object( output );
228     return 1;
229 }
230
231 static int get_console_mode( int handle )
232 {
233     struct object *obj;
234     int ret = 0;
235
236     if ((obj = get_handle_obj( current->process, handle, GENERIC_READ, NULL )))
237     {
238         if (obj->ops == &console_input_ops)
239             ret = ((struct console_input *)obj)->mode;
240         else if (obj->ops == &screen_buffer_ops)
241             ret = ((struct screen_buffer *)obj)->mode;
242         else
243             set_error( ERROR_INVALID_HANDLE );
244         release_object( obj );
245     }
246     return ret;
247 }
248
249 static int set_console_mode( int handle, int mode )
250 {
251     struct object *obj;
252     int ret = 0;
253
254     if (!(obj = get_handle_obj( current->process, handle, GENERIC_READ, NULL )))
255         return 0;
256     if (obj->ops == &console_input_ops)
257     {
258         ((struct console_input *)obj)->mode = mode;
259         ret = 1;
260     }
261     else if (obj->ops == &screen_buffer_ops)
262     {
263         ((struct screen_buffer *)obj)->mode = mode;
264         ret = 1;
265     }
266     else set_error( ERROR_INVALID_HANDLE );
267     release_object( obj );
268     return ret;
269 }
270
271 /* set misc console information (output handle only) */
272 static int set_console_info( int handle, struct set_console_info_request *req,
273                              const char *title, size_t len )
274 {
275     struct screen_buffer *console;
276     if (!(console = (struct screen_buffer *)get_handle_obj( current->process, handle,
277                                                             GENERIC_WRITE, &screen_buffer_ops )))
278         return 0;
279     if (req->mask & SET_CONSOLE_INFO_CURSOR)
280     {
281         console->cursor_size    = req->cursor_size;
282         console->cursor_visible = req->cursor_visible;
283     }
284     if (req->mask & SET_CONSOLE_INFO_TITLE)
285     {
286         char *new_title = mem_alloc( len + 1 );
287         if (new_title)
288         {
289             memcpy( new_title, title, len );
290             new_title[len] = 0;
291             if (console->title) free( console->title );
292             console->title = new_title;
293         }
294     }
295     release_object( console );
296     return 1;
297 }
298
299 /* add input events to a console input queue */
300 static int write_console_input( int handle, int count, INPUT_RECORD *records )
301 {
302     INPUT_RECORD *new_rec;
303     struct console_input *console;
304
305     if (!(console = (struct console_input *)get_handle_obj( current->process, handle,
306                                                             GENERIC_WRITE, &console_input_ops )))
307         return -1;
308     if (!(new_rec = realloc( console->records,
309                              (console->recnum + count) * sizeof(INPUT_RECORD) )))
310     {
311         set_error( ERROR_NOT_ENOUGH_MEMORY );
312         release_object( console );
313         return -1;
314     }
315     console->records = new_rec;
316     memcpy( new_rec + console->recnum, records, count * sizeof(INPUT_RECORD) );
317     console->recnum += count;
318     release_object( console );
319     return count;
320 }
321
322 /* retrieve a pointer to the console input records */
323 static int read_console_input( int handle, int count, INPUT_RECORD *rec, int max, int flush )
324 {
325     struct console_input *console;
326
327     if (!(console = (struct console_input *)get_handle_obj( current->process, handle,
328                                                             GENERIC_READ, &console_input_ops )))
329         return -1;
330     if ((count < 0) || (count > console->recnum)) count = console->recnum;
331     if (count > max) count = max;
332     memcpy( rec, console->records, count * sizeof(INPUT_RECORD) );
333     if (flush)
334     {
335         int i;
336         for (i = count; i < console->recnum; i++)
337             console->records[i-count] = console->records[i];
338         if ((console->recnum -= count) > 0)
339         {
340             INPUT_RECORD *new_rec = realloc( console->records,
341                                              console->recnum * sizeof(INPUT_RECORD) );
342             if (new_rec) console->records = new_rec;
343         }
344         else
345         {
346             free( console->records );
347             console->records = NULL;
348         }
349     }
350     release_object( console );
351     return count;
352 }
353
354 static void console_input_dump( struct object *obj, int verbose )
355 {
356     struct console_input *console = (struct console_input *)obj;
357     assert( obj->ops == &console_input_ops );
358     fprintf( stderr, "Console input fd=%d\n", console->select.fd );
359 }
360
361 static int console_input_add_queue( struct object *obj, struct wait_queue_entry *entry )
362 {
363     struct console_input *console = (struct console_input *)obj;
364     assert( obj->ops == &console_input_ops );
365     if (!obj->head)  /* first on the queue */
366         set_select_events( &console->select, READ_EVENT );
367     add_queue( obj, entry );
368     return 1;
369 }
370
371 static void console_input_remove_queue( struct object *obj, struct wait_queue_entry *entry )
372 {
373     struct console_input *console = (struct console_input *)grab_object(obj);
374     assert( obj->ops == &console_input_ops );
375
376     remove_queue( obj, entry );
377     if (!obj->head)  /* last on the queue is gone */
378         set_select_events( &console->select, 0 );
379     release_object( obj );
380 }
381
382 static int console_input_signaled( struct object *obj, struct thread *thread )
383 {
384     struct console_input *console = (struct console_input *)obj;
385     assert( obj->ops == &console_input_ops );
386
387     if (check_select_events( &console->select, READ_EVENT ))
388     {
389         /* stop waiting on select() if we are signaled */
390         set_select_events( &console->select, 0 );
391         return 1;
392     }
393     else
394     {
395         /* restart waiting on select() if we are no longer signaled */
396         if (obj->head) set_select_events( &console->select, READ_EVENT );
397         return 0;
398     }
399 }
400
401 static int console_input_get_read_fd( struct object *obj )
402 {
403     struct console_input *console = (struct console_input *)obj;
404     assert( obj->ops == &console_input_ops );
405     return dup( console->select.fd );
406 }
407
408 static int console_get_info( struct object *obj, struct get_file_info_request *req )
409 {
410     req->type        = FILE_TYPE_CHAR;
411     req->attr        = 0;
412     req->access_time = 0;
413     req->write_time  = 0;
414     req->size_high   = 0;
415     req->size_low    = 0;
416     req->links       = 0;
417     req->index_high  = 0;
418     req->index_low   = 0;
419     req->serial      = 0;
420     return 1;
421 }
422
423 static void console_input_destroy( struct object *obj )
424 {
425     struct console_input *console = (struct console_input *)obj;
426     assert( obj->ops == &console_input_ops );
427     unregister_select_user( &console->select );
428     close( console->select.fd );
429     if (console->output) console->output->input = NULL;
430 }
431
432 static void screen_buffer_dump( struct object *obj, int verbose )
433 {
434     struct screen_buffer *console = (struct screen_buffer *)obj;
435     assert( obj->ops == &screen_buffer_ops );
436     fprintf( stderr, "Console screen buffer fd=%d\n", console->select.fd );
437 }
438
439 static int screen_buffer_add_queue( struct object *obj, struct wait_queue_entry *entry )
440 {
441     struct screen_buffer *console = (struct screen_buffer *)obj;
442     assert( obj->ops == &screen_buffer_ops );
443     if (!obj->head)  /* first on the queue */
444         set_select_events( &console->select, WRITE_EVENT );
445     add_queue( obj, entry );
446     return 1;
447 }
448
449 static void screen_buffer_remove_queue( struct object *obj, struct wait_queue_entry *entry )
450 {
451     struct screen_buffer *console = (struct screen_buffer *)grab_object(obj);
452     assert( obj->ops == &screen_buffer_ops );
453
454     remove_queue( obj, entry );
455     if (!obj->head)  /* last on the queue is gone */
456         set_select_events( &console->select, 0 );
457     release_object( obj );
458 }
459
460 static int screen_buffer_signaled( struct object *obj, struct thread *thread )
461 {
462     struct screen_buffer *console = (struct screen_buffer *)obj;
463     assert( obj->ops == &screen_buffer_ops );
464
465     if (check_select_events( &console->select, WRITE_EVENT ))
466     {
467         /* stop waiting on select() if we are signaled */
468         set_select_events( &console->select, 0 );
469         return 1;
470     }
471     else
472     {
473         /* restart waiting on select() if we are no longer signaled */
474         if (obj->head) set_select_events( &console->select, WRITE_EVENT );
475         return 0;
476     }
477 }
478
479 static int screen_buffer_get_write_fd( struct object *obj )
480 {
481     struct screen_buffer *console = (struct screen_buffer *)obj;
482     assert( obj->ops == &screen_buffer_ops );
483     return dup( console->select.fd );
484 }
485
486 static void screen_buffer_destroy( struct object *obj )
487 {
488     struct screen_buffer *console = (struct screen_buffer *)obj;
489     assert( obj->ops == &screen_buffer_ops );
490     unregister_select_user( &console->select );
491     close( console->select.fd );
492     if (console->input) console->input->output = NULL;
493     if (console->title) free( console->title );
494 }
495
496 /* allocate a console for the current process */
497 DECL_HANDLER(alloc_console)
498 {
499     int in = -1, out = -1;
500
501     if (!alloc_console( current->process )) goto done;
502
503     if ((in = alloc_handle( current->process, current->process->console_in,
504                             req->access, req->inherit )) != -1)
505     {
506         if ((out = alloc_handle( current->process, current->process->console_out,
507                                  req->access, req->inherit )) != -1)
508             goto done;  /* everything is fine */
509         close_handle( current->process, in );
510         in = -1;
511     }
512     free_console( current->process );
513
514  done:
515     req->handle_in  = in;
516     req->handle_out = out;
517 }
518
519 /* free the console of the current process */
520 DECL_HANDLER(free_console)
521 {
522     free_console( current->process );
523 }
524
525 /* open a handle to the process console */
526 DECL_HANDLER(open_console)
527 {
528     struct object *obj= req->output ? current->process->console_out : current->process->console_in;
529
530     if (obj) req->handle = alloc_handle( current->process, obj, req->access, req->inherit );
531     else set_error( ERROR_ACCESS_DENIED );
532 }
533
534 /* set info about a console (output only) */
535 DECL_HANDLER(set_console_info)
536 {
537     size_t len = get_req_strlen( req->title );
538     set_console_info( req->handle, req, req->title, len );
539 }
540
541 /* get info about a console (output only) */
542 DECL_HANDLER(get_console_info)
543 {
544     struct screen_buffer *console;
545     if ((console = (struct screen_buffer *)get_handle_obj( current->process, req->handle,
546                                                            GENERIC_READ, &screen_buffer_ops )))
547     {
548         req->cursor_size    = console->cursor_size;
549         req->cursor_visible = console->cursor_visible;
550         req->pid            = console->pid;
551         strcpy( req->title, console->title ? console->title : "" );
552         release_object( console );
553     }
554 }
555
556 /* set a console fd */
557 DECL_HANDLER(set_console_fd)
558 {
559     struct object *obj;
560     int fd_in, fd_out;
561
562     if (!(obj = get_handle_obj( current->process, req->file_handle,
563                                 GENERIC_READ | GENERIC_WRITE, NULL ))) return;
564     if ((fd_in = obj->ops->get_read_fd( obj )) == -1)
565     {
566         release_object( obj );
567         return;
568     }
569     fd_out = obj->ops->get_write_fd( obj );
570     release_object( obj );
571     if (fd_out != -1)
572     {
573         if (set_console_fd( req->handle, fd_in, fd_out, req->pid )) return;
574         close( fd_out );
575     }
576     close( fd_in );
577 }
578
579 /* get a console mode (input or output) */
580 DECL_HANDLER(get_console_mode)
581 {
582     req->mode = get_console_mode( req->handle );
583 }
584
585 /* set a console mode (input or output) */
586 DECL_HANDLER(set_console_mode)
587 {
588     set_console_mode( req->handle, req->mode );
589 }
590
591 /* add input records to a console input queue */
592 DECL_HANDLER(write_console_input)
593 {
594     int max = get_req_size( req + 1, sizeof(INPUT_RECORD) );
595     int count = req->count;
596
597     if (count > max) count = max;
598     req->written = write_console_input( req->handle, count, (INPUT_RECORD *)(req + 1) );
599 }
600
601 /* fetch input records from a console input queue */
602 DECL_HANDLER(read_console_input)
603 {
604     int max = get_req_size( req + 1, sizeof(INPUT_RECORD) );
605     req->read = read_console_input( req->handle, req->count, (INPUT_RECORD *)(req + 1),
606                                     max, req->flush );
607 }