-
Notifications
You must be signed in to change notification settings - Fork 1
/
rcslex.c
342 lines (304 loc) · 7.51 KB
/
rcslex.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
/*-
* Copyright (c) 2012, Maxime Henrion <[email protected]>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <assert.h>
#include <ctype.h>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "rcslex.h"
struct rcslex {
int fd;
char *data;
size_t datalen;
int eof;
char *last;
size_t offset;
struct rcstok tok;
};
/* Map the RCS file in memory and initialize state variables. */
struct rcslex *
rcslex_new(const char *path)
{
struct stat sb;
struct rcslex *lex;
void *addr;
int error;
lex = malloc(sizeof(struct rcslex));
if (lex == NULL)
return (NULL);
memset(lex, 0, sizeof(struct rcslex));
lex->fd = open(path, O_RDONLY);
if (lex->fd == -1) {
rcslex_free(lex);
return (NULL);
}
error = fstat(lex->fd, &sb);
if (error) {
rcslex_free(lex);
return (NULL);
}
lex->datalen = sb.st_size;
addr = mmap(NULL, lex->datalen, PROT_READ, 0, lex->fd, 0);
if (addr == MAP_FAILED) {
rcslex_free(lex);
return (NULL);
}
lex->data = addr;
lex->last = lex->data + lex->datalen - 1;
lex->eof = 0;
lex->offset = 0;
memset(&lex->tok, 0, sizeof(lex->tok));
return (lex);
}
/*
* Lex and return the next token. There aren't many things the lexer can
* distinguish from because most of the token types depend on the structure
* of the RCS file. So we mostly eat whitespace and match semicolons, RCS
* strings or regular tokens.
*/
struct rcstok *
rcslex_get(struct rcslex *lex)
{
char *cp, *sep;
struct rcstok *tok;
if (lex->eof)
return (NULL);
/* Eat whitespace. */
cp = lex->data + lex->offset;
while (cp <= lex->last && isspace(*cp))
cp++;
/* Return 0 if we hit EOF. */
if (cp >= lex->last) {
lex->eof = 1;
return (NULL);
}
tok = &lex->tok;
tok->value = cp;
if (*cp == '@') {
/* This is a possibly binary RCS string, find its end. */
cp++;
tok->value = cp;
tok->type = -1;
while (tok->type == -1 && cp <= lex->last) {
sep = memchr(cp, '@', lex->last - cp);
if (sep == NULL)
return (NULL);
if (sep == lex->last || sep[1] != '@') {
tok->type = RCSLEX_STRING;
tok->len = sep - tok->value;
cp = sep + 1;
} else {
cp = sep + 2;
}
}
if (tok->type == -1)
return (NULL);
} else if (*cp == ';') {
tok->type = RCSLEX_SCOLON;
tok->len = 1;
cp++;
} else if (*cp == ':') {
tok->type = RCSLEX_COLON;
tok->len = 1;
cp++;
} else {
/* This is a regular symbol (sym, num, id or a keyword). */
while (cp <= lex->last && *cp != '@' && *cp != ';' &&
*cp != ':' && !isspace(*cp))
cp++;
tok->type = RCSLEX_ID;
tok->len = cp - tok->value;
}
lex->offset = cp - lex->data;
return (tok);
}
/* Get and check that the returned token matches what we want. */
struct rcstok *
rcslex_want(struct rcslex *lex, int type, size_t len, const char *value)
{
struct rcstok *tok;
tok = rcslex_get(lex);
if (tok == NULL)
return (NULL);
if (tok->type != type)
return (NULL);
if (len > 0 && tok->len != len)
return (NULL);
if (value != NULL && memcmp(value, tok->value, tok->len) != 0)
return (NULL);
return (tok);
}
char *
rcslex_get_num(struct rcslex *lex)
{
struct rcstok *tok;
tok = rcslex_want(lex, RCSLEX_ID, 0, NULL);
if (tok == NULL)
return (NULL);
if (!rcstok_validate_num(tok))
return (NULL);
return (rcslex_dup(lex, NULL));
}
char *
rcslex_get_id(struct rcslex *lex)
{
struct rcstok *tok;
tok = rcslex_want(lex, RCSLEX_ID, 0, NULL);
if (tok == NULL)
return (NULL);
if (!rcstok_validate_id(tok))
return (NULL);
return (rcslex_dup(lex, NULL));
}
char *
rcslex_get_sym(struct rcslex *lex)
{
const char *special = "$,.:;@";
struct rcstok *tok;
size_t i;
int c, idchar;
tok = rcslex_want(lex, RCSLEX_ID, 0, NULL);
if (tok == NULL)
return (NULL);
/* Validate that this is a proper "sym" token. */
idchar = 0;
for (i = 0; i < tok->len; i++) {
c = tok->value[i];
if (strchr(special, c) != NULL || !isprint(c))
return (NULL);
if (!idchar && !isdigit(c))
idchar = 1;
}
/* There needs to be at least one "idchar" character. */
if (!idchar)
return (NULL);
return (rcslex_dup(lex, NULL));
}
char *
rcslex_get_string(struct rcslex *lex, size_t *outlen)
{
struct rcstok *tok;
tok = rcslex_want(lex, RCSLEX_STRING, 0, NULL);
if (tok == NULL)
return (NULL);
return (rcslex_dup(lex, outlen));
}
/* Extract/convert the raw token in newly allocated memory. */
char *
rcslex_dup(struct rcslex *lex, size_t *outlen)
{
struct rcstok *tok;
char *value;
size_t len;
/* We do not actually convert the doubled '@' characters in the
RCS strings because they are expected this way later on. */
tok = &lex->tok;
/* We always terminate tokens with a NUL character, even RCS strings
that can legitimately be binary strings. This is because it is
convenient to treat some tokens (such as the parameter of "expand")
as C-style strings. */
len = tok->len + 1;
value = malloc(len);
if (value == NULL)
return (NULL);
memcpy(value, tok->value, tok->len);
value[tok->len] = '\0';
if (outlen != NULL) {
if (tok->type == RCSLEX_STRING)
len--;
*outlen = len;
}
return (value);
}
void
rcslex_unget(struct rcslex *lex)
{
struct rcstok *tok;
tok = &lex->tok;
lex->offset -= tok->len;
}
/* Did we hit EOF? */
int
rcslex_eof(struct rcslex *lex)
{
return (lex->eof);
}
/* Release all resources associated with the lexer. */
void
rcslex_free(struct rcslex *lex)
{
int error;
if (lex->data != NULL) {
error = munmap(lex->data, lex->datalen);
assert(!error);
}
if (lex->fd != -1) {
error = close(lex->fd);
assert(!error);
}
free(lex);
}
/* Validate that we have indeed an "id" token. */
int
rcstok_validate_id(struct rcstok *tok)
{
const char *special = "$,:;@";
int idchar, c;
size_t i;
if (tok->type != RCSLEX_ID)
return (0);
idchar = 0;
for (i = 0; i < tok->len; i++) {
c = tok->value[i];
if (strchr(special, c) != NULL || !isprint(c))
return (0);
if (!idchar && !isdigit(c) && c != '.')
idchar = 1;
}
/* There needs to be at least one "idchar" character. */
if (!idchar)
return (0);
return (1);
}
/* Validate that we have indeed a "num" token. */
int
rcstok_validate_num(struct rcstok *tok)
{
size_t i;
int c;
if (tok->type != RCSLEX_ID)
return (0);
for (i = 0; i < tok->len; i++) {
c = tok->value[i];
if (!isdigit(c) && c != '.')
return (0);
}
return (1);
}