xref: /titanic_50/usr/src/cmd/tic/tic_scan.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  * Copyright (c) 1999 by Sun Microsystems, Inc.
24*7c478bd9Sstevel@tonic-gate  * All rights reserved.
25*7c478bd9Sstevel@tonic-gate  */
26*7c478bd9Sstevel@tonic-gate /*	Copyright (c) 1988 AT&T	*/
27*7c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
28*7c478bd9Sstevel@tonic-gate 
29*7c478bd9Sstevel@tonic-gate 
30*7c478bd9Sstevel@tonic-gate /*
31*7c478bd9Sstevel@tonic-gate  * University Copyright- Copyright (c) 1982, 1986, 1988
32*7c478bd9Sstevel@tonic-gate  * The Regents of the University of California
33*7c478bd9Sstevel@tonic-gate  * All Rights Reserved
34*7c478bd9Sstevel@tonic-gate  *
35*7c478bd9Sstevel@tonic-gate  * University Acknowledgment- Portions of this document are derived from
36*7c478bd9Sstevel@tonic-gate  * software developed by the University of California, Berkeley, and its
37*7c478bd9Sstevel@tonic-gate  * contributors.
38*7c478bd9Sstevel@tonic-gate  */
39*7c478bd9Sstevel@tonic-gate 
40*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
41*7c478bd9Sstevel@tonic-gate 
42*7c478bd9Sstevel@tonic-gate /*
43*7c478bd9Sstevel@tonic-gate  *			COPYRIGHT NOTICE
44*7c478bd9Sstevel@tonic-gate  *
45*7c478bd9Sstevel@tonic-gate  *	This software is copyright(C) 1982 by Pavel Curtis
46*7c478bd9Sstevel@tonic-gate  *
47*7c478bd9Sstevel@tonic-gate  *	Permission is granted to reproduce and distribute
48*7c478bd9Sstevel@tonic-gate  *	this file by any means so long as no fee is charged
49*7c478bd9Sstevel@tonic-gate  *	above a nominal handling fee and so long as this
50*7c478bd9Sstevel@tonic-gate  *	notice is always included in the copies.
51*7c478bd9Sstevel@tonic-gate  *
52*7c478bd9Sstevel@tonic-gate  *	Other rights are reserved except as explicitly granted
53*7c478bd9Sstevel@tonic-gate  *	by written permission of the author.
54*7c478bd9Sstevel@tonic-gate  *		Pavel Curtis
55*7c478bd9Sstevel@tonic-gate  *		Computer Science Dept.
56*7c478bd9Sstevel@tonic-gate  *		405 Upson Hall
57*7c478bd9Sstevel@tonic-gate  *		Cornell University
58*7c478bd9Sstevel@tonic-gate  *		Ithaca, NY 14853
59*7c478bd9Sstevel@tonic-gate  *
60*7c478bd9Sstevel@tonic-gate  *		Ph- (607) 256-4934
61*7c478bd9Sstevel@tonic-gate  *
62*7c478bd9Sstevel@tonic-gate  *		Pavel.Cornell@Udel-Relay(ARPAnet)
63*7c478bd9Sstevel@tonic-gate  *		decvax!cornell!pavel(UUCPnet)
64*7c478bd9Sstevel@tonic-gate  */
65*7c478bd9Sstevel@tonic-gate 
66*7c478bd9Sstevel@tonic-gate /*
67*7c478bd9Sstevel@tonic-gate  *	comp_scan.c --- Lexical scanner for terminfo compiler.
68*7c478bd9Sstevel@tonic-gate  *
69*7c478bd9Sstevel@tonic-gate  *   $Log:	RCS/comp_scan.v $
70*7c478bd9Sstevel@tonic-gate  * Revision 2.1  82/10/25  14:45:55  pavel
71*7c478bd9Sstevel@tonic-gate  * Added Copyright Notice
72*7c478bd9Sstevel@tonic-gate  *
73*7c478bd9Sstevel@tonic-gate  * Revision 2.0  82/10/24  15:17:12  pavel
74*7c478bd9Sstevel@tonic-gate  * Beta-one Test Release
75*7c478bd9Sstevel@tonic-gate  *
76*7c478bd9Sstevel@tonic-gate  * Revision 1.3  82/08/23  22:30:03  pavel
77*7c478bd9Sstevel@tonic-gate  * The REAL Alpha-one Release Version
78*7c478bd9Sstevel@tonic-gate  *
79*7c478bd9Sstevel@tonic-gate  * Revision 1.2  82/08/19  19:10:06  pavel
80*7c478bd9Sstevel@tonic-gate  * Alpha Test Release One
81*7c478bd9Sstevel@tonic-gate  *
82*7c478bd9Sstevel@tonic-gate  * Revision 1.1  82/08/12  18:37:46  pavel
83*7c478bd9Sstevel@tonic-gate  * Initial revision
84*7c478bd9Sstevel@tonic-gate  *
85*7c478bd9Sstevel@tonic-gate  *
86*7c478bd9Sstevel@tonic-gate  */
87*7c478bd9Sstevel@tonic-gate 
88*7c478bd9Sstevel@tonic-gate 
89*7c478bd9Sstevel@tonic-gate #include <stdio.h>
90*7c478bd9Sstevel@tonic-gate #include <ctype.h>
91*7c478bd9Sstevel@tonic-gate #include "compiler.h"
92*7c478bd9Sstevel@tonic-gate 
93*7c478bd9Sstevel@tonic-gate #define	iswhite(ch)	(ch == ' ' || ch == '\t')
94*7c478bd9Sstevel@tonic-gate 
95*7c478bd9Sstevel@tonic-gate 
96*7c478bd9Sstevel@tonic-gate static int	first_column;		/* See 'next_char()' below */
97*7c478bd9Sstevel@tonic-gate 
98*7c478bd9Sstevel@tonic-gate 
99*7c478bd9Sstevel@tonic-gate 
100*7c478bd9Sstevel@tonic-gate /*
101*7c478bd9Sstevel@tonic-gate  *	int
102*7c478bd9Sstevel@tonic-gate  *	get_token()
103*7c478bd9Sstevel@tonic-gate  *
104*7c478bd9Sstevel@tonic-gate  *	Scans the input for the next token, storing the specifics in the
105*7c478bd9Sstevel@tonic-gate  *	global structure 'curr_token' and returning one of the following:
106*7c478bd9Sstevel@tonic-gate  *
107*7c478bd9Sstevel@tonic-gate  *		NAMES		A line beginning in column 1.  'name'
108*7c478bd9Sstevel@tonic-gate  *				will be set to point to everything up to
109*7c478bd9Sstevel@tonic-gate  *				but not including the first comma on the line.
110*7c478bd9Sstevel@tonic-gate  *		BOOLEAN		An entry consisting of a name followed by
111*7c478bd9Sstevel@tonic-gate  *				a comma.  'name' will be set to point to the
112*7c478bd9Sstevel@tonic-gate  *				name of the capability.
113*7c478bd9Sstevel@tonic-gate  *		NUMBER		An entry of the form
114*7c478bd9Sstevel@tonic-gate  *					name#digits,
115*7c478bd9Sstevel@tonic-gate  *				'name' will be set to point to the capability
116*7c478bd9Sstevel@tonic-gate  *				name and 'valnumber' to the number given.
117*7c478bd9Sstevel@tonic-gate  *		STRING		An entry of the form
118*7c478bd9Sstevel@tonic-gate  *					name=characters,
119*7c478bd9Sstevel@tonic-gate  *				'name' is set to the capability name and
120*7c478bd9Sstevel@tonic-gate  *				'valstring' to the string of characters, with
121*7c478bd9Sstevel@tonic-gate  *				input translations done.
122*7c478bd9Sstevel@tonic-gate  *		CANCEL		An entry of the form
123*7c478bd9Sstevel@tonic-gate  *					name@,
124*7c478bd9Sstevel@tonic-gate  *				'name' is set to the capability name and
125*7c478bd9Sstevel@tonic-gate  *				'valnumber' to -1.
126*7c478bd9Sstevel@tonic-gate  *		EOF		The end of the file has been reached.
127*7c478bd9Sstevel@tonic-gate  *
128*7c478bd9Sstevel@tonic-gate  */
129*7c478bd9Sstevel@tonic-gate 
130*7c478bd9Sstevel@tonic-gate int
131*7c478bd9Sstevel@tonic-gate get_token()
132*7c478bd9Sstevel@tonic-gate {
133*7c478bd9Sstevel@tonic-gate 	long		number;
134*7c478bd9Sstevel@tonic-gate 	int		type;
135*7c478bd9Sstevel@tonic-gate 	register int	ch;
136*7c478bd9Sstevel@tonic-gate 	static char	buffer[1024];
137*7c478bd9Sstevel@tonic-gate 	register char	*ptr;
138*7c478bd9Sstevel@tonic-gate 	int		dot_flag = FALSE;
139*7c478bd9Sstevel@tonic-gate 
140*7c478bd9Sstevel@tonic-gate 	while ((ch = next_char()) == '\n' || (isascii(ch) && iswhite(ch)));
141*7c478bd9Sstevel@tonic-gate 
142*7c478bd9Sstevel@tonic-gate 	if (ch == EOF)
143*7c478bd9Sstevel@tonic-gate 	    type = EOF;
144*7c478bd9Sstevel@tonic-gate 	else {
145*7c478bd9Sstevel@tonic-gate 	    if (ch == '.') {
146*7c478bd9Sstevel@tonic-gate 		dot_flag = TRUE;
147*7c478bd9Sstevel@tonic-gate 
148*7c478bd9Sstevel@tonic-gate 		while ((ch = next_char()) == ' ' || ch == '\t');
149*7c478bd9Sstevel@tonic-gate 	    }
150*7c478bd9Sstevel@tonic-gate 
151*7c478bd9Sstevel@tonic-gate 	    if (! isascii(ch) || ! isalnum(ch)) {
152*7c478bd9Sstevel@tonic-gate 		warning("Illegal character - '%c'", ch);
153*7c478bd9Sstevel@tonic-gate 		panic_mode(',');
154*7c478bd9Sstevel@tonic-gate 	    }
155*7c478bd9Sstevel@tonic-gate 
156*7c478bd9Sstevel@tonic-gate 	    ptr = buffer;
157*7c478bd9Sstevel@tonic-gate 	    if (ch != '\n') *(ptr++) = ch;
158*7c478bd9Sstevel@tonic-gate 
159*7c478bd9Sstevel@tonic-gate 	    if (first_column) {
160*7c478bd9Sstevel@tonic-gate 		while ((ch = next_char()) != ',' && ch != '\n' && ch != EOF)
161*7c478bd9Sstevel@tonic-gate 		    *(ptr++) = ch;
162*7c478bd9Sstevel@tonic-gate 
163*7c478bd9Sstevel@tonic-gate 		if (ch == EOF)
164*7c478bd9Sstevel@tonic-gate 		    err_abort("Premature EOF");
165*7c478bd9Sstevel@tonic-gate 		else if (ch == '\n') {
166*7c478bd9Sstevel@tonic-gate 		    warning("Newline in middle of terminal name");
167*7c478bd9Sstevel@tonic-gate 		    panic_mode(',');
168*7c478bd9Sstevel@tonic-gate 		}
169*7c478bd9Sstevel@tonic-gate 
170*7c478bd9Sstevel@tonic-gate 		*ptr = '\0';
171*7c478bd9Sstevel@tonic-gate 		curr_token.tk_name = buffer;
172*7c478bd9Sstevel@tonic-gate 		type = NAMES;
173*7c478bd9Sstevel@tonic-gate 	    } else {
174*7c478bd9Sstevel@tonic-gate 		ch = next_char();
175*7c478bd9Sstevel@tonic-gate 		while (isascii(ch) && isalnum(ch)) {
176*7c478bd9Sstevel@tonic-gate 		    *(ptr++) = ch;
177*7c478bd9Sstevel@tonic-gate 		    ch = next_char();
178*7c478bd9Sstevel@tonic-gate 		}
179*7c478bd9Sstevel@tonic-gate 
180*7c478bd9Sstevel@tonic-gate 		*ptr++ = '\0';
181*7c478bd9Sstevel@tonic-gate 		switch (ch) {
182*7c478bd9Sstevel@tonic-gate 		    case ',':
183*7c478bd9Sstevel@tonic-gate 			curr_token.tk_name = buffer;
184*7c478bd9Sstevel@tonic-gate 			type = BOOLEAN;
185*7c478bd9Sstevel@tonic-gate 			break;
186*7c478bd9Sstevel@tonic-gate 
187*7c478bd9Sstevel@tonic-gate 		    case '@':
188*7c478bd9Sstevel@tonic-gate 			if (next_char() != ',')
189*7c478bd9Sstevel@tonic-gate 			    warning("Missing comma");
190*7c478bd9Sstevel@tonic-gate 			curr_token.tk_name = buffer;
191*7c478bd9Sstevel@tonic-gate 			type = CANCEL;
192*7c478bd9Sstevel@tonic-gate 			break;
193*7c478bd9Sstevel@tonic-gate 
194*7c478bd9Sstevel@tonic-gate 		    case '#':
195*7c478bd9Sstevel@tonic-gate 			number = 0;
196*7c478bd9Sstevel@tonic-gate 			if ((ch = next_char()) == ',')
197*7c478bd9Sstevel@tonic-gate 				warning("Missing numeric value");
198*7c478bd9Sstevel@tonic-gate 			backspace();
199*7c478bd9Sstevel@tonic-gate 			if ((ch = next_char()) == '0') {
200*7c478bd9Sstevel@tonic-gate 			    if ((ch = next_char()) == 'x' || ch == 'X') {
201*7c478bd9Sstevel@tonic-gate 				while (isascii(ch = next_char()) &&
202*7c478bd9Sstevel@tonic-gate 				    isxdigit(ch)) {
203*7c478bd9Sstevel@tonic-gate 				    number *= 16;
204*7c478bd9Sstevel@tonic-gate 				    if (isdigit(ch))
205*7c478bd9Sstevel@tonic-gate 					number += ch - '0';
206*7c478bd9Sstevel@tonic-gate 				    else if (ch >= 'a' && ch <= 'f')
207*7c478bd9Sstevel@tonic-gate 					number += 10 + ch - 'a';
208*7c478bd9Sstevel@tonic-gate 				    else
209*7c478bd9Sstevel@tonic-gate 					number += 10 + ch - 'A';
210*7c478bd9Sstevel@tonic-gate 				}
211*7c478bd9Sstevel@tonic-gate 			    } else {
212*7c478bd9Sstevel@tonic-gate 				backspace();
213*7c478bd9Sstevel@tonic-gate 				while ((ch = next_char()) >= '0' &&
214*7c478bd9Sstevel@tonic-gate 				    ch <= '7')
215*7c478bd9Sstevel@tonic-gate 				    number = number * 8 + ch - '0';
216*7c478bd9Sstevel@tonic-gate 				}
217*7c478bd9Sstevel@tonic-gate 			    } else {
218*7c478bd9Sstevel@tonic-gate 				    backspace();
219*7c478bd9Sstevel@tonic-gate 				    while (isascii(ch = next_char()) &&
220*7c478bd9Sstevel@tonic-gate 					isdigit(ch))
221*7c478bd9Sstevel@tonic-gate 					number = number * 10 + ch - '0';
222*7c478bd9Sstevel@tonic-gate 			    }
223*7c478bd9Sstevel@tonic-gate 			if (ch != ',')
224*7c478bd9Sstevel@tonic-gate 			    warning("Missing comma");
225*7c478bd9Sstevel@tonic-gate 			curr_token.tk_name = buffer;
226*7c478bd9Sstevel@tonic-gate 			curr_token.tk_valnumber = number;
227*7c478bd9Sstevel@tonic-gate 			type = NUMBER;
228*7c478bd9Sstevel@tonic-gate 			break;
229*7c478bd9Sstevel@tonic-gate 
230*7c478bd9Sstevel@tonic-gate 		    case '=':
231*7c478bd9Sstevel@tonic-gate 			ch = trans_string(ptr);
232*7c478bd9Sstevel@tonic-gate 			if (ch != NULL && ch != ',')
233*7c478bd9Sstevel@tonic-gate 			    warning("Missing comma");
234*7c478bd9Sstevel@tonic-gate 			if (ch == NULL)
235*7c478bd9Sstevel@tonic-gate 				warning("NULL string value");
236*7c478bd9Sstevel@tonic-gate 			curr_token.tk_name = buffer;
237*7c478bd9Sstevel@tonic-gate 			curr_token.tk_valstring = ptr;
238*7c478bd9Sstevel@tonic-gate 			type = STRING;
239*7c478bd9Sstevel@tonic-gate 			break;
240*7c478bd9Sstevel@tonic-gate 
241*7c478bd9Sstevel@tonic-gate 		    default:
242*7c478bd9Sstevel@tonic-gate 			warning("Illegal character - '%c'", ch);
243*7c478bd9Sstevel@tonic-gate 		}
244*7c478bd9Sstevel@tonic-gate 	    } /* end else (first_column == FALSE) */
245*7c478bd9Sstevel@tonic-gate 	} /* end else (ch != EOF) */
246*7c478bd9Sstevel@tonic-gate 
247*7c478bd9Sstevel@tonic-gate 	if (dot_flag == TRUE)
248*7c478bd9Sstevel@tonic-gate 	    DEBUG(8, "Commented out ", "");
249*7c478bd9Sstevel@tonic-gate 
250*7c478bd9Sstevel@tonic-gate 	if (debug_level >= 8) {
251*7c478bd9Sstevel@tonic-gate 	    fprintf(stderr, "Token: ");
252*7c478bd9Sstevel@tonic-gate 	    switch (type) {
253*7c478bd9Sstevel@tonic-gate 		case BOOLEAN:
254*7c478bd9Sstevel@tonic-gate 			fprintf(stderr, "Boolean;  name='%s'\n",
255*7c478bd9Sstevel@tonic-gate 			    curr_token.tk_name);
256*7c478bd9Sstevel@tonic-gate 			break;
257*7c478bd9Sstevel@tonic-gate 
258*7c478bd9Sstevel@tonic-gate 		case NUMBER:
259*7c478bd9Sstevel@tonic-gate 			fprintf(stderr, "Number; name = '%s', value = %d\n",
260*7c478bd9Sstevel@tonic-gate 			    curr_token.tk_name, curr_token.tk_valnumber);
261*7c478bd9Sstevel@tonic-gate 			break;
262*7c478bd9Sstevel@tonic-gate 
263*7c478bd9Sstevel@tonic-gate 		case STRING:
264*7c478bd9Sstevel@tonic-gate 			fprintf(stderr, "String; name = '%s', value = '%s'\n",
265*7c478bd9Sstevel@tonic-gate 			    curr_token.tk_name, curr_token.tk_valstring);
266*7c478bd9Sstevel@tonic-gate 			break;
267*7c478bd9Sstevel@tonic-gate 
268*7c478bd9Sstevel@tonic-gate 		case CANCEL:
269*7c478bd9Sstevel@tonic-gate 			fprintf(stderr, "Cancel; name = '%s'\n",
270*7c478bd9Sstevel@tonic-gate 			    curr_token.tk_name);
271*7c478bd9Sstevel@tonic-gate 		    break;
272*7c478bd9Sstevel@tonic-gate 
273*7c478bd9Sstevel@tonic-gate 		case NAMES:
274*7c478bd9Sstevel@tonic-gate 			fprintf(stderr, "Names; value = '%s'\n",
275*7c478bd9Sstevel@tonic-gate 			    curr_token.tk_name);
276*7c478bd9Sstevel@tonic-gate 			break;
277*7c478bd9Sstevel@tonic-gate 
278*7c478bd9Sstevel@tonic-gate 		case EOF:
279*7c478bd9Sstevel@tonic-gate 			fprintf(stderr, "End of file\n");
280*7c478bd9Sstevel@tonic-gate 			break;
281*7c478bd9Sstevel@tonic-gate 
282*7c478bd9Sstevel@tonic-gate 		default:
283*7c478bd9Sstevel@tonic-gate 			warning("Bad token type");
284*7c478bd9Sstevel@tonic-gate 	    }
285*7c478bd9Sstevel@tonic-gate 	}
286*7c478bd9Sstevel@tonic-gate 
287*7c478bd9Sstevel@tonic-gate 	if (dot_flag == TRUE)	/* if commented out, use the next one */
288*7c478bd9Sstevel@tonic-gate 	    type = get_token();
289*7c478bd9Sstevel@tonic-gate 
290*7c478bd9Sstevel@tonic-gate 	return (type);
291*7c478bd9Sstevel@tonic-gate }
292*7c478bd9Sstevel@tonic-gate 
293*7c478bd9Sstevel@tonic-gate 
294*7c478bd9Sstevel@tonic-gate 
295*7c478bd9Sstevel@tonic-gate /*
296*7c478bd9Sstevel@tonic-gate  *	int
297*7c478bd9Sstevel@tonic-gate  *	next_char()
298*7c478bd9Sstevel@tonic-gate  *
299*7c478bd9Sstevel@tonic-gate  *	Returns the next character in the input stream.  Comments and leading
300*7c478bd9Sstevel@tonic-gate  *	white space are stripped.  The global state variable 'firstcolumn' is
301*7c478bd9Sstevel@tonic-gate  *	set TRUE if the character returned is from the first column of the
302*7c478bd9Sstevel@tonic-gate  * 	inputline.  The global variable curr_line is incremented for each new.
303*7c478bd9Sstevel@tonic-gate  *	line. The global variable curr_file_pos is set to the file offset
304*7c478bd9Sstevel@tonic-gate  *	of the beginning of each line.
305*7c478bd9Sstevel@tonic-gate  *
306*7c478bd9Sstevel@tonic-gate  */
307*7c478bd9Sstevel@tonic-gate 
308*7c478bd9Sstevel@tonic-gate int	curr_column = -1;
309*7c478bd9Sstevel@tonic-gate char	line[1024];
310*7c478bd9Sstevel@tonic-gate 
311*7c478bd9Sstevel@tonic-gate int
312*7c478bd9Sstevel@tonic-gate next_char()
313*7c478bd9Sstevel@tonic-gate {
314*7c478bd9Sstevel@tonic-gate 	char	*rtn_value;
315*7c478bd9Sstevel@tonic-gate 	long	ftell();
316*7c478bd9Sstevel@tonic-gate 	char	*p;
317*7c478bd9Sstevel@tonic-gate 
318*7c478bd9Sstevel@tonic-gate 	if (curr_column < 0 || curr_column > 1023 ||
319*7c478bd9Sstevel@tonic-gate 	    line[curr_column] == '\0') {
320*7c478bd9Sstevel@tonic-gate 	    do {
321*7c478bd9Sstevel@tonic-gate 			curr_file_pos = ftell(stdin);
322*7c478bd9Sstevel@tonic-gate 
323*7c478bd9Sstevel@tonic-gate 			if ((rtn_value = fgets(line, 1024, stdin)) == NULL)
324*7c478bd9Sstevel@tonic-gate 				return (EOF);
325*7c478bd9Sstevel@tonic-gate 			curr_line++;
326*7c478bd9Sstevel@tonic-gate 			p = &line[0];
327*7c478bd9Sstevel@tonic-gate 			while (*p && iswhite(*p)) {
328*7c478bd9Sstevel@tonic-gate 				p++;
329*7c478bd9Sstevel@tonic-gate 			}
330*7c478bd9Sstevel@tonic-gate 	    } while (*p == '#');
331*7c478bd9Sstevel@tonic-gate 
332*7c478bd9Sstevel@tonic-gate 	    curr_column = 0;
333*7c478bd9Sstevel@tonic-gate 	    while (isascii(line[curr_column]) && iswhite(line[curr_column]))
334*7c478bd9Sstevel@tonic-gate 		curr_column++;
335*7c478bd9Sstevel@tonic-gate 	}
336*7c478bd9Sstevel@tonic-gate 
337*7c478bd9Sstevel@tonic-gate 	if (curr_column == 0 && line[0] != '\n')
338*7c478bd9Sstevel@tonic-gate 	    first_column = TRUE;
339*7c478bd9Sstevel@tonic-gate 	else
340*7c478bd9Sstevel@tonic-gate 	    first_column = FALSE;
341*7c478bd9Sstevel@tonic-gate 
342*7c478bd9Sstevel@tonic-gate 	return (line[curr_column++]);
343*7c478bd9Sstevel@tonic-gate }
344*7c478bd9Sstevel@tonic-gate 
345*7c478bd9Sstevel@tonic-gate 
346*7c478bd9Sstevel@tonic-gate backspace()
347*7c478bd9Sstevel@tonic-gate {
348*7c478bd9Sstevel@tonic-gate 	curr_column--;
349*7c478bd9Sstevel@tonic-gate 
350*7c478bd9Sstevel@tonic-gate 	if (curr_column < 0)
351*7c478bd9Sstevel@tonic-gate 	    syserr_abort("Backspaced off beginning of line");
352*7c478bd9Sstevel@tonic-gate }
353*7c478bd9Sstevel@tonic-gate 
354*7c478bd9Sstevel@tonic-gate 
355*7c478bd9Sstevel@tonic-gate 
356*7c478bd9Sstevel@tonic-gate /*
357*7c478bd9Sstevel@tonic-gate  *	reset_input()
358*7c478bd9Sstevel@tonic-gate  *
359*7c478bd9Sstevel@tonic-gate  *	Resets the input-reading routines.  Used after a seek has been done.
360*7c478bd9Sstevel@tonic-gate  *
361*7c478bd9Sstevel@tonic-gate  */
362*7c478bd9Sstevel@tonic-gate 
363*7c478bd9Sstevel@tonic-gate reset_input()
364*7c478bd9Sstevel@tonic-gate {
365*7c478bd9Sstevel@tonic-gate 	curr_column = -1;
366*7c478bd9Sstevel@tonic-gate }
367*7c478bd9Sstevel@tonic-gate 
368*7c478bd9Sstevel@tonic-gate 
369*7c478bd9Sstevel@tonic-gate 
370*7c478bd9Sstevel@tonic-gate /*
371*7c478bd9Sstevel@tonic-gate  *	int
372*7c478bd9Sstevel@tonic-gate  *	trans_string(ptr)
373*7c478bd9Sstevel@tonic-gate  *
374*7c478bd9Sstevel@tonic-gate  *	Reads characters using next_char() until encountering a comma, a new
375*7c478bd9Sstevel@tonic-gate  *	entry, or end-of-file.  The returned value is the character which
376*7c478bd9Sstevel@tonic-gate  *	caused reading to stop.  The following translations are done on the
377*7c478bd9Sstevel@tonic-gate  *	input:
378*7c478bd9Sstevel@tonic-gate  *
379*7c478bd9Sstevel@tonic-gate  *		^X  goes to  ctrl-X (i.e. X & 037)
380*7c478bd9Sstevel@tonic-gate  *		{\E,\n,\r,\b,\t,\f}  go to
381*7c478bd9Sstevel@tonic-gate  *			{ESCAPE,newline,carriage-return,backspace,tab,formfeed}
382*7c478bd9Sstevel@tonic-gate  *		{\^,\\}  go to  {carat,backslash}
383*7c478bd9Sstevel@tonic-gate  *		\ddd (for ddd = up to three octal digits)  goes to
384*7c478bd9Sstevel@tonic-gate  *							the character ddd
385*7c478bd9Sstevel@tonic-gate  *
386*7c478bd9Sstevel@tonic-gate  *		\e == \E
387*7c478bd9Sstevel@tonic-gate  *		\0 == \200
388*7c478bd9Sstevel@tonic-gate  *
389*7c478bd9Sstevel@tonic-gate  */
390*7c478bd9Sstevel@tonic-gate 
391*7c478bd9Sstevel@tonic-gate int
392*7c478bd9Sstevel@tonic-gate trans_string(char *ptr)
393*7c478bd9Sstevel@tonic-gate {
394*7c478bd9Sstevel@tonic-gate 	register int	count = 0;
395*7c478bd9Sstevel@tonic-gate 	int		number;
396*7c478bd9Sstevel@tonic-gate 	register int	i;
397*7c478bd9Sstevel@tonic-gate 	register int	ch;
398*7c478bd9Sstevel@tonic-gate 
399*7c478bd9Sstevel@tonic-gate 	while ((ch = next_char()) != ',' && ch != EOF && !first_column) {
400*7c478bd9Sstevel@tonic-gate 	    if (ch == '^') {
401*7c478bd9Sstevel@tonic-gate 		ch = next_char();
402*7c478bd9Sstevel@tonic-gate 		if (ch == EOF)
403*7c478bd9Sstevel@tonic-gate 		    err_abort("Premature EOF");
404*7c478bd9Sstevel@tonic-gate 
405*7c478bd9Sstevel@tonic-gate 		if (!isascii(ch) || ! isprint(ch)) {
406*7c478bd9Sstevel@tonic-gate 		    warning("Illegal ^ character - '%c'", ch);
407*7c478bd9Sstevel@tonic-gate 		}
408*7c478bd9Sstevel@tonic-gate 
409*7c478bd9Sstevel@tonic-gate 		if (ch == '@')
410*7c478bd9Sstevel@tonic-gate 		    *(ptr++) = 0200;
411*7c478bd9Sstevel@tonic-gate 		else
412*7c478bd9Sstevel@tonic-gate 		    *(ptr++) = ch & 037;
413*7c478bd9Sstevel@tonic-gate 	    } else if (ch == '\\') {
414*7c478bd9Sstevel@tonic-gate 		ch = next_char();
415*7c478bd9Sstevel@tonic-gate 		if (ch == EOF)
416*7c478bd9Sstevel@tonic-gate 		    err_abort("Premature EOF");
417*7c478bd9Sstevel@tonic-gate 
418*7c478bd9Sstevel@tonic-gate 		if (ch >= '0' && ch <= '7') {
419*7c478bd9Sstevel@tonic-gate 		    number = ch - '0';
420*7c478bd9Sstevel@tonic-gate 		    for (i = 0; i < 2; i++) {
421*7c478bd9Sstevel@tonic-gate 			ch = next_char();
422*7c478bd9Sstevel@tonic-gate 			if (ch == EOF)
423*7c478bd9Sstevel@tonic-gate 			    err_abort("Premature EOF");
424*7c478bd9Sstevel@tonic-gate 
425*7c478bd9Sstevel@tonic-gate 			if (ch < '0' || ch > '7') {
426*7c478bd9Sstevel@tonic-gate 			    backspace();
427*7c478bd9Sstevel@tonic-gate 			    break;
428*7c478bd9Sstevel@tonic-gate 			}
429*7c478bd9Sstevel@tonic-gate 
430*7c478bd9Sstevel@tonic-gate 			number = number * 8 + ch - '0';
431*7c478bd9Sstevel@tonic-gate 		    }
432*7c478bd9Sstevel@tonic-gate 
433*7c478bd9Sstevel@tonic-gate 		    if (number == 0)
434*7c478bd9Sstevel@tonic-gate 			number = 0200;
435*7c478bd9Sstevel@tonic-gate 		    *(ptr++) = (char)number;
436*7c478bd9Sstevel@tonic-gate 		} else {
437*7c478bd9Sstevel@tonic-gate 		    switch (ch) {
438*7c478bd9Sstevel@tonic-gate 			case 'E':
439*7c478bd9Sstevel@tonic-gate 			case 'e':	*(ptr++) = '\033';	break;
440*7c478bd9Sstevel@tonic-gate 
441*7c478bd9Sstevel@tonic-gate 			case 'l':
442*7c478bd9Sstevel@tonic-gate 			case 'n':	*(ptr++) = '\n';	break;
443*7c478bd9Sstevel@tonic-gate 
444*7c478bd9Sstevel@tonic-gate 			case 'r':	*(ptr++) = '\r';	break;
445*7c478bd9Sstevel@tonic-gate 
446*7c478bd9Sstevel@tonic-gate 			case 'b':	*(ptr++) = '\010';	break;
447*7c478bd9Sstevel@tonic-gate 
448*7c478bd9Sstevel@tonic-gate 			case 's':	*(ptr++) = ' ';		break;
449*7c478bd9Sstevel@tonic-gate 
450*7c478bd9Sstevel@tonic-gate 			case 'f':	*(ptr++) = '\014';	break;
451*7c478bd9Sstevel@tonic-gate 
452*7c478bd9Sstevel@tonic-gate 			case 't':	*(ptr++) = '\t';	break;
453*7c478bd9Sstevel@tonic-gate 
454*7c478bd9Sstevel@tonic-gate 			case '\\':	*(ptr++) = '\\';	break;
455*7c478bd9Sstevel@tonic-gate 
456*7c478bd9Sstevel@tonic-gate 			case '^':	*(ptr++) = '^';		break;
457*7c478bd9Sstevel@tonic-gate 
458*7c478bd9Sstevel@tonic-gate 			case ',':	*(ptr++) = ',';		break;
459*7c478bd9Sstevel@tonic-gate 
460*7c478bd9Sstevel@tonic-gate 			case ':':	*(ptr++) = ':';		break;
461*7c478bd9Sstevel@tonic-gate 
462*7c478bd9Sstevel@tonic-gate 			default:
463*7c478bd9Sstevel@tonic-gate 			    warning("Illegal character in \\ sequence - '%c'",
464*7c478bd9Sstevel@tonic-gate 				ch);
465*7c478bd9Sstevel@tonic-gate 			    *(ptr++) = ch;
466*7c478bd9Sstevel@tonic-gate 		    } /* endswitch (ch) */
467*7c478bd9Sstevel@tonic-gate 		} /* endelse (ch < '0' ||  ch > '7') */
468*7c478bd9Sstevel@tonic-gate 	    } /* end else if (ch == '\\') */
469*7c478bd9Sstevel@tonic-gate 	    else {
470*7c478bd9Sstevel@tonic-gate 		if (ch != '\n') *(ptr++) = ch;
471*7c478bd9Sstevel@tonic-gate 	    }
472*7c478bd9Sstevel@tonic-gate 
473*7c478bd9Sstevel@tonic-gate 	    count ++;
474*7c478bd9Sstevel@tonic-gate 
475*7c478bd9Sstevel@tonic-gate 	    if (count > 1000)
476*7c478bd9Sstevel@tonic-gate 		warning("Very long string found.  Missing comma?");
477*7c478bd9Sstevel@tonic-gate 	} /* end while */
478*7c478bd9Sstevel@tonic-gate 
479*7c478bd9Sstevel@tonic-gate 	if (ch == EOF)
480*7c478bd9Sstevel@tonic-gate 	    warning("Premature EOF - missing comma?");
481*7c478bd9Sstevel@tonic-gate 	/* start of new description */
482*7c478bd9Sstevel@tonic-gate 	else if (first_column) {
483*7c478bd9Sstevel@tonic-gate 	    backspace();
484*7c478bd9Sstevel@tonic-gate 	    warning("Missing comma?");
485*7c478bd9Sstevel@tonic-gate 	    /* pretend we did get a comma */
486*7c478bd9Sstevel@tonic-gate 	    ch = ',';
487*7c478bd9Sstevel@tonic-gate 	}
488*7c478bd9Sstevel@tonic-gate 
489*7c478bd9Sstevel@tonic-gate 	*ptr = '\0';
490*7c478bd9Sstevel@tonic-gate 
491*7c478bd9Sstevel@tonic-gate 	if (count == 0)
492*7c478bd9Sstevel@tonic-gate 		return (NULL);
493*7c478bd9Sstevel@tonic-gate 	return (ch);
494*7c478bd9Sstevel@tonic-gate }
495*7c478bd9Sstevel@tonic-gate 
496*7c478bd9Sstevel@tonic-gate /*
497*7c478bd9Sstevel@tonic-gate  * Panic mode error recovery - skip everything until a "ch" is found.
498*7c478bd9Sstevel@tonic-gate  */
499*7c478bd9Sstevel@tonic-gate panic_mode(int ch)
500*7c478bd9Sstevel@tonic-gate {
501*7c478bd9Sstevel@tonic-gate 	register int c;
502*7c478bd9Sstevel@tonic-gate 
503*7c478bd9Sstevel@tonic-gate 	for (;;) {
504*7c478bd9Sstevel@tonic-gate 		c = next_char();
505*7c478bd9Sstevel@tonic-gate 		if (c == ch)
506*7c478bd9Sstevel@tonic-gate 			return;
507*7c478bd9Sstevel@tonic-gate 		if (c == EOF)
508*7c478bd9Sstevel@tonic-gate 			return;
509*7c478bd9Sstevel@tonic-gate 	}
510*7c478bd9Sstevel@tonic-gate }
511