Implemented makepath, rand() returns correct range, fixed fputc.
[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 #include "server/process.h"
27 #include "server/thread.h"
28
29 struct screen_buffer;
30
31 struct console_input
32 {
33     struct object         obj;           /* object header */
34     int                   fd;            /* Unix file descriptor */
35     int                   mode;          /* input mode */
36     struct screen_buffer *output;        /* associated screen buffer */
37     int                   recnum;        /* number of input records */
38     INPUT_RECORD         *records;       /* input records */
39 };
40
41 struct screen_buffer
42 {
43     struct object         obj;           /* object header */
44     int                   fd;            /* Unix file descriptor */
45     int                   mode;          /* output mode */
46     struct console_input *input;         /* associated console input */
47     int                   cursor_size;   /* size of cursor (percentage filled) */
48     int                   cursor_visible;/* cursor visibility flag */
49     int                   pid;           /* xterm pid (hack) */
50     char                 *title;         /* console title */
51 };
52
53
54 static void console_input_dump( struct object *obj, int verbose );
55 static int console_input_add_queue( struct object *obj, struct wait_queue_entry *entry );
56 static void console_input_remove_queue( struct object *obj, struct wait_queue_entry *entry );
57 static int console_input_signaled( struct object *obj, struct thread *thread );
58 static int console_input_get_read_fd( struct object *obj );
59 static void console_input_destroy( struct object *obj );
60
61 static void screen_buffer_dump( struct object *obj, int verbose );
62 static int screen_buffer_add_queue( struct object *obj, struct wait_queue_entry *entry );
63 static void screen_buffer_remove_queue( struct object *obj, struct wait_queue_entry *entry );
64 static int screen_buffer_signaled( struct object *obj, struct thread *thread );
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_reply *reply );
70
71 static const struct object_ops console_input_ops =
72 {
73     console_input_dump,
74     console_input_add_queue,
75     console_input_remove_queue,
76     console_input_signaled,
77     no_satisfied,
78     console_input_get_read_fd,
79     no_write_fd,
80     no_flush,
81     console_get_info,
82     console_input_destroy
83 };
84
85 static const struct object_ops screen_buffer_ops =
86 {
87     screen_buffer_dump,
88     screen_buffer_add_queue,
89     screen_buffer_remove_queue,
90     screen_buffer_signaled,
91     no_satisfied,
92     no_read_fd,
93     screen_buffer_get_write_fd,
94     no_flush,
95     console_get_info,
96     screen_buffer_destroy
97 };
98
99 static const struct select_ops select_ops =
100 {
101     default_select_event,
102     NULL   /* we never set a timeout on a console */
103 };
104
105 int create_console( int fd, struct object *obj[2] )
106 {
107     struct console_input *console_input;
108     struct screen_buffer *screen_buffer;
109     int read_fd, write_fd;
110
111     if ((read_fd = (fd != -1) ? dup(fd) : dup(0)) == -1)
112     {
113         file_set_error();
114         return 0;
115     }
116     if ((write_fd = (fd != -1) ? dup(fd) : dup(1)) == -1)
117     {
118         file_set_error();
119         close( read_fd );
120         return 0;
121     }
122     if (!(console_input = mem_alloc( sizeof(struct console_input) )))
123     {
124         close( read_fd );
125         close( write_fd );
126         return 0;
127     }
128     if (!(screen_buffer = mem_alloc( sizeof(struct screen_buffer) )))
129     {
130         close( read_fd );
131         close( write_fd );
132         free( console_input );
133         return 0;
134     }
135     init_object( &console_input->obj, &console_input_ops, NULL );
136     init_object( &screen_buffer->obj, &screen_buffer_ops, NULL );
137     console_input->fd             = read_fd;
138     console_input->mode           = ENABLE_PROCESSED_INPUT | ENABLE_LINE_INPUT |
139                                     ENABLE_ECHO_INPUT | ENABLE_MOUSE_INPUT;
140     console_input->output         = screen_buffer;
141     console_input->recnum         = 0;
142     console_input->records        = NULL;
143     screen_buffer->fd             = write_fd;
144     screen_buffer->mode           = ENABLE_PROCESSED_OUTPUT | ENABLE_WRAP_AT_EOL_OUTPUT;
145     screen_buffer->input          = console_input;
146     screen_buffer->cursor_size    = 100;
147     screen_buffer->cursor_visible = 1;
148     screen_buffer->pid            = 0;
149     screen_buffer->title          = strdup( "Wine console" );
150     CLEAR_ERROR();
151     obj[0] = &console_input->obj;
152     obj[1] = &screen_buffer->obj;
153     return 1;
154 }
155
156 int set_console_fd( int handle, int fd, int pid )
157 {
158     struct console_input *input;
159     struct screen_buffer *output;
160     struct object *obj;
161     int fd_in, fd_out;
162
163     if (!(obj = get_handle_obj( current->process, handle, 0, NULL )))
164         return 0;
165     if (obj->ops == &console_input_ops)
166     {
167         input = (struct console_input *)obj;
168         output = input->output;
169         grab_object( output );
170     }
171     else if (obj->ops == &screen_buffer_ops)
172     {
173         output = (struct screen_buffer *)obj;
174         input = output->input;
175         grab_object( input );
176     }
177     else
178     {
179         SET_ERROR( ERROR_INVALID_HANDLE );
180         release_object( obj );
181         return 0;
182     }
183
184     if ((fd_in = dup(fd)) == -1)
185     {
186         file_set_error();
187         release_object( input );
188         release_object( output );
189         return 0;
190     }
191     if ((fd_out = dup(fd)) == -1)
192     {
193         file_set_error();
194         close( fd_in );
195         release_object( input );
196         release_object( output );
197         return 0;
198     }
199     close( input->fd );
200     close( output->fd );
201     input->fd = fd_in;
202     output->fd = fd_out;
203     output->pid = pid;
204     release_object( input );
205     release_object( output );
206     return 1;
207 }
208
209 int get_console_mode( int handle, int *mode )
210 {
211     struct object *obj;
212     int ret = 0;
213
214     if (!(obj = get_handle_obj( current->process, handle, GENERIC_READ, NULL )))
215         return 0;
216     if (obj->ops == &console_input_ops)
217     {
218         *mode = ((struct console_input *)obj)->mode;
219         ret = 1;
220     }
221     else if (obj->ops == &screen_buffer_ops)
222     {
223         *mode = ((struct screen_buffer *)obj)->mode;
224         ret = 1;
225     }
226     else SET_ERROR( ERROR_INVALID_HANDLE );
227     release_object( obj );
228     return ret;
229 }
230
231 int set_console_mode( int handle, int mode )
232 {
233     struct object *obj;
234     int ret = 0;
235
236     if (!(obj = get_handle_obj( current->process, handle, GENERIC_READ, NULL )))
237         return 0;
238     if (obj->ops == &console_input_ops)
239     {
240         ((struct console_input *)obj)->mode = mode;
241         ret = 1;
242     }
243     else if (obj->ops == &screen_buffer_ops)
244     {
245         ((struct screen_buffer *)obj)->mode = mode;
246         ret = 1;
247     }
248     else SET_ERROR( ERROR_INVALID_HANDLE );
249     release_object( obj );
250     return ret;
251 }
252
253 /* set misc console information (output handle only) */
254 int set_console_info( int handle, struct set_console_info_request *req, const char *title )
255 {
256     struct screen_buffer *console;
257     if (!(console = (struct screen_buffer *)get_handle_obj( current->process, handle,
258                                                             GENERIC_WRITE, &screen_buffer_ops )))
259         return 0;
260     if (req->mask & SET_CONSOLE_INFO_CURSOR)
261     {
262         console->cursor_size    = req->cursor_size;
263         console->cursor_visible = req->cursor_visible;
264     }
265     if (req->mask & SET_CONSOLE_INFO_TITLE)
266     {
267         if (console->title) free( console->title );
268         console->title = strdup( title );
269     }
270     release_object( console );
271     return 1;
272 }
273
274 /* get misc console information (output handle only) */
275 int get_console_info( int handle, struct get_console_info_reply *reply, const char **title )
276 {
277     struct screen_buffer *console;
278     if (!(console = (struct screen_buffer *)get_handle_obj( current->process, handle,
279                                                             GENERIC_READ, &screen_buffer_ops )))
280         return 0;
281     reply->cursor_size    = console->cursor_size;
282     reply->cursor_visible = console->cursor_visible;
283     reply->pid            = console->pid;
284     *title                = console->title;
285     release_object( console );
286     return 1;
287 }
288
289 /* add input events to a console input queue */
290 int write_console_input( int handle, int count, INPUT_RECORD *records )
291 {
292     INPUT_RECORD *new_rec;
293     struct console_input *console;
294
295     if (!(console = (struct console_input *)get_handle_obj( current->process, handle,
296                                                             GENERIC_WRITE, &console_input_ops )))
297         return -1;
298     if (!(new_rec = realloc( console->records,
299                              (console->recnum + count) * sizeof(INPUT_RECORD) )))
300     {
301         SET_ERROR( ERROR_NOT_ENOUGH_MEMORY );
302         release_object( console );
303         return -1;
304     }
305     console->records = new_rec;
306     memcpy( new_rec + console->recnum, records, count * sizeof(INPUT_RECORD) );
307     console->recnum += count;
308     release_object( console );
309     return count;
310 }
311
312 /* retrieve a pointer to the console input records */
313 int read_console_input( int handle, int count, int flush )
314 {
315     struct console_input *console;
316
317     if (!(console = (struct console_input *)get_handle_obj( current->process, handle,
318                                                             GENERIC_READ, &console_input_ops )))
319         return -1;
320     if ((count < 0) || (count > console->recnum)) count = console->recnum;
321     send_reply( current, -1, 1, console->records, count * sizeof(INPUT_RECORD) );
322     if (flush)
323     {
324         int i;
325         for (i = count; i < console->recnum; i++)
326             console->records[i-count] = console->records[i];
327         if ((console->recnum -= count) > 0)
328         {
329             INPUT_RECORD *new_rec = realloc( console->records,
330                                              console->recnum * sizeof(INPUT_RECORD) );
331             if (new_rec) console->records = new_rec;
332         }
333         else
334         {
335             free( console->records );
336             console->records = NULL;
337         }
338     }
339     release_object( console );
340     return count;
341 }
342
343 static void console_input_dump( struct object *obj, int verbose )
344 {
345     struct console_input *console = (struct console_input *)obj;
346     assert( obj->ops == &console_input_ops );
347     fprintf( stderr, "Console input fd=%d\n", console->fd );
348 }
349
350 static int console_input_add_queue( struct object *obj, struct wait_queue_entry *entry )
351 {
352     struct console_input *console = (struct console_input *)obj;
353     assert( obj->ops == &console_input_ops );
354     if (!obj->head)  /* first on the queue */
355     {
356         if (!add_select_user( console->fd, READ_EVENT, &select_ops, console ))
357         {
358             SET_ERROR( ERROR_OUTOFMEMORY );
359             return 0;
360         }
361     }
362     add_queue( obj, entry );
363     return 1;
364 }
365
366 static void console_input_remove_queue( struct object *obj, struct wait_queue_entry *entry )
367 {
368     struct console_input *console = (struct console_input *)grab_object(obj);
369     assert( obj->ops == &console_input_ops );
370
371     remove_queue( obj, entry );
372     if (!obj->head)  /* last on the queue is gone */
373         remove_select_user( console->fd );
374     release_object( obj );
375 }
376
377 static int console_input_signaled( struct object *obj, struct thread *thread )
378 {
379     fd_set fds;
380     struct timeval tv = { 0, 0 };
381     struct console_input *console = (struct console_input *)obj;
382     assert( obj->ops == &console_input_ops );
383
384     FD_ZERO( &fds );
385     FD_SET( console->fd, &fds );
386     return select( console->fd + 1, &fds, NULL, NULL, &tv ) > 0;
387 }
388
389 static int console_input_get_read_fd( struct object *obj )
390 {
391     struct console_input *console = (struct console_input *)obj;
392     assert( obj->ops == &console_input_ops );
393     return dup( console->fd );
394 }
395
396 static int console_get_info( struct object *obj, struct get_file_info_reply *reply )
397 {
398     memset( reply, 0, sizeof(*reply) );
399     reply->type = FILE_TYPE_CHAR;
400     return 1;
401 }
402
403 static void console_input_destroy( struct object *obj )
404 {
405     struct console_input *console = (struct console_input *)obj;
406     assert( obj->ops == &console_input_ops );
407     close( console->fd );
408     if (console->output) console->output->input = NULL;
409     free( console );
410 }
411
412 static void screen_buffer_dump( struct object *obj, int verbose )
413 {
414     struct screen_buffer *console = (struct screen_buffer *)obj;
415     assert( obj->ops == &screen_buffer_ops );
416     fprintf( stderr, "Console screen buffer fd=%d\n", console->fd );
417 }
418
419 static int screen_buffer_add_queue( struct object *obj, struct wait_queue_entry *entry )
420 {
421     struct screen_buffer *console = (struct screen_buffer *)obj;
422     assert( obj->ops == &screen_buffer_ops );
423     if (!obj->head)  /* first on the queue */
424     {
425         if (!add_select_user( console->fd, WRITE_EVENT, &select_ops, console ))
426         {
427             SET_ERROR( ERROR_OUTOFMEMORY );
428             return 0;
429         }
430     }
431     add_queue( obj, entry );
432     return 1;
433 }
434
435 static void screen_buffer_remove_queue( struct object *obj, struct wait_queue_entry *entry )
436 {
437     struct screen_buffer *console = (struct screen_buffer *)grab_object(obj);
438     assert( obj->ops == &screen_buffer_ops );
439
440     remove_queue( obj, entry );
441     if (!obj->head)  /* last on the queue is gone */
442         remove_select_user( console->fd );
443     release_object( obj );
444 }
445
446 static int screen_buffer_signaled( struct object *obj, struct thread *thread )
447 {
448     fd_set fds;
449     struct timeval tv = { 0, 0 };
450     struct screen_buffer *console = (struct screen_buffer *)obj;
451     assert( obj->ops == &screen_buffer_ops );
452
453     FD_ZERO( &fds );
454     FD_SET( console->fd, &fds );
455     return select( console->fd + 1, NULL, &fds, NULL, &tv ) > 0;
456 }
457
458 static int screen_buffer_get_write_fd( struct object *obj )
459 {
460     struct screen_buffer *console = (struct screen_buffer *)obj;
461     assert( obj->ops == &screen_buffer_ops );
462     return dup( console->fd );
463 }
464
465 static void screen_buffer_destroy( struct object *obj )
466 {
467     struct screen_buffer *console = (struct screen_buffer *)obj;
468     assert( obj->ops == &screen_buffer_ops );
469     close( console->fd );
470     if (console->input) console->input->output = NULL;
471     if (console->pid) kill( console->pid, SIGTERM );
472     if (console->title) free( console->title );
473     free( console );
474 }