xref: /titanic_50/usr/src/cmd/localedef/scanner.c (revision 5aec55eb0591d2fcdd38d7dd5408a6ff3456e596)
16b5e5868SGarrett D'Amore /*
26b5e5868SGarrett D'Amore  * This file and its contents are supplied under the terms of the
36b5e5868SGarrett D'Amore  * Common Development and Distribution License ("CDDL"), version 1.0.
4*5aec55ebSGarrett D'Amore  * You may only use this file in accordance with the terms of version
5*5aec55ebSGarrett D'Amore  * 1.0 of the CDDL.
66b5e5868SGarrett D'Amore  *
76b5e5868SGarrett D'Amore  * A full copy of the text of the CDDL should have accompanied this
86b5e5868SGarrett D'Amore  * source.  A copy of the CDDL is also available via the Internet at
96b5e5868SGarrett D'Amore  * http://www.illumos.org/license/CDDL.
106b5e5868SGarrett D'Amore  */
116b5e5868SGarrett D'Amore 
126b5e5868SGarrett D'Amore /*
136b5e5868SGarrett D'Amore  * Copyright 2010 Nexenta Systems, Inc.  All rights reserved.
146b5e5868SGarrett D'Amore  */
156b5e5868SGarrett D'Amore 
166b5e5868SGarrett D'Amore /*
176b5e5868SGarrett D'Amore  * This file contains the "scanner", which tokenizes the input files
186b5e5868SGarrett D'Amore  * for localedef for processing by the higher level grammar processor.
196b5e5868SGarrett D'Amore  */
206b5e5868SGarrett D'Amore 
216b5e5868SGarrett D'Amore #include <stdio.h>
226b5e5868SGarrett D'Amore #include <stdlib.h>
236b5e5868SGarrett D'Amore #include <ctype.h>
246b5e5868SGarrett D'Amore #include <limits.h>
256b5e5868SGarrett D'Amore #include <string.h>
266b5e5868SGarrett D'Amore #include <widec.h>
276b5e5868SGarrett D'Amore #include <sys/types.h>
286b5e5868SGarrett D'Amore #include <assert.h>
296b5e5868SGarrett D'Amore #include "localedef.h"
306b5e5868SGarrett D'Amore #include "parser.tab.h"
316b5e5868SGarrett D'Amore 
326b5e5868SGarrett D'Amore int			com_char = '#';
336b5e5868SGarrett D'Amore int			esc_char = '\\';
346b5e5868SGarrett D'Amore int			mb_cur_min = 1;
356b5e5868SGarrett D'Amore int			mb_cur_max = 1;
366b5e5868SGarrett D'Amore int			lineno = 1;
376b5e5868SGarrett D'Amore int			warnings = 0;
386b5e5868SGarrett D'Amore static int		nextline;
396b5e5868SGarrett D'Amore static FILE		*input = stdin;
406b5e5868SGarrett D'Amore static const char	*filename = "<stdin>";
416b5e5868SGarrett D'Amore static int		instring = 0;
426b5e5868SGarrett D'Amore static int		escaped = 0;
436b5e5868SGarrett D'Amore 
446b5e5868SGarrett D'Amore /*
456b5e5868SGarrett D'Amore  * Token space ... grows on demand.
466b5e5868SGarrett D'Amore  */
476b5e5868SGarrett D'Amore static char *token = NULL;
486b5e5868SGarrett D'Amore static int tokidx;
496b5e5868SGarrett D'Amore static int toksz = 0;
506b5e5868SGarrett D'Amore static int hadtok = 0;
516b5e5868SGarrett D'Amore 
526b5e5868SGarrett D'Amore /*
536b5e5868SGarrett D'Amore  * Wide string space ... grows on demand.
546b5e5868SGarrett D'Amore  */
556b5e5868SGarrett D'Amore static wchar_t *widestr = NULL;
566b5e5868SGarrett D'Amore static int wideidx = 0;
576b5e5868SGarrett D'Amore static int widesz = 0;
586b5e5868SGarrett D'Amore 
596b5e5868SGarrett D'Amore /*
606b5e5868SGarrett D'Amore  * The last keyword seen.  This is useful to trigger the special lexer rules
616b5e5868SGarrett D'Amore  * for "copy" and also collating symbols and elements.
626b5e5868SGarrett D'Amore  */
636b5e5868SGarrett D'Amore int	last_kw = 0;
646b5e5868SGarrett D'Amore static int	category = T_END;
656b5e5868SGarrett D'Amore 
666b5e5868SGarrett D'Amore static struct token {
676b5e5868SGarrett D'Amore 	int id;
686b5e5868SGarrett D'Amore 	const char *name;
696b5e5868SGarrett D'Amore } keywords[] = {
706b5e5868SGarrett D'Amore 	{ T_COM_CHAR,		"comment_char" },
716b5e5868SGarrett D'Amore 	{ T_ESC_CHAR,		"escape_char" },
726b5e5868SGarrett D'Amore 	{ T_END,		"END" },
736b5e5868SGarrett D'Amore 	{ T_COPY,		"copy" },
746b5e5868SGarrett D'Amore 	{ T_MESSAGES,		"LC_MESSAGES" },
756b5e5868SGarrett D'Amore 	{ T_YESSTR,		"yesstr" },
766b5e5868SGarrett D'Amore 	{ T_YESEXPR,		"yesexpr" },
776b5e5868SGarrett D'Amore 	{ T_NOSTR,		"nostr" },
786b5e5868SGarrett D'Amore 	{ T_NOEXPR,		"noexpr" },
796b5e5868SGarrett D'Amore 	{ T_MONETARY,		"LC_MONETARY" },
806b5e5868SGarrett D'Amore 	{ T_INT_CURR_SYMBOL,	"int_curr_symbol" },
816b5e5868SGarrett D'Amore 	{ T_CURRENCY_SYMBOL,	"currency_symbol" },
826b5e5868SGarrett D'Amore 	{ T_MON_DECIMAL_POINT,	"mon_decimal_point" },
836b5e5868SGarrett D'Amore 	{ T_MON_THOUSANDS_SEP,	"mon_thousands_sep" },
846b5e5868SGarrett D'Amore 	{ T_POSITIVE_SIGN,	"positive_sign" },
856b5e5868SGarrett D'Amore 	{ T_NEGATIVE_SIGN,	"negative_sign" },
866b5e5868SGarrett D'Amore 	{ T_MON_GROUPING,	"mon_grouping" },
876b5e5868SGarrett D'Amore 	{ T_INT_FRAC_DIGITS,	"int_frac_digits" },
886b5e5868SGarrett D'Amore 	{ T_FRAC_DIGITS,	"frac_digits" },
896b5e5868SGarrett D'Amore 	{ T_P_CS_PRECEDES,	"p_cs_precedes" },
906b5e5868SGarrett D'Amore 	{ T_P_SEP_BY_SPACE,	"p_sep_by_space" },
916b5e5868SGarrett D'Amore 	{ T_N_CS_PRECEDES,	"n_cs_precedes" },
926b5e5868SGarrett D'Amore 	{ T_N_SEP_BY_SPACE,	"n_sep_by_space" },
936b5e5868SGarrett D'Amore 	{ T_P_SIGN_POSN,	"p_sign_posn" },
946b5e5868SGarrett D'Amore 	{ T_N_SIGN_POSN,	"n_sign_posn" },
956b5e5868SGarrett D'Amore 	{ T_INT_P_CS_PRECEDES,	"int_p_cs_precedes" },
966b5e5868SGarrett D'Amore 	{ T_INT_N_CS_PRECEDES,	"int_n_cs_precedes" },
976b5e5868SGarrett D'Amore 	{ T_INT_P_SEP_BY_SPACE,	"int_p_sep_by_space" },
986b5e5868SGarrett D'Amore 	{ T_INT_N_SEP_BY_SPACE,	"int_n_sep_by_space" },
996b5e5868SGarrett D'Amore 	{ T_INT_P_SIGN_POSN,	"int_p_sign_posn" },
1006b5e5868SGarrett D'Amore 	{ T_INT_N_SIGN_POSN,	"int_n_sign_posn" },
1016b5e5868SGarrett D'Amore 	{ T_COLLATE,		"LC_COLLATE" },
1026b5e5868SGarrett D'Amore 	{ T_COLLATING_SYMBOL,	"collating-symbol" },
1036b5e5868SGarrett D'Amore 	{ T_COLLATING_ELEMENT,	"collating-element" },
1046b5e5868SGarrett D'Amore 	{ T_FROM,		"from" },
1056b5e5868SGarrett D'Amore 	{ T_ORDER_START,	"order_start" },
1066b5e5868SGarrett D'Amore 	{ T_ORDER_END,		"order_end" },
1076b5e5868SGarrett D'Amore 	{ T_FORWARD,		"forward" },
1086b5e5868SGarrett D'Amore 	{ T_BACKWARD,		"backward" },
1096b5e5868SGarrett D'Amore 	{ T_POSITION,		"position" },
1106b5e5868SGarrett D'Amore 	{ T_IGNORE,		"IGNORE" },
1116b5e5868SGarrett D'Amore 	{ T_UNDEFINED,		"UNDEFINED" },
1126b5e5868SGarrett D'Amore 	{ T_NUMERIC,		"LC_NUMERIC" },
1136b5e5868SGarrett D'Amore 	{ T_DECIMAL_POINT,	"decimal_point" },
1146b5e5868SGarrett D'Amore 	{ T_THOUSANDS_SEP,	"thousands_sep" },
1156b5e5868SGarrett D'Amore 	{ T_GROUPING,		"grouping" },
1166b5e5868SGarrett D'Amore 	{ T_TIME,		"LC_TIME" },
1176b5e5868SGarrett D'Amore 	{ T_ABDAY,		"abday" },
1186b5e5868SGarrett D'Amore 	{ T_DAY,		"day" },
1196b5e5868SGarrett D'Amore 	{ T_ABMON,		"abmon" },
1206b5e5868SGarrett D'Amore 	{ T_MON,		"mon" },
1216b5e5868SGarrett D'Amore 	{ T_D_T_FMT,		"d_t_fmt" },
1226b5e5868SGarrett D'Amore 	{ T_D_FMT,		"d_fmt" },
1236b5e5868SGarrett D'Amore 	{ T_T_FMT,		"t_fmt" },
1246b5e5868SGarrett D'Amore 	{ T_AM_PM,		"am_pm" },
1256b5e5868SGarrett D'Amore 	{ T_T_FMT_AMPM,		"t_fmt_ampm" },
1266b5e5868SGarrett D'Amore 	{ T_ERA,		"era" },
1276b5e5868SGarrett D'Amore 	{ T_ERA_D_FMT,		"era_d_fmt" },
1286b5e5868SGarrett D'Amore 	{ T_ERA_T_FMT,		"era_t_fmt" },
1296b5e5868SGarrett D'Amore 	{ T_ERA_D_T_FMT,	"era_d_t_fmt" },
1306b5e5868SGarrett D'Amore 	{ T_ALT_DIGITS,		"alt_digits" },
1316b5e5868SGarrett D'Amore 	{ T_CTYPE,		"LC_CTYPE" },
1326b5e5868SGarrett D'Amore 	{ T_ISUPPER,		"upper" },
1336b5e5868SGarrett D'Amore 	{ T_ISLOWER,		"lower" },
1346b5e5868SGarrett D'Amore 	{ T_ISALPHA,		"alpha" },
1356b5e5868SGarrett D'Amore 	{ T_ISDIGIT,		"digit" },
1366b5e5868SGarrett D'Amore 	{ T_ISPUNCT,		"punct" },
1376b5e5868SGarrett D'Amore 	{ T_ISXDIGIT,		"xdigit" },
1386b5e5868SGarrett D'Amore 	{ T_ISSPACE,		"space" },
1396b5e5868SGarrett D'Amore 	{ T_ISPRINT,		"print" },
1406b5e5868SGarrett D'Amore 	{ T_ISGRAPH,		"graph" },
1416b5e5868SGarrett D'Amore 	{ T_ISBLANK,		"blank" },
1426b5e5868SGarrett D'Amore 	{ T_ISCNTRL,		"cntrl" },
1436b5e5868SGarrett D'Amore 	/*
1446b5e5868SGarrett D'Amore 	 * These entries are local additions, and not specified by
1456b5e5868SGarrett D'Amore 	 * TOG.  Note that they are not guaranteed to be accurate for
1466b5e5868SGarrett D'Amore 	 * all locales, and so applications should not depend on them.
1476b5e5868SGarrett D'Amore 	 */
1486b5e5868SGarrett D'Amore 	{ T_ISSPECIAL,		"special" },
1496b5e5868SGarrett D'Amore 	{ T_ISENGLISH,		"english" },
1506b5e5868SGarrett D'Amore 	{ T_ISPHONOGRAM,	"phonogram" },
1516b5e5868SGarrett D'Amore 	{ T_ISIDEOGRAM,		"ideogram" },
1526b5e5868SGarrett D'Amore 	{ T_ISNUMBER,		"number" },
1536b5e5868SGarrett D'Amore 	/*
1546b5e5868SGarrett D'Amore 	 * We have to support this in the grammar, but it would be a
1556b5e5868SGarrett D'Amore 	 * syntax error to define a character as one of these without
1566b5e5868SGarrett D'Amore 	 * also defining it as an alpha or digit.  We ignore it in our
1576b5e5868SGarrett D'Amore 	 * parsing.
1586b5e5868SGarrett D'Amore 	 */
1596b5e5868SGarrett D'Amore 	{ T_ISALNUM,		"alnum" },
1606b5e5868SGarrett D'Amore 	{ T_TOUPPER,		"toupper" },
1616b5e5868SGarrett D'Amore 	{ T_TOLOWER,		"tolower" },
1626b5e5868SGarrett D'Amore 
1636b5e5868SGarrett D'Amore 	/*
1646b5e5868SGarrett D'Amore 	 * These are keywords used in the charmap file.  Note that
1656b5e5868SGarrett D'Amore 	 * Solaris orginally used angle brackets to wrap some of them,
1666b5e5868SGarrett D'Amore 	 * but we removed that to simplify our parser.  The first of these
1676b5e5868SGarrett D'Amore 	 * items are "global items."
1686b5e5868SGarrett D'Amore 	 */
1696b5e5868SGarrett D'Amore 	{ T_CHARMAP,		"CHARMAP" },
1706b5e5868SGarrett D'Amore 	{ T_WIDTH,		"WIDTH" },
1716b5e5868SGarrett D'Amore 	{ T_WIDTH_DEFAULT,	"WIDTH_DEFAULT" },
1726b5e5868SGarrett D'Amore 
1736b5e5868SGarrett D'Amore 	{ -1, NULL },
1746b5e5868SGarrett D'Amore };
1756b5e5868SGarrett D'Amore 
1766b5e5868SGarrett D'Amore /*
1776b5e5868SGarrett D'Amore  * These special words are only used in a charmap file, enclosed in <>.
1786b5e5868SGarrett D'Amore  */
1796b5e5868SGarrett D'Amore static struct token symwords[] = {
1806b5e5868SGarrett D'Amore 	{ T_COM_CHAR,		"comment_char" },
1816b5e5868SGarrett D'Amore 	{ T_ESC_CHAR,		"escape_char" },
1826b5e5868SGarrett D'Amore 	{ T_CODE_SET,		"code_set_name" },
1836b5e5868SGarrett D'Amore 	{ T_MB_CUR_MAX,		"mb_cur_max" },
1846b5e5868SGarrett D'Amore 	{ T_MB_CUR_MIN,		"mb_cur_min" },
1856b5e5868SGarrett D'Amore 	{ -1, NULL },
1866b5e5868SGarrett D'Amore };
1876b5e5868SGarrett D'Amore 
1886b5e5868SGarrett D'Amore static int categories[] = {
1896b5e5868SGarrett D'Amore 	T_CHARMAP,
1906b5e5868SGarrett D'Amore 	T_CTYPE,
1916b5e5868SGarrett D'Amore 	T_COLLATE,
1926b5e5868SGarrett D'Amore 	T_MESSAGES,
1936b5e5868SGarrett D'Amore 	T_MONETARY,
1946b5e5868SGarrett D'Amore 	T_NUMERIC,
1956b5e5868SGarrett D'Amore 	T_TIME,
1966b5e5868SGarrett D'Amore 	0
1976b5e5868SGarrett D'Amore };
1986b5e5868SGarrett D'Amore 
1996b5e5868SGarrett D'Amore void
2006b5e5868SGarrett D'Amore reset_scanner(const char *fname)
2016b5e5868SGarrett D'Amore {
2026b5e5868SGarrett D'Amore 	if (fname == NULL) {
2036b5e5868SGarrett D'Amore 		filename = "<stdin>";
2046b5e5868SGarrett D'Amore 		input = stdin;
2056b5e5868SGarrett D'Amore 	} else {
2066b5e5868SGarrett D'Amore 		if (input != stdin)
2076b5e5868SGarrett D'Amore 			(void) fclose(input);
2086b5e5868SGarrett D'Amore 		if ((input = fopen(fname, "r")) == NULL) {
2096b5e5868SGarrett D'Amore 			perror("fopen");
2106b5e5868SGarrett D'Amore 			exit(4);
2116b5e5868SGarrett D'Amore 		}
2126b5e5868SGarrett D'Amore 		filename = fname;
2136b5e5868SGarrett D'Amore 	}
2146b5e5868SGarrett D'Amore 	com_char = '#';
2156b5e5868SGarrett D'Amore 	esc_char = '\\';
2166b5e5868SGarrett D'Amore 	instring = 0;
2176b5e5868SGarrett D'Amore 	escaped = 0;
2186b5e5868SGarrett D'Amore 	lineno = 1;
2196b5e5868SGarrett D'Amore 	nextline = 1;
2206b5e5868SGarrett D'Amore 	tokidx = 0;
2216b5e5868SGarrett D'Amore 	wideidx = 0;
2226b5e5868SGarrett D'Amore }
2236b5e5868SGarrett D'Amore 
2246b5e5868SGarrett D'Amore #define	hex(x)	\
2256b5e5868SGarrett D'Amore 	(isdigit(x) ? (x - '0') : ((islower(x) ? (x - 'a') : (x - 'A')) + 10))
2266b5e5868SGarrett D'Amore #define	isodigit(x)	((x >= '0') && (x <= '7'))
2276b5e5868SGarrett D'Amore 
2286b5e5868SGarrett D'Amore static int
2296b5e5868SGarrett D'Amore scanc(void)
2306b5e5868SGarrett D'Amore {
2316b5e5868SGarrett D'Amore 	int	c;
2326b5e5868SGarrett D'Amore 
2336b5e5868SGarrett D'Amore 	c = getc(input);
2346b5e5868SGarrett D'Amore 	lineno = nextline;
2356b5e5868SGarrett D'Amore 	if (c == '\n') {
2366b5e5868SGarrett D'Amore 		nextline++;
2376b5e5868SGarrett D'Amore 	}
2386b5e5868SGarrett D'Amore 	return (c);
2396b5e5868SGarrett D'Amore }
2406b5e5868SGarrett D'Amore 
2416b5e5868SGarrett D'Amore static void
2426b5e5868SGarrett D'Amore unscanc(int c)
2436b5e5868SGarrett D'Amore {
2446b5e5868SGarrett D'Amore 	if (c == '\n') {
2456b5e5868SGarrett D'Amore 		nextline--;
2466b5e5868SGarrett D'Amore 	}
2476b5e5868SGarrett D'Amore 	if (ungetc(c, input) < 0) {
2486b5e5868SGarrett D'Amore 		yyerror(_("ungetc failed"));
2496b5e5868SGarrett D'Amore 	}
2506b5e5868SGarrett D'Amore }
2516b5e5868SGarrett D'Amore 
2526b5e5868SGarrett D'Amore static int
2536b5e5868SGarrett D'Amore scan_hex_byte(void)
2546b5e5868SGarrett D'Amore {
2556b5e5868SGarrett D'Amore 	int	c1, c2;
2566b5e5868SGarrett D'Amore 	int	v;
2576b5e5868SGarrett D'Amore 
2586b5e5868SGarrett D'Amore 	c1 = scanc();
2596b5e5868SGarrett D'Amore 	if (!isxdigit(c1)) {
2606b5e5868SGarrett D'Amore 		yyerror(_("malformed hex digit"));
2616b5e5868SGarrett D'Amore 		return (0);
2626b5e5868SGarrett D'Amore 	}
2636b5e5868SGarrett D'Amore 	c2 = scanc();
2646b5e5868SGarrett D'Amore 	if (!isxdigit(c2)) {
2656b5e5868SGarrett D'Amore 		yyerror(_("malformed hex digit"));
2666b5e5868SGarrett D'Amore 		return (0);
2676b5e5868SGarrett D'Amore 	}
2686b5e5868SGarrett D'Amore 	v = ((hex(c1) << 4) | hex(c2));
2696b5e5868SGarrett D'Amore 	return (v);
2706b5e5868SGarrett D'Amore }
2716b5e5868SGarrett D'Amore 
2726b5e5868SGarrett D'Amore static int
2736b5e5868SGarrett D'Amore scan_dec_byte(void)
2746b5e5868SGarrett D'Amore {
2756b5e5868SGarrett D'Amore 	int	c1, c2, c3;
2766b5e5868SGarrett D'Amore 	int	b;
2776b5e5868SGarrett D'Amore 
2786b5e5868SGarrett D'Amore 	c1 = scanc();
2796b5e5868SGarrett D'Amore 	if (!isdigit(c1)) {
2806b5e5868SGarrett D'Amore 		yyerror(_("malformed decimal digit"));
2816b5e5868SGarrett D'Amore 		return (0);
2826b5e5868SGarrett D'Amore 	}
2836b5e5868SGarrett D'Amore 	b = c1 - '0';
2846b5e5868SGarrett D'Amore 	c2 = scanc();
2856b5e5868SGarrett D'Amore 	if (!isdigit(c2)) {
2866b5e5868SGarrett D'Amore 		yyerror(_("malformed decimal digit"));
2876b5e5868SGarrett D'Amore 		return (0);
2886b5e5868SGarrett D'Amore 	}
2896b5e5868SGarrett D'Amore 	b *= 10;
2906b5e5868SGarrett D'Amore 	b += (c2 - '0');
2916b5e5868SGarrett D'Amore 	c3 = scanc();
2926b5e5868SGarrett D'Amore 	if (!isdigit(c3)) {
2936b5e5868SGarrett D'Amore 		unscanc(c3);
2946b5e5868SGarrett D'Amore 	} else {
2956b5e5868SGarrett D'Amore 		b *= 10;
2966b5e5868SGarrett D'Amore 		b += (c3 - '0');
2976b5e5868SGarrett D'Amore 	}
2986b5e5868SGarrett D'Amore 	return (b);
2996b5e5868SGarrett D'Amore }
3006b5e5868SGarrett D'Amore 
3016b5e5868SGarrett D'Amore static int
3026b5e5868SGarrett D'Amore scan_oct_byte(void)
3036b5e5868SGarrett D'Amore {
3046b5e5868SGarrett D'Amore 	int c1, c2, c3;
3056b5e5868SGarrett D'Amore 	int	b;
3066b5e5868SGarrett D'Amore 
3076b5e5868SGarrett D'Amore 	b = 0;
3086b5e5868SGarrett D'Amore 
3096b5e5868SGarrett D'Amore 	c1 = scanc();
3106b5e5868SGarrett D'Amore 	if (!isodigit(c1)) {
3116b5e5868SGarrett D'Amore 		yyerror(_("malformed octal digit"));
3126b5e5868SGarrett D'Amore 		return (0);
3136b5e5868SGarrett D'Amore 	}
3146b5e5868SGarrett D'Amore 	b = c1 - '0';
3156b5e5868SGarrett D'Amore 	c2 = scanc();
3166b5e5868SGarrett D'Amore 	if (!isodigit(c2)) {
3176b5e5868SGarrett D'Amore 		yyerror(_("malformed octal digit"));
3186b5e5868SGarrett D'Amore 		return (0);
3196b5e5868SGarrett D'Amore 	}
3206b5e5868SGarrett D'Amore 	b *= 8;
3216b5e5868SGarrett D'Amore 	b += (c2 - '0');
3226b5e5868SGarrett D'Amore 	c3 = scanc();
3236b5e5868SGarrett D'Amore 	if (!isodigit(c3)) {
3246b5e5868SGarrett D'Amore 		unscanc(c3);
3256b5e5868SGarrett D'Amore 	} else {
3266b5e5868SGarrett D'Amore 		b *= 8;
3276b5e5868SGarrett D'Amore 		b += (c3 - '0');
3286b5e5868SGarrett D'Amore 	}
3296b5e5868SGarrett D'Amore 	return (b);
3306b5e5868SGarrett D'Amore }
3316b5e5868SGarrett D'Amore 
3326b5e5868SGarrett D'Amore void
3336b5e5868SGarrett D'Amore add_tok(int c)
3346b5e5868SGarrett D'Amore {
3356b5e5868SGarrett D'Amore 	if ((tokidx + 1) >= toksz) {
3366b5e5868SGarrett D'Amore 		toksz += 64;
3376b5e5868SGarrett D'Amore 		if ((token = realloc(token, toksz)) == NULL) {
3386b5e5868SGarrett D'Amore 			yyerror(_("out of memory"));
3396b5e5868SGarrett D'Amore 			tokidx = 0;
3406b5e5868SGarrett D'Amore 			toksz = 0;
3416b5e5868SGarrett D'Amore 			return;
3426b5e5868SGarrett D'Amore 		}
3436b5e5868SGarrett D'Amore 	}
3446b5e5868SGarrett D'Amore 
3456b5e5868SGarrett D'Amore 	token[tokidx++] = (char)c;
3466b5e5868SGarrett D'Amore 	token[tokidx] = 0;
3476b5e5868SGarrett D'Amore }
3486b5e5868SGarrett D'Amore void
3496b5e5868SGarrett D'Amore add_wcs(wchar_t c)
3506b5e5868SGarrett D'Amore {
3516b5e5868SGarrett D'Amore 	if ((wideidx + 1) >= widesz) {
3526b5e5868SGarrett D'Amore 		widesz += 64;
3536b5e5868SGarrett D'Amore 		widestr = realloc(widestr, (widesz * sizeof (wchar_t)));
3546b5e5868SGarrett D'Amore 		if (widestr == NULL) {
3556b5e5868SGarrett D'Amore 			yyerror(_("out of memory"));
3566b5e5868SGarrett D'Amore 			wideidx = 0;
3576b5e5868SGarrett D'Amore 			widesz = 0;
3586b5e5868SGarrett D'Amore 			return;
3596b5e5868SGarrett D'Amore 		}
3606b5e5868SGarrett D'Amore 	}
3616b5e5868SGarrett D'Amore 
3626b5e5868SGarrett D'Amore 	widestr[wideidx++] = c;
3636b5e5868SGarrett D'Amore 	widestr[wideidx] = 0;
3646b5e5868SGarrett D'Amore }
3656b5e5868SGarrett D'Amore 
3666b5e5868SGarrett D'Amore wchar_t *
3676b5e5868SGarrett D'Amore get_wcs(void)
3686b5e5868SGarrett D'Amore {
3696b5e5868SGarrett D'Amore 	wchar_t *ws = widestr;
3706b5e5868SGarrett D'Amore 	wideidx = 0;
3716b5e5868SGarrett D'Amore 	widestr = NULL;
3726b5e5868SGarrett D'Amore 	widesz = 0;
3736b5e5868SGarrett D'Amore 	if (ws == NULL) {
3746b5e5868SGarrett D'Amore 		if ((ws = wsdup(L"")) == NULL) {
3756b5e5868SGarrett D'Amore 			yyerror(_("out of memory"));
3766b5e5868SGarrett D'Amore 		}
3776b5e5868SGarrett D'Amore 	}
3786b5e5868SGarrett D'Amore 	return (ws);
3796b5e5868SGarrett D'Amore }
3806b5e5868SGarrett D'Amore 
3816b5e5868SGarrett D'Amore static int
3826b5e5868SGarrett D'Amore get_byte(void)
3836b5e5868SGarrett D'Amore {
3846b5e5868SGarrett D'Amore 	int	c;
3856b5e5868SGarrett D'Amore 
3866b5e5868SGarrett D'Amore 	if ((c = scanc()) != esc_char) {
3876b5e5868SGarrett D'Amore 		unscanc(c);
3886b5e5868SGarrett D'Amore 		return (EOF);
3896b5e5868SGarrett D'Amore 	}
3906b5e5868SGarrett D'Amore 	c = scanc();
3916b5e5868SGarrett D'Amore 
3926b5e5868SGarrett D'Amore 	switch (c) {
3936b5e5868SGarrett D'Amore 	case 'd':
3946b5e5868SGarrett D'Amore 	case 'D':
3956b5e5868SGarrett D'Amore 		return (scan_dec_byte());
3966b5e5868SGarrett D'Amore 	case 'x':
3976b5e5868SGarrett D'Amore 	case 'X':
3986b5e5868SGarrett D'Amore 		return (scan_hex_byte());
3996b5e5868SGarrett D'Amore 	case '0':
4006b5e5868SGarrett D'Amore 	case '1':
4016b5e5868SGarrett D'Amore 	case '2':
4026b5e5868SGarrett D'Amore 	case '3':
4036b5e5868SGarrett D'Amore 	case '4':
4046b5e5868SGarrett D'Amore 	case '5':
4056b5e5868SGarrett D'Amore 	case '6':
4066b5e5868SGarrett D'Amore 	case '7':
4076b5e5868SGarrett D'Amore 		/* put the character back so we can get it */
4086b5e5868SGarrett D'Amore 		unscanc(c);
4096b5e5868SGarrett D'Amore 		return (scan_oct_byte());
4106b5e5868SGarrett D'Amore 	default:
4116b5e5868SGarrett D'Amore 		unscanc(c);
4126b5e5868SGarrett D'Amore 		unscanc(esc_char);
4136b5e5868SGarrett D'Amore 		return (EOF);
4146b5e5868SGarrett D'Amore 	}
4156b5e5868SGarrett D'Amore }
4166b5e5868SGarrett D'Amore 
4176b5e5868SGarrett D'Amore int
4186b5e5868SGarrett D'Amore get_escaped(int c)
4196b5e5868SGarrett D'Amore {
4206b5e5868SGarrett D'Amore 	switch (c) {
4216b5e5868SGarrett D'Amore 	case 'n':
4226b5e5868SGarrett D'Amore 		return ('\n');
4236b5e5868SGarrett D'Amore 	case 'r':
4246b5e5868SGarrett D'Amore 		return ('\r');
4256b5e5868SGarrett D'Amore 	case 't':
4266b5e5868SGarrett D'Amore 		return ('\t');
4276b5e5868SGarrett D'Amore 	case 'f':
4286b5e5868SGarrett D'Amore 		return ('\f');
4296b5e5868SGarrett D'Amore 	case 'v':
4306b5e5868SGarrett D'Amore 		return ('\v');
4316b5e5868SGarrett D'Amore 	case 'b':
4326b5e5868SGarrett D'Amore 		return ('\b');
4336b5e5868SGarrett D'Amore 	case 'a':
4346b5e5868SGarrett D'Amore 		return ('\a');
4356b5e5868SGarrett D'Amore 	default:
4366b5e5868SGarrett D'Amore 		return (c);
4376b5e5868SGarrett D'Amore 	}
4386b5e5868SGarrett D'Amore }
4396b5e5868SGarrett D'Amore 
4406b5e5868SGarrett D'Amore int
4416b5e5868SGarrett D'Amore get_wide(void)
4426b5e5868SGarrett D'Amore {
4436b5e5868SGarrett D'Amore 	static char mbs[MB_LEN_MAX + 1] = "";
4446b5e5868SGarrett D'Amore 	static int mbi = 0;
4456b5e5868SGarrett D'Amore 	int c;
4466b5e5868SGarrett D'Amore 	wchar_t	wc;
4476b5e5868SGarrett D'Amore 
4486b5e5868SGarrett D'Amore 	if (mb_cur_max >= sizeof (mbs)) {
4496b5e5868SGarrett D'Amore 		yyerror(_("max multibyte character size too big"));
4506b5e5868SGarrett D'Amore 		mbi = 0;
4516b5e5868SGarrett D'Amore 		return (T_NULL);
4526b5e5868SGarrett D'Amore 	}
4536b5e5868SGarrett D'Amore 	for (;;) {
4546b5e5868SGarrett D'Amore 		if ((mbi == mb_cur_max) || ((c = get_byte()) == EOF)) {
4556b5e5868SGarrett D'Amore 			/*
4566b5e5868SGarrett D'Amore 			 * end of the byte sequence reached, but no
4576b5e5868SGarrett D'Amore 			 * valid wide decoding.  fatal error.
4586b5e5868SGarrett D'Amore 			 */
4596b5e5868SGarrett D'Amore 			mbi = 0;
4606b5e5868SGarrett D'Amore 			yyerror(_("not a valid character encoding"));
4616b5e5868SGarrett D'Amore 			return (T_NULL);
4626b5e5868SGarrett D'Amore 		}
4636b5e5868SGarrett D'Amore 		mbs[mbi++] = c;
4646b5e5868SGarrett D'Amore 		mbs[mbi] = 0;
4656b5e5868SGarrett D'Amore 
4666b5e5868SGarrett D'Amore 		/* does it decode? */
4676b5e5868SGarrett D'Amore 		if (to_wide(&wc, mbs) >= 0) {
4686b5e5868SGarrett D'Amore 			break;
4696b5e5868SGarrett D'Amore 		}
4706b5e5868SGarrett D'Amore 	}
4716b5e5868SGarrett D'Amore 
4726b5e5868SGarrett D'Amore 	mbi = 0;
4736b5e5868SGarrett D'Amore 	if (category != T_CHARMAP) {
4746b5e5868SGarrett D'Amore 		if (check_charmap(wc) < 0) {
4756b5e5868SGarrett D'Amore 			yyerror(_("no symbolic name for character"));
4766b5e5868SGarrett D'Amore 			return (T_NULL);
4776b5e5868SGarrett D'Amore 		}
4786b5e5868SGarrett D'Amore 	}
4796b5e5868SGarrett D'Amore 
4806b5e5868SGarrett D'Amore 	yylval.wc = wc;
4816b5e5868SGarrett D'Amore 	return (T_CHAR);
4826b5e5868SGarrett D'Amore }
4836b5e5868SGarrett D'Amore 
4846b5e5868SGarrett D'Amore int
4856b5e5868SGarrett D'Amore get_symbol(void)
4866b5e5868SGarrett D'Amore {
4876b5e5868SGarrett D'Amore 	int	c;
4886b5e5868SGarrett D'Amore 
4896b5e5868SGarrett D'Amore 	while ((c = scanc()) != EOF) {
4906b5e5868SGarrett D'Amore 		if (escaped) {
4916b5e5868SGarrett D'Amore 			escaped = 0;
4926b5e5868SGarrett D'Amore 			if (c == '\n')
4936b5e5868SGarrett D'Amore 				continue;
4946b5e5868SGarrett D'Amore 			add_tok(get_escaped(c));
4956b5e5868SGarrett D'Amore 			continue;
4966b5e5868SGarrett D'Amore 		}
4976b5e5868SGarrett D'Amore 		if (c == esc_char) {
4986b5e5868SGarrett D'Amore 			escaped = 1;
4996b5e5868SGarrett D'Amore 			continue;
5006b5e5868SGarrett D'Amore 		}
5016b5e5868SGarrett D'Amore 		if (c == '\n') {	/* well that's strange! */
5026b5e5868SGarrett D'Amore 			yyerror(_("unterminated symbolic name"));
5036b5e5868SGarrett D'Amore 			continue;
5046b5e5868SGarrett D'Amore 		}
5056b5e5868SGarrett D'Amore 		if (c == '>') {		/* end of symbol */
5066b5e5868SGarrett D'Amore 
5076b5e5868SGarrett D'Amore 			/*
5086b5e5868SGarrett D'Amore 			 * This restarts the token from the beginning
5096b5e5868SGarrett D'Amore 			 * the next time we scan a character.  (This
5106b5e5868SGarrett D'Amore 			 * token is complete.)
5116b5e5868SGarrett D'Amore 			 */
5126b5e5868SGarrett D'Amore 
5136b5e5868SGarrett D'Amore 			if (token == NULL) {
5146b5e5868SGarrett D'Amore 				yyerror(_("missing symbolic name"));
5156b5e5868SGarrett D'Amore 				return (T_NULL);
5166b5e5868SGarrett D'Amore 			}
5176b5e5868SGarrett D'Amore 			tokidx = 0;
5186b5e5868SGarrett D'Amore 
5196b5e5868SGarrett D'Amore 			/*
5206b5e5868SGarrett D'Amore 			 * A few symbols are handled as keywords outside
5216b5e5868SGarrett D'Amore 			 * of the normal categories.
5226b5e5868SGarrett D'Amore 			 */
5236b5e5868SGarrett D'Amore 			if (category == T_END) {
5246b5e5868SGarrett D'Amore 				int i;
5256b5e5868SGarrett D'Amore 				for (i = 0; symwords[i].name != 0; i++) {
5266b5e5868SGarrett D'Amore 					if (strcmp(token, symwords[i].name) ==
5276b5e5868SGarrett D'Amore 					    0) {
5286b5e5868SGarrett D'Amore 						last_kw = symwords[i].id;
5296b5e5868SGarrett D'Amore 						return (last_kw);
5306b5e5868SGarrett D'Amore 					}
5316b5e5868SGarrett D'Amore 				}
5326b5e5868SGarrett D'Amore 			}
5336b5e5868SGarrett D'Amore 			/*
5346b5e5868SGarrett D'Amore 			 * Contextual rule: Only literal characters are
5356b5e5868SGarrett D'Amore 			 * permitted in CHARMAP.  Anywhere else the symbolic
5366b5e5868SGarrett D'Amore 			 * forms are fine.
5376b5e5868SGarrett D'Amore 			 */
5386b5e5868SGarrett D'Amore 			if ((category != T_CHARMAP) &&
5396b5e5868SGarrett D'Amore 			    (lookup_charmap(token, &yylval.wc)) != -1) {
5406b5e5868SGarrett D'Amore 				return (T_CHAR);
5416b5e5868SGarrett D'Amore 			}
5426b5e5868SGarrett D'Amore 			if ((yylval.collsym = lookup_collsym(token)) != NULL) {
5436b5e5868SGarrett D'Amore 				return (T_COLLSYM);
5446b5e5868SGarrett D'Amore 			}
5456b5e5868SGarrett D'Amore 			if ((yylval.collelem = lookup_collelem(token)) !=
5466b5e5868SGarrett D'Amore 			    NULL) {
5476b5e5868SGarrett D'Amore 				return (T_COLLELEM);
5486b5e5868SGarrett D'Amore 			}
5496b5e5868SGarrett D'Amore 			/* its an undefined symbol */
5506b5e5868SGarrett D'Amore 			yylval.token = strdup(token);
5516b5e5868SGarrett D'Amore 			token = NULL;
5526b5e5868SGarrett D'Amore 			toksz = 0;
5536b5e5868SGarrett D'Amore 			tokidx = 0;
5546b5e5868SGarrett D'Amore 			return (T_SYMBOL);
5556b5e5868SGarrett D'Amore 		}
5566b5e5868SGarrett D'Amore 		add_tok(c);
5576b5e5868SGarrett D'Amore 	}
5586b5e5868SGarrett D'Amore 
5596b5e5868SGarrett D'Amore 	yyerror(_("unterminated symbolic name"));
5606b5e5868SGarrett D'Amore 	return (EOF);
5616b5e5868SGarrett D'Amore }
5626b5e5868SGarrett D'Amore 
5636b5e5868SGarrett D'Amore int
5646b5e5868SGarrett D'Amore get_category(void)
5656b5e5868SGarrett D'Amore {
5666b5e5868SGarrett D'Amore 	return (category);
5676b5e5868SGarrett D'Amore }
5686b5e5868SGarrett D'Amore 
5696b5e5868SGarrett D'Amore static int
5706b5e5868SGarrett D'Amore consume_token(void)
5716b5e5868SGarrett D'Amore {
5726b5e5868SGarrett D'Amore 	int	len = tokidx;
5736b5e5868SGarrett D'Amore 	int	i;
5746b5e5868SGarrett D'Amore 
5756b5e5868SGarrett D'Amore 	tokidx = 0;
5766b5e5868SGarrett D'Amore 	if (token == NULL)
5776b5e5868SGarrett D'Amore 		return (T_NULL);
5786b5e5868SGarrett D'Amore 
5796b5e5868SGarrett D'Amore 	/*
5806b5e5868SGarrett D'Amore 	 * this one is special, because we don't want it to alter the
5816b5e5868SGarrett D'Amore 	 * last_kw field.
5826b5e5868SGarrett D'Amore 	 */
5836b5e5868SGarrett D'Amore 	if (strcmp(token, "...") == 0) {
5846b5e5868SGarrett D'Amore 		return (T_ELLIPSIS);
5856b5e5868SGarrett D'Amore 	}
5866b5e5868SGarrett D'Amore 
5876b5e5868SGarrett D'Amore 	/* search for reserved words first */
5886b5e5868SGarrett D'Amore 	for (i = 0; keywords[i].name; i++) {
5896b5e5868SGarrett D'Amore 		int j;
5906b5e5868SGarrett D'Amore 		if (strcmp(keywords[i].name, token) != 0) {
5916b5e5868SGarrett D'Amore 			continue;
5926b5e5868SGarrett D'Amore 		}
5936b5e5868SGarrett D'Amore 
5946b5e5868SGarrett D'Amore 		last_kw = keywords[i].id;
5956b5e5868SGarrett D'Amore 
5966b5e5868SGarrett D'Amore 		/* clear the top level category if we're done with it */
5976b5e5868SGarrett D'Amore 		if (last_kw == T_END) {
5986b5e5868SGarrett D'Amore 			category = T_END;
5996b5e5868SGarrett D'Amore 		}
6006b5e5868SGarrett D'Amore 
6016b5e5868SGarrett D'Amore 		/* set the top level category if we're changing */
6026b5e5868SGarrett D'Amore 		for (j = 0; categories[j]; j++) {
6036b5e5868SGarrett D'Amore 			if (categories[j] != last_kw)
6046b5e5868SGarrett D'Amore 				continue;
6056b5e5868SGarrett D'Amore 			category = last_kw;
6066b5e5868SGarrett D'Amore 		}
6076b5e5868SGarrett D'Amore 
6086b5e5868SGarrett D'Amore 		return (keywords[i].id);
6096b5e5868SGarrett D'Amore 	}
6106b5e5868SGarrett D'Amore 
6116b5e5868SGarrett D'Amore 	/* maybe its a numeric constant? */
6126b5e5868SGarrett D'Amore 	if (isdigit(*token) || (*token == '-' && isdigit(token[1]))) {
6136b5e5868SGarrett D'Amore 		char *eptr;
6146b5e5868SGarrett D'Amore 		yylval.num = strtol(token, &eptr, 10);
6156b5e5868SGarrett D'Amore 		if (*eptr != 0)
6166b5e5868SGarrett D'Amore 			yyerror(_("malformed number"));
6176b5e5868SGarrett D'Amore 		return (T_NUMBER);
6186b5e5868SGarrett D'Amore 	}
6196b5e5868SGarrett D'Amore 
6206b5e5868SGarrett D'Amore 	/*
6216b5e5868SGarrett D'Amore 	 * A single lone character is treated as a character literal.
6226b5e5868SGarrett D'Amore 	 * To avoid duplication of effort, we stick in the charmap.
6236b5e5868SGarrett D'Amore 	 */
6246b5e5868SGarrett D'Amore 	if (len == 1) {
6256b5e5868SGarrett D'Amore 		yylval.wc = token[0];
6266b5e5868SGarrett D'Amore 		return (T_CHAR);
6276b5e5868SGarrett D'Amore 	}
6286b5e5868SGarrett D'Amore 
6296b5e5868SGarrett D'Amore 	/* anything else is treated as a symbolic name */
6306b5e5868SGarrett D'Amore 	yylval.token = strdup(token);
6316b5e5868SGarrett D'Amore 	token = NULL;
6326b5e5868SGarrett D'Amore 	toksz = 0;
6336b5e5868SGarrett D'Amore 	tokidx = 0;
6346b5e5868SGarrett D'Amore 	return (T_NAME);
6356b5e5868SGarrett D'Amore }
6366b5e5868SGarrett D'Amore 
6376b5e5868SGarrett D'Amore void
6386b5e5868SGarrett D'Amore scan_to_eol(void)
6396b5e5868SGarrett D'Amore {
6406b5e5868SGarrett D'Amore 	int	c;
6416b5e5868SGarrett D'Amore 	while ((c = scanc()) != '\n') {
6426b5e5868SGarrett D'Amore 		if (c == EOF) {
6436b5e5868SGarrett D'Amore 			/* end of file without newline! */
6446b5e5868SGarrett D'Amore 			errf(_("missing newline"));
6456b5e5868SGarrett D'Amore 			return;
6466b5e5868SGarrett D'Amore 		}
6476b5e5868SGarrett D'Amore 	}
6486b5e5868SGarrett D'Amore 	assert(c == '\n');
6496b5e5868SGarrett D'Amore }
6506b5e5868SGarrett D'Amore 
6516b5e5868SGarrett D'Amore int
6526b5e5868SGarrett D'Amore yylex(void)
6536b5e5868SGarrett D'Amore {
6546b5e5868SGarrett D'Amore 	int		c;
6556b5e5868SGarrett D'Amore 
6566b5e5868SGarrett D'Amore 	while ((c = scanc()) != EOF) {
6576b5e5868SGarrett D'Amore 
6586b5e5868SGarrett D'Amore 		/* special handling for quoted string */
6596b5e5868SGarrett D'Amore 		if (instring) {
6606b5e5868SGarrett D'Amore 			if (escaped) {
6616b5e5868SGarrett D'Amore 				escaped = 0;
6626b5e5868SGarrett D'Amore 
6636b5e5868SGarrett D'Amore 				/* if newline, just eat and forget it */
6646b5e5868SGarrett D'Amore 				if (c == '\n')
6656b5e5868SGarrett D'Amore 					continue;
6666b5e5868SGarrett D'Amore 
6676b5e5868SGarrett D'Amore 				if (strchr("xXd01234567", c)) {
6686b5e5868SGarrett D'Amore 					unscanc(c);
6696b5e5868SGarrett D'Amore 					unscanc(esc_char);
6706b5e5868SGarrett D'Amore 					return (get_wide());
6716b5e5868SGarrett D'Amore 				}
6726b5e5868SGarrett D'Amore 				yylval.wc = get_escaped(c);
6736b5e5868SGarrett D'Amore 				return (T_CHAR);
6746b5e5868SGarrett D'Amore 			}
6756b5e5868SGarrett D'Amore 			if (c == esc_char) {
6766b5e5868SGarrett D'Amore 				escaped = 1;
6776b5e5868SGarrett D'Amore 				continue;
6786b5e5868SGarrett D'Amore 			}
6796b5e5868SGarrett D'Amore 			switch (c) {
6806b5e5868SGarrett D'Amore 			case '<':
6816b5e5868SGarrett D'Amore 				return (get_symbol());
6826b5e5868SGarrett D'Amore 			case '>':
6836b5e5868SGarrett D'Amore 				/* oops! should generate syntax error  */
6846b5e5868SGarrett D'Amore 				return (T_GT);
6856b5e5868SGarrett D'Amore 			case '"':
6866b5e5868SGarrett D'Amore 				instring = 0;
6876b5e5868SGarrett D'Amore 				return (T_QUOTE);
6886b5e5868SGarrett D'Amore 			default:
6896b5e5868SGarrett D'Amore 				yylval.wc = c;
6906b5e5868SGarrett D'Amore 				return (T_CHAR);
6916b5e5868SGarrett D'Amore 			}
6926b5e5868SGarrett D'Amore 		}
6936b5e5868SGarrett D'Amore 
6946b5e5868SGarrett D'Amore 		/* escaped characters first */
6956b5e5868SGarrett D'Amore 		if (escaped) {
6966b5e5868SGarrett D'Amore 			escaped = 0;
6976b5e5868SGarrett D'Amore 			if (c == '\n') {
6986b5e5868SGarrett D'Amore 				/* eat the newline */
6996b5e5868SGarrett D'Amore 				continue;
7006b5e5868SGarrett D'Amore 			}
7016b5e5868SGarrett D'Amore 			hadtok = 1;
7026b5e5868SGarrett D'Amore 			if (tokidx) {
7036b5e5868SGarrett D'Amore 				/* an escape mid-token is nonsense */
7046b5e5868SGarrett D'Amore 				return (T_NULL);
7056b5e5868SGarrett D'Amore 			}
7066b5e5868SGarrett D'Amore 
7076b5e5868SGarrett D'Amore 			/* numeric escapes are treated as wide characters */
7086b5e5868SGarrett D'Amore 			if (strchr("xXd01234567", c)) {
7096b5e5868SGarrett D'Amore 				unscanc(c);
7106b5e5868SGarrett D'Amore 				unscanc(esc_char);
7116b5e5868SGarrett D'Amore 				return (get_wide());
7126b5e5868SGarrett D'Amore 			}
7136b5e5868SGarrett D'Amore 
7146b5e5868SGarrett D'Amore 			add_tok(get_escaped(c));
7156b5e5868SGarrett D'Amore 			continue;
7166b5e5868SGarrett D'Amore 		}
7176b5e5868SGarrett D'Amore 
7186b5e5868SGarrett D'Amore 		/* if it is the escape charter itself note it */
7196b5e5868SGarrett D'Amore 		if (c == esc_char) {
7206b5e5868SGarrett D'Amore 			escaped = 1;
7216b5e5868SGarrett D'Amore 			continue;
7226b5e5868SGarrett D'Amore 		}
7236b5e5868SGarrett D'Amore 
7246b5e5868SGarrett D'Amore 		/* remove from the comment char to end of line */
7256b5e5868SGarrett D'Amore 		if (c == com_char) {
7266b5e5868SGarrett D'Amore 			while (c != '\n') {
7276b5e5868SGarrett D'Amore 				if ((c = scanc()) == EOF) {
7286b5e5868SGarrett D'Amore 					/* end of file without newline! */
7296b5e5868SGarrett D'Amore 					return (EOF);
7306b5e5868SGarrett D'Amore 				}
7316b5e5868SGarrett D'Amore 			}
7326b5e5868SGarrett D'Amore 			assert(c == '\n');
7336b5e5868SGarrett D'Amore 			if (!hadtok) {
7346b5e5868SGarrett D'Amore 				/*
7356b5e5868SGarrett D'Amore 				 * If there were no tokens on this line,
7366b5e5868SGarrett D'Amore 				 * then just pretend it didn't exist at all.
7376b5e5868SGarrett D'Amore 				 */
7386b5e5868SGarrett D'Amore 				continue;
7396b5e5868SGarrett D'Amore 			}
7406b5e5868SGarrett D'Amore 			hadtok = 0;
7416b5e5868SGarrett D'Amore 			return (T_NL);
7426b5e5868SGarrett D'Amore 		}
7436b5e5868SGarrett D'Amore 
7446b5e5868SGarrett D'Amore 		if (strchr(" \t\n;()<>,\"", c) && (tokidx != 0)) {
7456b5e5868SGarrett D'Amore 			/*
7466b5e5868SGarrett D'Amore 			 * These are all token delimiters.  If there
7476b5e5868SGarrett D'Amore 			 * is a token already in progress, we need to
7486b5e5868SGarrett D'Amore 			 * process it.
7496b5e5868SGarrett D'Amore 			 */
7506b5e5868SGarrett D'Amore 			unscanc(c);
7516b5e5868SGarrett D'Amore 			return (consume_token());
7526b5e5868SGarrett D'Amore 		}
7536b5e5868SGarrett D'Amore 
7546b5e5868SGarrett D'Amore 		switch (c) {
7556b5e5868SGarrett D'Amore 		case '\n':
7566b5e5868SGarrett D'Amore 			if (!hadtok) {
7576b5e5868SGarrett D'Amore 				/*
7586b5e5868SGarrett D'Amore 				 * If the line was completely devoid of tokens,
7596b5e5868SGarrett D'Amore 				 * then just ignore it.
7606b5e5868SGarrett D'Amore 				 */
7616b5e5868SGarrett D'Amore 				continue;
7626b5e5868SGarrett D'Amore 			}
7636b5e5868SGarrett D'Amore 			/* we're starting a new line, reset the token state */
7646b5e5868SGarrett D'Amore 			hadtok = 0;
7656b5e5868SGarrett D'Amore 			return (T_NL);
7666b5e5868SGarrett D'Amore 		case ',':
7676b5e5868SGarrett D'Amore 			hadtok = 1;
7686b5e5868SGarrett D'Amore 			return (T_COMMA);
7696b5e5868SGarrett D'Amore 		case ';':
7706b5e5868SGarrett D'Amore 			hadtok = 1;
7716b5e5868SGarrett D'Amore 			return (T_SEMI);
7726b5e5868SGarrett D'Amore 		case '(':
7736b5e5868SGarrett D'Amore 			hadtok = 1;
7746b5e5868SGarrett D'Amore 			return (T_LPAREN);
7756b5e5868SGarrett D'Amore 		case ')':
7766b5e5868SGarrett D'Amore 			hadtok = 1;
7776b5e5868SGarrett D'Amore 			return (T_RPAREN);
7786b5e5868SGarrett D'Amore 		case '>':
7796b5e5868SGarrett D'Amore 			hadtok = 1;
7806b5e5868SGarrett D'Amore 			return (T_GT);
7816b5e5868SGarrett D'Amore 		case '<':
7826b5e5868SGarrett D'Amore 			/* symbol start! */
7836b5e5868SGarrett D'Amore 			hadtok = 1;
7846b5e5868SGarrett D'Amore 			return (get_symbol());
7856b5e5868SGarrett D'Amore 		case ' ':
7866b5e5868SGarrett D'Amore 		case '\t':
7876b5e5868SGarrett D'Amore 			/* whitespace, just ignore it */
7886b5e5868SGarrett D'Amore 			continue;
7896b5e5868SGarrett D'Amore 		case '"':
7906b5e5868SGarrett D'Amore 			hadtok = 1;
7916b5e5868SGarrett D'Amore 			instring = 1;
7926b5e5868SGarrett D'Amore 			return (T_QUOTE);
7936b5e5868SGarrett D'Amore 		default:
7946b5e5868SGarrett D'Amore 			hadtok = 1;
7956b5e5868SGarrett D'Amore 			add_tok(c);
7966b5e5868SGarrett D'Amore 			continue;
7976b5e5868SGarrett D'Amore 		}
7986b5e5868SGarrett D'Amore 	}
7996b5e5868SGarrett D'Amore 	return (EOF);
8006b5e5868SGarrett D'Amore }
8016b5e5868SGarrett D'Amore 
8026b5e5868SGarrett D'Amore void
8036b5e5868SGarrett D'Amore yyerror(const char *msg)
8046b5e5868SGarrett D'Amore {
8056b5e5868SGarrett D'Amore 	(void) fprintf(stderr, _("%s: %d: error: %s\n"),
8066b5e5868SGarrett D'Amore 	    filename, lineno, msg);
8076b5e5868SGarrett D'Amore 	exit(4);
8086b5e5868SGarrett D'Amore }
8096b5e5868SGarrett D'Amore 
8106b5e5868SGarrett D'Amore void
8116b5e5868SGarrett D'Amore errf(const char *fmt, ...)
8126b5e5868SGarrett D'Amore {
8136b5e5868SGarrett D'Amore 	char	*msg;
8146b5e5868SGarrett D'Amore 
8156b5e5868SGarrett D'Amore 	va_list	va;
8166b5e5868SGarrett D'Amore 	va_start(va, fmt);
8176b5e5868SGarrett D'Amore 	(void) vasprintf(&msg, fmt, va);
8186b5e5868SGarrett D'Amore 	va_end(va);
8196b5e5868SGarrett D'Amore 
8206b5e5868SGarrett D'Amore 	(void) fprintf(stderr, _("%s: %d: error: %s\n"),
8216b5e5868SGarrett D'Amore 	    filename, lineno, msg);
8226b5e5868SGarrett D'Amore 	free(msg);
8236b5e5868SGarrett D'Amore 	exit(4);
8246b5e5868SGarrett D'Amore }
8256b5e5868SGarrett D'Amore 
8266b5e5868SGarrett D'Amore void
8276b5e5868SGarrett D'Amore warn(const char *fmt, ...)
8286b5e5868SGarrett D'Amore {
8296b5e5868SGarrett D'Amore 	char	*msg;
8306b5e5868SGarrett D'Amore 
8316b5e5868SGarrett D'Amore 	va_list	va;
8326b5e5868SGarrett D'Amore 	va_start(va, fmt);
8336b5e5868SGarrett D'Amore 	(void) vasprintf(&msg, fmt, va);
8346b5e5868SGarrett D'Amore 	va_end(va);
8356b5e5868SGarrett D'Amore 
8366b5e5868SGarrett D'Amore 	(void) fprintf(stderr, _("%s: %d: warning: %s\n"),
8376b5e5868SGarrett D'Amore 	    filename, lineno, msg);
8386b5e5868SGarrett D'Amore 	free(msg);
8396b5e5868SGarrett D'Amore 	warnings++;
8406b5e5868SGarrett D'Amore 	if (!warnok)
8416b5e5868SGarrett D'Amore 		exit(4);
8426b5e5868SGarrett D'Amore }
843