Annotation of researchv10no/cmd/basic/bas/sys.c, revision 1.1

1.1     ! root        1: #include <stdio.h>
        !             2: #include "typedef.h"
        !             3: #include "basic.h"
        !             4: #include "tokens.h"
        !             5: #include <signal.h>
        !             6: 
        !             7: #define SEEK_EOF       2
        !             8: #define MAGIC          2249413169      /* magic number for image files */
        !             9: 
        !            10: char           *prompt = ">";          /* the prompt charcter */
        !            11: int            col = 1;                /* current output column */
        !            12: static int     symsize = MAXSYMSPACE;  /* size of symbol table */
        !            13: 
        !            14: FILE   *xopen();
        !            15: char   *sbrk(), *endstr();
        !            16: void   fpterr(), attn();
        !            17: double fabs();
        !            18: long   ftell();
        !            19: 
        !            20: 
        !            21: /*
        !            22:  * initsys --- perfor system-dependent initialization
        !            23:  */
        !            24: 
        !            25: initsys()
        !            26: {
        !            27: 
        !            28:        infile = stdin;
        !            29:        tty = isatty(fileno(stdin));
        !            30:        signal(SIGFPE, fpterr);
        !            31:        signal(SIGINT, attn);
        !            32:        lines = sbrk(MAXLINES + MAXSYMSPACE);   /* base of line buffer */
        !            33:        endlines = lines + MAXSYMSPACE;
        !            34:        symspace = endlines;    /* start of symbol table */
        !            35:        symend = sbrk(0);
        !            36:        if ((int)lines == -1)
        !            37:                error("unable to allocate initial space");
        !            38:        initio();
        !            39: }
        !            40: 
        !            41: 
        !            42: /*
        !            43:  * attn --- function invoked upon SIGINT
        !            44:  */
        !            45: 
        !            46: void attn()
        !            47: {
        !            48: 
        !            49:        signal(SIGINT, attn);
        !            50:        if (attnflg++)
        !            51:                err("attn!");
        !            52: }
        !            53: 
        !            54: 
        !            55: /*
        !            56:  * readline --- get a newline-terminated line from specified file
        !            57:  */
        !            58: 
        !            59: readline(linep, inf)
        !            60: char   *linep;
        !            61: FILE   *inf;
        !            62: {
        !            63:        register int    c;
        !            64:        register char   *p;
        !            65: 
        !            66:        for (p = linep; (c = getc(inf)) >= 0 ; ) {
        !            67:                if (c == '\r' || c == 0)
        !            68:                        ;
        !            69:                else if (c == '\n') {
        !            70:                        *p = 0;
        !            71:                        if (inf == stdin)
        !            72:                                col = 1;
        !            73:                        return(p - linep);
        !            74:                        }
        !            75:                else {
        !            76:                        if (inf == stdin)
        !            77:                                ++col;
        !            78:                        if (p >= linep + MAXLINELEN)
        !            79:                                err("line too long");
        !            80:                        *p++ = c;
        !            81:                        }
        !            82:                }
        !            83:        return(-1);
        !            84: }
        !            85: 
        !            86: 
        !            87: /*
        !            88:  * syserr --- do system-dependent error handling
        !            89:  */
        !            90: 
        !            91: syserr()
        !            92: {
        !            93: 
        !            94:        fseek(infile, 0L, SEEK_EOF);    /* in case input is a file */
        !            95:        if (infile != stdin) {          /* reset input to stdin */
        !            96:                xclose(infile);
        !            97:                infile = stdin;
        !            98:                }
        !            99: }
        !           100: 
        !           101: 
        !           102: /*
        !           103:  * error --- print an error message and exit
        !           104:  */
        !           105: 
        !           106: error(fmt, d1, d2, d3)
        !           107: {
        !           108: 
        !           109:        fprintf(stderr, fmt, d1, d2, d3);
        !           110:        exit(1);
        !           111: }
        !           112: 
        !           113: 
        !           114: /*
        !           115:  * err --- print an error message and jump back to main program
        !           116:  */
        !           117: 
        !           118: err(fmt, d1, d2, d3)
        !           119: {
        !           120: 
        !           121:        fprintf(stderr, fmt, d1, d2, d3);
        !           122:        if (curline != (Linep)NULL && curline != &immed)
        !           123:                fprintf(stderr, " at line %u", curline->l_lnr);
        !           124:        putc('\n', stderr);
        !           125:        attnflg = 0;
        !           126:        syserr();
        !           127:        leave(fmt);
        !           128:        exit(1);
        !           129: }
        !           130: 
        !           131: 
        !           132: /*
        !           133:  * fpterr --- function invoked upon SIGFPE
        !           134:  */
        !           135: 
        !           136: void fpterr()
        !           137: {
        !           138: 
        !           139:        signal(SIGFPE, fpterr);
        !           140:        err("floating point error");
        !           141: }
        !           142: 
        !           143: 
        !           144: /*
        !           145:  * fprint --- convert floating point to string
        !           146:  */
        !           147: 
        !           148: char *fprint(f)
        !           149: double f;
        !           150: {
        !           151:        register char   *s;
        !           152:        static char     fpbuff[30];
        !           153:        double          e;
        !           154:        int             decpt, sign;
        !           155: 
        !           156: #define MINF           0.00000001
        !           157: #define MAXF           10000000.0
        !           158: #define NDIGITS        5
        !           159: 
        !           160:        e = fabs(f);
        !           161:        if (e > MAXF || (e != 0.0 && e < MINF))
        !           162:                ftoa(f, fpbuff, NDIGITS, 'e');
        !           163:        else
        !           164:                ftoa(f, fpbuff, NDIGITS, 'f');
        !           165:        return(fpbuff);
        !           166: }
        !           167: 
        !           168: 
        !           169: /*
        !           170:  * ftoa --- do the real work for fprint
        !           171:  */
        !           172: 
        !           173: ftoa(f, ptr, ndig, format)
        !           174: double f;
        !           175: char   *ptr;
        !           176: {
        !           177:        register char   *s;
        !           178: 
        !           179:        if (format == 'e')
        !           180:                sprintf(ptr, "%.*e", ndig, f);
        !           181:        else {
        !           182:                sprintf(ptr, "%.*f", ndig, f);
        !           183:                s = endstr(ptr);
        !           184:                while (*--s == '0')
        !           185:                        ;
        !           186:                if (*s == '.')
        !           187:                        --s;
        !           188:                s[1] = 0;
        !           189:                }
        !           190: }
        !           191: 
        !           192: 
        !           193: /*
        !           194:  * morelines --- try to allocate more space for line storage
        !           195:  */
        !           196: 
        !           197: morelines()
        !           198: {
        !           199:        register char   *p;
        !           200: 
        !           201:        p = endlines + EXPANDLINES;
        !           202:        if ((int)sbrk(EXPANDLINES) == -1)
        !           203:                return(NO);
        !           204:        endlines = p;
        !           205:        symspace = endlines;
        !           206:        symend = symspace + symsize;
        !           207:        clrsym();
        !           208:        return(YES);
        !           209: }
        !           210: 
        !           211: 
        !           212: /*
        !           213:  * moresym --- try to allocate more space for the symbol table
        !           214:  */
        !           215: 
        !           216: moresym(p)
        !           217: char   *p;
        !           218: {
        !           219:        register int    n;
        !           220: 
        !           221:        n = p - symend;
        !           222:        n = (n + EXPANDSYM - 1) & ~(EXPANDSYM - 1);     /* make it even */
        !           223:        if (p < symend || (int)sbrk(n) == -1)
        !           224:                return(NO);
        !           225:        symend += n;
        !           226:        symsize = symend - symspace;
        !           227:        return(YES);
        !           228: }
        !           229: 
        !           230: 
        !           231: /*
        !           232:  * old --- read a program from the file with specified name
        !           233:  */
        !           234: 
        !           235: void old(file)
        !           236: char   *file;
        !           237: {
        !           238:        register FILE   *i, *oldfile;
        !           239: 
        !           240:        if (*file == 0)
        !           241:                err("file name expected");
        !           242:        if ((i = xopen(file, "r")) == (FILE *)NULL)
        !           243:                err("can't open %s", file);
        !           244:        if (file != tempfile)
        !           245:                strcpy(curfile, file);  /* save current file name */
        !           246:        init();
        !           247:        oldfile = infile;
        !           248:        infile = i;
        !           249:        if (readline(line, infile) > 0 && fastload(line, infile) == NO) {
        !           250:                compile();
        !           251:                while (readline(line, infile) > 0)
        !           252:                        compile();
        !           253:                }
        !           254:        xclose(infile);
        !           255:        infile = oldfile;
        !           256: }
        !           257: 
        !           258: 
        !           259: /*
        !           260:  * fastload --- try to load a program image from the specified file
        !           261:  */
        !           262: 
        !           263: fastload(lp, file)
        !           264: register char  *lp;
        !           265: FILE           *file;
        !           266: {
        !           267:        int     magic, bcount, nlines, check;
        !           268: 
        !           269:        if (sscanf(lp, "#%d %d %d %d\n", &magic, &bcount, &nlines, &check) != 4
        !           270:                        || magic != MAGIC
        !           271:                        || (magic ^ bcount ^ nlines ^ check))
        !           272:                return(NO);
        !           273:        while (endlines - lines < bcount && morelines())
        !           274:                ;
        !           275:        if (bcount > endlines - lines)
        !           276:                err("program image too big");
        !           277:        lastline = lines + bcount;
        !           278:        linecnt = nlines;
        !           279:        lseek(fileno(file), ftell(file), 0);
        !           280:        if (read(fileno(file), lines, bcount) != bcount)
        !           281:                err("error in loading program image");
        !           282:        return(YES);
        !           283: }
        !           284: 
        !           285: 
        !           286: /*
        !           287:  * save --- write the program onto the named file
        !           288:  */
        !           289: 
        !           290: save(file)
        !           291: char   *file;
        !           292: {
        !           293:        register FILE   *f;
        !           294:        int i, j;
        !           295: 
        !           296:        for (i = j = 0; file[i] != '\0'; i++)
        !           297:                if (file[i] != ' ')
        !           298:                        file[j++] = file[i];
        !           299:        file[j] = '\0';
        !           300: 
        !           301: /*     remove the blanks from the name         */
        !           302: 
        !           303: 
        !           304:        if (file[0] == 0 || (f = xopen(file, "w")) == (FILE *)NULL)
        !           305:                err("can't create file %s", file);
        !           306:        inptr = endstr(inptr);
        !           307:        list(MINLNR, MAXLNR, f);
        !           308:        xclose(f);
        !           309: }
        !           310: 
        !           311: 
        !           312: /*
        !           313:  * fastsave --- write a program image to the named file
        !           314:  */
        !           315: 
        !           316: fastsave(file)
        !           317: char   *file;
        !           318: {
        !           319:        register FILE   *f;
        !           320:        register int    check, bcount;
        !           321: 
        !           322:        if (file[0] == 0 || (f = xopen(file, "w")) == (FILE *)NULL)
        !           323:                err("can't create file %s", file);
        !           324:        inptr = endstr(inptr);
        !           325:        bcount = lastline - lines;
        !           326:        check = MAGIC ^ bcount ^ linecnt;
        !           327:        fprintf(f, "#%u %u %u %u\n", MAGIC, bcount, linecnt, check);
        !           328:        fflush(f);
        !           329:        if (write(fileno(f), lines, bcount) != bcount)
        !           330:                err("error in writing program image");
        !           331:        xclose(f);
        !           332: }

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.