Home | History | Annotate | Line # | Download | only in ntpd
keyword-gen.c revision 1.1.1.12
      1   1.1.1.8  christos /*	$NetBSD: keyword-gen.c,v 1.1.1.12 2018/04/07 00:15:48 christos Exp $	*/
      2   1.1.1.8  christos 
      3       1.1    kardel /*
      4       1.1    kardel  * keyword-gen.c -- generate keyword scanner finite state machine and
      5       1.1    kardel  *		    keyword_text array.
      6   1.1.1.3  christos  *
      7   1.1.1.3  christos  * This program is run to generate ntp_keyword.h
      8   1.1.1.3  christos  * After making a change here, two output files should be committed at
      9   1.1.1.3  christos  * the same time as keyword-gen.c:
     10   1.1.1.3  christos  *	ntp_keyword.h
     11   1.1.1.3  christos  *	keyword-gen-utd
     12   1.1.1.3  christos  *
     13   1.1.1.3  christos  * keyword-gen-utd is a sentinel used by Makefile.am to avoid compiling
     14   1.1.1.3  christos  * keyword_gen.c and generating ntp_keyword.h if the input keyword-gen.c
     15   1.1.1.3  christos  * has not changed.  This is not solely an optimization, it also breaks
     16   1.1.1.3  christos  * a dependency chain that otherwise would cause programs to be compiled
     17   1.1.1.3  christos  * when running "make dist" or "make distdir".  We want these to package
     18   1.1.1.3  christos  * the existing source without building anything but a tarball.  See
     19   1.1.1.3  christos  * [Bug 1470].
     20       1.1    kardel  */
     21       1.1    kardel #include <config.h>
     22       1.1    kardel #include <stdio.h>
     23       1.1    kardel #include <stdlib.h>
     24       1.1    kardel #include <time.h>
     25       1.1    kardel 
     26       1.1    kardel #include <ntp_stdlib.h>
     27       1.1    kardel #include <ntp_config.h>
     28       1.1    kardel #include <lib_strbuf.h>
     29       1.1    kardel #include "ntp_scanner.h"
     30       1.1    kardel #include "ntp_parser.h"
     31       1.1    kardel 
     32       1.1    kardel 
     33       1.1    kardel /* Define a structure to hold a (keyword, token) pair */
     34       1.1    kardel struct key_tok {
     35       1.1    kardel 	char *	key;		/* Keyword */
     36   1.1.1.3  christos 	u_short	token;		/* Associated Token */
     37       1.1    kardel 	follby	followedby;	/* nonzero indicates the next token(s)
     38       1.1    kardel 				   forced to be string(s) */
     39       1.1    kardel };
     40       1.1    kardel 
     41       1.1    kardel struct key_tok ntp_keywords[] = {
     42   1.1.1.2    kardel { "...",		T_Ellipsis,		FOLLBY_TOKEN },
     43   1.1.1.3  christos { "allpeers",		T_Allpeers,		FOLLBY_TOKEN },
     44       1.1    kardel { "automax",		T_Automax,		FOLLBY_TOKEN },
     45       1.1    kardel { "broadcast",		T_Broadcast,		FOLLBY_STRING },
     46       1.1    kardel { "broadcastclient",	T_Broadcastclient,	FOLLBY_TOKEN },
     47       1.1    kardel { "broadcastdelay",	T_Broadcastdelay,	FOLLBY_TOKEN },
     48   1.1.1.3  christos { "ctl",		T_Ctl,			FOLLBY_TOKEN },
     49       1.1    kardel { "disable",		T_Disable,		FOLLBY_TOKEN },
     50       1.1    kardel { "driftfile",		T_Driftfile,		FOLLBY_STRING },
     51   1.1.1.5  christos { "dscp",		T_Dscp,			FOLLBY_TOKEN },
     52       1.1    kardel { "enable",		T_Enable,		FOLLBY_TOKEN },
     53       1.1    kardel { "end",		T_End,			FOLLBY_TOKEN },
     54       1.1    kardel { "filegen",		T_Filegen,		FOLLBY_TOKEN },
     55       1.1    kardel { "fudge",		T_Fudge,		FOLLBY_STRING },
     56   1.1.1.3  christos { "io",			T_Io,			FOLLBY_TOKEN },
     57       1.1    kardel { "includefile",	T_Includefile,		FOLLBY_STRING },
     58       1.1    kardel { "leapfile",		T_Leapfile,		FOLLBY_STRING },
     59   1.1.1.5  christos { "leapsmearinterval",	T_Leapsmearinterval,	FOLLBY_TOKEN },
     60       1.1    kardel { "logconfig",		T_Logconfig,		FOLLBY_STRINGS_TO_EOC },
     61       1.1    kardel { "logfile",		T_Logfile,		FOLLBY_STRING },
     62       1.1    kardel { "manycastclient",	T_Manycastclient,	FOLLBY_STRING },
     63       1.1    kardel { "manycastserver",	T_Manycastserver,	FOLLBY_STRINGS_TO_EOC },
     64   1.1.1.3  christos { "mem",		T_Mem,			FOLLBY_TOKEN },
     65       1.1    kardel { "multicastclient",	T_Multicastclient,	FOLLBY_STRINGS_TO_EOC },
     66       1.1    kardel { "peer",		T_Peer,			FOLLBY_STRING },
     67       1.1    kardel { "phone",		T_Phone,		FOLLBY_STRINGS_TO_EOC },
     68       1.1    kardel { "pidfile",		T_Pidfile,		FOLLBY_STRING },
     69       1.1    kardel { "pool",		T_Pool,			FOLLBY_STRING },
     70       1.1    kardel { "discard",		T_Discard,		FOLLBY_TOKEN },
     71   1.1.1.3  christos { "reset",		T_Reset,		FOLLBY_TOKEN },
     72       1.1    kardel { "restrict",		T_Restrict,		FOLLBY_TOKEN },
     73   1.1.1.3  christos { "rlimit",		T_Rlimit,		FOLLBY_TOKEN },
     74       1.1    kardel { "server",		T_Server,		FOLLBY_STRING },
     75       1.1    kardel { "setvar",		T_Setvar,		FOLLBY_STRING },
     76       1.1    kardel { "statistics",		T_Statistics,		FOLLBY_TOKEN },
     77       1.1    kardel { "statsdir",		T_Statsdir,		FOLLBY_STRING },
     78   1.1.1.3  christos { "sys",		T_Sys,			FOLLBY_TOKEN },
     79       1.1    kardel { "tick",		T_Tick,			FOLLBY_TOKEN },
     80   1.1.1.3  christos { "timer",		T_Timer,		FOLLBY_TOKEN },
     81       1.1    kardel { "tinker",		T_Tinker,		FOLLBY_TOKEN },
     82       1.1    kardel { "tos",		T_Tos,			FOLLBY_TOKEN },
     83       1.1    kardel { "trap",		T_Trap,			FOLLBY_STRING },
     84       1.1    kardel { "unconfig",		T_Unconfig,		FOLLBY_STRING },
     85       1.1    kardel { "unpeer",		T_Unpeer,		FOLLBY_STRING },
     86       1.1    kardel /* authentication_command */
     87       1.1    kardel { "controlkey",		T_ControlKey,		FOLLBY_TOKEN },
     88       1.1    kardel { "crypto",		T_Crypto,		FOLLBY_TOKEN },
     89       1.1    kardel { "keys",		T_Keys,			FOLLBY_STRING },
     90       1.1    kardel { "keysdir",		T_Keysdir,		FOLLBY_STRING },
     91       1.1    kardel { "ntpsigndsocket",	T_NtpSignDsocket,	FOLLBY_STRING },
     92       1.1    kardel { "requestkey",		T_Requestkey,		FOLLBY_TOKEN },
     93       1.1    kardel { "revoke",		T_Revoke,		FOLLBY_TOKEN },
     94       1.1    kardel { "trustedkey",		T_Trustedkey,		FOLLBY_TOKEN },
     95       1.1    kardel /* IPv4/IPv6 protocol override flag */
     96       1.1    kardel { "-4",			T_Ipv4_flag,		FOLLBY_TOKEN },
     97       1.1    kardel { "-6",			T_Ipv6_flag,		FOLLBY_TOKEN },
     98       1.1    kardel /* option */
     99       1.1    kardel { "autokey",		T_Autokey,		FOLLBY_TOKEN },
    100       1.1    kardel { "burst",		T_Burst,		FOLLBY_TOKEN },
    101       1.1    kardel { "iburst",		T_Iburst,		FOLLBY_TOKEN },
    102       1.1    kardel { "key",		T_Key,			FOLLBY_TOKEN },
    103       1.1    kardel { "maxpoll",		T_Maxpoll,		FOLLBY_TOKEN },
    104   1.1.1.4  christos { "mdnstries",		T_Mdnstries,		FOLLBY_TOKEN },
    105       1.1    kardel { "minpoll",		T_Minpoll,		FOLLBY_TOKEN },
    106       1.1    kardel { "mode",		T_Mode,			FOLLBY_TOKEN },
    107       1.1    kardel { "noselect",		T_Noselect,		FOLLBY_TOKEN },
    108       1.1    kardel { "preempt",		T_Preempt,		FOLLBY_TOKEN },
    109       1.1    kardel { "true",		T_True,			FOLLBY_TOKEN },
    110       1.1    kardel { "prefer",		T_Prefer,		FOLLBY_TOKEN },
    111       1.1    kardel { "ttl",		T_Ttl,			FOLLBY_TOKEN },
    112       1.1    kardel { "version",		T_Version,		FOLLBY_TOKEN },
    113       1.1    kardel { "xleave",		T_Xleave,		FOLLBY_TOKEN },
    114       1.1    kardel /* crypto_command */
    115       1.1    kardel { "host",		T_Host,			FOLLBY_STRING },
    116       1.1    kardel { "ident",		T_Ident,		FOLLBY_STRING },
    117       1.1    kardel { "pw",			T_Pw,			FOLLBY_STRING },
    118       1.1    kardel { "randfile",		T_Randfile,		FOLLBY_STRING },
    119       1.1    kardel { "digest",		T_Digest,		FOLLBY_STRING },
    120       1.1    kardel /*** MONITORING COMMANDS ***/
    121       1.1    kardel /* stat */
    122       1.1    kardel { "clockstats",		T_Clockstats,		FOLLBY_TOKEN },
    123       1.1    kardel { "cryptostats",	T_Cryptostats,		FOLLBY_TOKEN },
    124       1.1    kardel { "loopstats",		T_Loopstats,		FOLLBY_TOKEN },
    125       1.1    kardel { "peerstats",		T_Peerstats,		FOLLBY_TOKEN },
    126       1.1    kardel { "rawstats",		T_Rawstats,		FOLLBY_TOKEN },
    127       1.1    kardel { "sysstats", 		T_Sysstats,		FOLLBY_TOKEN },
    128       1.1    kardel { "protostats",		T_Protostats,		FOLLBY_TOKEN },
    129       1.1    kardel { "timingstats",	T_Timingstats,		FOLLBY_TOKEN },
    130       1.1    kardel /* filegen_option */
    131       1.1    kardel { "file",		T_File,			FOLLBY_STRING },
    132       1.1    kardel { "link",		T_Link,			FOLLBY_TOKEN },
    133       1.1    kardel { "nolink",		T_Nolink,		FOLLBY_TOKEN },
    134       1.1    kardel { "type",		T_Type,			FOLLBY_TOKEN },
    135       1.1    kardel /* filegen_type */
    136       1.1    kardel { "age",		T_Age,			FOLLBY_TOKEN },
    137       1.1    kardel { "day",		T_Day,			FOLLBY_TOKEN },
    138       1.1    kardel { "month",		T_Month,		FOLLBY_TOKEN },
    139       1.1    kardel { "none",		T_None,			FOLLBY_TOKEN },
    140       1.1    kardel { "pid",		T_Pid,			FOLLBY_TOKEN },
    141       1.1    kardel { "week",		T_Week,			FOLLBY_TOKEN },
    142       1.1    kardel { "year",		T_Year,			FOLLBY_TOKEN },
    143       1.1    kardel /*** ORPHAN MODE COMMANDS ***/
    144       1.1    kardel /* tos_option */
    145       1.1    kardel { "minclock",		T_Minclock,		FOLLBY_TOKEN },
    146       1.1    kardel { "maxclock",		T_Maxclock,		FOLLBY_TOKEN },
    147       1.1    kardel { "minsane",		T_Minsane,		FOLLBY_TOKEN },
    148       1.1    kardel { "floor",		T_Floor,		FOLLBY_TOKEN },
    149       1.1    kardel { "ceiling",		T_Ceiling,		FOLLBY_TOKEN },
    150       1.1    kardel { "cohort",		T_Cohort,		FOLLBY_TOKEN },
    151       1.1    kardel { "mindist",		T_Mindist,		FOLLBY_TOKEN },
    152       1.1    kardel { "maxdist",		T_Maxdist,		FOLLBY_TOKEN },
    153  1.1.1.11  christos { "bcpollbstep",	T_Bcpollbstep,		FOLLBY_TOKEN },
    154       1.1    kardel { "beacon",		T_Beacon,		FOLLBY_TOKEN },
    155       1.1    kardel { "orphan",		T_Orphan,		FOLLBY_TOKEN },
    156   1.1.1.3  christos { "orphanwait",		T_Orphanwait,		FOLLBY_TOKEN },
    157   1.1.1.3  christos { "nonvolatile",	T_Nonvolatile,		FOLLBY_TOKEN },
    158  1.1.1.12  christos { "basedate",		T_Basedate,		FOLLBY_STRING },
    159       1.1    kardel /* access_control_flag */
    160       1.1    kardel { "default",		T_Default,		FOLLBY_TOKEN },
    161   1.1.1.3  christos { "source",		T_Source,		FOLLBY_TOKEN },
    162  1.1.1.12  christos { "epeer",		T_Epeer,		FOLLBY_TOKEN },
    163  1.1.1.12  christos { "noepeer",		T_Noepeer,		FOLLBY_TOKEN },
    164       1.1    kardel { "flake",		T_Flake,		FOLLBY_TOKEN },
    165       1.1    kardel { "ignore",		T_Ignore,		FOLLBY_TOKEN },
    166  1.1.1.12  christos { "ippeerlimit",	T_Ippeerlimit,		FOLLBY_TOKEN },
    167       1.1    kardel { "limited",		T_Limited,		FOLLBY_TOKEN },
    168       1.1    kardel { "mssntp",		T_Mssntp,		FOLLBY_TOKEN },
    169       1.1    kardel { "kod",		T_Kod,			FOLLBY_TOKEN },
    170       1.1    kardel { "lowpriotrap",	T_Lowpriotrap,		FOLLBY_TOKEN },
    171       1.1    kardel { "mask",		T_Mask,			FOLLBY_TOKEN },
    172       1.1    kardel { "nomodify",		T_Nomodify,		FOLLBY_TOKEN },
    173   1.1.1.3  christos { "nomrulist",		T_Nomrulist,		FOLLBY_TOKEN },
    174       1.1    kardel { "nopeer",		T_Nopeer,		FOLLBY_TOKEN },
    175       1.1    kardel { "noquery",		T_Noquery,		FOLLBY_TOKEN },
    176       1.1    kardel { "noserve",		T_Noserve,		FOLLBY_TOKEN },
    177       1.1    kardel { "notrap",		T_Notrap,		FOLLBY_TOKEN },
    178       1.1    kardel { "notrust",		T_Notrust,		FOLLBY_TOKEN },
    179       1.1    kardel { "ntpport",		T_Ntpport,		FOLLBY_TOKEN },
    180       1.1    kardel /* discard_option */
    181       1.1    kardel { "average",		T_Average,		FOLLBY_TOKEN },
    182       1.1    kardel { "minimum",		T_Minimum,		FOLLBY_TOKEN },
    183       1.1    kardel { "monitor",		T_Monitor,		FOLLBY_TOKEN },
    184   1.1.1.3  christos /* mru_option */
    185   1.1.1.3  christos { "incalloc",		T_Incalloc,		FOLLBY_TOKEN },
    186   1.1.1.3  christos { "incmem",		T_Incmem,		FOLLBY_TOKEN },
    187   1.1.1.3  christos { "initalloc",		T_Initalloc,		FOLLBY_TOKEN },
    188   1.1.1.3  christos { "initmem",		T_Initmem,		FOLLBY_TOKEN },
    189   1.1.1.3  christos { "mindepth",		T_Mindepth,		FOLLBY_TOKEN },
    190   1.1.1.3  christos { "maxage",		T_Maxage,		FOLLBY_TOKEN },
    191   1.1.1.3  christos { "maxdepth",		T_Maxdepth,		FOLLBY_TOKEN },
    192   1.1.1.3  christos { "maxmem",		T_Maxmem,		FOLLBY_TOKEN },
    193   1.1.1.3  christos { "mru",		T_Mru,			FOLLBY_TOKEN },
    194       1.1    kardel /* fudge_factor */
    195   1.1.1.3  christos { "abbrev",		T_Abbrev,		FOLLBY_STRING },
    196       1.1    kardel { "flag1",		T_Flag1,		FOLLBY_TOKEN },
    197       1.1    kardel { "flag2",		T_Flag2,		FOLLBY_TOKEN },
    198       1.1    kardel { "flag3",		T_Flag3,		FOLLBY_TOKEN },
    199       1.1    kardel { "flag4",		T_Flag4,		FOLLBY_TOKEN },
    200       1.1    kardel { "refid",		T_Refid,		FOLLBY_STRING },
    201       1.1    kardel { "stratum",		T_Stratum,		FOLLBY_TOKEN },
    202       1.1    kardel { "time1",		T_Time1,		FOLLBY_TOKEN },
    203       1.1    kardel { "time2",		T_Time2,		FOLLBY_TOKEN },
    204       1.1    kardel /* system_option */
    205       1.1    kardel { "auth",		T_Auth,			FOLLBY_TOKEN },
    206       1.1    kardel { "bclient",		T_Bclient,		FOLLBY_TOKEN },
    207       1.1    kardel { "calibrate",		T_Calibrate,		FOLLBY_TOKEN },
    208       1.1    kardel { "kernel",		T_Kernel,		FOLLBY_TOKEN },
    209   1.1.1.3  christos { "mode7",		T_Mode7,		FOLLBY_TOKEN },
    210  1.1.1.10  christos { "ntp",		T_Ntp,			FOLLBY_TOKEN },
    211  1.1.1.10  christos { "peer_clear_digest_early",	T_PCEdigest,	FOLLBY_TOKEN },
    212       1.1    kardel { "stats",		T_Stats,		FOLLBY_TOKEN },
    213   1.1.1.9  christos { "unpeer_crypto_early",	T_UEcrypto,	FOLLBY_TOKEN },
    214   1.1.1.9  christos { "unpeer_crypto_nak_early",	T_UEcryptonak,	FOLLBY_TOKEN },
    215   1.1.1.9  christos { "unpeer_digest_early",	T_UEdigest,	FOLLBY_TOKEN },
    216   1.1.1.3  christos /* rlimit_option */
    217   1.1.1.3  christos { "memlock",		T_Memlock,		FOLLBY_TOKEN },
    218   1.1.1.3  christos { "stacksize",		T_Stacksize,		FOLLBY_TOKEN },
    219   1.1.1.3  christos { "filenum",		T_Filenum,		FOLLBY_TOKEN },
    220       1.1    kardel /* tinker_option */
    221       1.1    kardel { "step",		T_Step,			FOLLBY_TOKEN },
    222   1.1.1.4  christos { "stepback",		T_Stepback,		FOLLBY_TOKEN },
    223   1.1.1.4  christos { "stepfwd",		T_Stepfwd,		FOLLBY_TOKEN },
    224       1.1    kardel { "panic",		T_Panic,		FOLLBY_TOKEN },
    225       1.1    kardel { "dispersion",		T_Dispersion,		FOLLBY_TOKEN },
    226       1.1    kardel { "stepout",		T_Stepout,		FOLLBY_TOKEN },
    227       1.1    kardel { "allan",		T_Allan,		FOLLBY_TOKEN },
    228       1.1    kardel { "huffpuff",		T_Huffpuff,		FOLLBY_TOKEN },
    229       1.1    kardel { "freq",		T_Freq,			FOLLBY_TOKEN },
    230       1.1    kardel /* miscellaneous_command */
    231       1.1    kardel { "port",		T_Port,			FOLLBY_TOKEN },
    232       1.1    kardel { "interface",		T_Interface,		FOLLBY_TOKEN },
    233       1.1    kardel { "saveconfigdir",	T_Saveconfigdir,	FOLLBY_STRING },
    234       1.1    kardel /* interface_command (ignore and interface already defined) */
    235       1.1    kardel { "nic",		T_Nic,			FOLLBY_TOKEN },
    236       1.1    kardel { "all",		T_All,			FOLLBY_TOKEN },
    237       1.1    kardel { "ipv4",		T_Ipv4,			FOLLBY_TOKEN },
    238       1.1    kardel { "ipv6",		T_Ipv6,			FOLLBY_TOKEN },
    239       1.1    kardel { "wildcard",		T_Wildcard,		FOLLBY_TOKEN },
    240       1.1    kardel { "listen",		T_Listen,		FOLLBY_TOKEN },
    241       1.1    kardel { "drop",		T_Drop,			FOLLBY_TOKEN },
    242       1.1    kardel /* simulator commands */
    243       1.1    kardel { "simulate",		T_Simulate,		FOLLBY_TOKEN },
    244       1.1    kardel { "simulation_duration",T_Sim_Duration,		FOLLBY_TOKEN },
    245       1.1    kardel { "beep_delay",		T_Beep_Delay,		FOLLBY_TOKEN },
    246       1.1    kardel { "duration",		T_Duration,		FOLLBY_TOKEN },
    247       1.1    kardel { "server_offset",	T_Server_Offset,	FOLLBY_TOKEN },
    248       1.1    kardel { "freq_offset",	T_Freq_Offset,		FOLLBY_TOKEN },
    249       1.1    kardel { "wander",		T_Wander,		FOLLBY_TOKEN },
    250       1.1    kardel { "jitter",		T_Jitter,		FOLLBY_TOKEN },
    251       1.1    kardel { "prop_delay",		T_Prop_Delay,		FOLLBY_TOKEN },
    252       1.1    kardel { "proc_delay",		T_Proc_Delay,		FOLLBY_TOKEN },
    253       1.1    kardel };
    254       1.1    kardel 
    255       1.1    kardel typedef struct big_scan_state_tag {
    256       1.1    kardel 	char	ch;		/* Character this state matches on */
    257       1.1    kardel 	char	followedby;	/* Forces next token(s) to T_String */
    258       1.1    kardel 	u_short	finishes_token;	/* nonzero ID if last keyword char */
    259       1.1    kardel 	u_short	match_next_s;	/* next state to check matching ch */
    260       1.1    kardel 	u_short	other_next_s;	/* next state to check if not ch */
    261       1.1    kardel } big_scan_state;
    262       1.1    kardel 
    263       1.1    kardel /*
    264       1.1    kardel  * Note: to increase MAXSTATES beyond 2048, be aware it is currently
    265       1.1    kardel  * crammed into 11 bits in scan_state form.  Raising to 4096 would be
    266       1.1    kardel  * relatively easy by storing the followedby value in a separate
    267       1.1    kardel  * array with one entry per token, and shrinking the char value to
    268       1.1    kardel  * 7 bits to free a bit for accepting/non-accepting.  More than 4096
    269       1.1    kardel  * states will require expanding scan_state beyond 32 bits each.
    270       1.1    kardel  */
    271   1.1.1.3  christos #define MAXSTATES	2048
    272   1.1.1.3  christos #define MAX_TOK_LEN	63
    273       1.1    kardel 
    274       1.1    kardel const char *	current_keyword;/* for error reporting */
    275       1.1    kardel big_scan_state	sst[MAXSTATES];	/* scanner FSM state entries */
    276   1.1.1.3  christos u_short		sst_highwater;	/* next entry index to consider */
    277       1.1    kardel char *		symb[1024];	/* map token ID to symbolic name */
    278       1.1    kardel 
    279       1.1    kardel /* for libntp */
    280       1.1    kardel const char *	progname = "keyword-gen";
    281       1.1    kardel 
    282       1.1    kardel int		main			(int, char **);
    283       1.1    kardel static void	generate_preamble	(void);
    284       1.1    kardel static void	generate_fsm		(void);
    285       1.1    kardel static void	generate_token_text	(void);
    286   1.1.1.3  christos static u_short	create_keyword_scanner	(void);
    287   1.1.1.3  christos static u_short	create_scan_states	(char *, u_short, follby, u_short);
    288   1.1.1.3  christos int		compare_key_tok_id	(const void *, const void *);
    289   1.1.1.3  christos int		compare_key_tok_text	(const void *, const void *);
    290       1.1    kardel void		populate_symb		(char *);
    291   1.1.1.3  christos const char *	symbname		(u_short);
    292       1.1    kardel 
    293       1.1    kardel 
    294       1.1    kardel int main(int argc, char **argv)
    295       1.1    kardel {
    296       1.1    kardel 	if (argc < 2) {
    297       1.1    kardel 		fprintf(stderr, "Usage:\n%s t_header.h\n", argv[0]);
    298       1.1    kardel 		exit(1);
    299       1.1    kardel 	}
    300   1.1.1.3  christos 	debug = 1;
    301   1.1.1.3  christos 
    302       1.1    kardel 	populate_symb(argv[1]);
    303       1.1    kardel 
    304       1.1    kardel 	generate_preamble();
    305       1.1    kardel 	generate_token_text();
    306       1.1    kardel 	generate_fsm();
    307       1.1    kardel 
    308       1.1    kardel 	return 0;
    309       1.1    kardel }
    310       1.1    kardel 
    311       1.1    kardel 
    312       1.1    kardel static void
    313       1.1    kardel generate_preamble(void)
    314       1.1    kardel {
    315       1.1    kardel 	time_t now;
    316       1.1    kardel 	char timestamp[128];
    317       1.1    kardel 	char preamble[] =
    318       1.1    kardel "/*\n"
    319       1.1    kardel " * ntp_keyword.h\n"
    320       1.1    kardel " * \n"
    321       1.1    kardel " * NOTE: edit this file with caution, it is generated by keyword-gen.c\n"
    322       1.1    kardel " *\t Generated %s UTC	  diff_ignore_line\n"
    323       1.1    kardel " *\n"
    324       1.1    kardel " */\n"
    325       1.1    kardel "#include \"ntp_scanner.h\"\n"
    326       1.1    kardel "#include \"ntp_parser.h\"\n"
    327       1.1    kardel "\n";
    328       1.1    kardel 
    329       1.1    kardel 	time(&now);
    330       1.1    kardel 	if (!strftime(timestamp, sizeof(timestamp),
    331       1.1    kardel 		      "%Y-%m-%d %H:%M:%S", gmtime(&now)))
    332       1.1    kardel 		timestamp[0] = '\0';
    333       1.1    kardel 
    334       1.1    kardel 	printf(preamble, timestamp);
    335       1.1    kardel }
    336       1.1    kardel 
    337       1.1    kardel 
    338       1.1    kardel static void
    339       1.1    kardel generate_fsm(void)
    340       1.1    kardel {
    341   1.1.1.3  christos 	char rprefix[MAX_TOK_LEN + 1];
    342   1.1.1.3  christos 	char prefix[MAX_TOK_LEN + 1];
    343   1.1.1.3  christos 	char token_id_comment[16 + MAX_TOK_LEN + 1];
    344   1.1.1.3  christos 	size_t prefix_len;
    345   1.1.1.3  christos 	char *p;
    346   1.1.1.3  christos 	char *r;
    347   1.1.1.3  christos 	u_short initial_state;
    348   1.1.1.3  christos 	u_short this_state;
    349   1.1.1.3  christos 	u_short state;
    350   1.1.1.3  christos 	u_short i;
    351   1.1.1.3  christos 	u_short token;
    352       1.1    kardel 
    353   1.1.1.5  christos 	/*
    354       1.1    kardel 	 * Sort ntp_keywords in alphabetical keyword order.  This is
    355       1.1    kardel 	 * not necessary, but minimizes nonfunctional changes in the
    356       1.1    kardel 	 * generated finite state machine when keywords are modified.
    357       1.1    kardel 	 */
    358       1.1    kardel 	qsort(ntp_keywords, COUNTOF(ntp_keywords),
    359       1.1    kardel 	      sizeof(ntp_keywords[0]), compare_key_tok_text);
    360       1.1    kardel 
    361       1.1    kardel 	/*
    362   1.1.1.5  christos 	 * To save space, reserve the state array entry matching each
    363       1.1    kardel 	 * token number for its terminal state, so the token identifier
    364       1.1    kardel 	 * does not need to be stored in each state, but can be
    365       1.1    kardel 	 * recovered trivially.  To mark the entry reserved,
    366       1.1    kardel 	 * finishes_token is nonzero.
    367       1.1    kardel 	 */
    368       1.1    kardel 
    369       1.1    kardel 	for (i = 0; i < COUNTOF(ntp_keywords); i++) {
    370       1.1    kardel 		token = ntp_keywords[i].token;
    371       1.1    kardel 		if (1 > token || token >= COUNTOF(sst)) {
    372       1.1    kardel 			fprintf(stderr,
    373       1.1    kardel 				"keyword-gen sst[%u] too small "
    374       1.1    kardel 				"for keyword '%s' id %d\n",
    375   1.1.1.4  christos 				(int)COUNTOF(sst),
    376       1.1    kardel 				ntp_keywords[i].key,
    377       1.1    kardel 				token);
    378       1.1    kardel 			exit(4);
    379       1.1    kardel 		}
    380       1.1    kardel 		sst[token].finishes_token = token;
    381       1.1    kardel 	}
    382       1.1    kardel 
    383       1.1    kardel 	initial_state = create_keyword_scanner();
    384       1.1    kardel 
    385       1.1    kardel 	fprintf(stderr,
    386       1.1    kardel 		"%d keywords consumed %d states of %d max.\n",
    387       1.1    kardel 		(int)COUNTOF(ntp_keywords),
    388       1.1    kardel 		sst_highwater - 1,
    389       1.1    kardel 		(int)COUNTOF(sst) - 1);
    390       1.1    kardel 
    391       1.1    kardel 	printf("#define SCANNER_INIT_S %d\n\n", initial_state);
    392       1.1    kardel 
    393       1.1    kardel 	printf("const scan_state sst[%d] = {\n"
    394       1.1    kardel 	       "/*SS_T( ch,\tf-by, match, other ),\t\t\t\t */\n"
    395       1.1    kardel 	       "  0,\t\t\t\t      /* %5d %-17s */\n",
    396       1.1    kardel 	       sst_highwater,
    397       1.1    kardel 	       0, "");
    398       1.1    kardel 
    399       1.1    kardel 	for (i = 1; i < sst_highwater; i++) {
    400       1.1    kardel 
    401       1.1    kardel 		/* verify fields will fit */
    402       1.1    kardel 		if (sst[i].followedby & ~0x3) {
    403       1.1    kardel 			fprintf(stderr,
    404       1.1    kardel 				"keyword-gen internal error "
    405       1.1    kardel 				"sst[%d].followedby %d too big\n",
    406       1.1    kardel 				i, sst[i].followedby);
    407       1.1    kardel 			exit(7);
    408       1.1    kardel 		}
    409       1.1    kardel 
    410       1.1    kardel 		if (sst_highwater <= sst[i].match_next_s
    411       1.1    kardel 		    || sst[i].match_next_s & ~0x7ff) {
    412       1.1    kardel 			fprintf(stderr,
    413       1.1    kardel 				"keyword-gen internal error "
    414       1.1    kardel 				"sst[%d].match_next_s %d too big\n",
    415       1.1    kardel 				i, sst[i].match_next_s);
    416       1.1    kardel 			exit(8);
    417       1.1    kardel 		}
    418       1.1    kardel 
    419       1.1    kardel 		if (sst_highwater <= sst[i].other_next_s
    420       1.1    kardel 		    || sst[i].other_next_s & ~0x7ff) {
    421       1.1    kardel 			fprintf(stderr,
    422       1.1    kardel 				"keyword-gen internal error "
    423       1.1    kardel 				"sst[%d].other_next_s %d too big\n",
    424       1.1    kardel 				i, sst[i].other_next_s);
    425       1.1    kardel 			exit(9);
    426       1.1    kardel 		}
    427       1.1    kardel 
    428   1.1.1.3  christos 		if (sst[i].finishes_token) {
    429   1.1.1.5  christos 			snprintf(token_id_comment,
    430       1.1    kardel 				 sizeof(token_id_comment), "%5d %-17s",
    431       1.1    kardel 				 i, symbname(sst[i].finishes_token));
    432       1.1    kardel 			if (i != sst[i].finishes_token) {
    433       1.1    kardel 				fprintf(stderr,
    434       1.1    kardel 					"keyword-gen internal error "
    435       1.1    kardel 					"entry %d finishes token %d\n",
    436       1.1    kardel 					i, sst[i].finishes_token);
    437       1.1    kardel 				exit(5);
    438       1.1    kardel 			}
    439   1.1.1.3  christos 		} else {
    440   1.1.1.3  christos 		/*
    441   1.1.1.3  christos 		 * Determine the keyword prefix that leads to this
    442   1.1.1.3  christos 		 * state.  This is expensive but keyword-gen is run
    443   1.1.1.3  christos 		 * only when it changes.  Distributing keyword-gen-utd
    444   1.1.1.3  christos 		 * achieves that, which is why it must be committed
    445   1.1.1.3  christos 		 * at the same time as keyword-gen.c and ntp_keyword.h.
    446   1.1.1.3  christos 		 *
    447   1.1.1.3  christos 		 * Scan the state array iteratively looking for a state
    448   1.1.1.3  christos 		 * which leads to the current one, collecting matching
    449   1.1.1.3  christos 		 * characters along the way.  There is only one such
    450   1.1.1.3  christos 		 * path back to the starting state given the way our
    451   1.1.1.3  christos 		 * scanner state machine is built and the practice of
    452   1.1.1.3  christos 		 * using the spelling of the keyword as its T_* token
    453   1.1.1.3  christos 		 * identifier, which results in never having two
    454   1.1.1.3  christos 		 * spellings result in the same T_* value.
    455   1.1.1.3  christos 		 */
    456   1.1.1.3  christos 			prefix_len = 0;
    457   1.1.1.3  christos 			this_state = i;
    458   1.1.1.3  christos 			do {
    459   1.1.1.3  christos 				for (state = 1; state < sst_highwater; state++)
    460   1.1.1.3  christos 					if (sst[state].other_next_s == this_state) {
    461   1.1.1.3  christos 						this_state = state;
    462   1.1.1.3  christos 						break;
    463   1.1.1.3  christos 					} else if (sst[state].match_next_s == this_state) {
    464   1.1.1.3  christos 						this_state = state;
    465   1.1.1.3  christos 						rprefix[prefix_len] = sst[state].ch;
    466   1.1.1.3  christos 						prefix_len++;
    467   1.1.1.3  christos 						break;
    468   1.1.1.3  christos 					}
    469   1.1.1.3  christos 			} while (this_state != initial_state);
    470   1.1.1.3  christos 
    471   1.1.1.3  christos 			if (prefix_len) {
    472   1.1.1.3  christos 				/* reverse rprefix into prefix */
    473   1.1.1.3  christos 				p = prefix + prefix_len;
    474   1.1.1.3  christos 				r = rprefix;
    475   1.1.1.3  christos 				while (r < rprefix + prefix_len)
    476   1.1.1.3  christos 					*--p = *r++;
    477   1.1.1.3  christos 			}
    478   1.1.1.3  christos 			prefix[prefix_len] = '\0';
    479   1.1.1.3  christos 
    480   1.1.1.3  christos 			snprintf(token_id_comment,
    481   1.1.1.3  christos 				 sizeof(token_id_comment), "%5d %-17s",
    482   1.1.1.5  christos 				 i, (initial_state == i)
    483   1.1.1.5  christos 					? "[initial state]"
    484   1.1.1.3  christos 					: prefix);
    485       1.1    kardel 		}
    486       1.1    kardel 
    487       1.1    kardel 		printf("  S_ST( '%c',\t%d,    %5u, %5u )%s /* %s */\n",
    488       1.1    kardel 		       sst[i].ch,
    489       1.1    kardel 		       sst[i].followedby,
    490       1.1    kardel 		       sst[i].match_next_s,
    491       1.1    kardel 		       sst[i].other_next_s,
    492       1.1    kardel 		       (i + 1 < sst_highwater)
    493       1.1    kardel 			   ? ","
    494       1.1    kardel 			   : " ",
    495       1.1    kardel 		       token_id_comment);
    496       1.1    kardel 	}
    497       1.1    kardel 
    498       1.1    kardel 	printf("};\n\n");
    499       1.1    kardel }
    500       1.1    kardel 
    501       1.1    kardel 
    502       1.1    kardel /* Define a function to create the states of the scanner. This function
    503       1.1    kardel  * is used by the create_keyword_scanner function below.
    504       1.1    kardel  *
    505       1.1    kardel  * This function takes a suffix of a keyword, the token to be returned on
    506       1.1    kardel  * recognizing the complete keyword, and any pre-existing state that exists
    507       1.1    kardel  * for some other keyword that has the same prefix as the current one.
    508       1.1    kardel  */
    509   1.1.1.3  christos static u_short
    510       1.1    kardel create_scan_states(
    511   1.1.1.5  christos 	char *	text,
    512   1.1.1.5  christos 	u_short	token,
    513       1.1    kardel 	follby	followedby,
    514   1.1.1.3  christos 	u_short	prev_state
    515       1.1    kardel 	)
    516       1.1    kardel {
    517   1.1.1.3  christos 	u_short my_state;
    518   1.1.1.3  christos 	u_short return_state;
    519   1.1.1.3  christos 	u_short prev_char_s;
    520   1.1.1.3  christos 	u_short curr_char_s;
    521       1.1    kardel 
    522       1.1    kardel 	return_state = prev_state;
    523       1.1    kardel 	curr_char_s = prev_state;
    524       1.1    kardel 	prev_char_s = 0;
    525       1.1    kardel 
    526   1.1.1.5  christos 	/* Find the correct position to insert the state.
    527       1.1    kardel 	 * All states should be in alphabetical order
    528       1.1    kardel 	 */
    529       1.1    kardel 	while (curr_char_s && (text[0] < sst[curr_char_s].ch)) {
    530       1.1    kardel 		prev_char_s = curr_char_s;
    531       1.1    kardel 		curr_char_s = sst[curr_char_s].other_next_s;
    532       1.1    kardel 	}
    533       1.1    kardel 
    534   1.1.1.5  christos 	/*
    535       1.1    kardel 	 * Check if a previously seen keyword has the same prefix as
    536       1.1    kardel 	 * the current keyword.  If so, simply use the state for that
    537       1.1    kardel 	 * keyword as my_state, otherwise, allocate a new state.
    538       1.1    kardel 	 */
    539       1.1    kardel 	if (curr_char_s && (text[0] == sst[curr_char_s].ch)) {
    540       1.1    kardel 		my_state = curr_char_s;
    541       1.1    kardel 		if ('\0' == text[1]) {
    542       1.1    kardel 			fprintf(stderr,
    543       1.1    kardel 				"Duplicate entries for keyword '%s' in"
    544       1.1    kardel 				" keyword_gen.c ntp_keywords[].\n",
    545       1.1    kardel 				current_keyword);
    546       1.1    kardel 			exit(2);
    547       1.1    kardel 		}
    548       1.1    kardel 	} else {
    549       1.1    kardel 		do
    550       1.1    kardel 			my_state = sst_highwater++;
    551       1.1    kardel 		while (my_state < COUNTOF(sst)
    552       1.1    kardel 		       && sst[my_state].finishes_token);
    553       1.1    kardel 		if (my_state >= COUNTOF(sst)) {
    554       1.1    kardel 			fprintf(stderr,
    555       1.1    kardel 				"fatal, keyword scanner state array "
    556       1.1    kardel 				"sst[%d] is too small, modify\n"
    557       1.1    kardel 				"keyword-gen.c to increase.\n",
    558       1.1    kardel 				(int)COUNTOF(sst));
    559       1.1    kardel 			exit(3);
    560       1.1    kardel 		}
    561       1.1    kardel 		/* Store the next character of the keyword */
    562   1.1.1.5  christos 		sst[my_state].ch = text[0];
    563       1.1    kardel 		sst[my_state].other_next_s = curr_char_s;
    564       1.1    kardel 		sst[my_state].followedby = FOLLBY_NON_ACCEPTING;
    565       1.1    kardel 
    566       1.1    kardel 		if (prev_char_s)
    567       1.1    kardel 			sst[prev_char_s].other_next_s = my_state;
    568       1.1    kardel 		else
    569       1.1    kardel 			return_state = my_state;
    570       1.1    kardel 	}
    571       1.1    kardel 
    572       1.1    kardel 	/* Check if the next character is '\0'.
    573       1.1    kardel 	 * If yes, we are done with the recognition and this is an accepting
    574       1.1    kardel 	 * state.
    575       1.1    kardel 	 * If not, we need to continue scanning
    576       1.1    kardel 	 */
    577       1.1    kardel 	if ('\0' == text[1]) {
    578       1.1    kardel 		sst[my_state].finishes_token = (u_short)token;
    579       1.1    kardel 		sst[my_state].followedby = (char)followedby;
    580       1.1    kardel 
    581       1.1    kardel 		if (sst[token].finishes_token != (u_short)token) {
    582       1.1    kardel 			fprintf(stderr,
    583       1.1    kardel 				"fatal, sst[%d] not reserved for %s.\n",
    584       1.1    kardel 				token, symbname(token));
    585       1.1    kardel 			exit(6);
    586       1.1    kardel 		}
    587       1.1    kardel 		/* relocate so token id is sst[] index */
    588       1.1    kardel 		if (my_state != token) {
    589       1.1    kardel 			sst[token] = sst[my_state];
    590   1.1.1.3  christos 			ZERO(sst[my_state]);
    591       1.1    kardel 			do
    592       1.1    kardel 				sst_highwater--;
    593       1.1    kardel 			while (sst[sst_highwater].finishes_token);
    594       1.1    kardel 			my_state = token;
    595       1.1    kardel 			if (prev_char_s)
    596       1.1    kardel 				sst[prev_char_s].other_next_s = my_state;
    597       1.1    kardel 			else
    598       1.1    kardel 				return_state = my_state;
    599       1.1    kardel 		}
    600       1.1    kardel 	} else
    601   1.1.1.5  christos 		sst[my_state].match_next_s =
    602       1.1    kardel 		    create_scan_states(
    603       1.1    kardel 			&text[1],
    604       1.1    kardel 			token,
    605       1.1    kardel 			followedby,
    606       1.1    kardel 			sst[my_state].match_next_s);
    607       1.1    kardel 
    608       1.1    kardel 	return return_state;
    609       1.1    kardel }
    610       1.1    kardel 
    611       1.1    kardel 
    612       1.1    kardel /* Define a function that takes a list of (keyword, token) values and
    613       1.1    kardel  * creates a keywords scanner out of it.
    614       1.1    kardel  */
    615       1.1    kardel 
    616   1.1.1.3  christos static u_short
    617       1.1    kardel create_keyword_scanner(void)
    618       1.1    kardel {
    619   1.1.1.3  christos 	u_short scanner;
    620   1.1.1.3  christos 	u_short i;
    621       1.1    kardel 
    622       1.1    kardel 	sst_highwater = 1;	/* index 0 invalid, unused */
    623       1.1    kardel 	scanner = 0;
    624       1.1    kardel 
    625       1.1    kardel 	for (i = 0; i < COUNTOF(ntp_keywords); i++) {
    626       1.1    kardel 		current_keyword = ntp_keywords[i].key;
    627       1.1    kardel 		scanner =
    628       1.1    kardel 		    create_scan_states(
    629   1.1.1.5  christos 			ntp_keywords[i].key,
    630   1.1.1.5  christos 			ntp_keywords[i].token,
    631       1.1    kardel 			ntp_keywords[i].followedby,
    632       1.1    kardel 			scanner);
    633       1.1    kardel 	}
    634       1.1    kardel 
    635       1.1    kardel 	return scanner;
    636       1.1    kardel }
    637       1.1    kardel 
    638       1.1    kardel 
    639       1.1    kardel static void
    640       1.1    kardel generate_token_text(void)
    641       1.1    kardel {
    642   1.1.1.3  christos 	u_short lowest_id;
    643   1.1.1.3  christos 	u_short highest_id;
    644   1.1.1.3  christos 	u_short id_count;
    645   1.1.1.3  christos 	u_short id;
    646   1.1.1.3  christos 	u_short i;
    647       1.1    kardel 
    648       1.1    kardel 	/* sort ntp_keywords in token ID order */
    649       1.1    kardel 	qsort(ntp_keywords, COUNTOF(ntp_keywords),
    650       1.1    kardel 	      sizeof(ntp_keywords[0]), compare_key_tok_id);
    651       1.1    kardel 
    652       1.1    kardel 	lowest_id = ntp_keywords[0].token;
    653       1.1    kardel 	highest_id = ntp_keywords[COUNTOF(ntp_keywords) - 1].token;
    654       1.1    kardel 	id_count = highest_id - lowest_id + 1;
    655       1.1    kardel 
    656       1.1    kardel 	printf("#define LOWEST_KEYWORD_ID %d\n\n", lowest_id);
    657       1.1    kardel 
    658       1.1    kardel 	printf("const char * const keyword_text[%d] = {", id_count);
    659       1.1    kardel 
    660       1.1    kardel 	id = lowest_id;
    661       1.1    kardel 	i = 0;
    662       1.1    kardel 	while (i < COUNTOF(ntp_keywords)) {
    663       1.1    kardel 		while (id < ntp_keywords[i].token) {
    664       1.1    kardel 			printf(",\n\t/* %-5d %5d %20s */\tNULL",
    665       1.1    kardel 			       id - lowest_id, id, symbname(id));
    666       1.1    kardel 			id++;
    667       1.1    kardel 		}
    668       1.1    kardel 		if (i > 0)
    669       1.1    kardel 			printf(",");
    670       1.1    kardel 		printf("\n\t/* %-5d %5d %20s */\t\"%s\"",
    671   1.1.1.5  christos 		       id - lowest_id, id, symbname(id),
    672       1.1    kardel 		       ntp_keywords[i].key);
    673       1.1    kardel 		i++;
    674       1.1    kardel 		id++;
    675       1.1    kardel 	}
    676       1.1    kardel 
    677       1.1    kardel 	printf("\n};\n\n");
    678       1.1    kardel }
    679       1.1    kardel 
    680   1.1.1.5  christos 
    681       1.1    kardel int
    682       1.1    kardel compare_key_tok_id(
    683   1.1.1.3  christos 	const void *a1,
    684   1.1.1.3  christos 	const void *a2
    685       1.1    kardel 	)
    686       1.1    kardel {
    687   1.1.1.3  christos 	const struct key_tok *p1 = a1;
    688   1.1.1.3  christos 	const struct key_tok *p2 = a2;
    689       1.1    kardel 
    690       1.1    kardel 	if (p1->token == p2->token)
    691       1.1    kardel 		return 0;
    692       1.1    kardel 
    693       1.1    kardel 	if (p1->token < p2->token)
    694       1.1    kardel 		return -1;
    695       1.1    kardel 	else
    696       1.1    kardel 		return 1;
    697       1.1    kardel }
    698       1.1    kardel 
    699       1.1    kardel 
    700       1.1    kardel int
    701       1.1    kardel compare_key_tok_text(
    702   1.1.1.3  christos 	const void *a1,
    703   1.1.1.3  christos 	const void *a2
    704       1.1    kardel 	)
    705       1.1    kardel {
    706   1.1.1.3  christos 	const struct key_tok *p1 = a1;
    707   1.1.1.3  christos 	const struct key_tok *p2 = a2;
    708       1.1    kardel 
    709       1.1    kardel 	return strcmp(p1->key, p2->key);
    710       1.1    kardel }
    711       1.1    kardel 
    712       1.1    kardel 
    713       1.1    kardel /*
    714       1.1    kardel  * populate_symb() - populate symb[] lookup array with symbolic token
    715       1.1    kardel  *		     names such that symb[T_Age] == "T_Age", etc.
    716       1.1    kardel  */
    717       1.1    kardel void
    718       1.1    kardel populate_symb(
    719       1.1    kardel 	char *header_file
    720       1.1    kardel 	)
    721       1.1    kardel {
    722       1.1    kardel 	FILE *	yh;
    723   1.1.1.3  christos 	char	line[2 * MAX_TOK_LEN];
    724   1.1.1.3  christos 	char	name[2 * MAX_TOK_LEN];
    725       1.1    kardel 	int	token;
    726       1.1    kardel 
    727       1.1    kardel 	yh = fopen(header_file, "r");
    728       1.1    kardel 	if (NULL == yh) {
    729       1.1    kardel 		perror("unable to open yacc/bison header file");
    730       1.1    kardel 		exit(4);
    731       1.1    kardel 	}
    732       1.1    kardel 
    733       1.1    kardel 	while (NULL != fgets(line, sizeof(line), yh))
    734       1.1    kardel 		if (2 == sscanf(line, "#define %s %d", name, &token)
    735       1.1    kardel 		    && 'T' == name[0] && '_' == name[1] && token >= 0
    736   1.1.1.3  christos 		    && token < COUNTOF(symb)) {
    737       1.1    kardel 
    738       1.1    kardel 			symb[token] = estrdup(name);
    739   1.1.1.3  christos 			if (strlen(name) > MAX_TOK_LEN) {
    740   1.1.1.3  christos 				fprintf(stderr,
    741   1.1.1.3  christos 					"MAX_TOK_LEN %d too small for '%s'\n"
    742   1.1.1.3  christos 					"Edit keyword-gen.c to raise.\n",
    743   1.1.1.3  christos 					MAX_TOK_LEN, name);
    744   1.1.1.3  christos 				exit(10);
    745   1.1.1.3  christos 			}
    746   1.1.1.3  christos 		}
    747       1.1    kardel 	fclose(yh);
    748       1.1    kardel }
    749       1.1    kardel 
    750       1.1    kardel 
    751       1.1    kardel const char *
    752       1.1    kardel symbname(
    753   1.1.1.3  christos 	u_short token
    754       1.1    kardel 	)
    755       1.1    kardel {
    756       1.1    kardel 	char *name;
    757       1.1    kardel 
    758   1.1.1.3  christos 	if (token < COUNTOF(symb) && symb[token] != NULL) {
    759   1.1.1.3  christos 		name = symb[token];
    760   1.1.1.3  christos 	} else {
    761   1.1.1.3  christos 		LIB_GETBUF(name);
    762   1.1.1.3  christos 		snprintf(name, LIB_BUFLENGTH, "%d", token);
    763   1.1.1.5  christos 	}
    764       1.1    kardel 
    765       1.1    kardel 	return name;
    766       1.1    kardel }
    767