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