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