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.87 2010/05/01 19:56:35 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) {
211 strcat(result, "\n");
213 if ((bufptr = bufstart) != 0) {
214 used = strlen(bufptr);
215 while (iswhite(*bufptr)) {
216 if (*bufptr == '\t') {
217 _nc_curr_col = (_nc_curr_col | 7) + 1;
225 * Treat a trailing <cr><lf> the same as a <newline> so we
226 * can read files on OS/2, etc.
228 if ((len = strlen(bufptr)) > 1) {
229 if (bufptr[len - 1] == '\n'
230 && bufptr[len - 2] == '\r') {
232 bufptr[len - 1] = '\n';
239 } while (bufptr[len - 1] != '\n'); /* complete a line */
240 } while (result[0] == '#'); /* ignore comments */
241 } else if (*bufptr == '\t') {
242 _nc_curr_col = (_nc_curr_col | 7);
245 first_column = (bufptr == bufstart);
250 the_char = *bufptr++;
251 return UChar(the_char);
256 /* push a character back onto the input stream */
258 if (bufptr == bufstart)
259 _nc_syserr_abort("Can't backspace off beginning of line");
266 /* return our current character position in the input stream */
268 return (yyin ? ftell(yyin) : (bufptr ? bufptr - bufstart : 0));
273 /* are we at end of input? */
275 return ((yyin ? feof(yyin) : (bufptr && *bufptr == '\0'))
279 /* Assume we may be looking at a termcap-style continuation */
280 static NCURSES_INLINE int
281 eat_escaped_newline(int ch)
284 while ((ch = next_char()) == '\n' || iswhite(ch))
289 #define TOK_BUF_SIZE MAX_ENTRY_SIZE
292 ((tok_ptr - tok_buf) < (TOK_BUF_SIZE - 2))
295 *tok_ptr++ = (char) ch; \
302 * Scans the input for the next token, storing the specifics in the
303 * global structure 'curr_token' and returning one of the following:
305 * NAMES A line beginning in column 1. 'name'
306 * will be set to point to everything up to but
307 * not including the first separator on the line.
308 * BOOLEAN An entry consisting of a name followed by
309 * a separator. 'name' will be set to point to
310 * the name of the capability.
311 * NUMBER An entry of the form
313 * 'name' will be set to point to the capability
314 * name and 'valnumber' to the number given.
315 * STRING An entry of the form
317 * 'name' is set to the capability name and
318 * 'valstring' to the string of characters, with
319 * input translations done.
320 * CANCEL An entry of the form
322 * 'name' is set to the capability name and
324 * EOF The end of the file has been reached.
326 * A `separator' is either a comma or a semicolon, depending on whether
327 * we are in termcap or terminfo mode.
332 _nc_get_token(bool silent)
334 static const char terminfo_punct[] = "@%&*!#";
335 static char *tok_buf;
344 int dot_flag = FALSE;
354 if (pushtype != NO_PUSHBACK) {
355 int retval = pushtype;
357 _nc_set_type(pushname != 0 ? pushname : "");
358 DEBUG(3, ("pushed-back token: `%s', class %d",
359 _nc_curr_token.tk_name, pushtype));
361 pushtype = NO_PUSHBACK;
365 /* currtok wasn't altered by _nc_push_token() */
369 if (end_of_stream()) {
371 next_char(); /* frees its allocated memory */
373 if (_nc_curr_token.tk_name == tok_buf)
374 _nc_curr_token.tk_name = 0;
375 FreeAndNull(tok_buf);
381 token_start = stream_pos();
382 while ((ch = next_char()) == '\n' || iswhite(ch)) {
388 ch = eat_escaped_newline(ch);
391 old_line = _nc_curr_line;
392 old_col = _nc_curr_col;
397 /* if this is a termcap entry, skip a leading separator */
398 if (separator == ':' && ch == ':')
402 #if NCURSES_EXT_FUNCS
403 && !_nc_disable_period
407 DEBUG(8, ("dot-flag set"));
409 while ((ch = next_char()) == '.' || iswhite(ch))
418 /* have to make some punctuation chars legal for terminfo */
419 if (!isalnum(UChar(ch))
420 #if NCURSES_EXT_FUNCS
421 && !(ch == '.' && _nc_disable_period)
423 && !strchr(terminfo_punct, (char) ch)) {
425 _nc_warning("Illegal character (expected alphanumeric or %s) - '%s'",
426 terminfo_punct, unctrl(UChar(ch)));
427 _nc_panic_mode(separator);
432 tok_buf = typeMalloc(char, TOK_BUF_SIZE);
435 old_line = _nc_curr_line;
436 old_col = _nc_curr_col;
442 _nc_comment_start = token_start;
443 _nc_comment_end = _nc_curr_file_pos;
444 _nc_start_line = _nc_curr_line;
449 while ((ch = next_char()) != '\n') {
451 _nc_err_abort(MSG_NO_INPUTS);
452 } else if (ch == '|') {
453 after_list = tok_ptr;
455 after_name = tok_ptr;
456 } else if (ch == ':' && last_char() != ',') {
457 _nc_syntax = SYN_TERMCAP;
460 } else if (ch == ',') {
461 _nc_syntax = SYN_TERMINFO;
464 * If we did not see a '|', then we found a name with no
465 * aliases or description.
470 * If we see a comma, we assume this is terminfo unless we
471 * subsequently run into a colon. But we don't stop
472 * looking for a colon until hitting a newline. This
473 * allows commas to be embedded in description fields of
477 ch = eat_escaped_newline(ch);
486 if (_nc_syntax == ERR) {
488 * Grrr...what we ought to do here is barf, complaining that
489 * the entry is malformed. But because a couple of name fields
490 * in the 8.2 termcap file end with |\, we just have to assume
491 * it's termcap syntax.
493 _nc_syntax = SYN_TERMCAP;
495 } else if (_nc_syntax == SYN_TERMINFO) {
496 /* throw away trailing /, *$/ */
498 iswhite(*tok_ptr) || *tok_ptr == ',';
505 * This is the soonest we have the terminal name fetched. Set up
506 * for following warning messages. If there's no '|', then there
509 if (after_name != 0) {
512 _nc_set_type(tok_buf);
513 *after_name = (char) ch;
517 * Compute the boundary between the aliases and the description
518 * field for syntax-checking purposes.
520 if (after_list != 0) {
522 if (*after_list == '\0')
523 _nc_warning("empty longname field");
524 else if (strchr(after_list, ' ') == 0)
525 _nc_warning("older tic versions may treat the description field as an alias");
528 after_list = tok_buf + strlen(tok_buf);
529 DEBUG(1, ("missing description"));
533 * Whitespace in a name field other than the long name can confuse
534 * rdist and some termcap tools. Slashes are a no-no. Other
535 * special characters can be dangerous due to shell expansion.
537 for (s = tok_buf; s < after_list; ++s) {
538 if (isspace(UChar(*s))) {
540 _nc_warning("whitespace in name or alias field");
542 } else if (*s == '/') {
544 _nc_warning("slashes aren't allowed in names or aliases");
546 } else if (strchr("$[]!*?", *s)) {
548 _nc_warning("dubious character `%c' in name or alias field", *s);
553 _nc_curr_token.tk_name = tok_buf;
556 if (had_newline && _nc_syntax == SYN_TERMCAP) {
557 _nc_warning("Missing backslash before newline");
560 while ((ch = next_char()) != EOF) {
561 if (!isalnum(UChar(ch))) {
562 if (_nc_syntax == SYN_TERMINFO) {
565 } else { /* allow ';' for "k;" */
578 *tok_ptr++ = '\0'; /* separate name/value in buffer */
583 _nc_err_abort("Separator inconsistent with syntax");
584 _nc_curr_token.tk_name = tok_buf;
588 if ((ch = next_char()) != separator && !silent)
589 _nc_warning("Missing separator after `%s', have %s",
590 tok_buf, unctrl(UChar(ch)));
591 _nc_curr_token.tk_name = tok_buf;
597 while (isalnum(ch = next_char())) {
598 numbuf[found++] = (char) ch;
599 if (found >= sizeof(numbuf) - 1)
602 numbuf[found] = '\0';
603 number = strtol(numbuf, &numchk, 0);
605 if (numchk == numbuf)
606 _nc_warning("no value given for `%s'", tok_buf);
607 if ((*numchk != '\0') || (ch != separator))
608 _nc_warning("Missing separator");
610 _nc_curr_token.tk_name = tok_buf;
611 _nc_curr_token.tk_valnumber = number;
616 ch = _nc_trans_string(tok_ptr, tok_buf + TOK_BUF_SIZE);
617 if (!silent && ch != separator)
618 _nc_warning("Missing separator");
619 _nc_curr_token.tk_name = tok_buf;
620 _nc_curr_token.tk_valstring = tok_ptr;
628 /* just to get rid of the compiler warning */
631 _nc_warning("Illegal character - '%s'", unctrl(UChar(ch)));
633 } /* end else (first_column == FALSE) */
634 } /* end else (ch != EOF) */
639 if (dot_flag == TRUE)
640 DEBUG(8, ("Commented out "));
642 if (_nc_tracing >= DEBUG_LEVEL(8)) {
643 _tracef("parsed %d.%d to %d.%d",
645 _nc_curr_line, _nc_curr_col);
647 if (_nc_tracing >= DEBUG_LEVEL(7)) {
650 _tracef("Token: Boolean; name='%s'",
651 _nc_curr_token.tk_name);
655 _tracef("Token: Number; name='%s', value=%d",
656 _nc_curr_token.tk_name,
657 _nc_curr_token.tk_valnumber);
661 _tracef("Token: String; name='%s', value=%s",
662 _nc_curr_token.tk_name,
663 _nc_visbuf(_nc_curr_token.tk_valstring));
667 _tracef("Token: Cancel; name='%s'",
668 _nc_curr_token.tk_name);
673 _tracef("Token: Names; value='%s'",
674 _nc_curr_token.tk_name);
678 _tracef("Token: End of file");
682 _nc_warning("Bad token type");
687 if (dot_flag == TRUE) /* if commented out, use the next one */
688 type = _nc_get_token(silent);
690 DEBUG(3, ("token: `%s', class %d",
691 ((_nc_curr_token.tk_name != 0)
692 ? _nc_curr_token.tk_name
703 * Reads characters using next_char() until encountering a separator, nl,
704 * or end-of-file. The returned value is the character which caused
705 * reading to stop. The following translations are done on the input:
707 * ^X goes to ctrl-X (i.e. X & 037)
708 * {\E,\n,\r,\b,\t,\f} go to
709 * {ESCAPE,newline,carriage-return,backspace,tab,formfeed}
710 * {\^,\\} go to {carat,backslash}
711 * \ddd (for ddd = up to three octal digits) goes to the character ddd
719 _nc_trans_string(char *ptr, char *last)
725 bool ignored = FALSE;
726 bool long_warning = FALSE;
728 while ((c = next_char()) != separator && c != EOF) {
729 if (ptr >= (last - 1)) {
731 while ((c = next_char()) != separator && c != EOF) {
737 if ((_nc_syntax == SYN_TERMCAP) && c == '\n')
739 if (c == '^' && last_ch != '%') {
742 _nc_err_abort(MSG_NO_INPUTS);
744 if (!(is7bits(c) && isprint(c))) {
745 _nc_warning("Illegal ^ character - '%s'", unctrl(UChar(c)));
750 _nc_warning("Allow ^? as synonym for \\177");
754 *(ptr++) = (char) (c);
756 } else if (c == '\\') {
759 _nc_err_abort(MSG_NO_INPUTS);
761 if (c >= '0' && c <= '7') {
763 for (i = 0; i < 2; i++) {
766 _nc_err_abort(MSG_NO_INPUTS);
768 if (c < '0' || c > '7') {
770 _nc_warning("Non-octal digit `%c' in \\ sequence", c);
771 /* allow the digit; it'll do less harm */
778 number = number * 8 + c - '0';
783 *(ptr++) = (char) number;
840 _nc_warning("Illegal character '%s' in \\ sequence",
845 } /* endswitch (c) */
846 } /* endelse (c < '0' || c > '7') */
848 /* end else if (c == '\\') */
849 else if (c == '\n' && (_nc_syntax == SYN_TERMINFO)) {
851 * Newlines embedded in a terminfo string are ignored, provided
852 * that the next line begins with whitespace.
860 if (_nc_curr_col <= 1) {
870 if (count > MAXCAPLEN && !long_warning) {
871 _nc_warning("Very long string found. Missing separator?");
884 * Push a token of given type so that it will be reread by the next
889 _nc_push_token(int tokclass)
892 * This implementation is kind of bogus, it will fail if we ever do more
893 * than one pushback at a time between get_token() calls. It relies on the
894 * fact that _nc_curr_token is static storage that nothing but
895 * _nc_get_token() touches.
899 pushname = typeMalloc(char, MAX_NAME_SIZE + 1);
900 _nc_get_type(pushname);
902 DEBUG(3, ("pushing token: `%s', class %d",
903 ((_nc_curr_token.tk_name != 0)
904 ? _nc_curr_token.tk_name
910 * Panic mode error recovery - skip everything until a "ch" is found.
913 _nc_panic_mode(char ch)
928 _nc_comp_scan_leaks(void)
931 FreeAndNull(pushname);