1 /****************************************************************************
2 * Copyright (c) 1998,1999,2000,2001 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 ****************************************************************************/
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>
52 #include <term_entry.h>
55 MODULE_ID("$Id: comp_scan.c,v 1.59 2001/09/23 00:56:29 tom Exp $")
58 * Maximum length of string capability we'll accept before raising an error.
59 * Yes, there is a real capability in /etc/termcap this long, an "is".
63 #define iswhite(ch) (ch == ' ' || ch == '\t')
65 NCURSES_EXPORT_VAR(int)
66 _nc_syntax = 0; /* termcap or terminfo? */
67 NCURSES_EXPORT_VAR(long)
68 _nc_curr_file_pos = 0; /* file offset of current line */
69 NCURSES_EXPORT_VAR(long)
70 _nc_comment_start = 0; /* start of comment range before name */
71 NCURSES_EXPORT_VAR(long)
72 _nc_comment_end = 0; /* end of comment range before name */
73 NCURSES_EXPORT_VAR(long)
74 _nc_start_line = 0; /* start line of current entry */
76 NCURSES_EXPORT_VAR(struct token)
82 /*****************************************************************************
84 * Token-grabbing machinery
86 *****************************************************************************/
88 static bool first_column; /* See 'next_char()' below */
89 static char separator; /* capability separator */
90 static int pushtype; /* type of pushback token */
91 static char *pushname;
94 NCURSES_EXPORT_VAR(bool)
95 _nc_disable_period = FALSE; /* used by tic -a option */
98 static int last_char(void);
99 static int next_char(void);
100 static long stream_pos(void);
101 static bool end_of_stream(void);
102 static void push_back(char c);
104 /* Assume we may be looking at a termcap-style continuation */
106 eat_escaped_newline(int ch)
109 while ((ch = next_char()) == '\n' || iswhite(ch))
118 * Scans the input for the next token, storing the specifics in the
119 * global structure 'curr_token' and returning one of the following:
121 * NAMES A line beginning in column 1. 'name'
122 * will be set to point to everything up to but
123 * not including the first separator on the line.
124 * BOOLEAN An entry consisting of a name followed by
125 * a separator. 'name' will be set to point to
126 * the name of the capability.
127 * NUMBER An entry of the form
129 * 'name' will be set to point to the capability
130 * name and 'valnumber' to the number given.
131 * STRING An entry of the form
133 * 'name' is set to the capability name and
134 * 'valstring' to the string of characters, with
135 * input translations done.
136 * CANCEL An entry of the form
138 * 'name' is set to the capability name and
140 * EOF The end of the file has been reached.
142 * A `separator' is either a comma or a semicolon, depending on whether
143 * we are in termcap or terminfo mode.
148 _nc_get_token(bool silent)
150 static const char terminfo_punct[] = "@%&*!#";
157 int dot_flag = FALSE;
163 if (pushtype != NO_PUSHBACK) {
164 int retval = pushtype;
166 _nc_set_type(pushname != 0 ? pushname : "");
167 DEBUG(3, ("pushed-back token: `%s', class %d",
168 _nc_curr_token.tk_name, pushtype));
170 pushtype = NO_PUSHBACK;
174 /* currtok wasn't altered by _nc_push_token() */
182 token_start = stream_pos();
183 while ((ch = next_char()) == '\n' || iswhite(ch))
186 ch = eat_escaped_newline(ch);
191 /* if this is a termcap entry, skip a leading separator */
192 if (separator == ':' && ch == ':')
196 #if NCURSES_EXT_FUNCS
197 && !_nc_disable_period
201 DEBUG(8, ("dot-flag set"));
203 while ((ch = next_char()) == '.' || iswhite(ch))
212 /* have to make some punctuation chars legal for terminfo */
214 #if NCURSES_EXT_FUNCS
215 && !(ch == '.' && _nc_disable_period)
217 && !strchr(terminfo_punct, (char) ch)) {
219 _nc_warning("Illegal character (expected alphanumeric or %s) - %s",
220 terminfo_punct, unctrl((chtype) ch));
221 _nc_panic_mode(separator);
226 buffer = _nc_doalloc(buffer, MAX_ENTRY_SIZE);
234 _nc_comment_start = token_start;
235 _nc_comment_end = _nc_curr_file_pos;
236 _nc_start_line = _nc_curr_line;
239 while ((ch = next_char()) != '\n') {
241 _nc_err_abort("premature EOF");
242 else if (ch == ':' && last_char() != ',') {
243 _nc_syntax = SYN_TERMCAP;
246 } else if (ch == ',') {
247 _nc_syntax = SYN_TERMINFO;
250 * Fall-through here is not an accident. The idea is that
251 * if we see a comma, we figure this is terminfo unless we
252 * subsequently run into a colon -- but we don't stop
253 * looking for that colon until hitting a newline. This
254 * allows commas to be embedded in description fields of
259 ch = eat_escaped_newline(ch);
264 if (_nc_syntax == ERR) {
266 * Grrr...what we ought to do here is barf, complaining that
267 * the entry is malformed. But because a couple of name fields
268 * in the 8.2 termcap file end with |\, we just have to assume
269 * it's termcap syntax.
271 _nc_syntax = SYN_TERMCAP;
273 } else if (_nc_syntax == SYN_TERMINFO) {
274 /* throw away trailing /, *$/ */
275 for (--ptr; iswhite(*ptr) || *ptr == ','; ptr--)
281 * This is the soonest we have the terminal name fetched. Set up
282 * for following warning messages.
284 ptr = strchr(buffer, '|');
285 if (ptr == (char *) NULL)
286 ptr = buffer + strlen(buffer);
289 _nc_set_type(buffer);
293 * Compute the boundary between the aliases and the description
294 * field for syntax-checking purposes.
296 desc = strrchr(buffer, '|');
297 if (!silent && desc) {
299 _nc_warning("empty longname field");
300 #ifndef FREEBSD_NATIVE
301 else if (strchr(desc, ' ') == (char *) NULL)
302 _nc_warning("older tic versions may treat the description field as an alias");
306 desc = buffer + strlen(buffer);
309 * Whitespace in a name field other than the long name can confuse
310 * rdist and some termcap tools. Slashes are a no-no. Other
311 * special characters can be dangerous due to shell expansion.
313 for (ptr = buffer; ptr < desc; ptr++) {
314 if (isspace(UChar(*ptr))) {
316 _nc_warning("whitespace in name or alias field");
318 } else if (*ptr == '/') {
320 _nc_warning("slashes aren't allowed in names or aliases");
322 } else if (strchr("$[]!*?", *ptr)) {
324 _nc_warning("dubious character `%c' in name or alias field", *ptr);
331 _nc_curr_token.tk_name = buffer;
334 while ((ch = next_char()) != EOF) {
336 if (_nc_syntax == SYN_TERMINFO) {
339 } else { /* allow ';' for "k;" */
352 _nc_err_abort("Separator inconsistent with syntax");
353 _nc_curr_token.tk_name = buffer;
357 if ((ch = next_char()) != separator && !silent)
358 _nc_warning("Missing separator after `%s', have %s",
359 buffer, unctrl((chtype) ch));
360 _nc_curr_token.tk_name = buffer;
366 while (isalnum(ch = next_char())) {
367 numbuf[found++] = ch;
368 if (found >= sizeof(numbuf) - 1)
371 numbuf[found] = '\0';
372 number = strtol(numbuf, &numchk, 0);
374 if (numchk == numbuf)
375 _nc_warning("no value given for `%s'", buffer);
376 if ((*numchk != '\0') || (ch != separator))
377 _nc_warning("Missing separator");
379 _nc_curr_token.tk_name = buffer;
380 _nc_curr_token.tk_valnumber = number;
385 ch = _nc_trans_string(ptr, buffer + MAX_ENTRY_SIZE);
386 if (!silent && ch != separator)
387 _nc_warning("Missing separator");
388 _nc_curr_token.tk_name = buffer;
389 _nc_curr_token.tk_valstring = ptr;
397 /* just to get rid of the compiler warning */
400 _nc_warning("Illegal character - %s", unctrl((chtype) ch));
402 } /* end else (first_column == FALSE) */
403 } /* end else (ch != EOF) */
408 if (dot_flag == TRUE)
409 DEBUG(8, ("Commented out "));
411 if (_nc_tracing >= DEBUG_LEVEL(7)) {
414 _tracef("Token: Boolean; name='%s'",
415 _nc_curr_token.tk_name);
419 _tracef("Token: Number; name='%s', value=%d",
420 _nc_curr_token.tk_name,
421 _nc_curr_token.tk_valnumber);
425 _tracef("Token: String; name='%s', value=%s",
426 _nc_curr_token.tk_name,
427 _nc_visbuf(_nc_curr_token.tk_valstring));
431 _tracef("Token: Cancel; name='%s'",
432 _nc_curr_token.tk_name);
437 _tracef("Token: Names; value='%s'",
438 _nc_curr_token.tk_name);
442 _tracef("Token: End of file");
446 _nc_warning("Bad token type");
451 if (dot_flag == TRUE) /* if commented out, use the next one */
452 type = _nc_get_token(silent);
454 DEBUG(3, ("token: `%s', class %d",
455 _nc_curr_token.tk_name != 0 ? _nc_curr_token.tk_name :
466 * Reads characters using next_char() until encountering a separator, nl,
467 * or end-of-file. The returned value is the character which caused
468 * reading to stop. The following translations are done on the input:
470 * ^X goes to ctrl-X (i.e. X & 037)
471 * {\E,\n,\r,\b,\t,\f} go to
472 * {ESCAPE,newline,carriage-return,backspace,tab,formfeed}
473 * {\^,\\} go to {carat,backslash}
474 * \ddd (for ddd = up to three octal digits) goes to the character ddd
482 _nc_trans_string(char *ptr, char *last)
487 chtype ch, last_ch = '\0';
488 bool ignored = FALSE;
489 bool long_warning = FALSE;
491 while ((ch = c = next_char()) != (chtype) separator && c != EOF) {
492 if (ptr == (last - 1))
494 if ((_nc_syntax == SYN_TERMCAP) && c == '\n')
496 if (ch == '^' && last_ch != '%') {
497 ch = c = next_char();
499 _nc_err_abort("Premature EOF");
501 if (!(is7bits(ch) && isprint(ch))) {
502 _nc_warning("Illegal ^ character - %s", unctrl(ch));
507 _nc_warning("Allow ^? as synonym for \\177");
509 if ((ch &= 037) == 0)
511 *(ptr++) = (char) (ch);
513 } else if (ch == '\\') {
514 ch = c = next_char();
516 _nc_err_abort("Premature EOF");
518 if (ch >= '0' && ch <= '7') {
520 for (i = 0; i < 2; i++) {
521 ch = c = next_char();
523 _nc_err_abort("Premature EOF");
525 if (c < '0' || c > '7') {
527 _nc_warning("Non-octal digit `%c' in \\ sequence", c);
528 /* allow the digit; it'll do less harm */
535 number = number * 8 + c - '0';
540 *(ptr++) = (char) number;
597 _nc_warning("Illegal character %s in \\ sequence",
599 *(ptr++) = (char) ch;
600 } /* endswitch (ch) */
601 } /* endelse (ch < '0' || ch > '7') */
603 /* end else if (ch == '\\') */
604 else if (ch == '\n' && (_nc_syntax == SYN_TERMINFO)) {
605 /* newlines embedded in a terminfo string are ignored */
608 *(ptr++) = (char) ch;
617 if (count > MAXCAPLEN && !long_warning) {
618 _nc_warning("Very long string found. Missing separator?");
631 * Push a token of given type so that it will be reread by the next
636 _nc_push_token(int tokclass)
639 * This implementation is kind of bogus, it will fail if we ever do more
640 * than one pushback at a time between get_token() calls. It relies on the
641 * fact that _nc_curr_token is static storage that nothing but
642 * _nc_get_token() touches.
646 pushname = _nc_doalloc(pushname, MAX_NAME_SIZE + 1);
647 _nc_get_type(pushname);
649 DEBUG(3, ("pushing token: `%s', class %d",
650 _nc_curr_token.tk_name, pushtype));
654 * Panic mode error recovery - skip everything until a "ch" is found.
657 _nc_panic_mode(char ch)
670 /*****************************************************************************
672 * Character-stream handling
674 *****************************************************************************/
676 #define LEXBUFSIZ 1024
678 static char *bufptr; /* otherwise, the input buffer pointer */
679 static char *bufstart; /* start of buffer so we can compute offsets */
680 static FILE *yyin; /* scanner's input file descriptor */
685 * Resets the input-reading routines. Used on initialization,
686 * or after a seek has been done. Exactly one argument must be
691 _nc_reset_input(FILE * fp, char *buf)
693 pushtype = NO_PUSHBACK;
697 bufstart = bufptr = buf;
698 _nc_curr_file_pos = 0L;
707 * Returns the final nonblank character on the current input buffer
712 size_t len = strlen(bufptr);
714 if (!isspace(UChar(bufptr[len])))
723 * Returns the next character in the input stream. Comments and leading
724 * white space are stripped.
726 * The global state variable 'firstcolumn' is set TRUE if the character
727 * returned is from the first column of the input line.
729 * The global variable _nc_curr_line is incremented for each new line.
730 * The global variable _nc_curr_file_pos is set to the file offset of the
731 * beginning of each line.
739 * An string with an embedded null will truncate the input. This is
740 * intentional (we don't read binary files here).
744 if (*bufptr == '\n') {
748 } else if (!bufptr || !*bufptr) {
750 * In theory this could be recoded to do its I/O one character at a
751 * time, saving the buffer space. In practice, this turns out to be
752 * quite hard to get completely right. Try it and see. If you
753 * succeed, don't forget to hack push_back() correspondingly.
756 static size_t allocated;
764 if (used + (LEXBUFSIZ / 4) >= allocated) {
765 allocated += (allocated + LEXBUFSIZ);
766 result = _nc_doalloc(result, allocated);
771 _nc_curr_file_pos = ftell(yyin);
773 if (fgets(result + used, allocated - used, yyin) != NULL) {
781 strcat(result, "\n");
783 if ((bufptr = bufstart) != 0) {
784 used = strlen(bufptr);
785 while (iswhite(*bufptr))
789 * Treat a trailing <cr><lf> the same as a <newline> so we
790 * can read files on OS/2, etc.
792 if ((len = strlen(bufptr)) > 1) {
793 if (bufptr[len - 1] == '\n'
794 && bufptr[len - 2] == '\r') {
796 bufptr[len - 1] = '\n';
803 } while (bufptr[len - 1] != '\n'); /* complete a line */
804 } while (result[0] == '#'); /* ignore comments */
807 first_column = (bufptr == bufstart);
815 /* push a character back onto the input stream */
817 if (bufptr == bufstart)
818 _nc_syserr_abort("Can't backspace off beginning of line");
824 /* return our current character position in the input stream */
826 return (yyin ? ftell(yyin) : (bufptr ? bufptr - bufstart : 0));
831 /* are we at end of input? */
833 return ((yyin ? feof(yyin) : (bufptr && *bufptr == '\0'))