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