3 * ===================================
4 * HARP | Host ATM Research Platform
5 * ===================================
8 * This Host ATM Research Platform ("HARP") file (the "Software") is
9 * made available by Network Computing Services, Inc. ("NetworkCS")
10 * "AS IS". NetworkCS does not provide maintenance, improvements or
11 * support of any kind.
13 * NETWORKCS MAKES NO WARRANTIES OR REPRESENTATIONS, EXPRESS OR IMPLIED,
14 * INCLUDING, BUT NOT LIMITED TO, IMPLIED WARRANTIES OF MERCHANTABILITY
15 * AND FITNESS FOR A PARTICULAR PURPOSE, AS TO ANY ELEMENT OF THE
16 * SOFTWARE OR ANY SUPPORT PROVIDED IN CONNECTION WITH THIS SOFTWARE.
17 * In no event shall NetworkCS be responsible for any damages, including
18 * but not limited to consequential damages, arising from or relating to
19 * any use of the Software or related support.
21 * Copyright 1994-1998 Network Computing Services, Inc.
23 * Copies of this Software may be made, however, the above copyright
24 * notice must be reproduced on all copies.
26 * @(#) $FreeBSD: src/usr.sbin/atm/scspd/scsp_config_lex.c,v 1.3 1999/08/28 01:15:32 peter Exp $
31 * Server Cache Synchronization Protocol (SCSP) Support
32 * ----------------------------------------------------
34 * Parse a configuration file into tokens
38 #include <sys/types.h>
39 #include <sys/param.h>
40 #include <sys/socket.h>
42 #include <netinet/in.h>
43 #include <netatm/port.h>
44 #include <netatm/queue.h>
45 #include <netatm/atm.h>
46 #include <netatm/atm_if.h>
47 #include <netatm/atm_sap.h>
48 #include <netatm/atm_sys.h>
49 #include <netatm/atm_ioctl.h>
62 #include "scsp_config_parse.h"
65 __RCSID("@(#) $FreeBSD: src/usr.sbin/atm/scspd/scsp_config_lex.c,v 1.3 1999/08/28 01:15:32 peter Exp $");
77 #define TOK_MAX_LEN 128
82 #define CHAR_INVALID 0 /* Not allowed */
83 #define CHAR_ALPHA 1 /* G-W, Y, Z */
84 #define CHAR_HEX_DIGIT 2 /* A-F */
85 #define CHAR_X 3 /* X */
86 #define CHAR_0 4 /* '0' */
87 #define CHAR_DIGIT 5 /* 1-9 */
88 #define CHAR_SPACE 6 /* space, tab */
89 #define CHAR_DECIMAL 7 /* period */
90 #define CHAR_SLASH 8 /* slash */
91 #define CHAR_ASTERISK 9 /* asterisk */
92 #define CHAR_HASH 10 /* pound sign */
93 #define CHAR_SPECIAL 11 /* semicolon, braces */
94 #define CHAR_MISC 12 /* chars allowd in file names */
95 #define CHAR_EOL 13 /* new line */
96 #define CHAR_EOF 14 /* EOF */
97 #define CHAR_CNT CHAR_EOF + 1
100 * Character class table (initialized by init_class_tbl())
102 static char class_tbl[128];
105 * State table element structure
122 #define TS_COMMENT_1 7
125 #define TS_CNT TS_HEX_1 + 1
128 * Token scanner state table
130 static struct state_entry token_state_tbl[CHAR_CNT][TS_CNT] = {
131 /* 0 1 2 3 4 5 6 7 8 9 */
132 /* bad */{{2,0},{2,0},{2,0},{2,0},{2,0},{2,0},{0,6},{0,6},{0,8},{2,0}},
133 /* g-z */{{1,1},{1,1},{1,1},{1,1},{2,0},{1,1},{0,6},{0,6},{0,8},{2,0}},
134 /* a-f */{{1,1},{1,1},{1,1},{1,1},{1,9},{1,1},{0,6},{0,6},{0,8},{1,4}},
135 /* x */{{1,1},{1,1},{1,4},{1,4},{2,0},{1,1},{0,6},{0,6},{0,8},{2,0}},
136 /* 0 */{{1,2},{1,1},{1,3},{1,3},{1,9},{1,1},{0,6},{0,6},{0,8},{1,4}},
137 /* 1-9 */{{1,3},{1,1},{1,3},{1,3},{1,9},{1,1},{0,6},{0,6},{0,8},{1,4}},
138 /* sp */{{0,0},{6,0},{8,0},{8,0},{7,0},{6,0},{0,6},{0,6},{0,8},{2,0}},
139 /* . */{{2,0},{1,1},{1,1},{1,1},{1,4},{1,1},{0,6},{0,6},{0,8},{2,0}},
140 /* / */{{1,5},{1,1},{1,1},{1,1},{7,0},{4,8},{0,6},{0,0},{0,8},{2,0}},
141 /* * */{{2,0},{6,0},{8,0},{8,0},{7,0},{4,6},{0,7},{0,7},{0,8},{2,0}},
142 /* # */{{0,8},{6,0},{8,0},{8,0},{7,0},{6,0},{0,6},{0,6},{0,8},{2,0}},
143 /* ;{} */{{3,0},{6,0},{8,0},{8,0},{7,0},{6,0},{0,6},{0,6},{0,8},{2,0}},
144 /* Msc */{{2,0},{1,1},{1,1},{1,1},{2,0},{1,1},{0,6},{0,6},{0,8},{2,0}},
145 /* EOL */{{0,0},{6,0},{8,0},{8,0},{7,0},{6,0},{0,6},{0,6},{0,0},{2,0}},
146 /* EOF */{{9,0},{6,0},{8,0},{8,0},{7,0},{6,0},{2,0},{2,0},{9,0},{2,0}},
156 } rsvd_word_tbl[] = {
157 { "ATMaddr", TOK_DCS_ADDR },
158 { "ATMARP", TOK_ATMARP },
159 { "CAReXmitInt", TOK_DCS_CA_REXMIT_INT },
160 { "CSUSReXmitInt", TOK_DCS_CSUS_REXMIT_INT },
161 { "CSUReXmitInt", TOK_DCS_CSU_REXMIT_INT },
162 { "CSUReXmitMax", TOK_DCS_CSU_REXMIT_MAX },
164 { "DHCP", TOK_DHCP },
165 { "familyID", TOK_FAMILY },
167 { "hops", TOK_DCS_HOP_CNT },
168 { "HelloDead", TOK_DCS_HELLO_DF },
169 { "HelloInt", TOK_DCS_HELLO_INT },
170 { "ID", TOK_DCS_ID },
171 { "LNNI", TOK_LNNI },
173 { "MARS", TOK_MARS },
174 { "netif", TOK_NETIF },
175 { "NHRP", TOK_NHRP },
176 { "protocol", TOK_PROTOCOL },
177 { "server", TOK_SERVER },
178 { "ServerGroupID", TOK_SRVGRP },
179 { "syslog", TOK_SYSLOG },
185 * Copy a character string
187 * Make a copy of a character string, using strdup. If strdup fails,
188 * meaning we're out of memory, then print an error message and exit.
191 * s string to be copied
194 * char * pointer to area provided by strdup
206 fprintf(stderr, "%s: strdup failed\n", prog);
215 * Push a character back onto the input stream.
218 * c character to be pushed
236 * Initialize the character class table.
238 * Set each entry in the character class table to the class
239 * corresponding to the character.
242 * tbl pointer to table to be initialized
255 * Set up the table for all ASCII characters
257 for (i=0; isascii((char)i); i++) {
261 tbl[i] = CHAR_INVALID;
264 * Set entries depending on character type
267 if (c == 'a' || c == 'b' || c == 'c' ||
268 c == 'd' || c == 'e' || c == 'f' ||
269 c == 'A' || c == 'B' || c == 'C' ||
270 c == 'D' || c == 'E' || c == 'F')
271 tbl[i] = CHAR_HEX_DIGIT;
272 else if (c == 'x' || c == 'X')
282 else if (c == ' ' || c == '\t')
287 tbl[i] = CHAR_ASTERISK;
289 tbl[i] = CHAR_DECIMAL;
292 else if (c == ';' || c == '{' || c == '}')
293 tbl[i] = CHAR_SPECIAL;
294 else if (c == '-' || c == '_' || c == '&' || c == '@' ||
302 * Get the class of a character.
305 * c character being scanned
308 * int character class
314 int class = CHAR_INVALID;
318 } else if (c < 0 || !isascii(c)) {
319 class = CHAR_INVALID;
321 class = class_tbl[(int)c];
329 * Print an error message when the scanner finds an error
332 * c character on which the error was recognized
333 * state scanner state at error
344 * Check for invalid character
346 if (char_class(c) == CHAR_INVALID) {
347 parse_error("Invalid character 0x%x encountered",
353 * Check for unexpected EOF
355 if (char_class(c) == CHAR_EOF) {
356 parse_error("Unexpected end of file");
361 * Error depends on state
365 parse_error("Syntax error at '%c'", c);
374 parse_error("Syntax error");
378 parse_error("Syntax error in hex string");
387 * Read a character at a time from the input file, assembling the
388 * characters into tokens as specified by the token scanner state
389 * table. Return the completed token.
395 * token the type of the token found
401 char c, token_buffer[TOK_MAX_LEN];
406 if (class_tbl['A'] != CHAR_HEX_DIGIT)
407 init_class_tbl(class_tbl);
409 UM_ZERO(token_buffer, sizeof(token_buffer));
410 UM_ZERO(&yylval, sizeof(yylval));
413 * Handle a character at a time until a token is built
417 * Read a character from the input file.
419 c = (char)getc(cfg_file);
425 printf("token_state: state=%d, char=%c, class=%d, action=%d, next=%d\n",
429 token_state_tbl[char_class][state].action,
430 token_state_tbl[char_class][state].next);
434 * Perform an action based on the state table
436 switch(token_state_tbl[char_class(c)][state].action) {
439 * Ignore the character
444 * Add character to buffer
446 if (strlen(token_buffer) < TOK_MAX_LEN) {
447 token_buffer[strlen(token_buffer)] = c;
452 * Error--print a message and start over
454 scan_error(c, state);
458 * Return special character
464 * Clear the token buffer
466 UM_ZERO(token_buffer, sizeof(token_buffer));
475 * Return character token
480 * Check for reserved words
482 for (i=0; rsvd_word_tbl[i].word; i++) {
483 if (strcasecmp(token_buffer,
484 rsvd_word_tbl[i].word) == 0)
487 if (rsvd_word_tbl[i].word) {
488 return(rsvd_word_tbl[i].token);
492 * Word isn't reserved, return alpha string
494 yylval.tv_alpha = copy_buffer(token_buffer);
499 * Return hex string (ATM address)
502 yylval.tv_hex = copy_buffer(token_buffer);
510 yylval.tv_int = atoi(token_buffer);
520 fprintf(stderr, "Invalid action indicator, state=%d, char=0x%02x\n",
526 * Set the next state and bump to the next character
528 state = token_state_tbl[char_class(c)][state].next;