File: lex.c

package info (click to toggle)
libreswan 5.2-2.4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 81,656 kB
  • sloc: ansic: 129,988; sh: 32,018; xml: 20,646; python: 10,303; makefile: 3,022; javascript: 1,506; sed: 574; yacc: 511; perl: 264; awk: 52
file content (275 lines) | stat: -rw-r--r-- 6,921 bytes parent folder | download | duplicates (2)
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
/*
 * lexer (lexical analyzer) for control files
 *
 * Copyright (C) 1998-2001  D. Hugh Redelmeier.
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2 of the License, or (at your
 * option) any later version.  See <https://www.gnu.org/licenses/gpl2.txt>.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * for more details.
 */
#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>

#include "sysdep.h"
#include "constants.h"
#include "lswlog.h"
#include "lex.h"
#include "lswlog.h"
#include "lswalloc.h"
#include "hunk.h"		/* for char_is_space() */

/*
 * Open a file for lexical processing.
 *
 * new_flp and name must point into storage and will live
 * at least until the file is closed.
 *
 * @param new_flp file position
 * @param name filename
 * @param bool optional
 * @return bool True if successful
 */
bool lexopen(struct file_lex_position **flp, const char *name,
	     bool optional, const struct file_lex_position *oflp)
{
	FILE *f = fopen(name, "r");
	if (f == NULL) {
		if (!optional || errno != ENOENT) {
			llog_error(oflp->logger, errno, "could not open \"%s\"", name);
		} else if (DBGP(DBG_TMI)) {
			llog_errno(DEBUG_STREAM, oflp->logger, errno,
				   "lex open: %s: ", name);
		}
		return false;
	}

	ldbgf(DBG_TMI, oflp->logger, "lex open: %s", name);
	struct file_lex_position *new_flp = alloc_thing(struct file_lex_position, name);
	new_flp->depth = oflp->depth + 1;
	new_flp->filename = clone_str(name, "lexopen filename");
	new_flp->fp = f;
	new_flp->lino = 0;
	new_flp->bdry = B_none;
	new_flp->cur = new_flp->buffer;	/* nothing loaded yet */
	new_flp->under = *new_flp->cur = '\0';
	new_flp->logger = oflp->logger;
	shift(new_flp);	/* prime tok */
	*flp = new_flp;
	return true;
}

/*
 * Close filehandle
 */
void lexclose(struct file_lex_position **flp)
{
	ldbgf(DBG_TMI, (*flp)->logger, "lex close:");
	fclose((*flp)->fp);
	pfreeany((*flp)->filename);
	pfree(*flp);
	*flp = NULL;
}

/*
 * Token decoding: shift() loads the next token into tok.  If a token
 * starts at the left margin, it is considered to be the first in a
 * record.  We create a special condition, Record Boundary (analogous
 * to EOF), just before such a token.  We are unwilling to shift
 * through a record boundary: it must be overridden first.
 *
 * Returns FALSE if Record Boundary or EOF (i.e. no token); tok will
 * then be NULL.
 */

/*
 * We have an end-of-line aka start-of-record: return it, deferring
 * "real" token Caller will clear .bdry so shift() can read the new
 * line and return the next token.
 */

static void start_record(struct file_lex_position *flp, char *p)
{
	flp->bdry = B_record;
	flp->tok = NULL;
	flp->under = *p;
	flp->cur = p;
}

/*
 * shift - load next token into tok
 *
 * @return bool True if successful (i.e., there is a token)
 */

bool shift(struct file_lex_position *flp)
{
	char *p = flp->cur;
	char *start_of_record = NULL;	/* start of record for any new lines */

	passert(flp->bdry == B_none);

	*p = flp->under;
	flp->under = '\0';
	flp->quote = '\0'; /* truthy */

	for (;;) {
		switch (*p) {
		case '\0':	/* end of line */
		case '#':	/* comment at end of line */
			/*
			 * Treat comment at end of line like line end,
			 * by getting the next line.
			 */
			if (fgets(flp->buffer, sizeof(flp->buffer) - 1,
					flp->fp) == NULL) {
				flp->bdry = B_file;
				flp->tok = flp->cur = NULL;
				ldbgf(DBG_TMI, flp->logger,
				      "lex shift: file(eof)");
				return false; /* no token */
			}

			/* strip trailing whitespace, including \n */
			for (p = flp->buffer + strlen(flp->buffer);
			     p > flp->buffer && char_isspace(p[-1]);
			     p--)
				;
			*p = '\0';

			flp->lino++;
			start_of_record = p = flp->buffer;
			break;	/* try again for a token */

		case ' ':	/* whitespace */
		case '\t':
			p++;
			break;	/* try again for a token */

		case '"':	/* quoted token */
		case '\'':
		case '`':	/* or execute quotes */
			if (p == start_of_record) {
				/*
				 * Need to return start of record
				 * before quoted string (on re-entry,
				 * P hasn't advanced, but
				 * START_OF_RECORD is NULL).
				 */
				start_record(flp, p);
				ldbgf(DBG_TMI, flp->logger, "lex shift: record(new line, with quotes)");
				return false; /* no token */
			}

			/*
			 * we have a quoted token:
			 * note and advance to its end
			 */
			flp->tok = p;
			p = strchr(p + 1, *p);
			if (p == NULL) {
				llog(RC_LOG, flp->logger,
				     "unterminated string");
				p = flp->tok + strlen(flp->tok);
			} else {
				/* strip quotes from token */
				flp->quote = *flp->tok;
				flp->tok++;
				*p = '\0';
				p++;
			}

			/*
			 * Remember token delimiter and replace with
			 * '\0' (kind of pointless but consistent).
			 */
			flp->under = *p;
			*p = '\0';
			flp->cur = p;
			ldbgf(DBG_TMI, flp->logger, "lex shift: '%s'", flp->tok);
			return true; /* token */

		default:
			if (p == start_of_record) {
				/*
				 * Need to return start of record
				 * before token (On re-entry, P hasn't
				 * advanced, but START_OF_RECORD is
				 * NULL).
				 */
				start_record(flp, p);
				ldbgf(DBG_TMI, flp->logger, "lex shift: record(new line, with quotes)");
				return false; /* no token */
			}

			/*
			 * we seem to have a token: note and advance
			 * to its end
			 */
			flp->tok = p;

			if (p[0] == '0' && p[1] == 't') {
				/* 0t... token goes to end of line */
				p += strlen(p);
			} else {
				/*
				 * "ordinary" token: up to whitespace
				 * or end of line
				 */
				do {
					p++;
				} while (*p != '\0' && !char_isblank(*p));

				/*
				 * fudge to separate ':' from a
				 * preceding adjacent token
				 */
				if (p - 1 > flp->tok && p[-1] == ':')
					p--;
			}

			/*
			 * remember token delimiter and replace with
			 * '\0'
			 */
			flp->under = *p;
			*p = '\0';
			flp->cur = p;
			ldbgf(DBG_TMI, flp->logger, "lex shift: '%s'", flp->tok);
			return true; /* token */
		}
	}
}

/*
 * ensures we are at a Record (or File) boundary, optionally warning if not
 *
 * @param m string
 * @return bool True if everything is ok
 */
bool flushline(struct file_lex_position *flp, const char *message)
{
	if (flp->bdry != B_none) {
		ldbgf(DBG_TMI, flp->logger, "lex flushline: already on eof or record boundary");
		return true;
	}

	/* discard tokens until boundary reached */
	ldbgf(DBG_TMI, flp->logger, "lex flushline: need to flush tokens");
	if (message != NULL) {
		llog(RC_LOG, flp->logger, "%s", message);
	}
	do {
		ldbgf(DBG_TMI, flp->logger, "lex flushline: discarding '%s'", flp->tok);
	} while (shift(flp));
	return false;
}