Annotation of researchv10no/cmd/lcc/c/input.c, revision 1.1.1.1

1.1       root        1: /* C compiler: input processing */
                      2: 
                      3: #include "c.h"
                      4: 
                      5: unsigned char *cp;     /* current input character */
                      6: char *file;            /* current input file name */
                      7: char *firstfile;       /* first input file */
                      8: unsigned char *limit;  /* points to last character + 1 */
                      9: char *line;            /* current line */
                     10: int lineno;            /* line number of current line */
                     11: 
                     12: static int infd;       /* input file descriptor */
                     13: static int bsize;      /* number of chars in last read */
                     14: static unsigned char buffer[MAXLINE+1 + BUFSIZE+1];    /* input buffer */
                     15: 
                     16: dclproto(static void pragma,(void));
                     17: dclproto(static void resynch,(void));
                     18: 
                     19: /* inputInit - initialize input processing */
                     20: void inputInit(fd) {
                     21:        limit = cp = &buffer[MAXLINE + 1];
                     22:        lineno = 0;
                     23:        file = 0;                       /* omit */
                     24:        bsize = -1;
                     25:        infd = fd;
                     26:        nextline();
                     27: }
                     28: 
                     29: /* inputstring - arrange to read str as next input */
                     30: void inputstring(str) char *str; {
                     31:        limit = cp = &buffer[MAXLINE+1];
                     32:        while (*limit++ = *str++)
                     33:                ;
                     34:        *limit = '\n';
                     35:        bsize = 0;
                     36: }
                     37: 
                     38: /* fillbuf - fill the input buffer, moving tail portion if necessary */
                     39: void fillbuf() {
                     40:        if (bsize == 0)
                     41:                return;
                     42:        if (limit <= cp)
                     43:                cp = &buffer[MAXLINE + 1];
                     44:        else {          /* move tail portion */
                     45:                int n = limit - cp;
                     46:                unsigned char *s = &buffer[MAXLINE + 1] - n;
                     47:                assert(s >= buffer);
                     48:                line = (char *)s - ((char *)cp - line);
                     49:                while (cp < limit)
                     50:                        *s++ = *cp++;
                     51:                cp = &buffer[MAXLINE + 1] - n;
                     52:        }
                     53:        bsize = read(infd, (char *)&buffer[MAXLINE + 1], BUFSIZE);
                     54:        assert(bsize >= 0);
                     55:        limit = &buffer[MAXLINE + 1 + bsize];
                     56:        *limit = '\n';
                     57: }
                     58: 
                     59: /* nextline - prepare to read next line */
                     60: void nextline() {
                     61:        if (cp >= limit) {      /* refill buffer */
                     62:                fillbuf();
                     63:                if (cp >= limit) {      /* signal end of file */
                     64:                        cp = limit = &buffer[MAXLINE+1];
                     65:                        *limit = '\0';
                     66:                }
                     67:                if (lineno > 0)
                     68:                        return;
                     69:        }
                     70:        lineno++;
                     71:        for (line = (char *)cp; *cp == ' ' || *cp == '\t'; cp++)
                     72:                ;
                     73:        if (*cp == '#') {                       /* omit */
                     74:                resynch();                      /* omit */
                     75:                nextline();                     /* omit */
                     76:        }                                       /* omit */
                     77: }
                     78: 
                     79: /* pragma - handle #pragma ref id... */
                     80: static void pragma() {
                     81:        if ((t = gettok()) == ID && strcmp(token, "ref") == 0)
                     82:                for (;;) {
                     83:                        while (*cp == ' ' || *cp == '\t')
                     84:                                cp++;
                     85:                        if (*cp == '\n' || *cp == 0)
                     86:                                break;
                     87:                        if ((t = gettok()) == ID && tsym) {
                     88:                                tsym->ref++;
                     89:                                use(tsym, src);
                     90:                        }       
                     91:                }
                     92: }
                     93: 
                     94: /* resynch - set line number/file name in # n [ "file" ] and #pragma ... */
                     95: static void resynch() {
                     96:        for (cp++; *cp == ' ' || *cp == '\t'; )
                     97:                cp++;
                     98:        if (limit - cp < MAXLINE)
                     99:                fillbuf();
                    100:        if (strncmp((char *)cp, "pragma", 6) == 0) {
                    101:                cp += 6;
                    102:                pragma();
                    103:        } else if (*cp >= '0' && *cp <= '9') {
                    104:        line:   for (lineno = 0; *cp >= '0' && *cp <= '9'; )
                    105:                        lineno = 10*lineno + *cp++ - '0';
                    106:                lineno--;
                    107:                while (*cp == ' ' || *cp == '\t')
                    108:                        cp++;
                    109:                if (*cp == '"') {
                    110:                        file = (char *)++cp;
                    111:                        while (*cp && *cp != '"' && *cp != '\n')
                    112:                                cp++;
                    113:                        file = stringn(file, (char *)cp - file);
                    114:                        if (*cp == '\n')
                    115:                                warning("missing \" in preprocessor line\n");
                    116:                        if (firstfile == 0)
                    117:                                firstfile = file;
                    118:                }
                    119:        } else if (strncmp((char *)cp, "line", 4) == 0) {
                    120:                for (cp += 4; *cp == ' ' || *cp == '\t'; )
                    121:                        cp++;
                    122:                if (*cp >= '0' && *cp <= '9')
                    123:                        goto line;
                    124:                if (Aflag >= 2)
                    125:                        warning("unrecognized control line\n");
                    126:        } else if (Aflag >= 2 && *cp != '\n')
                    127:                warning("unrecognized control line\n");
                    128:        while (*cp)
                    129:                if (*cp++ == '\n')
                    130:                        if (cp == limit + 1)
                    131:                                nextline();
                    132:                        else
                    133:                                break;
                    134: }

unix.superglobalmegacorp.com

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