1 /****************************************************************************
2 * Copyright (c) 1998-2008,2010 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.89 2010/12/25 23:06:37 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 (long) _nc_curr_file_pos = 0; /* file offset of current line */
65 NCURSES_EXPORT_VAR (long) _nc_comment_start = 0; /* start of comment range before name */
66 NCURSES_EXPORT_VAR (long) _nc_comment_end = 0; /* end of comment range before name */
67 NCURSES_EXPORT_VAR (long) _nc_start_line = 0; /* start line of current entry */
69 NCURSES_EXPORT_VAR (struct token) _nc_curr_token =
74 /*****************************************************************************
76 * Token-grabbing machinery
78 *****************************************************************************/
80 static bool first_column; /* See 'next_char()' below */
81 static bool had_newline;
82 static char separator; /* capability separator */
83 static int pushtype; /* type of pushback token */
84 static char *pushname;
87 NCURSES_EXPORT_VAR (bool) _nc_disable_period = FALSE; /* used by tic -a option */
90 /*****************************************************************************
92 * Character-stream handling
94 *****************************************************************************/
96 #define LEXBUFSIZ 1024
98 static char *bufptr; /* otherwise, the input buffer pointer */
99 static char *bufstart; /* start of buffer so we can compute offsets */
100 static FILE *yyin; /* scanner's input file descriptor */
105 * Resets the input-reading routines. Used on initialization,
106 * or after a seek has been done. Exactly one argument must be
111 _nc_reset_input(FILE *fp, char *buf)
113 pushtype = NO_PUSHBACK;
117 bufstart = bufptr = buf;
118 _nc_curr_file_pos = 0L;
127 * Returns the final nonblank character on the current input buffer
132 size_t len = strlen(bufptr);
134 if (!isspace(UChar(bufptr[len])))
143 * Returns the next character in the input stream. Comments and leading
144 * white space are stripped.
146 * The global state variable 'firstcolumn' is set TRUE if the character
147 * returned is from the first column of the input line.
149 * The global variable _nc_curr_line is incremented for each new line.
150 * The global variable _nc_curr_file_pos is set to the file offset of the
151 * beginning of each line.
158 static size_t allocated;
164 FreeAndNull(pushname);
168 * An string with an embedded null will truncate the input. This is
169 * intentional (we don't read binary files here).
171 if (bufptr == 0 || *bufptr == '\0')
173 if (*bufptr == '\n') {
176 } else if (*bufptr == '\t') {
177 _nc_curr_col = (_nc_curr_col | 7);
179 } else if (!bufptr || !*bufptr) {
181 * In theory this could be recoded to do its I/O one character at a
182 * time, saving the buffer space. In practice, this turns out to be
183 * quite hard to get completely right. Try it and see. If you
184 * succeed, don't forget to hack push_back() correspondingly.
193 if (used + (LEXBUFSIZ / 4) >= allocated) {
194 allocated += (allocated + LEXBUFSIZ);
195 result = typeRealloc(char, allocated, result);
201 _nc_curr_file_pos = ftell(yyin);
203 if (fgets(result + used, (int) (allocated - used), yyin) != 0) {
206 if (_nc_curr_line == 0
207 && IS_TIC_MAGIC(result)) {
208 _nc_err_abort("This is a compiled terminal description, not a source");
215 strcat(result, "\n");
217 if ((bufptr = bufstart) != 0) {
218 used = strlen(bufptr);
219 while (iswhite(*bufptr)) {
220 if (*bufptr == '\t') {
221 _nc_curr_col = (_nc_curr_col | 7) + 1;
229 * Treat a trailing <cr><lf> the same as a <newline> so we
230 * can read files on OS/2, etc.
232 if ((len = strlen(bufptr)) > 1) {
233 if (bufptr[len - 1] == '\n'
234 && bufptr[len - 2] == '\r') {
236 bufptr[len - 1] = '\n';
243 } while (bufptr[len - 1] != '\n'); /* complete a line */
244 } while (result[0] == '#'); /* ignore comments */
245 } else if (*bufptr == '\t') {
246 _nc_curr_col = (_nc_curr_col | 7);
249 first_column = (bufptr == bufstart);
254 the_char = *bufptr++;
255 return UChar(the_char);
260 /* push a character back onto the input stream */
262 if (bufptr == bufstart)
263 _nc_syserr_abort("Can't backspace off beginning of line");
270 /* return our current character position in the input stream */
272 return (yyin ? ftell(yyin) : (bufptr ? bufptr - bufstart : 0));
277 /* are we at end of input? */
279 return ((yyin ? feof(yyin) : (bufptr && *bufptr == '\0'))
283 /* Assume we may be looking at a termcap-style continuation */
284 static NCURSES_INLINE int
285 eat_escaped_newline(int ch)
288 while ((ch = next_char()) == '\n' || iswhite(ch))
293 #define TOK_BUF_SIZE MAX_ENTRY_SIZE
296 ((tok_ptr - tok_buf) < (TOK_BUF_SIZE - 2))
299 *tok_ptr++ = (char) ch; \
306 * Scans the input for the next token, storing the specifics in the
307 * global structure 'curr_token' and returning one of the following:
309 * NAMES A line beginning in column 1. 'name'
310 * will be set to point to everything up to but
311 * not including the first separator on the line.
312 * BOOLEAN An entry consisting of a name followed by
313 * a separator. 'name' will be set to point to
314 * the name of the capability.
315 * NUMBER An entry of the form
317 * 'name' will be set to point to the capability
318 * name and 'valnumber' to the number given.
319 * STRING An entry of the form
321 * 'name' is set to the capability name and
322 * 'valstring' to the string of characters, with
323 * input translations done.
324 * CANCEL An entry of the form
326 * 'name' is set to the capability name and
328 * EOF The end of the file has been reached.
330 * A `separator' is either a comma or a semicolon, depending on whether
331 * we are in termcap or terminfo mode.
336 _nc_get_token(bool silent)
338 static const char terminfo_punct[] = "@%&*!#";
339 static char *tok_buf;
348 int dot_flag = FALSE;
358 if (pushtype != NO_PUSHBACK) {
359 int retval = pushtype;
361 _nc_set_type(pushname != 0 ? pushname : "");
362 DEBUG(3, ("pushed-back token: `%s', class %d",
363 _nc_curr_token.tk_name, pushtype));
365 pushtype = NO_PUSHBACK;
369 /* currtok wasn't altered by _nc_push_token() */
373 if (end_of_stream()) {
375 next_char(); /* frees its allocated memory */
377 if (_nc_curr_token.tk_name == tok_buf)
378 _nc_curr_token.tk_name = 0;
379 FreeAndNull(tok_buf);
385 token_start = stream_pos();
386 while ((ch = next_char()) == '\n' || iswhite(ch)) {
392 ch = eat_escaped_newline(ch);
395 old_line = _nc_curr_line;
396 old_col = _nc_curr_col;
401 /* if this is a termcap entry, skip a leading separator */
402 if (separator == ':' && ch == ':')
406 #if NCURSES_EXT_FUNCS
407 && !_nc_disable_period
411 DEBUG(8, ("dot-flag set"));
413 while ((ch = next_char()) == '.' || iswhite(ch))
422 /* have to make some punctuation chars legal for terminfo */
423 if (!isalnum(UChar(ch))
424 #if NCURSES_EXT_FUNCS
425 && !(ch == '.' && _nc_disable_period)
427 && !strchr(terminfo_punct, (char) ch)) {
429 _nc_warning("Illegal character (expected alphanumeric or %s) - '%s'",
430 terminfo_punct, unctrl(UChar(ch)));
431 _nc_panic_mode(separator);
436 tok_buf = typeMalloc(char, TOK_BUF_SIZE);
439 old_line = _nc_curr_line;
440 old_col = _nc_curr_col;
446 _nc_comment_start = token_start;
447 _nc_comment_end = _nc_curr_file_pos;
448 _nc_start_line = _nc_curr_line;
453 while ((ch = next_char()) != '\n') {
455 _nc_err_abort(MSG_NO_INPUTS);
456 } else if (ch == '|') {
457 after_list = tok_ptr;
459 after_name = tok_ptr;
460 } else if (ch == ':' && last_char() != ',') {
461 _nc_syntax = SYN_TERMCAP;
464 } else if (ch == ',') {
465 _nc_syntax = SYN_TERMINFO;
468 * If we did not see a '|', then we found a name with no
469 * aliases or description.
474 * If we see a comma, we assume this is terminfo unless we
475 * subsequently run into a colon. But we don't stop
476 * looking for a colon until hitting a newline. This
477 * allows commas to be embedded in description fields of
481 ch = eat_escaped_newline(ch);
490 if (_nc_syntax == ERR) {
492 * Grrr...what we ought to do here is barf, complaining that
493 * the entry is malformed. But because a couple of name fields
494 * in the 8.2 termcap file end with |\, we just have to assume
495 * it's termcap syntax.
497 _nc_syntax = SYN_TERMCAP;
499 } else if (_nc_syntax == SYN_TERMINFO) {
500 /* throw away trailing /, *$/ */
502 iswhite(*tok_ptr) || *tok_ptr == ',';
509 * This is the soonest we have the terminal name fetched. Set up
510 * for following warning messages. If there's no '|', then there
513 if (after_name != 0) {
516 _nc_set_type(tok_buf);
517 *after_name = (char) ch;
521 * Compute the boundary between the aliases and the description
522 * field for syntax-checking purposes.
524 if (after_list != 0) {
526 if (*after_list == '\0')
527 _nc_warning("empty longname field");
528 else if (strchr(after_list, ' ') == 0)
529 _nc_warning("older tic versions may treat the description field as an alias");
532 after_list = tok_buf + strlen(tok_buf);
533 DEBUG(1, ("missing description"));
537 * Whitespace in a name field other than the long name can confuse
538 * rdist and some termcap tools. Slashes are a no-no. Other
539 * special characters can be dangerous due to shell expansion.
541 for (s = tok_buf; s < after_list; ++s) {
542 if (isspace(UChar(*s))) {
544 _nc_warning("whitespace in name or alias field");
546 } else if (*s == '/') {
548 _nc_warning("slashes aren't allowed in names or aliases");
550 } else if (strchr("$[]!*?", *s)) {
552 _nc_warning("dubious character `%c' in name or alias field", *s);
557 _nc_curr_token.tk_name = tok_buf;
560 if (had_newline && _nc_syntax == SYN_TERMCAP) {
561 _nc_warning("Missing backslash before newline");
564 while ((ch = next_char()) != EOF) {
565 if (!isalnum(UChar(ch))) {
566 if (_nc_syntax == SYN_TERMINFO) {
569 } else { /* allow ';' for "k;" */
582 *tok_ptr++ = '\0'; /* separate name/value in buffer */
587 _nc_err_abort("Separator inconsistent with syntax");
588 _nc_curr_token.tk_name = tok_buf;
592 if ((ch = next_char()) != separator && !silent)
593 _nc_warning("Missing separator after `%s', have %s",
594 tok_buf, unctrl(UChar(ch)));
595 _nc_curr_token.tk_name = tok_buf;
601 while (isalnum(ch = next_char())) {
602 numbuf[found++] = (char) ch;
603 if (found >= sizeof(numbuf) - 1)
606 numbuf[found] = '\0';
607 number = strtol(numbuf, &numchk, 0);
609 if (numchk == numbuf)
610 _nc_warning("no value given for `%s'", tok_buf);
611 if ((*numchk != '\0') || (ch != separator))
612 _nc_warning("Missing separator");
614 _nc_curr_token.tk_name = tok_buf;
615 _nc_curr_token.tk_valnumber = (int) number;
620 ch = _nc_trans_string(tok_ptr, tok_buf + TOK_BUF_SIZE);
621 if (!silent && ch != separator)
622 _nc_warning("Missing separator");
623 _nc_curr_token.tk_name = tok_buf;
624 _nc_curr_token.tk_valstring = tok_ptr;
632 /* just to get rid of the compiler warning */
635 _nc_warning("Illegal character - '%s'", unctrl(UChar(ch)));
637 } /* end else (first_column == FALSE) */
638 } /* end else (ch != EOF) */
643 if (dot_flag == TRUE)
644 DEBUG(8, ("Commented out "));
646 if (_nc_tracing >= DEBUG_LEVEL(8)) {
647 _tracef("parsed %d.%d to %d.%d",
649 _nc_curr_line, _nc_curr_col);
651 if (_nc_tracing >= DEBUG_LEVEL(7)) {
654 _tracef("Token: Boolean; name='%s'",
655 _nc_curr_token.tk_name);
659 _tracef("Token: Number; name='%s', value=%d",
660 _nc_curr_token.tk_name,
661 _nc_curr_token.tk_valnumber);
665 _tracef("Token: String; name='%s', value=%s",
666 _nc_curr_token.tk_name,
667 _nc_visbuf(_nc_curr_token.tk_valstring));
671 _tracef("Token: Cancel; name='%s'",
672 _nc_curr_token.tk_name);
677 _tracef("Token: Names; value='%s'",
678 _nc_curr_token.tk_name);
682 _tracef("Token: End of file");
686 _nc_warning("Bad token type");
691 if (dot_flag == TRUE) /* if commented out, use the next one */
692 type = _nc_get_token(silent);
694 DEBUG(3, ("token: `%s', class %d",
695 ((_nc_curr_token.tk_name != 0)
696 ? _nc_curr_token.tk_name
707 * Reads characters using next_char() until encountering a separator, nl,
708 * or end-of-file. The returned value is the character which caused
709 * reading to stop. The following translations are done on the input:
711 * ^X goes to ctrl-X (i.e. X & 037)
712 * {\E,\n,\r,\b,\t,\f} go to
713 * {ESCAPE,newline,carriage-return,backspace,tab,formfeed}
714 * {\^,\\} go to {carat,backslash}
715 * \ddd (for ddd = up to three octal digits) goes to the character ddd
723 _nc_trans_string(char *ptr, char *last)
729 bool ignored = FALSE;
730 bool long_warning = FALSE;
732 while ((c = next_char()) != separator && c != EOF) {
733 if (ptr >= (last - 1)) {
735 while ((c = next_char()) != separator && c != EOF) {
741 if ((_nc_syntax == SYN_TERMCAP) && c == '\n')
743 if (c == '^' && last_ch != '%') {
746 _nc_err_abort(MSG_NO_INPUTS);
748 if (!(is7bits(c) && isprint(c))) {
749 _nc_warning("Illegal ^ character - '%s'", unctrl(UChar(c)));
754 _nc_warning("Allow ^? as synonym for \\177");
758 *(ptr++) = (char) (c);
760 } else if (c == '\\') {
763 _nc_err_abort(MSG_NO_INPUTS);
765 if (c >= '0' && c <= '7') {
767 for (i = 0; i < 2; i++) {
770 _nc_err_abort(MSG_NO_INPUTS);
772 if (c < '0' || c > '7') {
774 _nc_warning("Non-octal digit `%c' in \\ sequence", c);
775 /* allow the digit; it'll do less harm */
782 number = number * 8 + c - '0';
787 *(ptr++) = (char) number;
844 _nc_warning("Illegal character '%s' in \\ sequence",
849 } /* endswitch (c) */
850 } /* endelse (c < '0' || c > '7') */
852 /* end else if (c == '\\') */
853 else if (c == '\n' && (_nc_syntax == SYN_TERMINFO)) {
855 * Newlines embedded in a terminfo string are ignored, provided
856 * that the next line begins with whitespace.
864 if (_nc_curr_col <= 1) {
874 if (count > MAXCAPLEN && !long_warning) {
875 _nc_warning("Very long string found. Missing separator?");
888 * Push a token of given type so that it will be reread by the next
893 _nc_push_token(int tokclass)
896 * This implementation is kind of bogus, it will fail if we ever do more
897 * than one pushback at a time between get_token() calls. It relies on the
898 * fact that _nc_curr_token is static storage that nothing but
899 * _nc_get_token() touches.
903 pushname = typeMalloc(char, MAX_NAME_SIZE + 1);
904 _nc_get_type(pushname);
906 DEBUG(3, ("pushing token: `%s', class %d",
907 ((_nc_curr_token.tk_name != 0)
908 ? _nc_curr_token.tk_name
914 * Panic mode error recovery - skip everything until a "ch" is found.
917 _nc_panic_mode(char ch)
932 _nc_comp_scan_leaks(void)
935 FreeAndNull(pushname);