aboutsummaryrefslogtreecommitdiff
path: root/6.6/def.c
diff options
context:
space:
mode:
Diffstat (limited to '6.6/def.c')
-rw-r--r--6.6/def.c72
1 files changed, 72 insertions, 0 deletions
diff --git a/6.6/def.c b/6.6/def.c
new file mode 100644
index 0000000..b3fef1f
--- /dev/null
+++ b/6.6/def.c
@@ -0,0 +1,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;
+}