-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathparse.c
167 lines (149 loc) · 2.74 KB
/
parse.c
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
#include <time.h>
#include "hardv.h"
static char *path;
static FILE *fp;
static size_t lineno;
static void
invalid(char *msg)
{
err("%s, line %zu: %s", path, lineno, msg);
}
static char *
parsehead(void)
{
char buf[MAXN], *s;
int ch;
size_t n;
n = 0;
while (isspace((unsigned char)(ch=getc(fp)))) {
if (n+1 == MAXN) err("head too large");
if ((buf[n++]=ch) == '\n') lineno++;
}
ungetc(ch, fp);
buf[n] = '\0';
if (n>0 && buf[n-1]!='\n' && ch!=EOF) invalid("unexpected space");
if (n == 0) return NULL;
if (!(s=strdup(buf))) syserr();
return s;
}
static char *
parsekey(void)
{
char buf[MAXN], *s;
int ch;
size_t n;
n = 0;
while ((ch=getc(fp)) != EOF) {
if (!strchr(KCHAR, ch)) break;
if (n+1 == MAXN) err("key too large");
if ((buf[n++]=ch) == '\n') lineno++;
}
ungetc(ch, fp);
buf[n] = '\0';
if (n == 0) return NULL;
if (!(s=strdup(buf))) syserr();
return s;
}
static char *
parseval(void)
{
char buf[MAXN], ahead, *s;
int ch;
size_t n;
ch = ungetc(getc(fp), fp);
if (ch == EOF) return NULL;
if (ch != '\t' && ch != '\n') invalid("expect tab or newline");
n = 0;
while ((ch=getc(fp)) != EOF) {
if (n+1 == MAXN) err("value too large");
if ((buf[n++]=ch) == '\n') lineno++;
ahead = ungetc(getc(fp), fp);
if (ch == '\n' && ahead != '\t' && ahead != '\n')
break;
}
buf[n] = '\0';
if (n == 0) return NULL;
if (!(s=strdup(buf))) syserr();
return s;
}
static struct field *
parsefield(void)
{
struct field *p;
char *key, *val;
if (!(key=parsekey())) return NULL;
val = parseval();
if ((p=malloc(sizeof *p)) == NULL) syserr();
p->key = key;
p->val = val;
p->next = NULL;
return p;
}
static char *
parsetail(void)
{
size_t n;
int ch;
char buf[MAXN], *s;
ch = ungetc(getc(fp), fp);
if (ch == EOF) return NULL;
if (ch != '%') invalid("expect '%'");
n = 0;
while ((ch=getc(fp)) != EOF) {
if (n+1 == MAXN) err("tail too large");
if ((buf[n++]=ch) == '\n') {
lineno++;
break;
}
}
buf[n] = '\0';
if (n == 0) return NULL;
if (!(s=strdup(buf))) syserr();
return s;
}
static struct field *
reverse(struct field *f)
{
struct field *r, *s;
r = NULL;
while (f) {
s = f->next;
f->next = r;
r = f;
f = s;
}
return r;
}
void
parseinit(char *file)
{
path = file;
lineno = 1;
fp = fopen(path, "r");
if (!fp) syserr();
}
void
parsedone(void)
{
fclose(fp);
}
struct card *
parsecard(struct card *dst)
{
struct field *f;
if (!(dst->file=strdup(path))) syserr();
dst->head = parsehead();
dst->field = NULL;
while ((f=parsefield()) != NULL) {
f->next = dst->field;
dst->field = f;
}
dst->field = reverse(dst->field);
dst->tail = parsetail();
if (!dst->head && !dst->field && !dst->tail) return NULL;
return dst;
}