summaryrefslogtreecommitdiffstats
path: root/usr.sbin/atm/scspd/scsp_config_lex.c
blob: d83357bf8ace0481d749f2c362e69cc14bbde40d (plain)
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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
/*
 *
 * ===================================
 * HARP  |  Host ATM Research Platform
 * ===================================
 *
 *
 * This Host ATM Research Platform ("HARP") file (the "Software") is
 * made available by Network Computing Services, Inc. ("NetworkCS")
 * "AS IS".  NetworkCS does not provide maintenance, improvements or
 * support of any kind.
 *
 * NETWORKCS MAKES NO WARRANTIES OR REPRESENTATIONS, EXPRESS OR IMPLIED,
 * INCLUDING, BUT NOT LIMITED TO, IMPLIED WARRANTIES OF MERCHANTABILITY
 * AND FITNESS FOR A PARTICULAR PURPOSE, AS TO ANY ELEMENT OF THE
 * SOFTWARE OR ANY SUPPORT PROVIDED IN CONNECTION WITH THIS SOFTWARE.
 * In no event shall NetworkCS be responsible for any damages, including
 * but not limited to consequential damages, arising from or relating to
 * any use of the Software or related support.
 *
 * Copyright 1994-1998 Network Computing Services, Inc.
 *
 * Copies of this Software may be made, however, the above copyright
 * notice must be reproduced on all copies.
 *
 *	@(#) $FreeBSD$
 *
 */

/*
 * Server Cache Synchronization Protocol (SCSP) Support
 * ----------------------------------------------------
 *
 * Parse a configuration file into tokens
 *
 */

#include <sys/types.h>
#include <sys/param.h>
#include <sys/socket.h>
#include <net/if.h>
#include <netinet/in.h>
#include <netatm/port.h> 
#include <netatm/queue.h> 
#include <netatm/atm.h>
#include <netatm/atm_if.h>
#include <netatm/atm_sap.h>
#include <netatm/atm_sys.h>
#include <netatm/atm_ioctl.h>
  
#include <ctype.h>
#include <errno.h>
#include <libatm.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <syslog.h>

#include "scsp_msg.h"
#include "scsp_if.h"
#include "scsp_var.h"
#include "scsp_config_parse.h"

#ifndef lint
__RCSID("@(#) $FreeBSD$");
#endif


/*
 * Global variables
 */
int		parse_line = 1;

/*
 * Local definitions
 */
#define	TOK_MAX_LEN	128

/*
 * Character classes
 */
#define	CHAR_INVALID	0		/* Not allowed */
#define	CHAR_ALPHA	1		/* G-W, Y, Z */
#define	CHAR_HEX_DIGIT	2		/* A-F */
#define	CHAR_X		3		/* X */
#define	CHAR_0		4		/* '0' */
#define	CHAR_DIGIT	5		/* 1-9 */
#define	CHAR_SPACE	6		/* space, tab */
#define	CHAR_DECIMAL	7		/* period */
#define	CHAR_SLASH	8		/* slash */
#define	CHAR_ASTERISK	9		/* asterisk */
#define	CHAR_HASH	10		/* pound sign */
#define	CHAR_SPECIAL	11		/* semicolon, braces */
#define	CHAR_MISC	12		/* chars allowd in file names */
#define	CHAR_EOL	13		/* new line */
#define	CHAR_EOF	14		/* EOF */
#define	CHAR_CNT	CHAR_EOF + 1

/*
 * Character class table (initialized by init_class_tbl())
 */
static char class_tbl[128];

/*
 * State table element structure
 */
struct state_entry {
	int	action;
	int	next;
};

/*
 * Scanner states
 */
#define	TS_INIT		0
#define	TS_ALPHA	1
#define	TS_INT_1	2
#define	TS_INT		3
#define	TS_HEX		4
#define	TS_SLASH_1	5
#define	TS_COMMENT	6
#define	TS_COMMENT_1	7
#define	TS_FLUSH	8
#define	TS_HEX_1	9
#define	TS_CNT		TS_HEX_1 + 1

/*
 * Token scanner state table
 */
static struct state_entry token_state_tbl[CHAR_CNT][TS_CNT] = {
/*          0     1     2     3     4     5     6     7     8     9   */
/* bad */{{2,0},{2,0},{2,0},{2,0},{2,0},{2,0},{0,6},{0,6},{0,8},{2,0}},
/* g-z */{{1,1},{1,1},{1,1},{1,1},{2,0},{1,1},{0,6},{0,6},{0,8},{2,0}},
/* a-f */{{1,1},{1,1},{1,1},{1,1},{1,9},{1,1},{0,6},{0,6},{0,8},{1,4}},
/*  x  */{{1,1},{1,1},{1,4},{1,4},{2,0},{1,1},{0,6},{0,6},{0,8},{2,0}},
/*  0  */{{1,2},{1,1},{1,3},{1,3},{1,9},{1,1},{0,6},{0,6},{0,8},{1,4}},
/* 1-9 */{{1,3},{1,1},{1,3},{1,3},{1,9},{1,1},{0,6},{0,6},{0,8},{1,4}},
/* sp  */{{0,0},{6,0},{8,0},{8,0},{7,0},{6,0},{0,6},{0,6},{0,8},{2,0}},
/*  .  */{{2,0},{1,1},{1,1},{1,1},{1,4},{1,1},{0,6},{0,6},{0,8},{2,0}},
/*  /  */{{1,5},{1,1},{1,1},{1,1},{7,0},{4,8},{0,6},{0,0},{0,8},{2,0}},
/*  *  */{{2,0},{6,0},{8,0},{8,0},{7,0},{4,6},{0,7},{0,7},{0,8},{2,0}},
/*  #  */{{0,8},{6,0},{8,0},{8,0},{7,0},{6,0},{0,6},{0,6},{0,8},{2,0}},
/* ;{} */{{3,0},{6,0},{8,0},{8,0},{7,0},{6,0},{0,6},{0,6},{0,8},{2,0}},
/* Msc */{{2,0},{1,1},{1,1},{1,1},{2,0},{1,1},{0,6},{0,6},{0,8},{2,0}},
/* EOL */{{0,0},{6,0},{8,0},{8,0},{7,0},{6,0},{0,6},{0,6},{0,0},{2,0}},
/* EOF */{{9,0},{6,0},{8,0},{8,0},{7,0},{6,0},{2,0},{2,0},{9,0},{2,0}},
};


/*
 * Reserved words
 */
static struct {
	char	*word;
	int	token;
} rsvd_word_tbl[] = {
	{ "ATMaddr",		TOK_DCS_ADDR },
	{ "ATMARP",		TOK_ATMARP },
	{ "CAReXmitInt",	TOK_DCS_CA_REXMIT_INT },
	{ "CSUSReXmitInt",	TOK_DCS_CSUS_REXMIT_INT },
	{ "CSUReXmitInt",	TOK_DCS_CSU_REXMIT_INT  },
	{ "CSUReXmitMax",	TOK_DCS_CSU_REXMIT_MAX },
	{ "DCS",		TOK_DCS },
	{ "DHCP",		TOK_DHCP },
	{ "familyID",		TOK_FAMILY },
	{ "file",		TOK_LFN },
	{ "hops",		TOK_DCS_HOP_CNT },
	{ "HelloDead",		TOK_DCS_HELLO_DF },
	{ "HelloInt",		TOK_DCS_HELLO_INT },
	{ "ID",			TOK_DCS_ID },
	{ "LNNI",		TOK_LNNI },
	{ "log",		TOK_LOG },
	{ "MARS",		TOK_MARS },
	{ "netif",		TOK_NETIF },
	{ "NHRP",		TOK_NHRP },
	{ "protocol",		TOK_PROTOCOL },
	{ "server",		TOK_SERVER },
	{ "ServerGroupID",	TOK_SRVGRP },
	{ "syslog",		TOK_SYSLOG },
	{ (char *)0,		0 },
};


/*
 * Copy a character string
 *
 * Make a copy of a character string, using strdup.  If strdup fails,
 * meaning we're out of memory, then print an error message and exit.
 *
 * Arguments:
 *	s	string to be copied
 *
 * Returns:
 *	char *	pointer to area provided by strdup
 *
 */
static char *
copy_buffer(s)
	char	*s;
{
	char	*t;

	t = strdup(s);

	if (!t) {
		fprintf(stderr, "%s: strdup failed\n", prog);
		exit(1);
	}

	return(t);
}


/*
 * Push a character back onto the input stream.
 *
 * Arguments:
 *	c	character to be pushed
 *
 * Returns:
 *	none
 *
 */
static void
push_char(c)
	char	c;
{
	if (c == '\n')
		parse_line--;

	ungetc(c, cfg_file);
}


/*
 * Initialize the character class table.
 *
 * Set each entry in the character class table to the class
 * corresponding to the character.
 *
 * Arguments:
 *	tbl	pointer to table to be initialized
 *
 * Returns:
 *	None
 */
static void
init_class_tbl(tbl)
	char *tbl;
{
	int	i;
	char	c;

	/*
	 * Set up the table for all ASCII characters
	 */
	for (i=0; isascii((char)i); i++) {
		/*
		 * Clear entry
		 */
		tbl[i] = CHAR_INVALID;

		/*
		 * Set entries depending on character type
		 */
		c = (char)i;
		if (c == 'a' || c == 'b' || c == 'c' ||
				c == 'd' || c == 'e' || c == 'f' ||
				c == 'A' || c == 'B' || c == 'C' ||
				c == 'D' || c == 'E' || c == 'F')
			tbl[i] = CHAR_HEX_DIGIT;
		else if (c == 'x' || c == 'X')
			tbl[i] = CHAR_X;
		else if (isalpha(c))
			tbl[i] = CHAR_ALPHA;
		else if (c == '0')
			tbl[i] = CHAR_0;
		else if (isdigit(c))
			tbl[i] = CHAR_DIGIT;
		else if (c == '\n')
			tbl[i] = CHAR_EOL;
		else if (c == ' ' || c == '\t')
			tbl[i] = CHAR_SPACE;
		else if (c == '#')
			tbl[i] = CHAR_HASH;
		else if (c == '*')
			tbl[i] = CHAR_ASTERISK;
		else if (c == '.')
			tbl[i] = CHAR_DECIMAL;
		else if (c == '/')
			tbl[i] = CHAR_SLASH;
		else if (c == ';' || c == '{' || c == '}')
			tbl[i] = CHAR_SPECIAL;
		else if (c == '-' || c == '_' || c == '&' || c == '@' ||
				c == '~')
			tbl[i] = CHAR_MISC;
	}
}


/*
 * Get the class of a character.
 *
 * Arguments:
 *	c	character being scanned
 *
 * Returns:
 *	int	character class
 */
static int
char_class(c)
	char	c;
{
	int	class = CHAR_INVALID;

	if (c == EOF) {
		class = CHAR_EOF;
	} else if (c < 0 || !isascii(c)) {
		class = CHAR_INVALID;
	} else {
		class = class_tbl[(int)c];
	}

	return(class);
}


/*
 * Print an error message when the scanner finds an error
 *
 * Arguments:
 *	c	character on which the error was recognized
 *	state	scanner state at error
 *
 * Returns:
 *	None
 */
static void
scan_error(c, state)
	char	c;
	int	state;
{
	/*
	 * Check for invalid character
	 */
	if (char_class(c) == CHAR_INVALID) {
		parse_error("Invalid character 0x%x encountered",
			c);
		return;
	}

	/*
	 * Check for unexpected EOF
	 */
	if (char_class(c) == CHAR_EOF) {
		parse_error("Unexpected end of file");
		return;
	}

	/*
	 * Error depends on state
	 */
	switch(state) {
	case TS_INIT:
		parse_error("Syntax error at '%c'", c);
		break;
	case TS_ALPHA:
	case TS_INT_1:
	case TS_INT:
	case TS_SLASH_1:
	case TS_COMMENT:
	case TS_COMMENT_1:
	case TS_FLUSH:
		parse_error("Syntax error");
		break;
	case TS_HEX:
	case TS_HEX_1:
		parse_error("Syntax error in hex string");
		break;
	}
}


/*
 * Assemble a token
 *
 * Read a character at a time from the input file, assembling the
 * characters into tokens as specified by the token scanner state
 * table.  Return the completed token.
 *
 * Arguments:
 *	None
 *
 * Returns:
 *	token	the type of the token found
 */
int
yylex()
{
	int		i, state;
	char		c, token_buffer[TOK_MAX_LEN];

	/*
	 * Initialize
	 */
	if (class_tbl['A'] != CHAR_HEX_DIGIT)
		init_class_tbl(class_tbl);
	state = TS_INIT;
	UM_ZERO(token_buffer, sizeof(token_buffer));
	UM_ZERO(&yylval, sizeof(yylval));

	/*
	 * Handle a character at a time until a token is built
	 */
	while(1) {
		/*
		 * Read a character from the input file.
		 */
		c = (char)getc(cfg_file);
		if (c == '\n') {
			parse_line++;
		}

#ifdef NOTDEF
		printf("token_state: state=%d, char=%c, class=%d, action=%d, next=%d\n",
				state,
				c,
				char_class(c),
				token_state_tbl[char_class][state].action,
				token_state_tbl[char_class][state].next);
#endif

		/*
		 * Perform an action based on the state table
		 */
		switch(token_state_tbl[char_class(c)][state].action) {
		case 0:
			/*
			 * Ignore the character
			 */
			break;
		case 1:
			/*
			 * Add character to buffer
			 */
			if (strlen(token_buffer) < TOK_MAX_LEN) {
				token_buffer[strlen(token_buffer)] = c;
			}
			break;
		case 2:
			/*
			 * Error--print a message and start over
			 */
			scan_error(c, state);
			break;
		case 3:
			/*
			 * Return special character
			 */
			return(c);
			break;
		case 4:
			/*
			 * Clear the token buffer
			 */
			UM_ZERO(token_buffer, sizeof(token_buffer));
			break;
		case 5:
			/*
			 * Not used
			 */
			break;
		case 6:
			/*
			 * Return character token
			 */
			push_char(c);

			/*
			 * Check for reserved words
			 */
			for (i=0; rsvd_word_tbl[i].word; i++) {
				if (strcasecmp(token_buffer,
						rsvd_word_tbl[i].word) == 0)
					break;
			}
			if (rsvd_word_tbl[i].word) {
				return(rsvd_word_tbl[i].token);
			}

			/*
			 * Word isn't reserved, return alpha string
			 */
			yylval.tv_alpha = copy_buffer(token_buffer);
			return(TOK_NAME);
			break;
		case 7:
			/*
			 * Return hex string (ATM address)
			 */
			push_char(c);
			yylval.tv_hex = copy_buffer(token_buffer);
			return(TOK_HEX);
			break;
		case 8:
			/*
			 * Return integer
			 */
			push_char(c);
			yylval.tv_int = atoi(token_buffer);
			return(TOK_INTEGER);
			break;
		case 9:
			/*
			 * Return EOF
			 */
			return(0);
			break;
		default:
			fprintf(stderr, "Invalid action indicator, state=%d, char=0x%02x\n",
					state, c);
			break;
		}

		/*
		 * Set the next state and bump to the next character
		 */
		state = token_state_tbl[char_class(c)][state].next;
	}
}
OpenPOWER on IntegriCloud