diff options
Diffstat (limited to '6.2/word.c')
-rw-r--r-- | 6.2/word.c | 63 |
1 files changed, 63 insertions, 0 deletions
diff --git a/6.2/word.c b/6.2/word.c new file mode 100644 index 0000000..1c5503a --- /dev/null +++ b/6.2/word.c @@ -0,0 +1,63 @@ +#include <stdio.h> +#include <ctype.h> +#include "getch.h" +#include "word.h" + +char scomment(void); + +int getword(char *word, int lim) +{ + char *w = word; + + while (isblank(*w = getch()) || *w == '"' || *w == '/') { + if (*w == '"') { + while ((*w = getch()) != '"' && *w != EOF) + ; + if (*w == EOF) + ungetch(*w); + } else if (*w == '/' && !scomment()) { + break; + } + } + + if (isalpha(*w)) { + while ((isalnum(*w) || *w == '_') && --lim > 0) + *++w = getch(); + ungetch(*w--); + } else if (*w == '#') { + --lim; + do { + *++w = getch(); + } while (isalpha(*w) && --lim > 0); + ungetch(*w--); + } + + *++w = '\0'; + return word[0]; +} + +char scomment(void) +{ + char c; + + switch (c = getch()) { + case '/' : + while ((c = getch()) != EOF && c != '\n') + ; + if (c == EOF) + ungetch(c); + + return '/'; + case '*' : + while ((c = getch()) != EOF && !(c == '*' && (c = getch()) == '/')) + if (c == '*') + ungetch(c); + if (c == EOF) + ungetch(c); + + return '*'; + default: + ungetch(c); + return 0; + } +} |