1 /****************************************************************************
2 ,* Copyright 2020-2021,2022 Thomas E. Dickey *
3 * Copyright 1998-2016,2017 Free Software Foundation, Inc. *
5 * Permission is hereby granted, free of charge, to any person obtaining a *
6 * copy of this software and associated documentation files (the *
7 * "Software"), to deal in the Software without restriction, including *
8 * without limitation the rights to use, copy, modify, merge, publish, *
9 * distribute, distribute with modifications, sublicense, and/or sell *
10 * copies of the Software, and to permit persons to whom the Software is *
11 * furnished to do so, subject to the following conditions: *
13 * The above copyright notice and this permission notice shall be included *
14 * in all copies or substantial portions of the Software. *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS *
17 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF *
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. *
19 * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, *
20 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR *
21 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR *
22 * THE USE OR OTHER DEALINGS IN THE SOFTWARE. *
24 * Except as contained in this notice, the name(s) of the above copyright *
25 * holders shall not be used in advertising or otherwise to promote the *
26 * sale, use or other dealings in this Software without prior written *
28 ****************************************************************************/
30 /****************************************************************************
31 * Author: Zeyd M. Ben-Halim <zmbenhal@netcom.com> 1992,1995 *
32 * and: Eric S. Raymond <esr@snark.thyrsus.com> *
33 * and: Thomas E. Dickey 1996 on *
34 ****************************************************************************/
37 * comp_scan.c --- Lexical scanner for terminfo compiler.
44 * long _nc_curr_file_pos;
45 * long _nc_comment_start;
46 * long _nc_comment_end;
49 #include <curses.priv.h>
54 MODULE_ID("$Id: comp_scan.c,v 1.119 2022/08/07 00:20:26 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) _nc_syntax = 0; /* termcap or terminfo? */
65 NCURSES_EXPORT_VAR (int) _nc_strict_bsd = 1; /* ncurses extended termcap? */
66 NCURSES_EXPORT_VAR (long) _nc_curr_file_pos = 0; /* file offset of current line */
67 NCURSES_EXPORT_VAR (long) _nc_comment_start = 0; /* start of comment range before name */
68 NCURSES_EXPORT_VAR (long) _nc_comment_end = 0; /* end of comment range before name */
69 NCURSES_EXPORT_VAR (long) _nc_start_line = 0; /* start line of current entry */
71 NCURSES_EXPORT_VAR (struct token) _nc_curr_token =
76 /*****************************************************************************
78 * Token-grabbing machinery
80 *****************************************************************************/
82 static bool first_column; /* See 'next_char()' below */
83 static bool had_newline;
84 static char separator; /* capability separator */
85 static int pushtype; /* type of pushback token */
86 static char *pushname;
89 NCURSES_EXPORT_VAR (bool) _nc_disable_period = FALSE; /* used by tic -a option */
92 /*****************************************************************************
94 * Character-stream handling
96 *****************************************************************************/
98 #define LEXBUFSIZ 1024
100 static char *bufptr; /* otherwise, the input buffer pointer */
101 static char *bufstart; /* start of buffer so we can compute offsets */
102 static FILE *yyin; /* scanner's input file descriptor */
107 * Resets the input-reading routines. Used on initialization,
108 * or after a seek has been done. Exactly one argument must be
113 _nc_reset_input(FILE *fp, char *buf)
116 (T_CALLED("_nc_reset_input(fp=%p, buf=%p)"), (void *) fp, buf));
118 pushtype = NO_PUSHBACK;
122 bufstart = bufptr = buf;
123 _nc_curr_file_pos = 0L;
134 * Returns the final nonblank character on the current input buffer
137 last_char(int from_end)
139 size_t len = strlen(bufptr);
143 if (!isspace(UChar(bufptr[len]))) {
144 if (from_end <= (int) len)
145 result = bufptr[(int) len - from_end];
153 * Read, like fgets(), but error-out if the input contains nulls.
156 get_text(char *buffer, int length)
159 int limit = length - 1;
161 while (limit-- > 0) {
162 int ch = fgetc(yyin);
165 _nc_err_abort("This is not a text-file");
166 } else if (ch == EOF) {
170 *buffer++ = (char) ch;
181 * Returns the next character in the input stream. Comments and leading
182 * white space are stripped.
184 * The global state variable 'firstcolumn' is set TRUE if the character
185 * returned is from the first column of the input line.
187 * The global variable _nc_curr_line is incremented for each new line.
188 * The global variable _nc_curr_file_pos is set to the file offset of the
189 * beginning of each line.
196 static size_t allocated;
202 FreeAndNull(pushname);
208 * An string with an embedded null will truncate the input. This is
209 * intentional (we don't read binary files here).
211 if (bufptr == 0 || *bufptr == '\0')
213 if (*bufptr == '\n') {
216 } else if (*bufptr == '\t') {
217 _nc_curr_col = (_nc_curr_col | 7);
219 } else if (!bufptr || !*bufptr) {
221 * In theory this could be recoded to do its I/O one character at a
222 * time, saving the buffer space. In practice, this turns out to be
223 * quite hard to get completely right. Try it and see. If you
224 * succeed, don't forget to hack push_back() correspondingly.
232 if (used + (LEXBUFSIZ / 4) >= allocated) {
233 allocated += (allocated + LEXBUFSIZ);
234 result = typeRealloc(char, allocated, result);
241 _nc_curr_file_pos = ftell(yyin);
243 if (get_text(result + used, (int) (allocated - used))) {
246 if (_nc_curr_line == 0
247 && IS_TIC_MAGIC(result)) {
248 _nc_err_abort("This is a compiled terminal description, not a source");
255 _nc_STRCAT(result, "\n", allocated);
257 if ((bufptr = bufstart) != 0) {
258 used = strlen(bufptr);
261 while (iswhite(*bufptr)) {
262 if (*bufptr == '\t') {
263 _nc_curr_col = (_nc_curr_col | 7) + 1;
271 * Treat a trailing <cr><lf> the same as a <newline> so we
272 * can read files on OS/2, etc.
274 if ((len = strlen(bufptr)) > 1) {
275 if (bufptr[len - 1] == '\n'
276 && bufptr[len - 2] == '\r') {
278 bufptr[len - 1] = '\n';
285 } while (bufptr[len - 1] != '\n'); /* complete a line */
286 } while (result[0] == '#'); /* ignore comments */
287 } else if (*bufptr == '\t') {
288 _nc_curr_col = (_nc_curr_col | 7);
291 first_column = (bufptr == bufstart);
296 the_char = *bufptr++;
297 return UChar(the_char);
302 /* push a character back onto the input stream */
304 if (bufptr == bufstart)
305 _nc_syserr_abort("Can't backspace off beginning of line");
306 *--bufptr = (char) c;
312 /* return our current character position in the input stream */
314 return (yyin ? ftell(yyin) : (bufptr ? bufptr - bufstart : 0));
319 /* are we at end of input? */
322 ? (feof(yyin) && (bufptr == NULL || *bufptr == '\0'))
323 : (bufptr && *bufptr == '\0'))
327 /* Assume we may be looking at a termcap-style continuation */
328 static NCURSES_INLINE int
329 eat_escaped_newline(int ch)
332 while ((ch = next_char()) == '\n' || iswhite(ch))
337 #define TOK_BUF_SIZE MAX_ENTRY_SIZE
340 ((tok_ptr - tok_buf) < (TOK_BUF_SIZE - 2))
343 *tok_ptr++ = (char) ch; \
346 static char *tok_buf;
352 * Scans the input for the next token, storing the specifics in the
353 * global structure 'curr_token' and returning one of the following:
355 * NAMES A line beginning in column 1. 'name'
356 * will be set to point to everything up to but
357 * not including the first separator on the line.
358 * BOOLEAN An entry consisting of a name followed by
359 * a separator. 'name' will be set to point to
360 * the name of the capability.
361 * NUMBER An entry of the form
363 * 'name' will be set to point to the capability
364 * name and 'valnumber' to the number given.
365 * STRING An entry of the form
367 * 'name' is set to the capability name and
368 * 'valstring' to the string of characters, with
369 * input translations done.
370 * CANCEL An entry of the form
372 * 'name' is set to the capability name and
374 * EOF The end of the file has been reached.
376 * A `separator' is either a comma or a semicolon, depending on whether
377 * we are in termcap or terminfo mode.
382 _nc_get_token(bool silent)
384 static const char terminfo_punct[] = "@%&*!#";
386 char *after_name; /* after primary name */
387 char *after_list; /* after primary and alias list */
393 int dot_flag = FALSE;
403 DEBUG(3, (T_CALLED("_nc_get_token(silent=%d)"), silent));
405 if (pushtype != NO_PUSHBACK) {
406 int retval = pushtype;
408 _nc_set_type(pushname != 0 ? pushname : "");
409 DEBUG(3, ("pushed-back token: `%s', class %d",
410 _nc_curr_token.tk_name, pushtype));
412 pushtype = NO_PUSHBACK;
416 /* currtok wasn't altered by _nc_push_token() */
417 DEBUG(3, (T_RETURN("%d"), retval));
421 if (end_of_stream()) {
423 (void) next_char(); /* frees its allocated memory */
425 if (_nc_curr_token.tk_name == tok_buf)
426 _nc_curr_token.tk_name = 0;
428 DEBUG(3, (T_RETURN("%d"), EOF));
433 token_start = stream_pos();
434 while ((ch = next_char()) == '\n' || iswhite(ch)) {
440 ch = eat_escaped_newline(ch);
441 _nc_curr_token.tk_valstring = 0;
444 old_line = _nc_curr_line;
445 old_col = _nc_curr_col;
450 /* if this is a termcap entry, skip a leading separator */
451 if (separator == ':' && ch == ':')
455 #if NCURSES_EXT_FUNCS
456 && !_nc_disable_period
460 DEBUG(8, ("dot-flag set"));
462 while ((ch = next_char()) == '.' || iswhite(ch))
471 /* have to make some punctuation chars legal for terminfo */
472 if (!isalnum(UChar(ch))
473 #if NCURSES_EXT_FUNCS
474 && !(ch == '.' && _nc_disable_period)
476 && ((strchr) (terminfo_punct, (char) ch) == 0)) {
478 _nc_warning("Illegal character (expected alphanumeric or %s) - '%s'",
479 terminfo_punct, unctrl(UChar(ch)));
480 _nc_panic_mode(separator);
485 tok_buf = typeMalloc(char, TOK_BUF_SIZE);
488 old_line = _nc_curr_line;
489 old_col = _nc_curr_col;
495 _nc_comment_start = token_start;
496 _nc_comment_end = _nc_curr_file_pos;
497 _nc_start_line = _nc_curr_line;
502 while ((ch = next_char()) != '\n') {
504 _nc_err_abort(MSG_NO_INPUTS);
505 } else if (ch == '|') {
506 after_list = tok_ptr;
508 after_name = tok_ptr;
509 } else if (ch == ':' && last_char(0) != ',') {
510 _nc_syntax = SYN_TERMCAP;
513 } else if (ch == ',') {
514 _nc_syntax = SYN_TERMINFO;
517 * If we did not see a '|', then we found a name with no
518 * aliases or description.
523 * We saw a comma, but are not entirely sure this is
524 * terminfo format, since we can still be parsing the
525 * description field (for either syntax).
527 * A properly formatted termcap line ends with either a
528 * colon, or a backslash after a colon. It is possible
529 * to have a backslash in the middle of a capability, but
530 * then there would be no leading whitespace on the next
531 * line - something we want to discourage.
535 if (c1 != ':' && c0 != '\\' && c0 != ':') {
536 bool capability = FALSE;
539 * Since it is not termcap, assume the line is terminfo
540 * format. However, the comma can be embedded in a
541 * description field. It also can be a separator
542 * between a description field and a capability.
544 * Improve the guess by checking if the next word after
545 * the comma does not look like a capability. In that
546 * case, extend the description past the comma.
548 for (s = bufptr; isspace(UChar(*s)); ++s) {
551 if (islower(UChar(*s))) {
553 while (isalnum(UChar(*s))) {
556 if (*s == '#' || *s == '=' || *s == '@') {
558 * Checking solely with syntax allows us to
559 * support extended capabilities with string
563 } else if (*s == ',') {
567 * Otherwise, we can handle predefined boolean
568 * capabilities, still aided by syntax.
570 if (_nc_find_entry(name,
571 _nc_get_hash_table(FALSE))) {
582 ch = eat_escaped_newline(ch);
591 if (_nc_syntax == ERR) {
593 * Grrr...what we ought to do here is barf, complaining that
594 * the entry is malformed. But because a couple of name fields
595 * in the 8.2 termcap file end with |\, we just have to assume
596 * it is termcap syntax.
598 _nc_syntax = SYN_TERMCAP;
600 } else if (_nc_syntax == SYN_TERMINFO) {
601 /* throw away trailing /, *$/ */
603 iswhite(*tok_ptr) || *tok_ptr == ',';
610 * This is the soonest we have the terminal name fetched. Set up
611 * for following warning messages. If there's no '|', then there
614 if (after_name != 0) {
617 _nc_set_type(tok_buf);
618 *after_name = (char) ch;
622 * Compute the boundary between the aliases and the description
623 * field for syntax-checking purposes.
625 if (after_list != 0) {
627 if (*after_list == '\0' || strchr("|", after_list[1]) != NULL) {
628 _nc_warning("empty longname field");
629 } else if (strchr(after_list, ' ') == 0) {
630 _nc_warning("older tic versions may treat the description field as an alias");
634 after_list = tok_buf + strlen(tok_buf);
635 DEBUG(2, ("missing description"));
639 * Whitespace in a name field other than the long name can confuse
640 * rdist and some termcap tools. Slashes are a no-no. Other
641 * special characters can be dangerous due to shell expansion.
643 for (s = tok_buf; s < after_list; ++s) {
644 if (isspace(UChar(*s))) {
646 _nc_warning("whitespace in name or alias field");
648 } else if (*s == '/') {
650 _nc_warning("slashes aren't allowed in names or aliases");
652 } else if (strchr("$[]!*?", *s)) {
654 _nc_warning("dubious character `%c' in name or alias field", *s);
659 _nc_curr_token.tk_name = tok_buf;
662 if (had_newline && _nc_syntax == SYN_TERMCAP) {
663 _nc_warning("Missing backslash before newline");
666 while ((ch = next_char()) != EOF) {
667 if (!isalnum(UChar(ch))) {
668 if (_nc_syntax == SYN_TERMINFO) {
671 } else { /* allow ';' for "k;" */
684 *tok_ptr++ = '\0'; /* separate name/value in buffer */
689 _nc_err_abort("Separator inconsistent with syntax");
690 _nc_curr_token.tk_name = tok_buf;
694 if ((ch = next_char()) != separator && !silent)
695 _nc_warning("Missing separator after `%s', have %s",
696 tok_buf, unctrl(UChar(ch)));
697 _nc_curr_token.tk_name = tok_buf;
703 while (isalnum(ch = next_char())) {
704 numbuf[found++] = (char) ch;
705 if (found >= sizeof(numbuf) - 1)
708 numbuf[found] = '\0';
709 number = strtol(numbuf, &numchk, 0);
711 if (numchk == numbuf)
712 _nc_warning("no value given for `%s'", tok_buf);
713 if ((*numchk != '\0') || (ch != separator))
714 _nc_warning("Missing separator for `%s'", tok_buf);
716 _nc_warning("value of `%s' cannot be negative", tok_buf);
717 if (number > MAX_OF_TYPE(NCURSES_INT2)) {
718 _nc_warning("limiting value of `%s' from %#lx to %#x",
720 number, MAX_OF_TYPE(NCURSES_INT2));
721 number = MAX_OF_TYPE(NCURSES_INT2);
724 _nc_curr_token.tk_name = tok_buf;
725 _nc_curr_token.tk_valnumber = (int) number;
730 ch = _nc_trans_string(tok_ptr, tok_buf + TOK_BUF_SIZE);
731 if (!silent && ch != separator)
732 _nc_warning("Missing separator");
733 _nc_curr_token.tk_name = tok_buf;
734 _nc_curr_token.tk_valstring = tok_ptr;
742 /* just to get rid of the compiler warning */
745 _nc_warning("Illegal character - '%s'", unctrl(UChar(ch)));
747 } /* end else (first_column == FALSE) */
748 } /* end else (ch != EOF) */
753 if (dot_flag == TRUE)
754 DEBUG(8, ("Commented out "));
756 if (_nc_tracing >= DEBUG_LEVEL(8)) {
757 _tracef("parsed %d.%d to %d.%d",
759 _nc_curr_line, _nc_curr_col);
761 if (_nc_tracing >= DEBUG_LEVEL(7)) {
764 _tracef("Token: Boolean; name='%s'",
765 _nc_curr_token.tk_name);
769 _tracef("Token: Number; name='%s', value=%d",
770 _nc_curr_token.tk_name,
771 _nc_curr_token.tk_valnumber);
775 _tracef("Token: String; name='%s', value=%s",
776 _nc_curr_token.tk_name,
777 _nc_visbuf(_nc_curr_token.tk_valstring));
781 _tracef("Token: Cancel; name='%s'",
782 _nc_curr_token.tk_name);
787 _tracef("Token: Names; value='%s'",
788 _nc_curr_token.tk_name);
792 _tracef("Token: End of file");
796 _nc_warning("Bad token type");
801 if (dot_flag == TRUE) /* if commented out, use the next one */
802 type = _nc_get_token(silent);
804 DEBUG(3, ("token: `%s', class %d",
805 ((_nc_curr_token.tk_name != 0)
806 ? _nc_curr_token.tk_name
810 DEBUG(3, (T_RETURN("%d"), type));
818 * Reads characters using next_char() until encountering a separator, nl,
819 * or end-of-file. The returned value is the character which caused
820 * reading to stop. The following translations are done on the input:
822 * ^X goes to ctrl-X (i.e. X & 037)
823 * {\E,\n,\r,\b,\t,\f} go to
824 * {ESCAPE,newline,carriage-return,backspace,tab,formfeed}
825 * {\^,\\} go to {carat,backslash}
826 * \ddd (for ddd = up to three octal digits) goes to the character ddd
834 _nc_trans_string(char *ptr, char *last)
840 bool ignored = FALSE;
841 bool long_warning = FALSE;
843 while ((c = next_char()) != separator && c != EOF) {
844 if (ptr >= (last - 1)) {
846 while ((c = next_char()) != separator && c != EOF) {
852 if ((_nc_syntax == SYN_TERMCAP) && c == '\n')
854 if (c == '^' && last_ch != '%') {
857 _nc_err_abort(MSG_NO_INPUTS);
859 if (!(is7bits(c) && isprint(c))) {
860 _nc_warning("Illegal ^ character - '%s'", unctrl(UChar(c)));
862 if (c == '?' && (_nc_syntax != SYN_TERMCAP)) {
867 *(ptr++) = (char) (c);
869 } else if (c == '\\') {
870 bool strict_bsd = ((_nc_syntax == SYN_TERMCAP) && _nc_strict_bsd);
874 _nc_err_abort(MSG_NO_INPUTS);
876 if (isoctal(c) || (strict_bsd && isdigit(c))) {
878 for (i = 0; i < 2; i++) {
881 _nc_err_abort(MSG_NO_INPUTS);
886 _nc_warning("Non-octal digit `%c' in \\ sequence", c);
887 /* allow the digit; it'll do less harm */
895 number = number * 8 + c - '0';
898 number = UChar(number);
899 if (number == 0 && !strict_bsd)
901 *(ptr++) = (char) number;
944 if ((_nc_syntax == SYN_TERMINFO) || !_nc_strict_bsd) {
962 _nc_warning("Illegal character '%s' in \\ sequence",
970 } /* endswitch (c) */
971 } /* endelse (c < '0' || c > '7') */
973 /* end else if (c == '\\') */
974 else if (c == '\n' && (_nc_syntax == SYN_TERMINFO)) {
976 * Newlines embedded in a terminfo string are ignored, provided
977 * that the next line begins with whitespace.
985 if (_nc_curr_col <= 1) {
995 if (count > MAXCAPLEN && !long_warning) {
996 _nc_warning("Very long string found. Missing separator?");
1009 * Push a token of given type so that it will be reread by the next
1013 NCURSES_EXPORT(void)
1014 _nc_push_token(int tokclass)
1017 * This implementation is kind of bogus, it will fail if we ever do more
1018 * than one pushback at a time between get_token() calls. It relies on the
1019 * fact that _nc_curr_token is static storage that nothing but
1020 * _nc_get_token() touches.
1022 pushtype = tokclass;
1024 pushname = typeMalloc(char, MAX_NAME_SIZE + 1);
1025 _nc_get_type(pushname);
1027 DEBUG(3, ("pushing token: `%s', class %d",
1028 ((_nc_curr_token.tk_name != 0)
1029 ? _nc_curr_token.tk_name
1035 * Panic mode error recovery - skip everything until a "ch" is found.
1037 NCURSES_EXPORT(void)
1038 _nc_panic_mode(char ch)
1041 int c = next_char();
1050 NCURSES_EXPORT(void)
1051 _nc_comp_scan_leaks(void)
1053 if (pushname != 0) {
1054 FreeAndNull(pushname);
1057 FreeAndNull(tok_buf);