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.116 2022/05/08 00:11:44 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];
155 * Returns the next character in the input stream. Comments and leading
156 * white space are stripped.
158 * The global state variable 'firstcolumn' is set TRUE if the character
159 * returned is from the first column of the input line.
161 * The global variable _nc_curr_line is incremented for each new line.
162 * The global variable _nc_curr_file_pos is set to the file offset of the
163 * beginning of each line.
170 static size_t allocated;
176 FreeAndNull(pushname);
182 * An string with an embedded null will truncate the input. This is
183 * intentional (we don't read binary files here).
185 if (bufptr == 0 || *bufptr == '\0')
187 if (*bufptr == '\n') {
190 } else if (*bufptr == '\t') {
191 _nc_curr_col = (_nc_curr_col | 7);
193 } else if (!bufptr || !*bufptr) {
195 * In theory this could be recoded to do its I/O one character at a
196 * time, saving the buffer space. In practice, this turns out to be
197 * quite hard to get completely right. Try it and see. If you
198 * succeed, don't forget to hack push_back() correspondingly.
206 if (used + (LEXBUFSIZ / 4) >= allocated) {
207 allocated += (allocated + LEXBUFSIZ);
208 result = typeRealloc(char, allocated, result);
215 _nc_curr_file_pos = ftell(yyin);
217 if (fgets(result + used, (int) (allocated - used), yyin) != 0) {
220 if (_nc_curr_line == 0
221 && IS_TIC_MAGIC(result)) {
222 _nc_err_abort("This is a compiled terminal description, not a source");
229 _nc_STRCAT(result, "\n", allocated);
231 if ((bufptr = bufstart) != 0) {
232 used = strlen(bufptr);
235 while (iswhite(*bufptr)) {
236 if (*bufptr == '\t') {
237 _nc_curr_col = (_nc_curr_col | 7) + 1;
245 * Treat a trailing <cr><lf> the same as a <newline> so we
246 * can read files on OS/2, etc.
248 if ((len = strlen(bufptr)) > 1) {
249 if (bufptr[len - 1] == '\n'
250 && bufptr[len - 2] == '\r') {
252 bufptr[len - 1] = '\n';
259 } while (bufptr[len - 1] != '\n'); /* complete a line */
260 } while (result[0] == '#'); /* ignore comments */
261 } else if (*bufptr == '\t') {
262 _nc_curr_col = (_nc_curr_col | 7);
265 first_column = (bufptr == bufstart);
270 the_char = *bufptr++;
271 return UChar(the_char);
276 /* push a character back onto the input stream */
278 if (bufptr == bufstart)
279 _nc_syserr_abort("Can't backspace off beginning of line");
280 *--bufptr = (char) c;
286 /* return our current character position in the input stream */
288 return (yyin ? ftell(yyin) : (bufptr ? bufptr - bufstart : 0));
293 /* are we at end of input? */
295 return ((yyin ? feof(yyin) : (bufptr && *bufptr == '\0'))
299 /* Assume we may be looking at a termcap-style continuation */
300 static NCURSES_INLINE int
301 eat_escaped_newline(int ch)
304 while ((ch = next_char()) == '\n' || iswhite(ch))
309 #define TOK_BUF_SIZE MAX_ENTRY_SIZE
312 ((tok_ptr - tok_buf) < (TOK_BUF_SIZE - 2))
315 *tok_ptr++ = (char) ch; \
318 static char *tok_buf;
324 * Scans the input for the next token, storing the specifics in the
325 * global structure 'curr_token' and returning one of the following:
327 * NAMES A line beginning in column 1. 'name'
328 * will be set to point to everything up to but
329 * not including the first separator on the line.
330 * BOOLEAN An entry consisting of a name followed by
331 * a separator. 'name' will be set to point to
332 * the name of the capability.
333 * NUMBER An entry of the form
335 * 'name' will be set to point to the capability
336 * name and 'valnumber' to the number given.
337 * STRING An entry of the form
339 * 'name' is set to the capability name and
340 * 'valstring' to the string of characters, with
341 * input translations done.
342 * CANCEL An entry of the form
344 * 'name' is set to the capability name and
346 * EOF The end of the file has been reached.
348 * A `separator' is either a comma or a semicolon, depending on whether
349 * we are in termcap or terminfo mode.
354 _nc_get_token(bool silent)
356 static const char terminfo_punct[] = "@%&*!#";
358 char *after_name; /* after primary name */
359 char *after_list; /* after primary and alias list */
365 int dot_flag = FALSE;
375 DEBUG(3, (T_CALLED("_nc_get_token(silent=%d)"), silent));
377 if (pushtype != NO_PUSHBACK) {
378 int retval = pushtype;
380 _nc_set_type(pushname != 0 ? pushname : "");
381 DEBUG(3, ("pushed-back token: `%s', class %d",
382 _nc_curr_token.tk_name, pushtype));
384 pushtype = NO_PUSHBACK;
388 /* currtok wasn't altered by _nc_push_token() */
389 DEBUG(3, (T_RETURN("%d"), retval));
393 if (end_of_stream()) {
395 (void) next_char(); /* frees its allocated memory */
397 if (_nc_curr_token.tk_name == tok_buf)
398 _nc_curr_token.tk_name = 0;
400 DEBUG(3, (T_RETURN("%d"), EOF));
405 token_start = stream_pos();
406 while ((ch = next_char()) == '\n' || iswhite(ch)) {
412 ch = eat_escaped_newline(ch);
413 _nc_curr_token.tk_valstring = 0;
416 old_line = _nc_curr_line;
417 old_col = _nc_curr_col;
422 /* if this is a termcap entry, skip a leading separator */
423 if (separator == ':' && ch == ':')
427 #if NCURSES_EXT_FUNCS
428 && !_nc_disable_period
432 DEBUG(8, ("dot-flag set"));
434 while ((ch = next_char()) == '.' || iswhite(ch))
443 /* have to make some punctuation chars legal for terminfo */
444 if (!isalnum(UChar(ch))
445 #if NCURSES_EXT_FUNCS
446 && !(ch == '.' && _nc_disable_period)
448 && ((strchr) (terminfo_punct, (char) ch) == 0)) {
450 _nc_warning("Illegal character (expected alphanumeric or %s) - '%s'",
451 terminfo_punct, unctrl(UChar(ch)));
452 _nc_panic_mode(separator);
457 tok_buf = typeMalloc(char, TOK_BUF_SIZE);
460 old_line = _nc_curr_line;
461 old_col = _nc_curr_col;
467 _nc_comment_start = token_start;
468 _nc_comment_end = _nc_curr_file_pos;
469 _nc_start_line = _nc_curr_line;
474 while ((ch = next_char()) != '\n') {
476 _nc_err_abort(MSG_NO_INPUTS);
477 } else if (ch == '|') {
478 after_list = tok_ptr;
480 after_name = tok_ptr;
481 } else if (ch == ':' && last_char(0) != ',') {
482 _nc_syntax = SYN_TERMCAP;
485 } else if (ch == ',') {
486 _nc_syntax = SYN_TERMINFO;
489 * If we did not see a '|', then we found a name with no
490 * aliases or description.
495 * We saw a comma, but are not entirely sure this is
496 * terminfo format, since we can still be parsing the
497 * description field (for either syntax).
499 * A properly formatted termcap line ends with either a
500 * colon, or a backslash after a colon. It is possible
501 * to have a backslash in the middle of a capability, but
502 * then there would be no leading whitespace on the next
503 * line - something we want to discourage.
507 if (c1 != ':' && c0 != '\\' && c0 != ':') {
508 bool capability = FALSE;
511 * Since it is not termcap, assume the line is terminfo
512 * format. However, the comma can be embedded in a
513 * description field. It also can be a separator
514 * between a description field and a capability.
516 * Improve the guess by checking if the next word after
517 * the comma does not look like a capability. In that
518 * case, extend the description past the comma.
520 for (s = bufptr; isspace(UChar(*s)); ++s) {
523 if (islower(UChar(*s))) {
525 while (isalnum(UChar(*s))) {
528 if (*s == '#' || *s == '=' || *s == '@') {
530 * Checking solely with syntax allows us to
531 * support extended capabilities with string
535 } else if (*s == ',') {
539 * Otherwise, we can handle predefined boolean
540 * capabilities, still aided by syntax.
542 if (_nc_find_entry(name,
543 _nc_get_hash_table(FALSE))) {
554 ch = eat_escaped_newline(ch);
563 if (_nc_syntax == ERR) {
565 * Grrr...what we ought to do here is barf, complaining that
566 * the entry is malformed. But because a couple of name fields
567 * in the 8.2 termcap file end with |\, we just have to assume
568 * it is termcap syntax.
570 _nc_syntax = SYN_TERMCAP;
572 } else if (_nc_syntax == SYN_TERMINFO) {
573 /* throw away trailing /, *$/ */
575 iswhite(*tok_ptr) || *tok_ptr == ',';
582 * This is the soonest we have the terminal name fetched. Set up
583 * for following warning messages. If there's no '|', then there
586 if (after_name != 0) {
589 _nc_set_type(tok_buf);
590 *after_name = (char) ch;
594 * Compute the boundary between the aliases and the description
595 * field for syntax-checking purposes.
597 if (after_list != 0) {
599 if (*after_list == '\0' || strchr("|", after_list[1]) != NULL) {
600 _nc_warning("empty longname field");
601 } else if (strchr(after_list, ' ') == 0) {
602 _nc_warning("older tic versions may treat the description field as an alias");
606 after_list = tok_buf + strlen(tok_buf);
607 DEBUG(2, ("missing description"));
611 * Whitespace in a name field other than the long name can confuse
612 * rdist and some termcap tools. Slashes are a no-no. Other
613 * special characters can be dangerous due to shell expansion.
615 for (s = tok_buf; s < after_list; ++s) {
616 if (isspace(UChar(*s))) {
618 _nc_warning("whitespace in name or alias field");
620 } else if (*s == '/') {
622 _nc_warning("slashes aren't allowed in names or aliases");
624 } else if (strchr("$[]!*?", *s)) {
626 _nc_warning("dubious character `%c' in name or alias field", *s);
631 _nc_curr_token.tk_name = tok_buf;
634 if (had_newline && _nc_syntax == SYN_TERMCAP) {
635 _nc_warning("Missing backslash before newline");
638 while ((ch = next_char()) != EOF) {
639 if (!isalnum(UChar(ch))) {
640 if (_nc_syntax == SYN_TERMINFO) {
643 } else { /* allow ';' for "k;" */
656 *tok_ptr++ = '\0'; /* separate name/value in buffer */
661 _nc_err_abort("Separator inconsistent with syntax");
662 _nc_curr_token.tk_name = tok_buf;
666 if ((ch = next_char()) != separator && !silent)
667 _nc_warning("Missing separator after `%s', have %s",
668 tok_buf, unctrl(UChar(ch)));
669 _nc_curr_token.tk_name = tok_buf;
675 while (isalnum(ch = next_char())) {
676 numbuf[found++] = (char) ch;
677 if (found >= sizeof(numbuf) - 1)
680 numbuf[found] = '\0';
681 number = strtol(numbuf, &numchk, 0);
683 if (numchk == numbuf)
684 _nc_warning("no value given for `%s'", tok_buf);
685 if ((*numchk != '\0') || (ch != separator))
686 _nc_warning("Missing separator for `%s'", tok_buf);
688 _nc_warning("value of `%s' cannot be negative", tok_buf);
689 if (number > MAX_OF_TYPE(NCURSES_INT2)) {
690 _nc_warning("limiting value of `%s' from %#lx to %#x",
692 number, MAX_OF_TYPE(NCURSES_INT2));
693 number = MAX_OF_TYPE(NCURSES_INT2);
696 _nc_curr_token.tk_name = tok_buf;
697 _nc_curr_token.tk_valnumber = (int) number;
702 ch = _nc_trans_string(tok_ptr, tok_buf + TOK_BUF_SIZE);
703 if (!silent && ch != separator)
704 _nc_warning("Missing separator");
705 _nc_curr_token.tk_name = tok_buf;
706 _nc_curr_token.tk_valstring = tok_ptr;
714 /* just to get rid of the compiler warning */
717 _nc_warning("Illegal character - '%s'", unctrl(UChar(ch)));
719 } /* end else (first_column == FALSE) */
720 } /* end else (ch != EOF) */
725 if (dot_flag == TRUE)
726 DEBUG(8, ("Commented out "));
728 if (_nc_tracing >= DEBUG_LEVEL(8)) {
729 _tracef("parsed %d.%d to %d.%d",
731 _nc_curr_line, _nc_curr_col);
733 if (_nc_tracing >= DEBUG_LEVEL(7)) {
736 _tracef("Token: Boolean; name='%s'",
737 _nc_curr_token.tk_name);
741 _tracef("Token: Number; name='%s', value=%d",
742 _nc_curr_token.tk_name,
743 _nc_curr_token.tk_valnumber);
747 _tracef("Token: String; name='%s', value=%s",
748 _nc_curr_token.tk_name,
749 _nc_visbuf(_nc_curr_token.tk_valstring));
753 _tracef("Token: Cancel; name='%s'",
754 _nc_curr_token.tk_name);
759 _tracef("Token: Names; value='%s'",
760 _nc_curr_token.tk_name);
764 _tracef("Token: End of file");
768 _nc_warning("Bad token type");
773 if (dot_flag == TRUE) /* if commented out, use the next one */
774 type = _nc_get_token(silent);
776 DEBUG(3, ("token: `%s', class %d",
777 ((_nc_curr_token.tk_name != 0)
778 ? _nc_curr_token.tk_name
782 DEBUG(3, (T_RETURN("%d"), type));
790 * Reads characters using next_char() until encountering a separator, nl,
791 * or end-of-file. The returned value is the character which caused
792 * reading to stop. The following translations are done on the input:
794 * ^X goes to ctrl-X (i.e. X & 037)
795 * {\E,\n,\r,\b,\t,\f} go to
796 * {ESCAPE,newline,carriage-return,backspace,tab,formfeed}
797 * {\^,\\} go to {carat,backslash}
798 * \ddd (for ddd = up to three octal digits) goes to the character ddd
806 _nc_trans_string(char *ptr, char *last)
812 bool ignored = FALSE;
813 bool long_warning = FALSE;
815 while ((c = next_char()) != separator && c != EOF) {
816 if (ptr >= (last - 1)) {
818 while ((c = next_char()) != separator && c != EOF) {
824 if ((_nc_syntax == SYN_TERMCAP) && c == '\n')
826 if (c == '^' && last_ch != '%') {
829 _nc_err_abort(MSG_NO_INPUTS);
831 if (!(is7bits(c) && isprint(c))) {
832 _nc_warning("Illegal ^ character - '%s'", unctrl(UChar(c)));
834 if (c == '?' && (_nc_syntax != SYN_TERMCAP)) {
839 *(ptr++) = (char) (c);
841 } else if (c == '\\') {
842 bool strict_bsd = ((_nc_syntax == SYN_TERMCAP) && _nc_strict_bsd);
846 _nc_err_abort(MSG_NO_INPUTS);
848 if (isoctal(c) || (strict_bsd && isdigit(c))) {
850 for (i = 0; i < 2; i++) {
853 _nc_err_abort(MSG_NO_INPUTS);
858 _nc_warning("Non-octal digit `%c' in \\ sequence", c);
859 /* allow the digit; it'll do less harm */
867 number = number * 8 + c - '0';
870 number = UChar(number);
871 if (number == 0 && !strict_bsd)
873 *(ptr++) = (char) number;
916 if ((_nc_syntax == SYN_TERMINFO) || !_nc_strict_bsd) {
934 _nc_warning("Illegal character '%s' in \\ sequence",
942 } /* endswitch (c) */
943 } /* endelse (c < '0' || c > '7') */
945 /* end else if (c == '\\') */
946 else if (c == '\n' && (_nc_syntax == SYN_TERMINFO)) {
948 * Newlines embedded in a terminfo string are ignored, provided
949 * that the next line begins with whitespace.
957 if (_nc_curr_col <= 1) {
967 if (count > MAXCAPLEN && !long_warning) {
968 _nc_warning("Very long string found. Missing separator?");
981 * Push a token of given type so that it will be reread by the next
986 _nc_push_token(int tokclass)
989 * This implementation is kind of bogus, it will fail if we ever do more
990 * than one pushback at a time between get_token() calls. It relies on the
991 * fact that _nc_curr_token is static storage that nothing but
992 * _nc_get_token() touches.
996 pushname = typeMalloc(char, MAX_NAME_SIZE + 1);
997 _nc_get_type(pushname);
999 DEBUG(3, ("pushing token: `%s', class %d",
1000 ((_nc_curr_token.tk_name != 0)
1001 ? _nc_curr_token.tk_name
1007 * Panic mode error recovery - skip everything until a "ch" is found.
1009 NCURSES_EXPORT(void)
1010 _nc_panic_mode(char ch)
1013 int c = next_char();
1022 NCURSES_EXPORT(void)
1023 _nc_comp_scan_leaks(void)
1025 if (pushname != 0) {
1026 FreeAndNull(pushname);
1029 FreeAndNull(tok_buf);