1 /****************************************************************************
2 * Copyright (c) 1998-2005,2006 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 * and: Thomas E. Dickey 1996 on *
33 ****************************************************************************/
36 * comp_scan.c --- Lexical scanner for terminfo compiler.
43 * long _nc_curr_file_pos;
44 * long _nc_comment_start;
45 * long _nc_comment_end;
48 #include <curses.priv.h>
51 #include <term_entry.h>
54 MODULE_ID("$Id: comp_scan.c,v 1.78 2006/12/16 19:17:01 tom Exp $")
57 * Maximum length of string capability we'll accept before raising an error.
58 * Yes, there is a real capability in /etc/termcap this long, an "is".
62 #define iswhite(ch) (ch == ' ' || ch == '\t')
64 NCURSES_EXPORT_VAR(int)
65 _nc_syntax = 0; /* termcap or terminfo? */
66 NCURSES_EXPORT_VAR(long)
67 _nc_curr_file_pos = 0; /* file offset of current line */
68 NCURSES_EXPORT_VAR(long)
69 _nc_comment_start = 0; /* start of comment range before name */
70 NCURSES_EXPORT_VAR(long)
71 _nc_comment_end = 0; /* end of comment range before name */
72 NCURSES_EXPORT_VAR(long)
73 _nc_start_line = 0; /* start line of current entry */
75 NCURSES_EXPORT_VAR(struct token)
81 /*****************************************************************************
83 * Token-grabbing machinery
85 *****************************************************************************/
87 static bool first_column; /* See 'next_char()' below */
88 static bool had_newline;
89 static char separator; /* capability separator */
90 static int pushtype; /* type of pushback token */
91 static char *pushname;
94 NCURSES_EXPORT_VAR(bool)
95 _nc_disable_period = FALSE; /* used by tic -a option */
98 /*****************************************************************************
100 * Character-stream handling
102 *****************************************************************************/
104 #define LEXBUFSIZ 1024
106 static char *bufptr; /* otherwise, the input buffer pointer */
107 static char *bufstart; /* start of buffer so we can compute offsets */
108 static FILE *yyin; /* scanner's input file descriptor */
113 * Resets the input-reading routines. Used on initialization,
114 * or after a seek has been done. Exactly one argument must be
119 _nc_reset_input(FILE *fp, char *buf)
121 pushtype = NO_PUSHBACK;
125 bufstart = bufptr = buf;
126 _nc_curr_file_pos = 0L;
135 * Returns the final nonblank character on the current input buffer
140 size_t len = strlen(bufptr);
142 if (!isspace(UChar(bufptr[len])))
151 * Returns the next character in the input stream. Comments and leading
152 * white space are stripped.
154 * The global state variable 'firstcolumn' is set TRUE if the character
155 * returned is from the first column of the input line.
157 * The global variable _nc_curr_line is incremented for each new line.
158 * The global variable _nc_curr_file_pos is set to the file offset of the
159 * beginning of each line.
166 static size_t allocated;
172 FreeAndNull(pushname);
176 * An string with an embedded null will truncate the input. This is
177 * intentional (we don't read binary files here).
179 if (bufptr == 0 || *bufptr == '\0')
181 if (*bufptr == '\n') {
184 } else if (*bufptr == '\t') {
185 _nc_curr_col = (_nc_curr_col | 7);
187 } else if (!bufptr || !*bufptr) {
189 * In theory this could be recoded to do its I/O one character at a
190 * time, saving the buffer space. In practice, this turns out to be
191 * quite hard to get completely right. Try it and see. If you
192 * succeed, don't forget to hack push_back() correspondingly.
201 if (used + (LEXBUFSIZ / 4) >= allocated) {
202 allocated += (allocated + LEXBUFSIZ);
203 result = typeRealloc(char, allocated, result);
208 _nc_curr_file_pos = ftell(yyin);
210 if (fgets(result + used, (int) (allocated - used), yyin) != 0) {
218 strcat(result, "\n");
220 if ((bufptr = bufstart) != 0) {
221 used = strlen(bufptr);
222 while (iswhite(*bufptr)) {
223 if (*bufptr == '\t') {
224 _nc_curr_col = (_nc_curr_col | 7) + 1;
232 * Treat a trailing <cr><lf> the same as a <newline> so we
233 * can read files on OS/2, etc.
235 if ((len = strlen(bufptr)) > 1) {
236 if (bufptr[len - 1] == '\n'
237 && bufptr[len - 2] == '\r') {
239 bufptr[len - 1] = '\n';
246 } while (bufptr[len - 1] != '\n'); /* complete a line */
247 } while (result[0] == '#'); /* ignore comments */
248 } else if (*bufptr == '\t') {
249 _nc_curr_col = (_nc_curr_col | 7);
252 first_column = (bufptr == bufstart);
257 the_char = *bufptr++;
258 return UChar(the_char);
263 /* push a character back onto the input stream */
265 if (bufptr == bufstart)
266 _nc_syserr_abort("Can't backspace off beginning of line");
273 /* return our current character position in the input stream */
275 return (yyin ? ftell(yyin) : (bufptr ? bufptr - bufstart : 0));
280 /* are we at end of input? */
282 return ((yyin ? feof(yyin) : (bufptr && *bufptr == '\0'))
286 /* Assume we may be looking at a termcap-style continuation */
287 static NCURSES_INLINE int
288 eat_escaped_newline(int ch)
291 while ((ch = next_char()) == '\n' || iswhite(ch))
300 * Scans the input for the next token, storing the specifics in the
301 * global structure 'curr_token' and returning one of the following:
303 * NAMES A line beginning in column 1. 'name'
304 * will be set to point to everything up to but
305 * not including the first separator on the line.
306 * BOOLEAN An entry consisting of a name followed by
307 * a separator. 'name' will be set to point to
308 * the name of the capability.
309 * NUMBER An entry of the form
311 * 'name' will be set to point to the capability
312 * name and 'valnumber' to the number given.
313 * STRING An entry of the form
315 * 'name' is set to the capability name and
316 * 'valstring' to the string of characters, with
317 * input translations done.
318 * CANCEL An entry of the form
320 * 'name' is set to the capability name and
322 * EOF The end of the file has been reached.
324 * A `separator' is either a comma or a semicolon, depending on whether
325 * we are in termcap or terminfo mode.
330 _nc_get_token(bool silent)
332 static const char terminfo_punct[] = "@%&*!#";
342 int dot_flag = FALSE;
352 if (pushtype != NO_PUSHBACK) {
353 int retval = pushtype;
355 _nc_set_type(pushname != 0 ? pushname : "");
356 DEBUG(3, ("pushed-back token: `%s', class %d",
357 _nc_curr_token.tk_name, pushtype));
359 pushtype = NO_PUSHBACK;
363 /* currtok wasn't altered by _nc_push_token() */
367 if (end_of_stream()) {
369 next_char(); /* frees its allocated memory */
371 if (_nc_curr_token.tk_name == buffer)
372 _nc_curr_token.tk_name = 0;
379 token_start = stream_pos();
380 while ((ch = next_char()) == '\n' || iswhite(ch)) {
386 ch = eat_escaped_newline(ch);
389 old_line = _nc_curr_line;
390 old_col = _nc_curr_col;
395 /* if this is a termcap entry, skip a leading separator */
396 if (separator == ':' && ch == ':')
400 #if NCURSES_EXT_FUNCS
401 && !_nc_disable_period
405 DEBUG(8, ("dot-flag set"));
407 while ((ch = next_char()) == '.' || iswhite(ch))
416 /* have to make some punctuation chars legal for terminfo */
417 if (!isalnum(UChar(ch))
418 #if NCURSES_EXT_FUNCS
419 && !(ch == '.' && _nc_disable_period)
421 && !strchr(terminfo_punct, (char) ch)) {
423 _nc_warning("Illegal character (expected alphanumeric or %s) - '%s'",
424 terminfo_punct, unctrl((chtype) ch));
425 _nc_panic_mode(separator);
430 buffer = typeMalloc(char, MAX_ENTRY_SIZE);
433 old_line = _nc_curr_line;
434 old_col = _nc_curr_col;
440 _nc_comment_start = token_start;
441 _nc_comment_end = _nc_curr_file_pos;
442 _nc_start_line = _nc_curr_line;
447 while ((ch = next_char()) != '\n') {
449 _nc_err_abort(MSG_NO_INPUTS);
450 } else if (ch == '|') {
454 } else if (ch == ':' && last_char() != ',') {
455 _nc_syntax = SYN_TERMCAP;
458 } else if (ch == ',') {
459 _nc_syntax = SYN_TERMINFO;
462 * If we did not see a '|', then we found a name with no
463 * aliases or description.
468 * If we see a comma, we assume this is terminfo unless we
469 * subsequently run into a colon. But we don't stop
470 * looking for a colon until hitting a newline. This
471 * allows commas to be embedded in description fields of
475 ch = eat_escaped_newline(ch);
480 if (_nc_syntax == ERR) {
482 * Grrr...what we ought to do here is barf, complaining that
483 * the entry is malformed. But because a couple of name fields
484 * in the 8.2 termcap file end with |\, we just have to assume
485 * it's termcap syntax.
487 _nc_syntax = SYN_TERMCAP;
489 } else if (_nc_syntax == SYN_TERMINFO) {
490 /* throw away trailing /, *$/ */
491 for (--ptr; iswhite(*ptr) || *ptr == ','; ptr--)
497 * This is the soonest we have the terminal name fetched. Set up
498 * for following warning messages. If there's no '|', then there
501 if (after_name != 0) {
504 _nc_set_type(buffer);
509 * Compute the boundary between the aliases and the description
510 * field for syntax-checking purposes.
512 if (after_list != 0) {
514 if (*after_list == '\0')
515 _nc_warning("empty longname field");
516 else if (strchr(after_list, ' ') == 0)
517 _nc_warning("older tic versions may treat the description field as an alias");
520 after_list = buffer + strlen(buffer);
521 DEBUG(1, ("missing description"));
525 * Whitespace in a name field other than the long name can confuse
526 * rdist and some termcap tools. Slashes are a no-no. Other
527 * special characters can be dangerous due to shell expansion.
529 for (s = buffer; s < after_list; ++s) {
530 if (isspace(UChar(*s))) {
532 _nc_warning("whitespace in name or alias field");
534 } else if (*s == '/') {
536 _nc_warning("slashes aren't allowed in names or aliases");
538 } else if (strchr("$[]!*?", *s)) {
540 _nc_warning("dubious character `%c' in name or alias field", *s);
545 _nc_curr_token.tk_name = buffer;
548 if (had_newline && _nc_syntax == SYN_TERMCAP) {
549 _nc_warning("Missing backslash before newline");
552 while ((ch = next_char()) != EOF) {
553 if (!isalnum(UChar(ch))) {
554 if (_nc_syntax == SYN_TERMINFO) {
557 } else { /* allow ';' for "k;" */
570 _nc_err_abort("Separator inconsistent with syntax");
571 _nc_curr_token.tk_name = buffer;
575 if ((ch = next_char()) != separator && !silent)
576 _nc_warning("Missing separator after `%s', have %s",
577 buffer, unctrl((chtype) ch));
578 _nc_curr_token.tk_name = buffer;
584 while (isalnum(ch = next_char())) {
585 numbuf[found++] = ch;
586 if (found >= sizeof(numbuf) - 1)
589 numbuf[found] = '\0';
590 number = strtol(numbuf, &numchk, 0);
592 if (numchk == numbuf)
593 _nc_warning("no value given for `%s'", buffer);
594 if ((*numchk != '\0') || (ch != separator))
595 _nc_warning("Missing separator");
597 _nc_curr_token.tk_name = buffer;
598 _nc_curr_token.tk_valnumber = number;
603 ch = _nc_trans_string(ptr, buffer + MAX_ENTRY_SIZE);
604 if (!silent && ch != separator)
605 _nc_warning("Missing separator");
606 _nc_curr_token.tk_name = buffer;
607 _nc_curr_token.tk_valstring = ptr;
615 /* just to get rid of the compiler warning */
618 _nc_warning("Illegal character - '%s'", unctrl((chtype) ch));
620 } /* end else (first_column == FALSE) */
621 } /* end else (ch != EOF) */
626 if (dot_flag == TRUE)
627 DEBUG(8, ("Commented out "));
629 if (_nc_tracing >= DEBUG_LEVEL(8)) {
630 _tracef("parsed %d.%d to %d.%d",
632 _nc_curr_line, _nc_curr_col);
634 if (_nc_tracing >= DEBUG_LEVEL(7)) {
637 _tracef("Token: Boolean; name='%s'",
638 _nc_curr_token.tk_name);
642 _tracef("Token: Number; name='%s', value=%d",
643 _nc_curr_token.tk_name,
644 _nc_curr_token.tk_valnumber);
648 _tracef("Token: String; name='%s', value=%s",
649 _nc_curr_token.tk_name,
650 _nc_visbuf(_nc_curr_token.tk_valstring));
654 _tracef("Token: Cancel; name='%s'",
655 _nc_curr_token.tk_name);
660 _tracef("Token: Names; value='%s'",
661 _nc_curr_token.tk_name);
665 _tracef("Token: End of file");
669 _nc_warning("Bad token type");
674 if (dot_flag == TRUE) /* if commented out, use the next one */
675 type = _nc_get_token(silent);
677 DEBUG(3, ("token: `%s', class %d",
678 ((_nc_curr_token.tk_name != 0)
679 ? _nc_curr_token.tk_name
690 * Reads characters using next_char() until encountering a separator, nl,
691 * or end-of-file. The returned value is the character which caused
692 * reading to stop. The following translations are done on the input:
694 * ^X goes to ctrl-X (i.e. X & 037)
695 * {\E,\n,\r,\b,\t,\f} go to
696 * {ESCAPE,newline,carriage-return,backspace,tab,formfeed}
697 * {\^,\\} go to {carat,backslash}
698 * \ddd (for ddd = up to three octal digits) goes to the character ddd
706 _nc_trans_string(char *ptr, char *last)
711 chtype ch, last_ch = '\0';
712 bool ignored = FALSE;
713 bool long_warning = FALSE;
715 while ((ch = c = next_char()) != (chtype) separator && c != EOF) {
716 if (ptr == (last - 1))
718 if ((_nc_syntax == SYN_TERMCAP) && c == '\n')
720 if (ch == '^' && last_ch != '%') {
721 ch = c = next_char();
723 _nc_err_abort(MSG_NO_INPUTS);
725 if (!(is7bits(ch) && isprint(ch))) {
726 _nc_warning("Illegal ^ character - '%s'", unctrl(ch));
731 _nc_warning("Allow ^? as synonym for \\177");
733 if ((ch &= 037) == 0)
735 *(ptr++) = (char) (ch);
737 } else if (ch == '\\') {
738 ch = c = next_char();
740 _nc_err_abort(MSG_NO_INPUTS);
742 if (ch >= '0' && ch <= '7') {
744 for (i = 0; i < 2; i++) {
745 ch = c = next_char();
747 _nc_err_abort(MSG_NO_INPUTS);
749 if (c < '0' || c > '7') {
751 _nc_warning("Non-octal digit `%c' in \\ sequence", c);
752 /* allow the digit; it'll do less harm */
759 number = number * 8 + c - '0';
764 *(ptr++) = (char) number;
821 _nc_warning("Illegal character '%s' in \\ sequence",
825 *(ptr++) = (char) ch;
826 } /* endswitch (ch) */
827 } /* endelse (ch < '0' || ch > '7') */
829 /* end else if (ch == '\\') */
830 else if (ch == '\n' && (_nc_syntax == SYN_TERMINFO)) {
832 * Newlines embedded in a terminfo string are ignored, provided
833 * that the next line begins with whitespace.
837 *(ptr++) = (char) ch;
841 if (_nc_curr_col <= 1) {
851 if (count > MAXCAPLEN && !long_warning) {
852 _nc_warning("Very long string found. Missing separator?");
865 * Push a token of given type so that it will be reread by the next
870 _nc_push_token(int tokclass)
873 * This implementation is kind of bogus, it will fail if we ever do more
874 * than one pushback at a time between get_token() calls. It relies on the
875 * fact that _nc_curr_token is static storage that nothing but
876 * _nc_get_token() touches.
880 pushname = typeMalloc(char, MAX_NAME_SIZE + 1);
881 _nc_get_type(pushname);
883 DEBUG(3, ("pushing token: `%s', class %d",
884 ((_nc_curr_token.tk_name != 0)
885 ? _nc_curr_token.tk_name
891 * Panic mode error recovery - skip everything until a "ch" is found.
894 _nc_panic_mode(char ch)
909 _nc_comp_scan_leaks(void)
912 FreeAndNull(pushname);