| 1 | Original BTL Ratfor System for 4.2 |
| 2 | #ifndef lint |
| 3 | static char sccsid[] = "@(#)rio.c 1.2 (Berkeley) %G%"; |
| 4 | #endif |
| 5 | |
| 6 | #include "r.h" |
| 7 | char ibuf[BUFSIZ]; |
| 8 | char *ip = ibuf; |
| 9 | |
| 10 | char type[] = { |
| 11 | 0, CRAP, CRAP, CRAP, CRAP, CRAP, CRAP, CRAP, |
| 12 | CRAP, '\t', '\n', CRAP, CRAP, CRAP, CRAP, CRAP, |
| 13 | CRAP, CRAP, CRAP, CRAP, CRAP, CRAP, CRAP, CRAP, |
| 14 | CRAP, CRAP, CRAP, CRAP, CRAP, CRAP, CRAP, CRAP, |
| 15 | ' ', '!', '"', '#', '$', '%', '&', '\'', |
| 16 | '(', ')', '*', '+', ',', '-', '.', '/', |
| 17 | DIG, DIG, DIG, DIG, DIG, DIG, DIG, DIG, |
| 18 | DIG, DIG, ':', ';', '<', '=', '>', '?', |
| 19 | '@', LET, LET, LET, LET, LET, LET, LET, |
| 20 | LET, LET, LET, LET, LET, LET, LET, LET, |
| 21 | LET, LET, LET, LET, LET, LET, LET, LET, |
| 22 | LET, LET, LET, '[', '\\', ']', '^', '_', |
| 23 | '`', LET, LET, LET, LET, LET, LET, LET, |
| 24 | LET, LET, LET, LET, LET, LET, LET, LET, |
| 25 | LET, LET, LET, LET, LET, LET, LET, LET, |
| 26 | LET, LET, LET, '{', '|', '}', '~', 0, |
| 27 | }; |
| 28 | |
| 29 | gtok(s) char *s; { /* get token into s */ |
| 30 | register c, t; |
| 31 | register char *p; |
| 32 | struct nlist *q; |
| 33 | |
| 34 | for(;;) { |
| 35 | p = s; |
| 36 | *p++ = c = getchr(); |
| 37 | switch(t = type[c]) { |
| 38 | case 0: |
| 39 | if (infptr > 0) { |
| 40 | fclose(infile[infptr]); |
| 41 | infptr--; |
| 42 | continue; |
| 43 | } |
| 44 | if (svargc > 1) { |
| 45 | svargc--; |
| 46 | svargv++; |
| 47 | if (infile[infptr] != stdin) |
| 48 | fclose(infile[infptr]); |
| 49 | if( (infile[infptr] = fopen(*svargv,"r")) == NULL ) |
| 50 | cant(*svargv); |
| 51 | linect[infptr] = 0; |
| 52 | curfile[infptr] = *svargv; |
| 53 | continue; |
| 54 | } |
| 55 | return(EOF); /* real eof */ |
| 56 | case ' ': |
| 57 | case '\t': |
| 58 | while ((c = getchr()) == ' ' || c == '\t') |
| 59 | ; /* skip others */ |
| 60 | if (c == COMMENT || c == '_') { |
| 61 | putbak(c); |
| 62 | continue; |
| 63 | } |
| 64 | if (c != '\n') { |
| 65 | putbak(c); |
| 66 | *p = '\0'; |
| 67 | return(' '); |
| 68 | } else { |
| 69 | *s = '\n'; |
| 70 | *(s+1) = '\0'; |
| 71 | return(*s); |
| 72 | } |
| 73 | case '_': |
| 74 | while ((c = getchr()) == ' ' || c == '\t') |
| 75 | ; |
| 76 | if (c == COMMENT) { |
| 77 | putbak(c); |
| 78 | gtok(s); /* recursive */ |
| 79 | } |
| 80 | else if (c != '\n') |
| 81 | putbak(c); |
| 82 | continue; |
| 83 | case LET: |
| 84 | case DIG: |
| 85 | while ((t=type[*p = getchr()]) == LET || t == DIG) |
| 86 | p++; |
| 87 | putbak(*p); |
| 88 | *p = '\0'; |
| 89 | if ((q = lookup(s))->name != NULL && q->ydef == 0) { /* found but not keyword */ |
| 90 | if (q->def != fcnloc) { /* not "function" */ |
| 91 | pbstr(q->def); |
| 92 | continue; |
| 93 | } |
| 94 | getfname(); /* recursive gtok */ |
| 95 | } |
| 96 | for (p=s; *p; p++) |
| 97 | if (*p>='A' && *p<='Z') |
| 98 | *p += 'a' - 'A'; |
| 99 | for (p=s; *p; p++) |
| 100 | if (*p < '0' || *p > '9') |
| 101 | return(LET); |
| 102 | return(DIG); |
| 103 | case '[': |
| 104 | *p = '\0'; |
| 105 | return('{'); |
| 106 | case ']': |
| 107 | *p = '\0'; |
| 108 | return('}'); |
| 109 | case '$': |
| 110 | case '\\': |
| 111 | if ((*p = getchr()) == '(' || *p == ')') { |
| 112 | putbak(*p=='(' ? '{' : '}'); |
| 113 | continue; |
| 114 | } |
| 115 | if (*p == '"' || *p == '\'') |
| 116 | p++; |
| 117 | else |
| 118 | putbak(*p); |
| 119 | *p = '\0'; |
| 120 | return('$'); |
| 121 | case COMMENT: |
| 122 | comment[comptr++] = 'c'; |
| 123 | while ((comment[comptr++] = getchr()) != '\n') |
| 124 | ; |
| 125 | flushcom(); |
| 126 | *s = '\n'; |
| 127 | *(s+1) = '\0'; |
| 128 | return(*s); |
| 129 | case '"': |
| 130 | case '\'': |
| 131 | for (; (*p = getchr()) != c; p++) { |
| 132 | if (*p == '\\') |
| 133 | *++p = getchr(); |
| 134 | if (*p == '\n') { |
| 135 | error("missing quote"); |
| 136 | putbak('\n'); |
| 137 | break; |
| 138 | } |
| 139 | } |
| 140 | *p++ = c; |
| 141 | *p = '\0'; |
| 142 | return(QUOTE); |
| 143 | case '%': |
| 144 | while ((*p = getchr()) != '\n') |
| 145 | p++; |
| 146 | putbak(*p); |
| 147 | *p = '\0'; |
| 148 | return('%'); |
| 149 | case '>': case '<': case '=': case '!': case '^': |
| 150 | return(peek(p, '=')); |
| 151 | case '&': |
| 152 | return(peek(p, '&')); |
| 153 | case '|': |
| 154 | return(peek(p, '|')); |
| 155 | case CRAP: |
| 156 | continue; |
| 157 | default: |
| 158 | *p = '\0'; |
| 159 | return(*s); |
| 160 | } |
| 161 | } |
| 162 | } |
| 163 | |
| 164 | gnbtok(s) char *s; { |
| 165 | register c; |
| 166 | while ((c = gtok(s)) == ' ' || c == '\t') |
| 167 | ; |
| 168 | return(c); |
| 169 | } |
| 170 | |
| 171 | getfname() { |
| 172 | while (gtok(fcname) == ' ') |
| 173 | ; |
| 174 | pbstr(fcname); |
| 175 | putbak(' '); |
| 176 | } |
| 177 | |
| 178 | peek(p, c1) char *p, c1; { |
| 179 | register c; |
| 180 | c = *(p-1); |
| 181 | if ((*p = getchr()) == c1) |
| 182 | p++; |
| 183 | else |
| 184 | putbak(*p); |
| 185 | *p = '\0'; |
| 186 | return(c); |
| 187 | } |
| 188 | |
| 189 | pbstr(str) |
| 190 | register char *str; |
| 191 | { |
| 192 | register char *p; |
| 193 | |
| 194 | p = str; |
| 195 | while (*p++); |
| 196 | --p; |
| 197 | if (ip >= &ibuf[BUFSIZ]) { |
| 198 | error("pushback overflow"); |
| 199 | exit(1); |
| 200 | } |
| 201 | while (p > str) |
| 202 | putbak(*--p); |
| 203 | } |
| 204 | |
| 205 | getchr() { |
| 206 | register c; |
| 207 | |
| 208 | if (ip > ibuf) |
| 209 | return(*--ip); |
| 210 | c = getc(infile[infptr]); |
| 211 | if (c == '\n') |
| 212 | linect[infptr]++; |
| 213 | if (c == EOF) |
| 214 | return(0); |
| 215 | return(c); |
| 216 | } |