|
|
1.1 ! root 1: /* util.c: utility routines */ ! 2: ! 3: #include "fdevelop.h" ! 4: #include <ctype.h> ! 5: ! 6: error(f, s) ! 7: int f; ! 8: char *s; ! 9: { ! 10: fprintf(stderr, "%s: %s\n", cmdname, s); ! 11: if (lineno) ! 12: fprintf(stderr, " source line number %d\n", lineno); ! 13: if (f) ! 14: exit(2); ! 15: } ! 16: ! 17: int strisnum(p) /* 1 if string p represents a float */ ! 18: register char *p; ! 19: { ! 20: register int digits = 0; ! 21: register int n; ! 22: /* REG EXPR: */ ! 23: if (*p == '-') /* -? */ ! 24: p++; ! 25: while (isdigit(*p)) { ! 26: digits++; /* [0-9]* */ ! 27: p++; ! 28: } ! 29: if (*p == '.') { /* (.[0-9]*)? */ ! 30: p++; ! 31: while (isdigit(*p)) { ! 32: digits++; ! 33: p++; ! 34: } ! 35: } ! 36: if (digits == 0) /* >0 digits */ ! 37: return 0; ! 38: if (tolower(*p) == 'e') { /* ([eE] */ ! 39: *p++; ! 40: if (*p == '+' || *p == '-') /* [+-]? */ ! 41: p++; ! 42: digits = 1; ! 43: if (!isdigit(*p)) /* [0-9] */ ! 44: return 0; ! 45: n = *p++ - '0'; ! 46: while (isdigit(*p)) { /* [0-9]+ */ ! 47: digits++; ! 48: n = 10*n + *p++ - '0'; ! 49: if (n > 30) ! 50: return 0; ! 51: } ! 52: if (digits == 0) ! 53: return 0; /* )? */ ! 54: } ! 55: return (*p == '\0'); ! 56: } ! 57: ! 58: int moreinput(fp) ! 59: FILE *fp; ! 60: { ! 61: int c; ! 62: ! 63: if (lexsaweof) ! 64: return 0; ! 65: c = getc(fp); ! 66: if (c == EOF) ! 67: return 0; ! 68: ungetc(c, fp); ! 69: return 1; ! 70: } ! 71: ! 72: #define CHECKEOF if (c == EOF) {\ ! 73: lexsaweof = 1;\ ! 74: error(WARN, "file does not end with newline");\ ! 75: return;\ ! 76: } ! 77: ! 78: ! 79: lex(fp) /* put next string of non-white into buf */ ! 80: register FILE *fp; ! 81: { ! 82: register int c; ! 83: register char *p, *danger; ! 84: ! 85: danger = &buf[MAXSTR-1]; ! 86: while ((c = getc(fp)) == ' ' || c == '\t') ! 87: ; ! 88: CHECKEOF ! 89: if (c == '\n') { ! 90: ungetc(c, fp); ! 91: buf[0] = '\0'; ! 92: return; ! 93: } ! 94: p = buf; ! 95: *p++ = c; ! 96: while ((c = getc(fp)) != EOF && c != ' ' && c != '\t' && c != '\n') { ! 97: if (p < danger) ! 98: *p++ = c; ! 99: else if (p == danger) ! 100: error(WARN, "string too long -- truncated"); ! 101: } ! 102: *p = '\0'; ! 103: CHECKEOF ! 104: if (c == '\n') ! 105: ungetc(c, fp); ! 106: /* fprintf(stderr, "lex returning: %s\n", buf); */ ! 107: } ! 108: ! 109: ! 110: lexstr(fp) /* like lex, but go til newline and handle quotes */ ! 111: FILE *fp; ! 112: { ! 113: int c; ! 114: int quoted = 0; ! 115: char *p, *danger; ! 116: ! 117: while ((c = getc(fp)) == ' ' || c == '\t') ! 118: ; ! 119: CHECKEOF ! 120: if (c == '\"') { ! 121: quoted = 1; ! 122: c = getc(fp); ! 123: CHECKEOF ! 124: } ! 125: if (c == '\n') { ! 126: buf[0] = '\0'; ! 127: return; ! 128: } ! 129: p = buf; ! 130: *p++ = c; ! 131: danger = &buf[MAXBUF-1]; ! 132: while ((c = getc(fp)) != EOF && c != '\n') { ! 133: if (p < danger) ! 134: *p++ = c; ! 135: else if (p == danger) ! 136: error(WARN, "text string too long -- truncated"); ! 137: } ! 138: CHECKEOF ! 139: *p = '\0'; ! 140: if (quoted && *(--p) == '\"') ! 141: *p = '\0'; ! 142: } ! 143: ! 144: lexrest(fp) /* get rest of line into buf */ ! 145: /* no error checking; error free from Pass 1 */ ! 146: FILE *fp; ! 147: { ! 148: int c; ! 149: char *p; ! 150: ! 151: p = buf; ! 152: while ((c = getc(fp)) != EOF && c != '\n') { ! 153: *p++ = c; ! 154: } ! 155: *p = '\0'; ! 156: } ! 157: ! 158: gobble(fp) /* chew space til EOF; complain if nonwhite */ ! 159: FILE *fp; ! 160: { ! 161: int c; ! 162: ! 163: while ((c = getc(fp)) == ' ' || c == '\t') ! 164: ; ! 165: CHECKEOF ! 166: if (c != '\n') { ! 167: error(WARN, "garbage at end of line"); ! 168: while ((c = getc(fp)) != EOF && c != '\n') ! 169: ; ! 170: CHECKEOF ! 171: } ! 172: } ! 173: ! 174: gobble2(fp) /* chew space til EOF, no complaints */ ! 175: FILE *fp; ! 176: { ! 177: int c; ! 178: ! 179: while ((c = getc(fp)) != EOF && c != '\n') ! 180: ; ! 181: CHECKEOF ! 182: } ! 183: ! 184: #define WANTPROF 0 ! 185: #if WANTPROF ! 186: int hmallocinit; ! 187: FILE *hmallocfp; ! 188: #endif ! 189: ! 190: char *emalloc(n) /* check return from malloc */ ! 191: int n; ! 192: { ! 193: char *p; ! 194: extern char *malloc(); ! 195: ! 196: #if WANTPROF ! 197: if (hmallocinit == 0) { ! 198: hmallocinit = 1; ! 199: if ((hmallocfp = fopen("/tmp/malloc.hist", "w")) == NULL) ! 200: error(FATAL, "malloc history bug: can't open file"); ! 201: } ! 202: #endif ! 203: p = malloc((unsigned) n); ! 204: if (p == NULL) ! 205: error(FATAL, "out of memory"); ! 206: #if WANTPROF ! 207: fprintf(hmallocfp, "m\t%d\t%d\n", (int) p, n); ! 208: #endif ! 209: return p; ! 210: } ! 211: ! 212: efree(p) /* personal version of free to match emalloc */ ! 213: char *p; ! 214: { ! 215: #if WANTPROF ! 216: if (hmallocinit == 0) ! 217: error(FATAL, "malloc history bug: first free before malloc"); ! 218: fprintf(hmallocfp, "f\t%d\n", (int) p); ! 219: #endif ! 220: free(p); ! 221: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.