1 /***************************************************************************
3 ****************************************************************************
4 * ncurses is copyright (C) 1992-1995 *
6 * zmbenhal@netcom.com *
8 * esr@snark.thyrsus.com *
10 * Permission is hereby granted to reproduce and distribute ncurses *
11 * by any means and for any fee, whether alone or as part of a *
12 * larger distribution, in source or in binary form, PROVIDED *
13 * this notice is included with any such distribution, and is not *
14 * removed from any of its header files. Mention of ncurses in any *
15 * applications linked with it is highly appreciated. *
17 * ncurses comes AS IS with no warranty, implied or expressed. *
19 ***************************************************************************/
22 * comp_scan.c --- Lexical scanner for terminfo compiler.
29 * long _nc_curr_file_pos;
30 * long _nc_comment_start;
31 * long _nc_comment_end;
34 #include <curses.priv.h>
39 MODULE_ID("$Id: comp_scan.c,v 1.21 1997/04/24 10:37:34 tom Exp $")
42 * Maximum length of string capability we'll accept before raising an error.
43 * Yes, there is a real capability in /etc/termcap this long, an "is".
47 #define iswhite(ch) (ch == ' ' || ch == '\t')
49 int _nc_syntax; /* termcap or terminfo? */
50 int _nc_curr_line; /* current line # in input */
51 int _nc_curr_col; /* current column # in input */
52 long _nc_curr_file_pos; /* file offset of current line */
53 long _nc_comment_start; /* start of comment range before name */
54 long _nc_comment_end; /* end of comment range before name */
55 long _nc_start_line; /* start line of current entry */
57 /*****************************************************************************
59 * Token-grabbing machinery
61 *****************************************************************************/
63 static bool first_column; /* See 'next_char()' below */
64 static char separator; /* capability separator */
65 static int pushtype; /* type of pushback token */
66 static char pushname[MAX_NAME_SIZE+1];
68 static int next_char(void);
69 static long stream_pos(void);
70 static bool end_of_stream(void);
71 static char trans_string(char *);
72 static void push_back(char c);
74 /* Assume we may be looking at a termcap-style continuation */
75 static inline int eat_escaped_newline(int ch)
78 while ((ch = next_char()) == '\n' || iswhite(ch))
87 * Scans the input for the next token, storing the specifics in the
88 * global structure 'curr_token' and returning one of the following:
90 * NAMES A line beginning in column 1. 'name'
91 * will be set to point to everything up to but
92 * not including the first separator on the line.
93 * BOOLEAN An entry consisting of a name followed by
94 * a separator. 'name' will be set to point to
95 * the name of the capability.
96 * NUMBER An entry of the form
98 * 'name' will be set to point to the capability
99 * name and 'valnumber' to the number given.
100 * STRING An entry of the form
102 * 'name' is set to the capability name and
103 * 'valstring' to the string of characters, with
104 * input translations done.
105 * CANCEL An entry of the form
107 * 'name' is set to the capability name and
109 * EOF The end of the file has been reached.
111 * A `separator' is either a comma or a semicolon, depending on whether
112 * we are in termcap or terminfo mode.
116 int _nc_get_token(void)
118 static const char terminfo_punct[] = "@%&*!#";
123 static char buffer[MAX_ENTRY_SIZE];
125 int dot_flag = FALSE;
128 if (pushtype != NO_PUSHBACK)
130 int retval = pushtype;
132 _nc_set_type(pushname);
133 DEBUG(3, ("pushed-back token: `%s', class %d",
134 _nc_curr_token.tk_name, pushtype));
136 pushtype = NO_PUSHBACK;
139 /* currtok wasn't altered by _nc_push_token() */
147 token_start = stream_pos();
148 while ((ch = next_char()) == '\n' || iswhite(ch))
151 ch = eat_escaped_newline(ch);
156 /* if this is a termcap entry, skip a leading separator */
157 if (separator == ':' && ch == ':')
162 DEBUG(8, ("dot-flag set"));
164 while ((ch = next_char())=='.' || iswhite(ch))
173 /* have to make some punctuation chars legal for terminfo */
174 if (!isalnum(ch) && !strchr(terminfo_punct, (char)ch)) {
175 _nc_warning("Illegal character (expected alphanumeric or %s) - %s",
176 terminfo_punct, _tracechar((chtype)ch));
177 _nc_panic_mode(separator);
187 _nc_comment_start = token_start;
188 _nc_comment_end = _nc_curr_file_pos;
189 _nc_start_line = _nc_curr_line;
192 while ((ch = next_char()) != '\n')
195 _nc_err_abort("premature EOF");
198 _nc_syntax = SYN_TERMCAP;
204 _nc_syntax = SYN_TERMINFO;
207 * Fall-through here is not an accident.
208 * The idea is that if we see a comma, we
209 * figure this is terminfo unless we
210 * subsequently run into a colon -- but
211 * we don't stop looking for that colon until
212 * hitting a newline. This allows commas to
213 * be embedded in description fields of
219 ch = eat_escaped_newline(ch);
224 if (_nc_syntax == ERR)
227 * Grrr...what we ought to do here is barf,
228 * complaining that the entry is malformed.
229 * But because a couple of name fields in the
230 * 8.2 termcap file end with |\, we just have
231 * to assume it's termcap syntax.
233 _nc_syntax = SYN_TERMCAP;
236 else if (_nc_syntax == SYN_TERMINFO)
238 /* throw away trailing /, *$/ */
239 for (--ptr; iswhite(*ptr) || *ptr == ','; ptr--)
245 * This is the soonest we have the terminal name
246 * fetched. Set up for following warning messages.
248 ptr = strchr(buffer, '|');
249 if (ptr == (char *)NULL)
250 ptr = buffer + strlen(buffer);
253 _nc_set_type(buffer);
257 * Compute the boundary between the aliases and the
258 * description field for syntax-checking purposes.
260 desc = strrchr(buffer, '|');
263 _nc_warning("empty longname field");
264 else if (strchr(desc, ' ') == (char *)NULL)
265 _nc_warning("older tic versions may treat the description field as an alias");
267 desc = buffer + strlen(buffer);
270 * Whitespace in a name field other than the long name
271 * can confuse rdist and some termcap tools. Slashes
272 * are a no-no. Other special characters can be
273 * dangerous due to shell expansion.
275 for (ptr = buffer; ptr < desc; ptr++)
279 _nc_warning("whitespace in name or alias field");
282 else if (*ptr == '/')
284 _nc_warning("slashes aren't allowed in names or aliases");
287 else if (strchr("$[]!*?", *ptr))
289 _nc_warning("dubious character `%c' in name or alias field", *ptr);
296 _nc_curr_token.tk_name = buffer;
299 while ((ch = next_char()) != EOF) {
301 if (_nc_syntax == SYN_TERMINFO) {
304 } else { /* allow ';' for "k;" */
317 _nc_err_abort("Separator inconsistent with syntax");
318 _nc_curr_token.tk_name = buffer;
322 if ((ch = next_char()) != separator)
323 _nc_warning("Missing separator after `%s', have %s",
324 buffer, _tracechar((chtype)ch));
325 _nc_curr_token.tk_name = buffer;
332 while (isdigit(ch = next_char())) {
333 number = number * 10 + ch - '0';
337 _nc_warning("no value given for `%s'", buffer);
339 _nc_warning("Missing separator");
340 _nc_curr_token.tk_name = buffer;
341 _nc_curr_token.tk_valnumber = number;
346 ch = trans_string(ptr);
348 _nc_warning("Missing separator");
349 _nc_curr_token.tk_name = buffer;
350 _nc_curr_token.tk_valstring = ptr;
358 /* just to get rid of the compiler warning */
360 _nc_warning("Illegal character - %s",
361 _tracechar((chtype)ch));
363 } /* end else (first_column == FALSE) */
364 } /* end else (ch != EOF) */
367 if (dot_flag == TRUE)
368 DEBUG(8, ("Commented out "));
370 if (_nc_tracing & TRACE_IEVENT)
372 fprintf(stderr, "Token: ");
376 fprintf(stderr, "Boolean; name='%s'\n",
377 _nc_curr_token.tk_name);
381 fprintf(stderr, "Number; name='%s', value=%d\n",
382 _nc_curr_token.tk_name,
383 _nc_curr_token.tk_valnumber);
387 fprintf(stderr, "String; name='%s', value=%s\n",
388 _nc_curr_token.tk_name,
389 _nc_visbuf(_nc_curr_token.tk_valstring));
393 fprintf(stderr, "Cancel; name='%s'\n",
394 _nc_curr_token.tk_name);
399 fprintf(stderr, "Names; value='%s'\n",
400 _nc_curr_token.tk_name);
404 fprintf(stderr, "End of file\n");
408 _nc_warning("Bad token type");
412 if (dot_flag == TRUE) /* if commented out, use the next one */
413 type = _nc_get_token();
415 DEBUG(3, ("token: `%s', class %d", _nc_curr_token.tk_name, type));
424 * Reads characters using next_char() until encountering a separator, nl,
425 * or end-of-file. The returned value is the character which caused
426 * reading to stop. The following translations are done on the input:
428 * ^X goes to ctrl-X (i.e. X & 037)
429 * {\E,\n,\r,\b,\t,\f} go to
430 * {ESCAPE,newline,carriage-return,backspace,tab,formfeed}
431 * {\^,\\} go to {carat,backslash}
432 * \ddd (for ddd = up to three octal digits) goes to the character ddd
440 trans_string(char *ptr)
445 chtype ch, last_ch = '\0';
447 while ((ch = c = next_char()) != (chtype)separator && c != EOF) {
448 if ((_nc_syntax == SYN_TERMCAP) && c == '\n')
450 if (ch == '^' && last_ch != '%') {
451 ch = c = next_char();
453 _nc_err_abort("Premature EOF");
455 if (! (is7bits(ch) && isprint(ch))) {
456 _nc_warning("Illegal ^ character - %s",
457 _tracechar((unsigned char)ch));
462 *(ptr++) = (char)(ch & 037);
464 else if (ch == '\\') {
465 ch = c = next_char();
467 _nc_err_abort("Premature EOF");
469 if (ch >= '0' && ch <= '7') {
471 for (i=0; i < 2; i++) {
472 ch = c = next_char();
474 _nc_err_abort("Premature EOF");
476 if (c < '0' || c > '7') {
478 _nc_warning("Non-octal digit `%c' in \\ sequence", c);
479 /* allow the digit; it'll do less harm */
486 number = number * 8 + c - '0';
491 *(ptr++) = (char) number;
495 case 'e': *(ptr++) = '\033'; break;
498 case 'n': *(ptr++) = '\n'; break;
500 case 'r': *(ptr++) = '\r'; break;
502 case 'b': *(ptr++) = '\010'; break;
504 case 's': *(ptr++) = ' '; break;
506 case 'f': *(ptr++) = '\014'; break;
508 case 't': *(ptr++) = '\t'; break;
510 case '\\': *(ptr++) = '\\'; break;
512 case '^': *(ptr++) = '^'; break;
514 case ',': *(ptr++) = ','; break;
516 case ':': *(ptr++) = ':'; break;
522 _nc_warning("Illegal character %s in \\ sequence",
523 _tracechar((unsigned char)ch));
525 } /* endswitch (ch) */
526 } /* endelse (ch < '0' || ch > '7') */
527 } /* end else if (ch == '\\') */
536 if (count > MAXCAPLEN)
537 _nc_warning("Very long string found. Missing separator?");
548 * Push a token of given type so that it will be reread by the next
552 void _nc_push_token(int class)
555 * This implementation is kind of bogus, it will fail if we ever do
556 * more than one pushback at a time between get_token() calls. It
557 * relies on the fact that curr_tok is static storage that nothing
558 * but get_token() touches.
561 _nc_get_type(pushname);
563 DEBUG(3, ("pushing token: `%s', class %d",
564 _nc_curr_token.tk_name, pushtype));
568 * Panic mode error recovery - skip everything until a "ch" is found.
570 void _nc_panic_mode(char ch)
583 /*****************************************************************************
585 * Character-stream handling
587 *****************************************************************************/
589 #define LEXBUFSIZ 1024
591 static char *bufptr; /* otherwise, the input buffer pointer */
592 static char *bufstart; /* start of buffer so we can compute offsets */
593 static FILE *yyin; /* scanner's input file descriptor */
598 * Resets the input-reading routines. Used on initialization,
599 * or after a seek has been done. Exactly one argument must be
603 void _nc_reset_input(FILE *fp, char *buf)
605 pushtype = NO_PUSHBACK;
608 bufstart = bufptr = buf;
609 _nc_curr_file_pos = 0L;
618 * Returns the next character in the input stream. Comments and leading
619 * white space are stripped.
621 * The global state variable 'firstcolumn' is set TRUE if the character
622 * returned is from the first column of the input line.
624 * The global variable _nc_curr_line is incremented for each new line.
625 * The global variable _nc_curr_file_pos is set to the file offset of the
626 * beginning of each line.
636 if (*bufptr == '\n') {
641 else if (!bufptr || !*bufptr)
644 * In theory this could be recoded to do its I/O one
645 * character at a time, saving the buffer space. In
646 * practice, this turns out to be quite hard to get
647 * completely right. Try it and see. If you succeed,
648 * don't forget to hack push_back() correspondingly.
650 static char line[LEXBUFSIZ];
653 _nc_curr_file_pos = ftell(yyin);
655 if ((bufstart = fgets(line, LEXBUFSIZ, yyin)) != NULL) {
661 (bufstart != NULL && line[0] == '#');
663 if (bufstart == NULL)
666 while (iswhite(*bufptr))
670 first_column = (bufptr == bufstart);
676 static void push_back(char c)
677 /* push a character back onto the input stream */
679 if (bufptr == bufstart)
680 _nc_syserr_abort("Can't backspace off beginning of line");
684 static long stream_pos(void)
685 /* return our current character position in the input stream */
687 return (yyin ? ftell(yyin) : (bufptr ? bufptr - bufstart : 0));
690 static bool end_of_stream(void)
691 /* are we at end of input? */
693 return (yyin ? feof(yyin) : (bufptr && *bufptr == '\0'));
696 /* comp_scan.c ends here */