home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / unix / volume28 / m0 / part04 / m0c.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-06-06  |  6.1 KB  |  262 lines

  1. /*
  2.     m0c.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.  
  26. #include <stdlib.h>
  27. #include <signal.h>
  28.  
  29. #ifdef __MSDOS__
  30. # include <conio.h>
  31. #endif
  32.  
  33. #include "l_proto.h"
  34. #include "c_proto.h"
  35. #include "copyrght.h"
  36.  
  37.  
  38. #define VERSION        "0.11"
  39. #define INI_FILE    "cons_ini.m0"
  40.  
  41.  
  42. byte server_queue[8];
  43. byte request_queue[8];
  44. char *bin, *lib, *inc;
  45.  
  46.  
  47. /* The \xxx..x\ key is used to (a) globally deposit the data field
  48.    (=request) and (b) to restart the queue associated with that key -
  49.    the server already knows how to send back results: */
  50. byte request[]="\\xxxxxxxxxxxxxxxx\\_ctk _1I_dat:0Q";
  51.  
  52. char line[2024];
  53.  
  54. static void    init(void);
  55. static void    usage(FILE *f);
  56.  
  57. main(argc, argv)
  58. int argc;
  59. char *argv[];
  60. {
  61.     while (argc > 1 && argv[1][0] == '-') {
  62.         argc--, argv++;
  63.         switch(argv[0][1]) {
  64.             case 'h': usage(stdout); break;
  65.             case 'I': inc = argv[0] + 2; break;
  66. #ifdef DEBUG
  67.             case 't': trace = atoi(argv[0]+2); break;
  68. #endif
  69.             case 'v':
  70.             printf("The M0 local console, version %s (compiled %s at %s)\n",
  71.                 VERSION, __DATE__, __TIME__);
  72.             exit(0);
  73.             default:
  74.             usage(stderr); break;
  75.         }
  76.     }
  77.  
  78.  
  79.     bin = getenv("M0BIN");
  80.     lib = getenv("M0LIB");
  81.  
  82.     init();
  83.  
  84.  
  85.     /* unfortunately we can not do a while loop yet:
  86.        the end of treatement of a requests is not
  87.        specially signalled by the server process
  88.        (one should reprogram cons_ini.m0 ... */
  89.     if (argc > 1) {
  90.         byteptr download, msgr;
  91.         uint msgrlen;
  92.         eindex m;
  93.  
  94.         argc--; argv++;
  95.         download = load_m0(bin?bin:DEFBINPATH, inc?inc:".", argv[0]);
  96.         if (!download) {
  97.             fprintf(stderr, "cannot load file %s\n", argv[0]);
  98.             exit(1);
  99.             /* continue; */
  100.         }
  101.         msgr = make_msgr(request_queue,
  102.             request, strlen((char*)request),
  103.             download, strlen((char*)download),
  104.             &msgrlen);
  105.         free((char*)download);
  106.         m = str_import(0, msgr, msgrlen, msgrlen);
  107.         new_proc(m, 0);
  108.         decref(0, m);
  109.         run();
  110.     }
  111.  
  112.     for (;;) {
  113.         long to;
  114.  
  115.         for (;;) {
  116.         if (runable())
  117.             run();
  118.         to = next_timeout();
  119.         if (to == 0)
  120.             timeout(time_queue);
  121.         if (!incoming_wouldblock()) {
  122.             to = 0;
  123.             break;
  124.         }
  125.         if (to != 0 && !runable()) {
  126.             remove_refcycles();
  127.             break;
  128.         }
  129.         }
  130.         if (msgr_was_empty)
  131.         break;
  132.         serve_incoming(to);
  133.     }
  134.     printf("## M0 local console terminated\n");
  135.     return 0;
  136. }
  137.  
  138.  
  139. void usage(FILE *f)
  140. {
  141.     fprintf(f, "The M0 local console, version %s (compiled %s at %s)\n",
  142.                 VERSION, __DATE__, __TIME__);
  143.     fprintf(f, "%s\n", COPYRIGHT);
  144.     fprintf(f,    "usage: m0c  [options] [<downloadfile>]\n");
  145.     fprintf(f, "options:\n"
  146.             "\t-help\n"
  147.             "\t-I<includepath>\n"
  148. #ifdef DEBUG
  149.             "\t-t<trace_level>\n"
  150. #endif
  151. /*
  152.             "\t-m <M0_ip_address>\n"
  153.             "\t-p <M0_udp_port>\n"
  154. */
  155.             "\t-version\n"
  156.         );
  157.     fprintf(f, "environment variables:\n"
  158.             "\tM0BIN  path for finding the m0strip binary\n"
  159.             "\tM0LIB  path for finding the %s startup file\n", INI_FILE);
  160.     exit(f == stdout ? 0 : 1);
  161. }
  162.  
  163.  
  164. void
  165. init(void)
  166. {
  167.     int i;
  168.     uint msgrlen;
  169.     byteptr server_ini, msgr;
  170.     eindex m;
  171.     byte h[8];
  172.  
  173.     printf("## Welcome to the M0 local console (v %s, compiled %s at %s)\n",
  174.                         VERSION, __DATE__, __TIME__);
  175.     printf("## %s\n", COPYRIGHT);
  176.  
  177.     if (   low_level_init() != OK || operator_init() != OK) {
  178.         fprintf(stderr, "low level initialization error\n");
  179.         exit(1);
  180.     }
  181.  
  182.     random64(server_queue);
  183.     random64(request_queue);
  184.     for (i = 0; i < 8; i++)
  185.         sprintf((char*)request+1+2*i, "%02x", server_queue[i]);
  186.     request[17] = '\\';
  187.     TRACE(4, printf("Request: \"%s\"\n", request))
  188.  
  189. #ifdef CHANNEL_CONSOLE
  190.     console_init(request_queue, request);
  191. #endif
  192. #ifdef CHANNEL_NIT
  193.     /* all interfaces that can be found */
  194.     nit_init(get_eth_devices(), ETHERNET_TYPE);
  195. #endif
  196. #ifdef CHANNEL_UDP
  197.     if (udp_init(get_ip_addresses(), UDP_SERVERPORT) != 0)
  198.         fprintf(stderr, "## Cannot find an empty UDP port\n");
  199.     else {
  200.         int i;
  201.         printf("## Listening on UDP port 0x%04x at IP address(es)",UDP_SERVERPORT);
  202.         for (i = 0; i < eplen(gaddr(udp_addr)); i++) {
  203.             eindex e = array_get(0, array_get(0, udp_addr, i), 0);
  204.             printf(" %s (=%u)", iptoa(gaddr(e)->V.i), gaddr(e)->V.i);
  205.         }
  206.         printf("\n");
  207.     }
  208. #endif
  209.  
  210.     if (channel_defs() != OK) {
  211.         fprintf(stderr, "channel level initialization error\n");
  212.         exit(1);
  213.     }
  214.  
  215. #ifdef SUNOS5
  216.     setuid(getuid());
  217.     seteuid(getuid());
  218. #else
  219. # ifndef __MSDOS__
  220.     setreuid(getuid(), getuid());
  221. # endif
  222. #endif
  223.  
  224.     if( init_interpreter(bin?bin:DEFBINPATH, lib?lib:DEFLIBPATH) != OK) {
  225.         fprintf(stderr, "error initializing the M0 interpreter\n");
  226.         exit(1);
  227.     }
  228.  
  229. #ifdef __MSDOS__
  230.     printf("## remaining memory: 0x%lx bytes (%d kbytes)\n",
  231.             farcoreleft(), (int)(farcoreleft()/1024));
  232. #endif
  233.  
  234.     fillin_hostid(h);
  235.     printf("## This is M0 host \\%02x%02x%02x%02x%02x%02x%02x%02x\\\n\n",
  236.             h[0], h[1], h[2], h[3], h[4], h[5], h[6], h[7]);
  237.  
  238.     server_ini = load_m0(bin?bin:DEFBINPATH, lib?lib:DEFLIBPATH, INI_FILE);
  239.     if (!server_ini) {
  240.         fprintf(stderr, "loading console initialisation file %s failed\n", INI_FILE);
  241.         exit(1);
  242.     }
  243.     TRACE(4, printf("Server_ini: \"%s\"\n", server_ini))
  244.  
  245.     strcpy(line, "{null0{}_sys'_chaG'consoleG'_keyG!}");
  246.     msgr = make_msgr(server_queue,
  247.             server_ini, strlen((char*)server_ini),
  248.             (byteptr)line, strlen(line),
  249.             &msgrlen);
  250.     free((char*)server_ini);
  251.     m = str_import(0, msgr, msgrlen, msgrlen);
  252.     new_proc(m, 0);
  253.     decref(0, m);
  254.     run();
  255.  
  256.     signal(SIGINT, terminate);
  257.     signal(SIGTERM, terminate);
  258. #ifndef __MSDOS__
  259.     signal(SIGHUP, terminate);
  260. #endif
  261. }
  262.