aboutsummaryrefslogtreecommitdiff
path: root/6.1.1/word.c
blob: 538198f387ecc35fbd4a838261fd2884ae08b5c1 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include <stdio.h>
#include <ctype.h>
#include "getch.h"
#include "word.h"

char scomment();

int getword(char *word, int lim)
{
	int instr = 0;
	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 > 0) {
		do {
			*++w = getch();
		} while (isalpha(*w) && --lim > 0);
		ungetch(*w--);
	}

	*++w = '\0';
	return word[0];
}

char scomment()
{
	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;
	}
}