aboutsummaryrefslogtreecommitdiff
path: root/6.6/def.c
blob: b3fef1f8c1e0fabdf7e617eea039e0d63d24da49 (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
64
65
66
67
68
69
70
71
72
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <ctype.h>
#include "word.h"
#include "hash.h"
#include "getch.h"
#include "def.h"

#define MAXWORD	100

static void error(char c, char *s);
void skipblanks(void);
int endline(char *s, int lim);

void getdef(struct nlist *hashtab[], uint32_t hashsize)
{
	char dir[MAXWORD], name[MAXWORD], def[MAXWORD];

	skipblanks();
	if (!isalpha(getword(dir, MAXWORD))) {
		error(dir[0], "getdef: expecting a directive after #");
	} else if (!strcmp(dir, "define")) {
		skipblanks();
		if (!isalpha(getword(name, MAXWORD))) {
		 	error(name[0], "getdef: non-alpha in undef");
		} else {
			if(endline(def, MAXWORD) == MAXWORD)
				error(def[0], "getdef: incomplete define");
			else
				install(hashtab, hashsize, name, def);
		}
	} else if (!strcmp(dir, "undef")) {
		skipblanks();
		if (isalpha(getword(name, MAXWORD)))
			undef(hashtab, hashsize, name);
		else
		 	error(name[0], "getdef: non-alpha in undef");
	} else {
		error(dir[0], "getdef: directive invalid or not implemented");
	}
}

static void error(char c, char *s)
{
	printf("err: %s\n", s);

	while (c != EOF && c != '\n')
		c = getch();
}

void skipblanks(void)
{
	char c;

	while (isblank(c = getch()))
		;

	ungetch(c);
}

int endline(char *s, int lim)
{
	int i;

	skipblanks();
	for (i = 0; i < lim - 1; ++i)
		if ((s[i] = getch()) == EOF || s[i] == '\n')
			break;
	s[i] = '\0';
	return lim - i;
}