home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / unix / volume28 / m0 / part01 / c_con.c next >
Encoding:
C/C++ Source or Header  |  1994-06-06  |  2.4 KB  |  108 lines

  1. /*
  2.     c_con.c
  3. */
  4. /*  Copyright (c) 1994 Christian F. Tschudin. All rights reserved.
  5.  
  6.     Distributed under the terms of the GNU General Public License
  7.     version 2 of june 1991 as published by the Free Software
  8.     Foundation, Inc.
  9.  
  10.              This file is part of M0.
  11.  
  12. M0 is distributed in the hope that it will be useful, but WITHOUT ANY
  13. WARRANTY.  No author or distributor accepts responsibility to anyone for
  14. the consequences of using it or for whether it serves any particular
  15. purpose or works at all, unless he says so in writing.  Refer to the GNU
  16. General Public License for full details. 
  17.  
  18. Everyone is granted permission to copy, modify and redistribute M0, but
  19. only under the conditions described in the GNU General Public License. 
  20. A copy of this license is supposed to have been given to you along with
  21. M0 so you can know your rights and responsibilities.  It should be in a
  22. file named LICENSE.  Among other things, the copyright notice and this
  23. notice must be preserved on all copies.  */
  24.  
  25. #include "c_proto.h"
  26.  
  27.  
  28. #ifdef CHANNEL_CONSOLE
  29.  
  30. int msgr_was_empty = 0;
  31. eindex console_addr;
  32. eindex console_name;
  33. static byteptr k_queue, k_vect;
  34.  
  35. static void
  36. line2msgr(int fd, eindex *m, eindex *o)
  37. {
  38. #define LINESIZE    256
  39.     byte line[LINESIZE];
  40.     int cnt;
  41.  
  42.     cnt = read(0, line, LINESIZE);
  43.     if (cnt > 0) {
  44.         uint msgrlen;
  45.         byteptr s =  make_msgr(k_queue,
  46.                     k_vect, strlen((char*)k_vect),
  47.                     cnt>1?(byteptr)line:0, cnt-1,
  48.                     &msgrlen);
  49.  
  50.         TRACE(3, printf("console line2msgr, %d bytes\n", cnt))
  51.  
  52.         *m = str_import(0, s, msgrlen, msgrlen);
  53.         *o = null_val;
  54.         increfp(gaddr(null_val));
  55.     } else
  56.         *m = 0;
  57. }
  58.  
  59.  
  60. int
  61. console_init(byteptr keyboard_queue, byteptr keyboard_vect)
  62. {
  63.     k_queue = keyboard_queue;
  64.     k_vect = keyboard_vect;
  65.  
  66.     console_name = name_add((byteptr)"console", 7, A_EXECUTABLE);
  67.     add_incoming(0, line2msgr, console_name, 0); /* stdin */
  68.  
  69.     console_addr = new_array(0, 1);
  70.  
  71.     return 0;
  72. }
  73.  
  74.  
  75. static void
  76. console_submit(mproc p, void *data, eindex m)
  77. {
  78.     eindex key = (eindex) data;
  79.     uint len = elen(p,m);
  80.  
  81.     TRACE(3, printf("console submit %d bytes\n", len))
  82.  
  83.     if (len) {
  84.         byteptr s = malloc(len);
  85.         str_export(p, s, m, 0, len);
  86.         write(1, s, len);
  87.         free((char*)s);
  88.     } else
  89.         msgr_was_empty = 1;
  90.     dict_undef(0, channeldict, key);
  91.     return;
  92. }
  93.  
  94.  
  95. eindex
  96. add_console_channel()
  97. {
  98.     eindex key;
  99.     byte bits[8];
  100.  
  101.     random64(bits);
  102.     key = key_add(bits);
  103.     new_channel(key, (void*)key, console_submit);
  104.  
  105.     return key;
  106. }
  107. #endif
  108.