1/* ntp_scanner.h
2 *
3 * The header file for a simple lexical analyzer.
4 *
5 * Written By:	Sachin Kamboj
6 *		University of Delaware
7 *		Newark, DE 19711
8 * Copyright (c) 2006
9 */
10
11#ifndef NTP_SCANNER_H
12#define NTP_SCANNER_H
13
14/*
15 * ntp.conf syntax is slightly irregular in that some tokens such as
16 * hostnames do not require quoting even if they might otherwise be
17 * recognized as T_ terminal tokens.  This hand-crafted lexical scanner
18 * uses a "followed by" value associated with each keyword to indicate
19 * normal scanning of the next token, forced scanning of the next token
20 * alone as a T_String, or forced scanning of all tokens to the end of
21 * the command as T_String.
22 * In the past the identifiers for this functionality ended in _ARG:
23 *
24 * NO_ARG	->	FOLLBY_TOKEN
25 * SINGLE_ARG	->	FOLLBY_STRING
26 * MULTIPLE_ARG	->	FOLLBY_STRINGS_TO_EOC
27 *
28 * Note that some tokens use FOLLBY_TOKEN even though they sometimes
29 * are followed by strings.  FOLLBY_STRING is used only when needed to
30 * avoid the keyword scanner matching a token where a string is needed.
31 *
32 * FOLLBY_NON_ACCEPT is an overloading of this field to distinguish
33 * non-accepting states (where the state number does not match a T_
34 * value).
35 */
36typedef enum {
37	FOLLBY_TOKEN = 0,
38	FOLLBY_STRING,
39	FOLLBY_STRINGS_TO_EOC,
40	FOLLBY_NON_ACCEPTING
41} follby;
42
43#define MAXLINE		1024	/* maximum length of line */
44#define MAXINCLUDELEVEL	5	/* maximum include file levels */
45
46/* STRUCTURES
47 * ----------
48 */
49
50/*
51 * Define a structure to hold the FSA for the keywords.
52 * The structure is actually a trie.
53 *
54 * To save space, a single u_int32 encodes four fields, and a fifth
55 * (the token completed for terminal states) is implied by the index of
56 * the rule within the scan state array, taking advantage of the fact
57 * there are more scan states than the highest T_ token number.
58 *
59 * The lowest 8 bits hold the character the state matches on.
60 * Bits 8 and 9 hold the followedby value (0 - 3).  For non-accepting
61 *   states (which do not match a completed token) the followedby
62 *   value 3 (FOLLBY_NONACCEPTING) denotes that fact.  For accepting
63 *   states, values 0 - 2 control whether the scanner forces the
64 *   following token(s) to strings.
65 * Bits 10 through 20 hold the next state to check not matching
66 * this state's character.
67 * Bits 21 through 31 hold the next state to check matching the char.
68 */
69
70#define S_ST(ch, fb, match_n, other_n) (			\
71	(u_char)((ch) & 0xff) |					\
72	((u_int32)(fb) << 8) |					\
73	((u_int32)(match_n) << 10) |				\
74	((u_int32)(other_n) << 21)				\
75)
76
77#define SS_CH(ss)	((char)(u_char)((ss) & 0xff))
78#define SS_FB(ss)	(((u_int)(ss) >>  8) & 0x3)
79#define SS_MATCH_N(ss)	(((u_int)(ss) >> 10) & 0x7ff)
80#define SS_OTHER_N(ss)	(((u_int)(ss) >> 21) & 0x7ff)
81
82typedef u_int32 scan_state;
83
84
85/* Structure to hold a filename, file pointer and positional info */
86struct FILE_INFO {
87	const char *	fname;			/* Path to the file */
88	FILE *		fd;			/* File Descriptor */
89	int		line_no;		/* Line Number */
90	int		col_no;			/* Column Number */
91	int		prev_line_col_no;	/* Col No on the
92						   previous line when a
93						   '\n' was seen */
94	int		prev_token_line_no;	/* Line at start of
95						   token */
96	int		prev_token_col_no;	/* Col No at start of
97						   token */
98	int		err_line_no;
99	int		err_col_no;
100};
101
102
103/* SCANNER GLOBAL VARIABLES
104 * ------------------------
105 */
106extern struct config_tree cfgt;	  /* Parser output stored here */
107extern int curr_include_level;    /* The current include level */
108
109extern struct FILE_INFO *ip_file; /* Pointer to the configuration file stream */
110
111/* VARIOUS EXTERNAL DECLARATIONS
112 * -----------------------------
113 */
114extern short default_ai_family;
115extern int old_config_style;
116extern int input_from_file;
117extern struct FILE_INFO *fp[];
118
119/* VARIOUS SUBROUTINE DECLARATIONS
120 * -------------------------------
121 */
122extern const char *keyword(int token);
123extern char *quote_if_needed(char *str);
124int yylex(void);
125
126struct FILE_INFO *F_OPEN(const char *path, const char *mode);
127int FGETC(struct FILE_INFO *stream);
128int UNGETC(int ch, struct FILE_INFO *stream);
129int FCLOSE(struct FILE_INFO *stream);
130
131void push_back_char(int ch);
132
133#endif	/* NTP_SCANNER_H */
134