1 /****************************************************************************
2 * Copyright (c) 1998,1999,2000 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 ****************************************************************************/
35 * comp_scan.c --- Lexical scanner for terminfo compiler.
42 * long _nc_curr_file_pos;
43 * long _nc_comment_start;
44 * long _nc_comment_end;
47 #include <curses.priv.h>
50 #include <term_entry.h>
53 MODULE_ID("$Id: comp_scan.c,v 1.47 2000/09/24 01:15:17 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 int _nc_syntax = 0; /* termcap or terminfo? */
64 long _nc_curr_file_pos = 0; /* file offset of current line */
65 long _nc_comment_start = 0; /* start of comment range before name */
66 long _nc_comment_end = 0; /* end of comment range before name */
67 long _nc_start_line = 0; /* start line of current entry */
69 struct token _nc_curr_token =
72 /*****************************************************************************
74 * Token-grabbing machinery
76 *****************************************************************************/
78 static bool first_column; /* See 'next_char()' below */
79 static char separator; /* capability separator */
80 static int pushtype; /* type of pushback token */
81 static char pushname[MAX_NAME_SIZE + 1];
84 bool _nc_disable_period = FALSE; /* used by tic -a option */
87 static int last_char(void);
88 static int next_char(void);
89 static long stream_pos(void);
90 static bool end_of_stream(void);
91 static void push_back(char c);
93 /* Assume we may be looking at a termcap-style continuation */
95 eat_escaped_newline(int ch)
98 while ((ch = next_char()) == '\n' || iswhite(ch))
107 * Scans the input for the next token, storing the specifics in the
108 * global structure 'curr_token' and returning one of the following:
110 * NAMES A line beginning in column 1. 'name'
111 * will be set to point to everything up to but
112 * not including the first separator on the line.
113 * BOOLEAN An entry consisting of a name followed by
114 * a separator. 'name' will be set to point to
115 * the name of the capability.
116 * NUMBER An entry of the form
118 * 'name' will be set to point to the capability
119 * name and 'valnumber' to the number given.
120 * STRING An entry of the form
122 * 'name' is set to the capability name and
123 * 'valstring' to the string of characters, with
124 * input translations done.
125 * CANCEL An entry of the form
127 * 'name' is set to the capability name and
129 * EOF The end of the file has been reached.
131 * A `separator' is either a comma or a semicolon, depending on whether
132 * we are in termcap or terminfo mode.
139 static const char terminfo_punct[] = "@%&*!#";
146 static char buffer[MAX_ENTRY_SIZE];
148 int dot_flag = FALSE;
151 if (pushtype != NO_PUSHBACK) {
152 int retval = pushtype;
154 _nc_set_type(pushname);
155 DEBUG(3, ("pushed-back token: `%s', class %d",
156 _nc_curr_token.tk_name, pushtype));
158 pushtype = NO_PUSHBACK;
161 /* currtok wasn't altered by _nc_push_token() */
169 token_start = stream_pos();
170 while ((ch = next_char()) == '\n' || iswhite(ch))
173 ch = eat_escaped_newline(ch);
178 /* if this is a termcap entry, skip a leading separator */
179 if (separator == ':' && ch == ':')
183 #if NCURSES_EXT_FUNCS
184 && !_nc_disable_period
188 DEBUG(8, ("dot-flag set"));
190 while ((ch = next_char()) == '.' || iswhite(ch))
199 /* have to make some punctuation chars legal for terminfo */
201 #if NCURSES_EXT_FUNCS
202 && !(ch == '.' && _nc_disable_period)
204 && !strchr(terminfo_punct, (char) ch)) {
205 _nc_warning("Illegal character (expected alphanumeric or %s) - %s",
206 terminfo_punct, unctrl(ch));
207 _nc_panic_mode(separator);
217 _nc_comment_start = token_start;
218 _nc_comment_end = _nc_curr_file_pos;
219 _nc_start_line = _nc_curr_line;
222 while ((ch = next_char()) != '\n') {
224 _nc_err_abort("premature EOF");
225 else if (ch == ':' && last_char() != ',') {
226 _nc_syntax = SYN_TERMCAP;
229 } else if (ch == ',') {
230 _nc_syntax = SYN_TERMINFO;
233 * Fall-through here is not an accident.
234 * The idea is that if we see a comma, we
235 * figure this is terminfo unless we
236 * subsequently run into a colon -- but
237 * we don't stop looking for that colon until
238 * hitting a newline. This allows commas to
239 * be embedded in description fields of
244 ch = eat_escaped_newline(ch);
249 if (_nc_syntax == ERR) {
251 * Grrr...what we ought to do here is barf,
252 * complaining that the entry is malformed.
253 * But because a couple of name fields in the
254 * 8.2 termcap file end with |\, we just have
255 * to assume it's termcap syntax.
257 _nc_syntax = SYN_TERMCAP;
259 } else if (_nc_syntax == SYN_TERMINFO) {
260 /* throw away trailing /, *$/ */
261 for (--ptr; iswhite(*ptr) || *ptr == ','; ptr--)
267 * This is the soonest we have the terminal name
268 * fetched. Set up for following warning messages.
270 ptr = strchr(buffer, '|');
271 if (ptr == (char *) NULL)
272 ptr = buffer + strlen(buffer);
275 _nc_set_type(buffer);
279 * Compute the boundary between the aliases and the
280 * description field for syntax-checking purposes.
282 desc = strrchr(buffer, '|');
285 _nc_warning("empty longname field");
286 else if (strchr(desc, ' ') == (char *) NULL)
287 _nc_warning("older tic versions may treat the description field as an alias");
290 desc = buffer + strlen(buffer);
293 * Whitespace in a name field other than the long name
294 * can confuse rdist and some termcap tools. Slashes
295 * are a no-no. Other special characters can be
296 * dangerous due to shell expansion.
298 for (ptr = buffer; ptr < desc; ptr++) {
300 _nc_warning("whitespace in name or alias field");
302 } else if (*ptr == '/') {
303 _nc_warning("slashes aren't allowed in names or aliases");
305 } else if (strchr("$[]!*?", *ptr)) {
306 _nc_warning("dubious character `%c' in name or alias field", *ptr);
313 _nc_curr_token.tk_name = buffer;
316 while ((ch = next_char()) != EOF) {
318 if (_nc_syntax == SYN_TERMINFO) {
321 } else { /* allow ';' for "k;" */
334 _nc_err_abort("Separator inconsistent with syntax");
335 _nc_curr_token.tk_name = buffer;
339 if ((ch = next_char()) != separator)
340 _nc_warning("Missing separator after `%s', have %s",
342 _nc_curr_token.tk_name = buffer;
348 while (isalnum(ch = next_char())) {
349 numbuf[found++] = ch;
350 if (found >= sizeof(numbuf) - 1)
353 numbuf[found] = '\0';
354 number = strtol(numbuf, &numchk, 0);
355 if (numchk == numbuf)
356 _nc_warning("no value given for `%s'", buffer);
357 if ((*numchk != '\0') || (ch != separator))
358 _nc_warning("Missing separator");
359 _nc_curr_token.tk_name = buffer;
360 _nc_curr_token.tk_valnumber = number;
365 ch = _nc_trans_string(ptr, buffer + sizeof(buffer));
367 _nc_warning("Missing separator");
368 _nc_curr_token.tk_name = buffer;
369 _nc_curr_token.tk_valstring = ptr;
377 /* just to get rid of the compiler warning */
379 _nc_warning("Illegal character - %s", unctrl(ch));
381 } /* end else (first_column == FALSE) */
382 } /* end else (ch != EOF) */
387 if (dot_flag == TRUE)
388 DEBUG(8, ("Commented out "));
390 if (_nc_tracing >= DEBUG_LEVEL(7)) {
393 _tracef("Token: Boolean; name='%s'",
394 _nc_curr_token.tk_name);
398 _tracef("Token: Number; name='%s', value=%d",
399 _nc_curr_token.tk_name,
400 _nc_curr_token.tk_valnumber);
404 _tracef("Token: String; name='%s', value=%s",
405 _nc_curr_token.tk_name,
406 _nc_visbuf(_nc_curr_token.tk_valstring));
410 _tracef("Token: Cancel; name='%s'",
411 _nc_curr_token.tk_name);
416 _tracef("Token: Names; value='%s'",
417 _nc_curr_token.tk_name);
421 _tracef("Token: End of file");
425 _nc_warning("Bad token type");
430 if (dot_flag == TRUE) /* if commented out, use the next one */
431 type = _nc_get_token();
433 DEBUG(3, ("token: `%s', class %d", _nc_curr_token.tk_name, type));
442 * Reads characters using next_char() until encountering a separator, nl,
443 * or end-of-file. The returned value is the character which caused
444 * reading to stop. The following translations are done on the input:
446 * ^X goes to ctrl-X (i.e. X & 037)
447 * {\E,\n,\r,\b,\t,\f} go to
448 * {ESCAPE,newline,carriage-return,backspace,tab,formfeed}
449 * {\^,\\} go to {carat,backslash}
450 * \ddd (for ddd = up to three octal digits) goes to the character ddd
458 _nc_trans_string(char *ptr, char *last)
463 chtype ch, last_ch = '\0';
464 bool ignored = FALSE;
465 bool long_warning = FALSE;
467 while ((ch = c = next_char()) != (chtype) separator && c != EOF) {
468 if (ptr == (last - 1))
470 if ((_nc_syntax == SYN_TERMCAP) && c == '\n')
472 if (ch == '^' && last_ch != '%') {
473 ch = c = next_char();
475 _nc_err_abort("Premature EOF");
477 if (!(is7bits(ch) && isprint(ch))) {
478 _nc_warning("Illegal ^ character - %s", unctrl(ch));
483 _nc_warning("Allow ^? as synonym for \\177");
485 if ((ch &= 037) == 0)
487 *(ptr++) = (char) (ch);
489 } else if (ch == '\\') {
490 ch = c = next_char();
492 _nc_err_abort("Premature EOF");
494 if (ch >= '0' && ch <= '7') {
496 for (i = 0; i < 2; i++) {
497 ch = c = next_char();
499 _nc_err_abort("Premature EOF");
501 if (c < '0' || c > '7') {
503 _nc_warning("Non-octal digit `%c' in \\ sequence", c);
504 /* allow the digit; it'll do less harm */
511 number = number * 8 + c - '0';
516 *(ptr++) = (char) number;
573 _nc_warning("Illegal character %s in \\ sequence",
575 *(ptr++) = (char) ch;
576 } /* endswitch (ch) */
577 } /* endelse (ch < '0' || ch > '7') */
579 /* end else if (ch == '\\') */
580 else if (ch == '\n' && (_nc_syntax == SYN_TERMINFO)) {
581 /* newlines embedded in a terminfo string are ignored */
584 *(ptr++) = (char) ch;
593 if (count > MAXCAPLEN && !long_warning) {
594 _nc_warning("Very long string found. Missing separator?");
607 * Push a token of given type so that it will be reread by the next
612 _nc_push_token(int tokclass)
615 * This implementation is kind of bogus, it will fail if we ever do
616 * more than one pushback at a time between get_token() calls. It
617 * relies on the fact that curr_tok is static storage that nothing
618 * but get_token() touches.
621 _nc_get_type(pushname);
623 DEBUG(3, ("pushing token: `%s', class %d",
624 _nc_curr_token.tk_name, pushtype));
628 * Panic mode error recovery - skip everything until a "ch" is found.
631 _nc_panic_mode(char ch)
644 /*****************************************************************************
646 * Character-stream handling
648 *****************************************************************************/
650 #define LEXBUFSIZ 1024
652 static char *bufptr; /* otherwise, the input buffer pointer */
653 static char *bufstart; /* start of buffer so we can compute offsets */
654 static FILE *yyin; /* scanner's input file descriptor */
659 * Resets the input-reading routines. Used on initialization,
660 * or after a seek has been done. Exactly one argument must be
665 _nc_reset_input(FILE * fp, char *buf)
667 pushtype = NO_PUSHBACK;
670 bufstart = bufptr = buf;
671 _nc_curr_file_pos = 0L;
680 * Returns the final nonblank character on the current input buffer
685 size_t len = strlen(bufptr);
687 if (!isspace(bufptr[len]))
696 * Returns the next character in the input stream. Comments and leading
697 * white space are stripped.
699 * The global state variable 'firstcolumn' is set TRUE if the character
700 * returned is from the first column of the input line.
702 * The global variable _nc_curr_line is incremented for each new line.
703 * The global variable _nc_curr_file_pos is set to the file offset of the
704 * beginning of each line.
713 if (*bufptr == '\n') {
717 } else if (!bufptr || !*bufptr) {
719 * In theory this could be recoded to do its I/O one
720 * character at a time, saving the buffer space. In
721 * practice, this turns out to be quite hard to get
722 * completely right. Try it and see. If you succeed,
723 * don't forget to hack push_back() correspondingly.
725 static char line[LEXBUFSIZ];
729 _nc_curr_file_pos = ftell(yyin);
731 if ((bufstart = fgets(line, LEXBUFSIZ, yyin)) != NULL) {
737 (bufstart != NULL && line[0] == '#');
739 if (bufstart == NULL || *bufstart == 0)
742 while (iswhite(*bufptr))
746 * Treat a trailing <cr><lf> the same as a <newline> so we can read
747 * files on OS/2, etc.
749 if ((len = strlen(bufptr)) > 1) {
750 if (bufptr[len - 1] == '\n'
751 && bufptr[len - 2] == '\r') {
753 bufptr[len - 1] = '\n';
759 * If we don't have a trailing newline, it's because the line is simply
760 * too long. Give up. (FIXME: We could instead reallocate the line
761 * buffer and allow arbitrary-length lines).
763 if (len == 0 || (bufptr[len - 1] != '\n'))
767 first_column = (bufptr == bufstart);
775 /* push a character back onto the input stream */
777 if (bufptr == bufstart)
778 _nc_syserr_abort("Can't backspace off beginning of line");
784 /* return our current character position in the input stream */
786 return (yyin ? ftell(yyin) : (bufptr ? bufptr - bufstart : 0));
791 /* are we at end of input? */
793 return ((yyin ? feof(yyin) : (bufptr && *bufptr == '\0'))
797 /* comp_scan.c ends here */