home *** CD-ROM | disk | FTP | other *** search
- /*
- l_misc.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 "l_proto.h"
-
- char *error_names[] = {
- "stack_underflow",
- "oper_stack_overflow",
- "dict_stack_overflow",
- "exec_stack_overflow",
- "type_check",
- "range_check",
- "access_check",
- "division_by_zero",
- "no_matching_loop",
- "no_matching_halted",
- "no_matching_mark",
- "dict_full",
- "invalid_channel",
- "undefined",
- "syntax",
- "not_implemented",
- "implementation_limit",
- "circular_data",
-
- "__the_last_error__",
- "ok",
- "init_failure",
- "invalid_file_name",
- "cannot_read_file",
- "malloc_failed",
- "no_more_locals",
- "short_msgr",
- "invalid_crc",
- "invalid_msgr_version",
- "no_process",
- "yield_cpu",
- "abort",
- "idle"
- };
-
-
- char *
- unique_filename(char *pref)
- {
- static char fn[128];
-
- strcpy(fn, pref);
- strcat(fn, "_XXXXXX");
- mktemp(fn);
-
- return fn;
- }
-
-
- /* returns 64 random bits, and guarantees that they are not all 0 */
-
- void
- random64(byte *b)
- {
- #ifdef __MSDOS__
- sint *ip;
- do {
- /* rand returns a short, i.e. 2 bytes */
- ip = (sint*)b;
- *ip++ = ((sint)rand() << 16) ^ (sint)rand();
- *ip = ((sint)rand() << 16) ^ (sint)rand();
- } while( !*ip);
- #else
- sint r[2];
-
- do {
- /* random returns an int, i.e. 4 bytes, but only 31 bits */
- r[0] = (random() << 16) ^ random();
- r[1] = (random() << 16) ^ random();
- } while( !r[0]);
- memcpy(b, r, 8);
- #endif
- }
-
-
-
- byteptr
- load_m0(char *binpath, char *libpath, char *filename)
- {
- char *outfile, cmd[512], *content;
- int cnt;
- long len;
- FILE *f;
-
- outfile = unique_filename("stup");
- if (!access(filename, 4))
- sprintf(cmd, "%s%cm0strip -I%s <%s >%s",
- binpath, DIRDELIMIT,
- libpath, filename,
- outfile);
- else
- sprintf(cmd, "%s%cm0strip -I%s <%s%c%s >%s",
- binpath, DIRDELIMIT,
- libpath,
- libpath, DIRDELIMIT, filename,
- outfile);
-
- TRACE(1, printf("Executing \"%s\"\n", cmd))
-
- if (system(cmd))
- return 0;
- if (!(f = fopen(outfile, "r")))
- return 0;
- fseek(f, 0L, 2);
- len = ftell(f);
- fseek(f, 0L, 0);
- content = malloc(len+1);
- if (content) {
- cnt = fread(content, 1, len, f);
- content[cnt] = '\0';
- }
- fclose(f);
- unlink(outfile);
- return (byteptr)content;
- }
-
-
- retcode
- new_channel(eindex key, void *data, submitfct fct)
- {
- eindex ch;
- eptr chp;
-
- ch = new_element(0, T_CHANNEL);
- chp = gaddr(ch);
- chp->V.cha.data = data;
- chp->V.cha.submit = fct;
- dict_def(0, channeldict, key, ch);
- decref(0, key);
- decref(0, ch);
-
- return OK;
- }
-