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