1 /****************************************************************************
2 * Copyright (c) 1998-2012,2013 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.102 2013/11/16 19:57:50 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.
199 if (used + (LEXBUFSIZ / 4) >= allocated) {
200 allocated += (allocated + LEXBUFSIZ);
201 result = typeRealloc(char, allocated, result);
208 _nc_curr_file_pos = ftell(yyin);
210 if (fgets(result + used, (int) (allocated - used), yyin) != 0) {
213 if (_nc_curr_line == 0
214 && IS_TIC_MAGIC(result)) {
215 _nc_err_abort("This is a compiled terminal description, not a source");
222 _nc_STRCAT(result, "\n", allocated);
224 if ((bufptr = bufstart) != 0) {
225 used = strlen(bufptr);
226 while (iswhite(*bufptr)) {
227 if (*bufptr == '\t') {
228 _nc_curr_col = (_nc_curr_col | 7) + 1;
236 * Treat a trailing <cr><lf> the same as a <newline> so we
237 * can read files on OS/2, etc.
239 if ((len = strlen(bufptr)) > 1) {
240 if (bufptr[len - 1] == '\n'
241 && bufptr[len - 2] == '\r') {
243 bufptr[len - 1] = '\n';
250 } while (bufptr[len - 1] != '\n'); /* complete a line */
251 } while (result[0] == '#'); /* ignore comments */
252 } else if (*bufptr == '\t') {
253 _nc_curr_col = (_nc_curr_col | 7);
256 first_column = (bufptr == bufstart);
261 the_char = *bufptr++;
262 return UChar(the_char);
267 /* push a character back onto the input stream */
269 if (bufptr == bufstart)
270 _nc_syserr_abort("Can't backspace off beginning of line");
271 *--bufptr = (char) c;
277 /* return our current character position in the input stream */
279 return (yyin ? ftell(yyin) : (bufptr ? bufptr - bufstart : 0));
284 /* are we at end of input? */
286 return ((yyin ? feof(yyin) : (bufptr && *bufptr == '\0'))
290 /* Assume we may be looking at a termcap-style continuation */
291 static NCURSES_INLINE int
292 eat_escaped_newline(int ch)
295 while ((ch = next_char()) == '\n' || iswhite(ch))
300 #define TOK_BUF_SIZE MAX_ENTRY_SIZE
303 ((tok_ptr - tok_buf) < (TOK_BUF_SIZE - 2))
306 *tok_ptr++ = (char) ch; \
309 static char *tok_buf;
315 * Scans the input for the next token, storing the specifics in the
316 * global structure 'curr_token' and returning one of the following:
318 * NAMES A line beginning in column 1. 'name'
319 * will be set to point to everything up to but
320 * not including the first separator on the line.
321 * BOOLEAN An entry consisting of a name followed by
322 * a separator. 'name' will be set to point to
323 * the name of the capability.
324 * NUMBER An entry of the form
326 * 'name' will be set to point to the capability
327 * name and 'valnumber' to the number given.
328 * STRING An entry of the form
330 * 'name' is set to the capability name and
331 * 'valstring' to the string of characters, with
332 * input translations done.
333 * CANCEL An entry of the form
335 * 'name' is set to the capability name and
337 * EOF The end of the file has been reached.
339 * A `separator' is either a comma or a semicolon, depending on whether
340 * we are in termcap or terminfo mode.
345 _nc_get_token(bool silent)
347 static const char terminfo_punct[] = "@%&*!#";
349 char *after_name; /* after primary name */
350 char *after_list; /* after primary and alias list */
356 int dot_flag = FALSE;
366 if (pushtype != NO_PUSHBACK) {
367 int retval = pushtype;
369 _nc_set_type(pushname != 0 ? pushname : "");
370 DEBUG(3, ("pushed-back token: `%s', class %d",
371 _nc_curr_token.tk_name, pushtype));
373 pushtype = NO_PUSHBACK;
377 /* currtok wasn't altered by _nc_push_token() */
381 if (end_of_stream()) {
383 (void) next_char(); /* frees its allocated memory */
385 if (_nc_curr_token.tk_name == tok_buf)
386 _nc_curr_token.tk_name = 0;
392 token_start = stream_pos();
393 while ((ch = next_char()) == '\n' || iswhite(ch)) {
399 ch = eat_escaped_newline(ch);
400 _nc_curr_token.tk_valstring = 0;
403 old_line = _nc_curr_line;
404 old_col = _nc_curr_col;
409 /* if this is a termcap entry, skip a leading separator */
410 if (separator == ':' && ch == ':')
414 #if NCURSES_EXT_FUNCS
415 && !_nc_disable_period
419 DEBUG(8, ("dot-flag set"));
421 while ((ch = next_char()) == '.' || iswhite(ch))
430 /* have to make some punctuation chars legal for terminfo */
431 if (!isalnum(UChar(ch))
432 #if NCURSES_EXT_FUNCS
433 && !(ch == '.' && _nc_disable_period)
435 && ((strchr) (terminfo_punct, (char) ch) == 0)) {
437 _nc_warning("Illegal character (expected alphanumeric or %s) - '%s'",
438 terminfo_punct, unctrl(UChar(ch)));
439 _nc_panic_mode(separator);
444 tok_buf = typeMalloc(char, TOK_BUF_SIZE);
447 old_line = _nc_curr_line;
448 old_col = _nc_curr_col;
454 _nc_comment_start = token_start;
455 _nc_comment_end = _nc_curr_file_pos;
456 _nc_start_line = _nc_curr_line;
461 while ((ch = next_char()) != '\n') {
463 _nc_err_abort(MSG_NO_INPUTS);
464 } else if (ch == '|') {
465 after_list = tok_ptr;
467 after_name = tok_ptr;
468 } else if (ch == ':' && last_char(0) != ',') {
469 _nc_syntax = SYN_TERMCAP;
472 } else if (ch == ',') {
473 _nc_syntax = SYN_TERMINFO;
476 * If we did not see a '|', then we found a name with no
477 * aliases or description.
482 * We saw a comma, but are not entirely sure this is
483 * terminfo format, since we can still be parsing the
484 * description field (for either syntax).
486 * A properly formatted termcap line ends with either a
487 * colon, or a backslash after a colon. It is possible
488 * to have a backslash in the middle of a capability, but
489 * then there would be no leading whitespace on the next
490 * line - something we want to discourage.
494 if (c1 != ':' && c0 != '\\' && c0 != ':') {
495 bool capability = FALSE;
498 * Since it is not termcap, assume the line is terminfo
499 * format. However, the comma can be embedded in a
500 * description field. It also can be a separator
501 * between a description field and a capability.
503 * Improve the guess by checking if the next word after
504 * the comma does not look like a capability. In that
505 * case, extend the description past the comma.
507 for (s = bufptr; isspace(UChar(*s)); ++s) {
510 if (islower(UChar(*s))) {
512 while (isalnum(UChar(*s))) {
515 if (*s == '#' || *s == '=' || *s == '@') {
517 * Checking solely with syntax allows us to
518 * support extended capabilities with string
522 } else if (*s == ',') {
526 * Otherwise, we can handle predefined boolean
527 * capabilities, still aided by syntax.
529 if (_nc_find_entry(name,
530 _nc_get_hash_table(FALSE))) {
541 ch = eat_escaped_newline(ch);
550 if (_nc_syntax == ERR) {
552 * Grrr...what we ought to do here is barf, complaining that
553 * the entry is malformed. But because a couple of name fields
554 * in the 8.2 termcap file end with |\, we just have to assume
555 * it's termcap syntax.
557 _nc_syntax = SYN_TERMCAP;
559 } else if (_nc_syntax == SYN_TERMINFO) {
560 /* throw away trailing /, *$/ */
562 iswhite(*tok_ptr) || *tok_ptr == ',';
569 * This is the soonest we have the terminal name fetched. Set up
570 * for following warning messages. If there's no '|', then there
573 if (after_name != 0) {
576 _nc_set_type(tok_buf);
577 *after_name = (char) ch;
581 * Compute the boundary between the aliases and the description
582 * field for syntax-checking purposes.
584 if (after_list != 0) {
586 if (*after_list == '\0')
587 _nc_warning("empty longname field");
588 #ifndef DRAGONFLY_NATIVE
589 else if (strchr(after_list, ' ') == 0)
590 _nc_warning("older tic versions may treat the description field as an alias");
594 after_list = tok_buf + strlen(tok_buf);
595 DEBUG(1, ("missing description"));
599 * Whitespace in a name field other than the long name can confuse
600 * rdist and some termcap tools. Slashes are a no-no. Other
601 * special characters can be dangerous due to shell expansion.
603 for (s = tok_buf; s < after_list; ++s) {
604 if (isspace(UChar(*s))) {
606 _nc_warning("whitespace in name or alias field");
608 } else if (*s == '/') {
610 _nc_warning("slashes aren't allowed in names or aliases");
612 } else if (strchr("$[]!*?", *s)) {
614 _nc_warning("dubious character `%c' in name or alias field", *s);
619 _nc_curr_token.tk_name = tok_buf;
622 if (had_newline && _nc_syntax == SYN_TERMCAP) {
623 _nc_warning("Missing backslash before newline");
626 while ((ch = next_char()) != EOF) {
627 if (!isalnum(UChar(ch))) {
628 if (_nc_syntax == SYN_TERMINFO) {
631 } else { /* allow ';' for "k;" */
644 *tok_ptr++ = '\0'; /* separate name/value in buffer */
649 _nc_err_abort("Separator inconsistent with syntax");
650 _nc_curr_token.tk_name = tok_buf;
654 if ((ch = next_char()) != separator && !silent)
655 _nc_warning("Missing separator after `%s', have %s",
656 tok_buf, unctrl(UChar(ch)));
657 _nc_curr_token.tk_name = tok_buf;
663 while (isalnum(ch = next_char())) {
664 numbuf[found++] = (char) ch;
665 if (found >= sizeof(numbuf) - 1)
668 numbuf[found] = '\0';
669 number = strtol(numbuf, &numchk, 0);
671 if (numchk == numbuf)
672 _nc_warning("no value given for `%s'", tok_buf);
673 if ((*numchk != '\0') || (ch != separator))
674 _nc_warning("Missing separator");
676 _nc_curr_token.tk_name = tok_buf;
677 _nc_curr_token.tk_valnumber = (int) number;
682 ch = _nc_trans_string(tok_ptr, tok_buf + TOK_BUF_SIZE);
683 if (!silent && ch != separator)
684 _nc_warning("Missing separator");
685 _nc_curr_token.tk_name = tok_buf;
686 _nc_curr_token.tk_valstring = tok_ptr;
694 /* just to get rid of the compiler warning */
697 _nc_warning("Illegal character - '%s'", unctrl(UChar(ch)));
699 } /* end else (first_column == FALSE) */
700 } /* end else (ch != EOF) */
705 if (dot_flag == TRUE)
706 DEBUG(8, ("Commented out "));
708 if (_nc_tracing >= DEBUG_LEVEL(8)) {
709 _tracef("parsed %d.%d to %d.%d",
711 _nc_curr_line, _nc_curr_col);
713 if (_nc_tracing >= DEBUG_LEVEL(7)) {
716 _tracef("Token: Boolean; name='%s'",
717 _nc_curr_token.tk_name);
721 _tracef("Token: Number; name='%s', value=%d",
722 _nc_curr_token.tk_name,
723 _nc_curr_token.tk_valnumber);
727 _tracef("Token: String; name='%s', value=%s",
728 _nc_curr_token.tk_name,
729 _nc_visbuf(_nc_curr_token.tk_valstring));
733 _tracef("Token: Cancel; name='%s'",
734 _nc_curr_token.tk_name);
739 _tracef("Token: Names; value='%s'",
740 _nc_curr_token.tk_name);
744 _tracef("Token: End of file");
748 _nc_warning("Bad token type");
753 if (dot_flag == TRUE) /* if commented out, use the next one */
754 type = _nc_get_token(silent);
756 DEBUG(3, ("token: `%s', class %d",
757 ((_nc_curr_token.tk_name != 0)
758 ? _nc_curr_token.tk_name
769 * Reads characters using next_char() until encountering a separator, nl,
770 * or end-of-file. The returned value is the character which caused
771 * reading to stop. The following translations are done on the input:
773 * ^X goes to ctrl-X (i.e. X & 037)
774 * {\E,\n,\r,\b,\t,\f} go to
775 * {ESCAPE,newline,carriage-return,backspace,tab,formfeed}
776 * {\^,\\} go to {carat,backslash}
777 * \ddd (for ddd = up to three octal digits) goes to the character ddd
785 _nc_trans_string(char *ptr, char *last)
791 bool ignored = FALSE;
792 bool long_warning = FALSE;
794 while ((c = next_char()) != separator && c != EOF) {
795 if (ptr >= (last - 1)) {
797 while ((c = next_char()) != separator && c != EOF) {
803 if ((_nc_syntax == SYN_TERMCAP) && c == '\n')
805 if (c == '^' && last_ch != '%') {
808 _nc_err_abort(MSG_NO_INPUTS);
810 if (!(is7bits(c) && isprint(c))) {
811 _nc_warning("Illegal ^ character - '%s'", unctrl(UChar(c)));
813 if (c == '?' && (_nc_syntax != SYN_TERMCAP)) {
816 _nc_warning("Allow ^? as synonym for \\177");
820 *(ptr++) = (char) (c);
822 } else if (c == '\\') {
823 bool strict_bsd = ((_nc_syntax == SYN_TERMCAP) && _nc_strict_bsd);
827 _nc_err_abort(MSG_NO_INPUTS);
829 #define isoctal(c) ((c) >= '0' && (c) <= '7')
831 if (isoctal(c) || (strict_bsd && isdigit(c))) {
833 for (i = 0; i < 2; i++) {
836 _nc_err_abort(MSG_NO_INPUTS);
841 _nc_warning("Non-octal digit `%c' in \\ sequence", c);
842 /* allow the digit; it'll do less harm */
850 number = number * 8 + c - '0';
853 number = UChar(number);
854 if (number == 0 && !strict_bsd)
856 *(ptr++) = (char) number;
899 if ((_nc_syntax == SYN_TERMINFO) || !_nc_strict_bsd) {
917 _nc_warning("Illegal character '%s' in \\ sequence",
925 } /* endswitch (c) */
926 } /* endelse (c < '0' || c > '7') */
928 /* end else if (c == '\\') */
929 else if (c == '\n' && (_nc_syntax == SYN_TERMINFO)) {
931 * Newlines embedded in a terminfo string are ignored, provided
932 * that the next line begins with whitespace.
940 if (_nc_curr_col <= 1) {
950 if (count > MAXCAPLEN && !long_warning) {
951 _nc_warning("Very long string found. Missing separator?");
964 * Push a token of given type so that it will be reread by the next
969 _nc_push_token(int tokclass)
972 * This implementation is kind of bogus, it will fail if we ever do more
973 * than one pushback at a time between get_token() calls. It relies on the
974 * fact that _nc_curr_token is static storage that nothing but
975 * _nc_get_token() touches.
979 pushname = typeMalloc(char, MAX_NAME_SIZE + 1);
980 _nc_get_type(pushname);
982 DEBUG(3, ("pushing token: `%s', class %d",
983 ((_nc_curr_token.tk_name != 0)
984 ? _nc_curr_token.tk_name
990 * Panic mode error recovery - skip everything until a "ch" is found.
993 _nc_panic_mode(char ch)
1007 NCURSES_EXPORT(void)
1008 _nc_comp_scan_leaks(void)
1010 if (pushname != 0) {
1011 FreeAndNull(pushname);
1014 FreeAndNull(tok_buf);