1 /****************************************************************************
2 * Copyright (c) 1998 Free Software Foundation, Inc. *
4 * Permission is hereby granted, free of charge, to any person obtaining a *
5 * copy of this software and associated documentation files (the *
6 * "Software"), to deal in the Software without restriction, including *
7 * without limitation the rights to use, copy, modify, merge, publish, *
8 * distribute, distribute with modifications, sublicense, and/or sell *
9 * copies of the Software, and to permit persons to whom the Software is *
10 * furnished to do so, subject to the following conditions: *
12 * The above copyright notice and this permission notice shall be included *
13 * in all copies or substantial portions of the Software. *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS *
16 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF *
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. *
18 * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, *
19 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR *
20 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR *
21 * THE USE OR OTHER DEALINGS IN THE SOFTWARE. *
23 * Except as contained in this notice, the name(s) of the above copyright *
24 * holders shall not be used in advertising or otherwise to promote the *
25 * sale, use or other dealings in this Software without prior written *
27 ****************************************************************************/
29 /****************************************************************************
30 * Author: Zeyd M. Ben-Halim <zmbenhal@netcom.com> 1992,1995 *
31 * and: Eric S. Raymond <esr@snark.thyrsus.com> *
32 ****************************************************************************/
35 * comp_scan.c --- Lexical scanner for terminfo compiler.
42 * long _nc_curr_file_pos;
43 * long _nc_comment_start;
44 * long _nc_comment_end;
47 #include <curses.priv.h>
52 MODULE_ID("$Id: comp_scan.c,v 1.34 1998/11/01 00:56:39 tom Exp $")
55 * Maximum length of string capability we'll accept before raising an error.
56 * Yes, there is a real capability in /etc/termcap this long, an "is".
60 #define iswhite(ch) (ch == ' ' || ch == '\t')
62 int _nc_syntax; /* termcap or terminfo? */
63 long _nc_curr_file_pos; /* file offset of current line */
64 long _nc_comment_start; /* start of comment range before name */
65 long _nc_comment_end; /* end of comment range before name */
66 long _nc_start_line; /* start line of current entry */
68 /*****************************************************************************
70 * Token-grabbing machinery
72 *****************************************************************************/
74 static bool first_column; /* See 'next_char()' below */
75 static char separator; /* capability separator */
76 static int pushtype; /* type of pushback token */
77 static char pushname[MAX_NAME_SIZE+1];
79 static int last_char(void);
80 static int next_char(void);
81 static long stream_pos(void);
82 static bool end_of_stream(void);
83 static void push_back(char c);
85 /* Assume we may be looking at a termcap-style continuation */
86 static inline int eat_escaped_newline(int ch)
89 while ((ch = next_char()) == '\n' || iswhite(ch))
98 * Scans the input for the next token, storing the specifics in the
99 * global structure 'curr_token' and returning one of the following:
101 * NAMES A line beginning in column 1. 'name'
102 * will be set to point to everything up to but
103 * not including the first separator on the line.
104 * BOOLEAN An entry consisting of a name followed by
105 * a separator. 'name' will be set to point to
106 * the name of the capability.
107 * NUMBER An entry of the form
109 * 'name' will be set to point to the capability
110 * name and 'valnumber' to the number given.
111 * STRING An entry of the form
113 * 'name' is set to the capability name and
114 * 'valstring' to the string of characters, with
115 * input translations done.
116 * CANCEL An entry of the form
118 * 'name' is set to the capability name and
120 * EOF The end of the file has been reached.
122 * A `separator' is either a comma or a semicolon, depending on whether
123 * we are in termcap or terminfo mode.
127 int _nc_get_token(void)
129 static const char terminfo_punct[] = "@%&*!#";
136 static char buffer[MAX_ENTRY_SIZE];
138 int dot_flag = FALSE;
141 if (pushtype != NO_PUSHBACK)
143 int retval = pushtype;
145 _nc_set_type(pushname);
146 DEBUG(3, ("pushed-back token: `%s', class %d",
147 _nc_curr_token.tk_name, pushtype));
149 pushtype = NO_PUSHBACK;
152 /* currtok wasn't altered by _nc_push_token() */
160 token_start = stream_pos();
161 while ((ch = next_char()) == '\n' || iswhite(ch))
164 ch = eat_escaped_newline(ch);
169 /* if this is a termcap entry, skip a leading separator */
170 if (separator == ':' && ch == ':')
175 DEBUG(8, ("dot-flag set"));
177 while ((ch = next_char())=='.' || iswhite(ch))
186 /* have to make some punctuation chars legal for terminfo */
187 if (!isalnum(ch) && !strchr(terminfo_punct, (char)ch)) {
188 _nc_warning("Illegal character (expected alphanumeric or %s) - %s",
189 terminfo_punct, _tracechar((chtype)ch));
190 _nc_panic_mode(separator);
200 _nc_comment_start = token_start;
201 _nc_comment_end = _nc_curr_file_pos;
202 _nc_start_line = _nc_curr_line;
205 while ((ch = next_char()) != '\n')
208 _nc_err_abort("premature EOF");
209 else if (ch == ':' && last_char() != ',')
211 _nc_syntax = SYN_TERMCAP;
217 _nc_syntax = SYN_TERMINFO;
220 * Fall-through here is not an accident.
221 * The idea is that if we see a comma, we
222 * figure this is terminfo unless we
223 * subsequently run into a colon -- but
224 * we don't stop looking for that colon until
225 * hitting a newline. This allows commas to
226 * be embedded in description fields of
232 ch = eat_escaped_newline(ch);
237 if (_nc_syntax == ERR)
240 * Grrr...what we ought to do here is barf,
241 * complaining that the entry is malformed.
242 * But because a couple of name fields in the
243 * 8.2 termcap file end with |\, we just have
244 * to assume it's termcap syntax.
246 _nc_syntax = SYN_TERMCAP;
249 else if (_nc_syntax == SYN_TERMINFO)
251 /* throw away trailing /, *$/ */
252 for (--ptr; iswhite(*ptr) || *ptr == ','; ptr--)
258 * This is the soonest we have the terminal name
259 * fetched. Set up for following warning messages.
261 ptr = strchr(buffer, '|');
262 if (ptr == (char *)NULL)
263 ptr = buffer + strlen(buffer);
266 _nc_set_type(buffer);
270 * Compute the boundary between the aliases and the
271 * description field for syntax-checking purposes.
273 desc = strrchr(buffer, '|');
276 _nc_warning("empty longname field");
277 else if (strchr(desc, ' ') == (char *)NULL)
278 _nc_warning("older tic versions may treat the description field as an alias");
281 desc = buffer + strlen(buffer);
284 * Whitespace in a name field other than the long name
285 * can confuse rdist and some termcap tools. Slashes
286 * are a no-no. Other special characters can be
287 * dangerous due to shell expansion.
289 for (ptr = buffer; ptr < desc; ptr++)
293 _nc_warning("whitespace in name or alias field");
296 else if (*ptr == '/')
298 _nc_warning("slashes aren't allowed in names or aliases");
301 else if (strchr("$[]!*?", *ptr))
303 _nc_warning("dubious character `%c' in name or alias field", *ptr);
310 _nc_curr_token.tk_name = buffer;
313 while ((ch = next_char()) != EOF) {
315 if (_nc_syntax == SYN_TERMINFO) {
318 } else { /* allow ';' for "k;" */
331 _nc_err_abort("Separator inconsistent with syntax");
332 _nc_curr_token.tk_name = buffer;
336 if ((ch = next_char()) != separator)
337 _nc_warning("Missing separator after `%s', have %s",
338 buffer, _tracechar((chtype)ch));
339 _nc_curr_token.tk_name = buffer;
345 while (isalnum(ch = next_char())) {
346 numbuf[found++] = ch;
347 if (found >= sizeof(numbuf)-1)
350 numbuf[found] = '\0';
351 number = strtol(numbuf, &numchk, 0);
352 if (numchk == numbuf)
353 _nc_warning("no value given for `%s'", buffer);
354 if ((*numchk != '\0') || (ch != separator))
355 _nc_warning("Missing separator");
356 _nc_curr_token.tk_name = buffer;
357 _nc_curr_token.tk_valnumber = number;
362 ch = _nc_trans_string(ptr);
364 _nc_warning("Missing separator");
365 _nc_curr_token.tk_name = buffer;
366 _nc_curr_token.tk_valstring = ptr;
374 /* just to get rid of the compiler warning */
376 _nc_warning("Illegal character - %s",
377 _tracechar((chtype)ch));
379 } /* end else (first_column == FALSE) */
380 } /* end else (ch != EOF) */
385 if (dot_flag == TRUE)
386 DEBUG(8, ("Commented out "));
388 if (_nc_tracing & TRACE_IEVENT)
390 fprintf(stderr, "Token: ");
394 fprintf(stderr, "Boolean; name='%s'\n",
395 _nc_curr_token.tk_name);
399 fprintf(stderr, "Number; name='%s', value=%d\n",
400 _nc_curr_token.tk_name,
401 _nc_curr_token.tk_valnumber);
405 fprintf(stderr, "String; name='%s', value=%s\n",
406 _nc_curr_token.tk_name,
407 _nc_visbuf(_nc_curr_token.tk_valstring));
411 fprintf(stderr, "Cancel; name='%s'\n",
412 _nc_curr_token.tk_name);
417 fprintf(stderr, "Names; value='%s'\n",
418 _nc_curr_token.tk_name);
422 fprintf(stderr, "End of file\n");
426 _nc_warning("Bad token type");
431 if (dot_flag == TRUE) /* if commented out, use the next one */
432 type = _nc_get_token();
434 DEBUG(3, ("token: `%s', class %d", _nc_curr_token.tk_name, type));
443 * Reads characters using next_char() until encountering a separator, nl,
444 * or end-of-file. The returned value is the character which caused
445 * reading to stop. The following translations are done on the input:
447 * ^X goes to ctrl-X (i.e. X & 037)
448 * {\E,\n,\r,\b,\t,\f} go to
449 * {ESCAPE,newline,carriage-return,backspace,tab,formfeed}
450 * {\^,\\} go to {carat,backslash}
451 * \ddd (for ddd = up to three octal digits) goes to the character ddd
459 _nc_trans_string(char *ptr)
464 chtype ch, last_ch = '\0';
465 bool ignored = FALSE;
467 while ((ch = c = next_char()) != (chtype)separator && c != EOF) {
468 if ((_nc_syntax == SYN_TERMCAP) && c == '\n')
470 if (ch == '^' && last_ch != '%') {
471 ch = c = next_char();
473 _nc_err_abort("Premature EOF");
475 if (! (is7bits(ch) && isprint(ch))) {
476 _nc_warning("Illegal ^ character - %s",
477 _tracechar((unsigned char)ch));
482 if ((ch &= 037) == 0)
484 *(ptr++) = (char)(ch);
487 else if (ch == '\\') {
488 ch = c = next_char();
490 _nc_err_abort("Premature EOF");
492 if (ch >= '0' && ch <= '7') {
494 for (i=0; i < 2; i++) {
495 ch = c = next_char();
497 _nc_err_abort("Premature EOF");
499 if (c < '0' || c > '7') {
501 _nc_warning("Non-octal digit `%c' in \\ sequence", c);
502 /* allow the digit; it'll do less harm */
509 number = number * 8 + c - '0';
514 *(ptr++) = (char) number;
518 case 'e': *(ptr++) = '\033'; break;
520 case 'a': *(ptr++) = '\007'; break;
523 case 'n': *(ptr++) = '\n'; break;
525 case 'r': *(ptr++) = '\r'; break;
527 case 'b': *(ptr++) = '\010'; break;
529 case 's': *(ptr++) = ' '; break;
531 case 'f': *(ptr++) = '\014'; break;
533 case 't': *(ptr++) = '\t'; break;
535 case '\\': *(ptr++) = '\\'; break;
537 case '^': *(ptr++) = '^'; break;
539 case ',': *(ptr++) = ','; break;
541 case ':': *(ptr++) = ':'; break;
547 _nc_warning("Illegal character %s in \\ sequence",
548 _tracechar((unsigned char)ch));
550 } /* endswitch (ch) */
551 } /* endelse (ch < '0' || ch > '7') */
552 } /* end else if (ch == '\\') */
553 else if (ch == '\n' && (_nc_syntax == SYN_TERMINFO)) {
554 /* newlines embedded in a terminfo string are ignored */
566 if (count > MAXCAPLEN)
567 _nc_warning("Very long string found. Missing separator?");
578 * Push a token of given type so that it will be reread by the next
582 void _nc_push_token(int tokclass)
585 * This implementation is kind of bogus, it will fail if we ever do
586 * more than one pushback at a time between get_token() calls. It
587 * relies on the fact that curr_tok is static storage that nothing
588 * but get_token() touches.
591 _nc_get_type(pushname);
593 DEBUG(3, ("pushing token: `%s', class %d",
594 _nc_curr_token.tk_name, pushtype));
598 * Panic mode error recovery - skip everything until a "ch" is found.
600 void _nc_panic_mode(char ch)
613 /*****************************************************************************
615 * Character-stream handling
617 *****************************************************************************/
619 #define LEXBUFSIZ 1024
621 static char *bufptr; /* otherwise, the input buffer pointer */
622 static char *bufstart; /* start of buffer so we can compute offsets */
623 static FILE *yyin; /* scanner's input file descriptor */
628 * Resets the input-reading routines. Used on initialization,
629 * or after a seek has been done. Exactly one argument must be
633 void _nc_reset_input(FILE *fp, char *buf)
635 pushtype = NO_PUSHBACK;
638 bufstart = bufptr = buf;
639 _nc_curr_file_pos = 0L;
648 * Returns the final nonblank character on the current input buffer
653 size_t len = strlen(bufptr);
655 if (!isspace(bufptr[len]))
664 * Returns the next character in the input stream. Comments and leading
665 * white space are stripped.
667 * The global state variable 'firstcolumn' is set TRUE if the character
668 * returned is from the first column of the input line.
670 * The global variable _nc_curr_line is incremented for each new line.
671 * The global variable _nc_curr_file_pos is set to the file offset of the
672 * beginning of each line.
682 if (*bufptr == '\n') {
687 else if (!bufptr || !*bufptr)
690 * In theory this could be recoded to do its I/O one
691 * character at a time, saving the buffer space. In
692 * practice, this turns out to be quite hard to get
693 * completely right. Try it and see. If you succeed,
694 * don't forget to hack push_back() correspondingly.
696 static char line[LEXBUFSIZ];
700 _nc_curr_file_pos = ftell(yyin);
702 if ((bufstart = fgets(line, LEXBUFSIZ, yyin)) != NULL) {
708 (bufstart != NULL && line[0] == '#');
710 if (bufstart == NULL)
713 while (iswhite(*bufptr))
717 * Treat a trailing <cr><lf> the same as a <newline> so we can read
718 * files on OS/2, etc.
720 if ((len = strlen(bufptr)) > 1) {
721 if (bufptr[len-1] == '\n'
722 && bufptr[len-2] == '\r') {
723 bufptr[len-2] = '\n';
724 bufptr[len-1] = '\0';
729 first_column = (bufptr == bufstart);
735 static void push_back(char c)
736 /* push a character back onto the input stream */
738 if (bufptr == bufstart)
739 _nc_syserr_abort("Can't backspace off beginning of line");
743 static long stream_pos(void)
744 /* return our current character position in the input stream */
746 return (yyin ? ftell(yyin) : (bufptr ? bufptr - bufstart : 0));
749 static bool end_of_stream(void)
750 /* are we at end of input? */
752 return ((yyin ? feof(yyin) : (bufptr && *bufptr == '\0'))
756 /* comp_scan.c ends here */