1 /****************************************************************************
2 * Copyright (c) 1998-2013,2016 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>
53 MODULE_ID("$Id: comp_scan.c,v 1.103 2016/05/28 23:22:52 tom Exp $")
56 * Maximum length of string capability we'll accept before raising an error.
57 * Yes, there is a real capability in /etc/termcap this long, an "is".
61 #define iswhite(ch) (ch == ' ' || ch == '\t')
63 NCURSES_EXPORT_VAR (int) _nc_syntax = 0; /* termcap or terminfo? */
64 NCURSES_EXPORT_VAR (int) _nc_strict_bsd = 1; /* ncurses extended termcap? */
65 NCURSES_EXPORT_VAR (long) _nc_curr_file_pos = 0; /* file offset of current line */
66 NCURSES_EXPORT_VAR (long) _nc_comment_start = 0; /* start of comment range before name */
67 NCURSES_EXPORT_VAR (long) _nc_comment_end = 0; /* end of comment range before name */
68 NCURSES_EXPORT_VAR (long) _nc_start_line = 0; /* start line of current entry */
70 NCURSES_EXPORT_VAR (struct token) _nc_curr_token =
75 /*****************************************************************************
77 * Token-grabbing machinery
79 *****************************************************************************/
81 static bool first_column; /* See 'next_char()' below */
82 static bool had_newline;
83 static char separator; /* capability separator */
84 static int pushtype; /* type of pushback token */
85 static char *pushname;
88 NCURSES_EXPORT_VAR (bool) _nc_disable_period = FALSE; /* used by tic -a option */
91 /*****************************************************************************
93 * Character-stream handling
95 *****************************************************************************/
97 #define LEXBUFSIZ 1024
99 static char *bufptr; /* otherwise, the input buffer pointer */
100 static char *bufstart; /* start of buffer so we can compute offsets */
101 static FILE *yyin; /* scanner's input file descriptor */
106 * Resets the input-reading routines. Used on initialization,
107 * or after a seek has been done. Exactly one argument must be
112 _nc_reset_input(FILE *fp, char *buf)
114 pushtype = NO_PUSHBACK;
118 bufstart = bufptr = buf;
119 _nc_curr_file_pos = 0L;
128 * Returns the final nonblank character on the current input buffer
131 last_char(int from_end)
133 size_t len = strlen(bufptr);
137 if (!isspace(UChar(bufptr[len]))) {
138 if (from_end < (int) len)
139 result = bufptr[(int) len - from_end];
149 * Returns the next character in the input stream. Comments and leading
150 * white space are stripped.
152 * The global state variable 'firstcolumn' is set TRUE if the character
153 * returned is from the first column of the input line.
155 * The global variable _nc_curr_line is incremented for each new line.
156 * The global variable _nc_curr_file_pos is set to the file offset of the
157 * beginning of each line.
164 static size_t allocated;
170 FreeAndNull(pushname);
174 * An string with an embedded null will truncate the input. This is
175 * intentional (we don't read binary files here).
177 if (bufptr == 0 || *bufptr == '\0')
179 if (*bufptr == '\n') {
182 } else if (*bufptr == '\t') {
183 _nc_curr_col = (_nc_curr_col | 7);
185 } else if (!bufptr || !*bufptr) {
187 * In theory this could be recoded to do its I/O one character at a
188 * time, saving the buffer space. In practice, this turns out to be
189 * quite hard to get completely right. Try it and see. If you
190 * succeed, don't forget to hack push_back() correspondingly.
198 if (used + (LEXBUFSIZ / 4) >= allocated) {
199 allocated += (allocated + LEXBUFSIZ);
200 result = typeRealloc(char, allocated, result);
207 _nc_curr_file_pos = ftell(yyin);
209 if (fgets(result + used, (int) (allocated - used), yyin) != 0) {
212 if (_nc_curr_line == 0
213 && IS_TIC_MAGIC(result)) {
214 _nc_err_abort("This is a compiled terminal description, not a source");
221 _nc_STRCAT(result, "\n", allocated);
223 if ((bufptr = bufstart) != 0) {
224 used = strlen(bufptr);
225 while (iswhite(*bufptr)) {
226 if (*bufptr == '\t') {
227 _nc_curr_col = (_nc_curr_col | 7) + 1;
235 * Treat a trailing <cr><lf> the same as a <newline> so we
236 * can read files on OS/2, etc.
238 if ((len = strlen(bufptr)) > 1) {
239 if (bufptr[len - 1] == '\n'
240 && bufptr[len - 2] == '\r') {
242 bufptr[len - 1] = '\n';
249 } while (bufptr[len - 1] != '\n'); /* complete a line */
250 } while (result[0] == '#'); /* ignore comments */
251 } else if (*bufptr == '\t') {
252 _nc_curr_col = (_nc_curr_col | 7);
255 first_column = (bufptr == bufstart);
260 the_char = *bufptr++;
261 return UChar(the_char);
266 /* push a character back onto the input stream */
268 if (bufptr == bufstart)
269 _nc_syserr_abort("Can't backspace off beginning of line");
270 *--bufptr = (char) c;
276 /* return our current character position in the input stream */
278 return (yyin ? ftell(yyin) : (bufptr ? bufptr - bufstart : 0));
283 /* are we at end of input? */
285 return ((yyin ? feof(yyin) : (bufptr && *bufptr == '\0'))
289 /* Assume we may be looking at a termcap-style continuation */
290 static NCURSES_INLINE int
291 eat_escaped_newline(int ch)
294 while ((ch = next_char()) == '\n' || iswhite(ch))
299 #define TOK_BUF_SIZE MAX_ENTRY_SIZE
302 ((tok_ptr - tok_buf) < (TOK_BUF_SIZE - 2))
305 *tok_ptr++ = (char) ch; \
308 static char *tok_buf;
314 * Scans the input for the next token, storing the specifics in the
315 * global structure 'curr_token' and returning one of the following:
317 * NAMES A line beginning in column 1. 'name'
318 * will be set to point to everything up to but
319 * not including the first separator on the line.
320 * BOOLEAN An entry consisting of a name followed by
321 * a separator. 'name' will be set to point to
322 * the name of the capability.
323 * NUMBER An entry of the form
325 * 'name' will be set to point to the capability
326 * name and 'valnumber' to the number given.
327 * STRING An entry of the form
329 * 'name' is set to the capability name and
330 * 'valstring' to the string of characters, with
331 * input translations done.
332 * CANCEL An entry of the form
334 * 'name' is set to the capability name and
336 * EOF The end of the file has been reached.
338 * A `separator' is either a comma or a semicolon, depending on whether
339 * we are in termcap or terminfo mode.
344 _nc_get_token(bool silent)
346 static const char terminfo_punct[] = "@%&*!#";
348 char *after_name; /* after primary name */
349 char *after_list; /* after primary and alias list */
355 int dot_flag = FALSE;
365 if (pushtype != NO_PUSHBACK) {
366 int retval = pushtype;
368 _nc_set_type(pushname != 0 ? pushname : "");
369 DEBUG(3, ("pushed-back token: `%s', class %d",
370 _nc_curr_token.tk_name, pushtype));
372 pushtype = NO_PUSHBACK;
376 /* currtok wasn't altered by _nc_push_token() */
380 if (end_of_stream()) {
382 (void) next_char(); /* frees its allocated memory */
384 if (_nc_curr_token.tk_name == tok_buf)
385 _nc_curr_token.tk_name = 0;
391 token_start = stream_pos();
392 while ((ch = next_char()) == '\n' || iswhite(ch)) {
398 ch = eat_escaped_newline(ch);
399 _nc_curr_token.tk_valstring = 0;
402 old_line = _nc_curr_line;
403 old_col = _nc_curr_col;
408 /* if this is a termcap entry, skip a leading separator */
409 if (separator == ':' && ch == ':')
413 #if NCURSES_EXT_FUNCS
414 && !_nc_disable_period
418 DEBUG(8, ("dot-flag set"));
420 while ((ch = next_char()) == '.' || iswhite(ch))
429 /* have to make some punctuation chars legal for terminfo */
430 if (!isalnum(UChar(ch))
431 #if NCURSES_EXT_FUNCS
432 && !(ch == '.' && _nc_disable_period)
434 && ((strchr) (terminfo_punct, (char) ch) == 0)) {
436 _nc_warning("Illegal character (expected alphanumeric or %s) - '%s'",
437 terminfo_punct, unctrl(UChar(ch)));
438 _nc_panic_mode(separator);
443 tok_buf = typeMalloc(char, TOK_BUF_SIZE);
446 old_line = _nc_curr_line;
447 old_col = _nc_curr_col;
453 _nc_comment_start = token_start;
454 _nc_comment_end = _nc_curr_file_pos;
455 _nc_start_line = _nc_curr_line;
460 while ((ch = next_char()) != '\n') {
462 _nc_err_abort(MSG_NO_INPUTS);
463 } else if (ch == '|') {
464 after_list = tok_ptr;
466 after_name = tok_ptr;
467 } else if (ch == ':' && last_char(0) != ',') {
468 _nc_syntax = SYN_TERMCAP;
471 } else if (ch == ',') {
472 _nc_syntax = SYN_TERMINFO;
475 * If we did not see a '|', then we found a name with no
476 * aliases or description.
481 * We saw a comma, but are not entirely sure this is
482 * terminfo format, since we can still be parsing the
483 * description field (for either syntax).
485 * A properly formatted termcap line ends with either a
486 * colon, or a backslash after a colon. It is possible
487 * to have a backslash in the middle of a capability, but
488 * then there would be no leading whitespace on the next
489 * line - something we want to discourage.
493 if (c1 != ':' && c0 != '\\' && c0 != ':') {
494 bool capability = FALSE;
497 * Since it is not termcap, assume the line is terminfo
498 * format. However, the comma can be embedded in a
499 * description field. It also can be a separator
500 * between a description field and a capability.
502 * Improve the guess by checking if the next word after
503 * the comma does not look like a capability. In that
504 * case, extend the description past the comma.
506 for (s = bufptr; isspace(UChar(*s)); ++s) {
509 if (islower(UChar(*s))) {
511 while (isalnum(UChar(*s))) {
514 if (*s == '#' || *s == '=' || *s == '@') {
516 * Checking solely with syntax allows us to
517 * support extended capabilities with string
521 } else if (*s == ',') {
525 * Otherwise, we can handle predefined boolean
526 * capabilities, still aided by syntax.
528 if (_nc_find_entry(name,
529 _nc_get_hash_table(FALSE))) {
540 ch = eat_escaped_newline(ch);
549 if (_nc_syntax == ERR) {
551 * Grrr...what we ought to do here is barf, complaining that
552 * the entry is malformed. But because a couple of name fields
553 * in the 8.2 termcap file end with |\, we just have to assume
554 * it's termcap syntax.
556 _nc_syntax = SYN_TERMCAP;
558 } else if (_nc_syntax == SYN_TERMINFO) {
559 /* throw away trailing /, *$/ */
561 iswhite(*tok_ptr) || *tok_ptr == ',';
568 * This is the soonest we have the terminal name fetched. Set up
569 * for following warning messages. If there's no '|', then there
572 if (after_name != 0) {
575 _nc_set_type(tok_buf);
576 *after_name = (char) ch;
580 * Compute the boundary between the aliases and the description
581 * field for syntax-checking purposes.
583 if (after_list != 0) {
585 if (*after_list == '\0')
586 _nc_warning("empty longname field");
587 else if (strchr(after_list, ' ') == 0)
588 _nc_warning("older tic versions may treat the description field as an alias");
591 after_list = tok_buf + strlen(tok_buf);
592 DEBUG(1, ("missing description"));
596 * Whitespace in a name field other than the long name can confuse
597 * rdist and some termcap tools. Slashes are a no-no. Other
598 * special characters can be dangerous due to shell expansion.
600 for (s = tok_buf; s < after_list; ++s) {
601 if (isspace(UChar(*s))) {
603 _nc_warning("whitespace in name or alias field");
605 } else if (*s == '/') {
607 _nc_warning("slashes aren't allowed in names or aliases");
609 } else if (strchr("$[]!*?", *s)) {
611 _nc_warning("dubious character `%c' in name or alias field", *s);
616 _nc_curr_token.tk_name = tok_buf;
619 if (had_newline && _nc_syntax == SYN_TERMCAP) {
620 _nc_warning("Missing backslash before newline");
623 while ((ch = next_char()) != EOF) {
624 if (!isalnum(UChar(ch))) {
625 if (_nc_syntax == SYN_TERMINFO) {
628 } else { /* allow ';' for "k;" */
641 *tok_ptr++ = '\0'; /* separate name/value in buffer */
646 _nc_err_abort("Separator inconsistent with syntax");
647 _nc_curr_token.tk_name = tok_buf;
651 if ((ch = next_char()) != separator && !silent)
652 _nc_warning("Missing separator after `%s', have %s",
653 tok_buf, unctrl(UChar(ch)));
654 _nc_curr_token.tk_name = tok_buf;
660 while (isalnum(ch = next_char())) {
661 numbuf[found++] = (char) ch;
662 if (found >= sizeof(numbuf) - 1)
665 numbuf[found] = '\0';
666 number = strtol(numbuf, &numchk, 0);
668 if (numchk == numbuf)
669 _nc_warning("no value given for `%s'", tok_buf);
670 if ((*numchk != '\0') || (ch != separator))
671 _nc_warning("Missing separator");
673 _nc_curr_token.tk_name = tok_buf;
674 _nc_curr_token.tk_valnumber = (int) number;
679 ch = _nc_trans_string(tok_ptr, tok_buf + TOK_BUF_SIZE);
680 if (!silent && ch != separator)
681 _nc_warning("Missing separator");
682 _nc_curr_token.tk_name = tok_buf;
683 _nc_curr_token.tk_valstring = tok_ptr;
691 /* just to get rid of the compiler warning */
694 _nc_warning("Illegal character - '%s'", unctrl(UChar(ch)));
696 } /* end else (first_column == FALSE) */
697 } /* end else (ch != EOF) */
702 if (dot_flag == TRUE)
703 DEBUG(8, ("Commented out "));
705 if (_nc_tracing >= DEBUG_LEVEL(8)) {
706 _tracef("parsed %d.%d to %d.%d",
708 _nc_curr_line, _nc_curr_col);
710 if (_nc_tracing >= DEBUG_LEVEL(7)) {
713 _tracef("Token: Boolean; name='%s'",
714 _nc_curr_token.tk_name);
718 _tracef("Token: Number; name='%s', value=%d",
719 _nc_curr_token.tk_name,
720 _nc_curr_token.tk_valnumber);
724 _tracef("Token: String; name='%s', value=%s",
725 _nc_curr_token.tk_name,
726 _nc_visbuf(_nc_curr_token.tk_valstring));
730 _tracef("Token: Cancel; name='%s'",
731 _nc_curr_token.tk_name);
736 _tracef("Token: Names; value='%s'",
737 _nc_curr_token.tk_name);
741 _tracef("Token: End of file");
745 _nc_warning("Bad token type");
750 if (dot_flag == TRUE) /* if commented out, use the next one */
751 type = _nc_get_token(silent);
753 DEBUG(3, ("token: `%s', class %d",
754 ((_nc_curr_token.tk_name != 0)
755 ? _nc_curr_token.tk_name
766 * Reads characters using next_char() until encountering a separator, nl,
767 * or end-of-file. The returned value is the character which caused
768 * reading to stop. The following translations are done on the input:
770 * ^X goes to ctrl-X (i.e. X & 037)
771 * {\E,\n,\r,\b,\t,\f} go to
772 * {ESCAPE,newline,carriage-return,backspace,tab,formfeed}
773 * {\^,\\} go to {carat,backslash}
774 * \ddd (for ddd = up to three octal digits) goes to the character ddd
782 _nc_trans_string(char *ptr, char *last)
788 bool ignored = FALSE;
789 bool long_warning = FALSE;
791 while ((c = next_char()) != separator && c != EOF) {
792 if (ptr >= (last - 1)) {
794 while ((c = next_char()) != separator && c != EOF) {
800 if ((_nc_syntax == SYN_TERMCAP) && c == '\n')
802 if (c == '^' && last_ch != '%') {
805 _nc_err_abort(MSG_NO_INPUTS);
807 if (!(is7bits(c) && isprint(c))) {
808 _nc_warning("Illegal ^ character - '%s'", unctrl(UChar(c)));
810 if (c == '?' && (_nc_syntax != SYN_TERMCAP)) {
813 _nc_warning("Allow ^? as synonym for \\177");
817 *(ptr++) = (char) (c);
819 } else if (c == '\\') {
820 bool strict_bsd = ((_nc_syntax == SYN_TERMCAP) && _nc_strict_bsd);
824 _nc_err_abort(MSG_NO_INPUTS);
826 #define isoctal(c) ((c) >= '0' && (c) <= '7')
828 if (isoctal(c) || (strict_bsd && isdigit(c))) {
830 for (i = 0; i < 2; i++) {
833 _nc_err_abort(MSG_NO_INPUTS);
838 _nc_warning("Non-octal digit `%c' in \\ sequence", c);
839 /* allow the digit; it'll do less harm */
847 number = number * 8 + c - '0';
850 number = UChar(number);
851 if (number == 0 && !strict_bsd)
853 *(ptr++) = (char) number;
896 if ((_nc_syntax == SYN_TERMINFO) || !_nc_strict_bsd) {
914 _nc_warning("Illegal character '%s' in \\ sequence",
922 } /* endswitch (c) */
923 } /* endelse (c < '0' || c > '7') */
925 /* end else if (c == '\\') */
926 else if (c == '\n' && (_nc_syntax == SYN_TERMINFO)) {
928 * Newlines embedded in a terminfo string are ignored, provided
929 * that the next line begins with whitespace.
937 if (_nc_curr_col <= 1) {
947 if (count > MAXCAPLEN && !long_warning) {
948 _nc_warning("Very long string found. Missing separator?");
961 * Push a token of given type so that it will be reread by the next
966 _nc_push_token(int tokclass)
969 * This implementation is kind of bogus, it will fail if we ever do more
970 * than one pushback at a time between get_token() calls. It relies on the
971 * fact that _nc_curr_token is static storage that nothing but
972 * _nc_get_token() touches.
976 pushname = typeMalloc(char, MAX_NAME_SIZE + 1);
977 _nc_get_type(pushname);
979 DEBUG(3, ("pushing token: `%s', class %d",
980 ((_nc_curr_token.tk_name != 0)
981 ? _nc_curr_token.tk_name
987 * Panic mode error recovery - skip everything until a "ch" is found.
990 _nc_panic_mode(char ch)
1002 NCURSES_EXPORT(void)
1003 _nc_comp_scan_leaks(void)
1005 if (pushname != 0) {
1006 FreeAndNull(pushname);
1009 FreeAndNull(tok_buf);