home *** CD-ROM | disk | FTP | other *** search
- /*
- m0c.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 <stdlib.h>
- #include <signal.h>
-
- #ifdef __MSDOS__
- # include <conio.h>
- #endif
-
- #include "l_proto.h"
- #include "c_proto.h"
- #include "copyrght.h"
-
-
- #define VERSION "0.11"
- #define INI_FILE "cons_ini.m0"
-
-
- byte server_queue[8];
- byte request_queue[8];
- char *bin, *lib, *inc;
-
-
- /* The \xxx..x\ key is used to (a) globally deposit the data field
- (=request) and (b) to restart the queue associated with that key -
- the server already knows how to send back results: */
- byte request[]="\\xxxxxxxxxxxxxxxx\\_ctk _1I_dat:0Q";
-
- char line[2024];
-
- static void init(void);
- static void usage(FILE *f);
-
- main(argc, argv)
- int argc;
- char *argv[];
- {
- while (argc > 1 && argv[1][0] == '-') {
- argc--, argv++;
- switch(argv[0][1]) {
- case 'h': usage(stdout); break;
- case 'I': inc = argv[0] + 2; break;
- #ifdef DEBUG
- case 't': trace = atoi(argv[0]+2); break;
- #endif
- case 'v':
- printf("The M0 local console, version %s (compiled %s at %s)\n",
- VERSION, __DATE__, __TIME__);
- exit(0);
- default:
- usage(stderr); break;
- }
- }
-
-
- bin = getenv("M0BIN");
- lib = getenv("M0LIB");
-
- init();
-
-
- /* unfortunately we can not do a while loop yet:
- the end of treatement of a requests is not
- specially signalled by the server process
- (one should reprogram cons_ini.m0 ... */
- if (argc > 1) {
- byteptr download, msgr;
- uint msgrlen;
- eindex m;
-
- argc--; argv++;
- download = load_m0(bin?bin:DEFBINPATH, inc?inc:".", argv[0]);
- if (!download) {
- fprintf(stderr, "cannot load file %s\n", argv[0]);
- exit(1);
- /* continue; */
- }
- msgr = make_msgr(request_queue,
- request, strlen((char*)request),
- download, strlen((char*)download),
- &msgrlen);
- free((char*)download);
- m = str_import(0, msgr, msgrlen, msgrlen);
- new_proc(m, 0);
- decref(0, m);
- run();
- }
-
- for (;;) {
- long to;
-
- for (;;) {
- if (runable())
- run();
- to = next_timeout();
- if (to == 0)
- timeout(time_queue);
- if (!incoming_wouldblock()) {
- to = 0;
- break;
- }
- if (to != 0 && !runable()) {
- remove_refcycles();
- break;
- }
- }
- if (msgr_was_empty)
- break;
- serve_incoming(to);
- }
- printf("## M0 local console terminated\n");
- return 0;
- }
-
-
- void usage(FILE *f)
- {
- fprintf(f, "The M0 local console, version %s (compiled %s at %s)\n",
- VERSION, __DATE__, __TIME__);
- fprintf(f, "%s\n", COPYRIGHT);
- fprintf(f, "usage: m0c [options] [<downloadfile>]\n");
- fprintf(f, "options:\n"
- "\t-help\n"
- "\t-I<includepath>\n"
- #ifdef DEBUG
- "\t-t<trace_level>\n"
- #endif
- /*
- "\t-m <M0_ip_address>\n"
- "\t-p <M0_udp_port>\n"
- */
- "\t-version\n"
- );
- fprintf(f, "environment variables:\n"
- "\tM0BIN path for finding the m0strip binary\n"
- "\tM0LIB path for finding the %s startup file\n", INI_FILE);
- exit(f == stdout ? 0 : 1);
- }
-
-
- void
- init(void)
- {
- int i;
- uint msgrlen;
- byteptr server_ini, msgr;
- eindex m;
- byte h[8];
-
- printf("## Welcome to the M0 local console (v %s, compiled %s at %s)\n",
- VERSION, __DATE__, __TIME__);
- printf("## %s\n", COPYRIGHT);
-
- if ( low_level_init() != OK || operator_init() != OK) {
- fprintf(stderr, "low level initialization error\n");
- exit(1);
- }
-
- random64(server_queue);
- random64(request_queue);
- for (i = 0; i < 8; i++)
- sprintf((char*)request+1+2*i, "%02x", server_queue[i]);
- request[17] = '\\';
- TRACE(4, printf("Request: \"%s\"\n", request))
-
- #ifdef CHANNEL_CONSOLE
- console_init(request_queue, request);
- #endif
- #ifdef CHANNEL_NIT
- /* all interfaces that can be found */
- nit_init(get_eth_devices(), ETHERNET_TYPE);
- #endif
- #ifdef CHANNEL_UDP
- if (udp_init(get_ip_addresses(), UDP_SERVERPORT) != 0)
- fprintf(stderr, "## Cannot find an empty UDP port\n");
- else {
- int i;
- printf("## Listening on UDP port 0x%04x at IP address(es)",UDP_SERVERPORT);
- for (i = 0; i < eplen(gaddr(udp_addr)); i++) {
- eindex e = array_get(0, array_get(0, udp_addr, i), 0);
- printf(" %s (=%u)", iptoa(gaddr(e)->V.i), gaddr(e)->V.i);
- }
- printf("\n");
- }
- #endif
-
- if (channel_defs() != OK) {
- fprintf(stderr, "channel level initialization error\n");
- exit(1);
- }
-
- #ifdef SUNOS5
- setuid(getuid());
- seteuid(getuid());
- #else
- # ifndef __MSDOS__
- setreuid(getuid(), getuid());
- # endif
- #endif
-
- if( init_interpreter(bin?bin:DEFBINPATH, lib?lib:DEFLIBPATH) != OK) {
- fprintf(stderr, "error initializing the M0 interpreter\n");
- exit(1);
- }
-
- #ifdef __MSDOS__
- printf("## remaining memory: 0x%lx bytes (%d kbytes)\n",
- farcoreleft(), (int)(farcoreleft()/1024));
- #endif
-
- fillin_hostid(h);
- printf("## This is M0 host \\%02x%02x%02x%02x%02x%02x%02x%02x\\\n\n",
- h[0], h[1], h[2], h[3], h[4], h[5], h[6], h[7]);
-
- server_ini = load_m0(bin?bin:DEFBINPATH, lib?lib:DEFLIBPATH, INI_FILE);
- if (!server_ini) {
- fprintf(stderr, "loading console initialisation file %s failed\n", INI_FILE);
- exit(1);
- }
- TRACE(4, printf("Server_ini: \"%s\"\n", server_ini))
-
- strcpy(line, "{null0{}_sys'_chaG'consoleG'_keyG!}");
- msgr = make_msgr(server_queue,
- server_ini, strlen((char*)server_ini),
- (byteptr)line, strlen(line),
- &msgrlen);
- free((char*)server_ini);
- m = str_import(0, msgr, msgrlen, msgrlen);
- new_proc(m, 0);
- decref(0, m);
- run();
-
- signal(SIGINT, terminate);
- signal(SIGTERM, terminate);
- #ifndef __MSDOS__
- signal(SIGHUP, terminate);
- #endif
- }
-