home *** CD-ROM | disk | FTP | other *** search
- /*
- c_con.c
- */
- /* Copyright (c) 1994 Christian F. Tschudin. All rights reserved.
-
- Distributed under the terms of the GNU General Public License
- version 2 of june 1991 as published by the Free Software
- Foundation, Inc.
-
- This file is part of M0.
-
- M0 is distributed in the hope that it will be useful, but WITHOUT ANY
- WARRANTY. No author or distributor accepts responsibility to anyone for
- the consequences of using it or for whether it serves any particular
- purpose or works at all, unless he says so in writing. Refer to the GNU
- General Public License for full details.
-
- Everyone is granted permission to copy, modify and redistribute M0, but
- only under the conditions described in the GNU General Public License.
- A copy of this license is supposed to have been given to you along with
- M0 so you can know your rights and responsibilities. It should be in a
- file named LICENSE. Among other things, the copyright notice and this
- notice must be preserved on all copies. */
-
- #include "c_proto.h"
-
-
- #ifdef CHANNEL_CONSOLE
-
- int msgr_was_empty = 0;
- eindex console_addr;
- eindex console_name;
- static byteptr k_queue, k_vect;
-
- static void
- line2msgr(int fd, eindex *m, eindex *o)
- {
- #define LINESIZE 256
- byte line[LINESIZE];
- int cnt;
-
- cnt = read(0, line, LINESIZE);
- if (cnt > 0) {
- uint msgrlen;
- byteptr s = make_msgr(k_queue,
- k_vect, strlen((char*)k_vect),
- cnt>1?(byteptr)line:0, cnt-1,
- &msgrlen);
-
- TRACE(3, printf("console line2msgr, %d bytes\n", cnt))
-
- *m = str_import(0, s, msgrlen, msgrlen);
- *o = null_val;
- increfp(gaddr(null_val));
- } else
- *m = 0;
- }
-
-
- int
- console_init(byteptr keyboard_queue, byteptr keyboard_vect)
- {
- k_queue = keyboard_queue;
- k_vect = keyboard_vect;
-
- console_name = name_add((byteptr)"console", 7, A_EXECUTABLE);
- add_incoming(0, line2msgr, console_name, 0); /* stdin */
-
- console_addr = new_array(0, 1);
-
- return 0;
- }
-
-
- static void
- console_submit(mproc p, void *data, eindex m)
- {
- eindex key = (eindex) data;
- uint len = elen(p,m);
-
- TRACE(3, printf("console submit %d bytes\n", len))
-
- if (len) {
- byteptr s = malloc(len);
- str_export(p, s, m, 0, len);
- write(1, s, len);
- free((char*)s);
- } else
- msgr_was_empty = 1;
- dict_undef(0, channeldict, key);
- return;
- }
-
-
- eindex
- add_console_channel()
- {
- eindex key;
- byte bits[8];
-
- random64(bits);
- key = key_add(bits);
- new_channel(key, (void*)key, console_submit);
-
- return key;
- }
- #endif
-