#include #include #include "getinput.h" #define BUFLEN 100 /* size of character buffer */ char buffer[BUFLEN] = "\0"; int nxtbuf = 0; /* next free buffer element */ int getch(void) { return (nxtbuf == 0) ? getchar() : buffer[--nxtbuf]; } int ungetch(int c) { if (nxtbuf < BUFLEN) { buffer[nxtbuf++] = c; return 0; } return -1; } /* gets word max length lim and puts in word, skipping comments, preprocessor directives, and string literals */ int getword(char *word, int lim) { int c; char *w = word; while ((c = getch()) == ' ' || c == '\t'); if (c != EOF) *w++ = c; if (!isalpha(c)) { *w = '\0'; if (c == '#') { /* preprocessor directives */ while ((c = getch()) != '\n' && c != EOF) if (c == '/' && (c = getch()) == '*') { ungetch('*'); c = '/'; break; } } if (c == '/') { /* comments */ if ((c = getch()) == '*') { while ((c = getch()) != EOF) { if (c == '*') if ((c = getch()) == '/') return c; } } else { ungetch(c); c = '/'; } } if (c == '\"') /* string constants */ while ((c = getch()) != '\"' && c != EOF); return c; } for ( ; --lim > 0; w++) if (!isalnum(*w = getch()) && *w != '_') { ungetch(*w); break; } *w = '\0'; return word[0]; } /* getword() */ /* puts line, max length len, into s, returning length */ int getline(char *s, int len) { char c; int i; for (i = 0; i < len-1 && (c = getchar()) != EOF && c != '\n'; i++) s[i] = c; if (c == '\n') s[i++] = '\n'; s[i++] = '\0'; return i; } /* getline() */