1 /****************************************************************************
2 * Copyright (c) 1998-2001,2002 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.61 2002/09/07 20:04:09 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)
64 _nc_syntax = 0; /* termcap or terminfo? */
65 NCURSES_EXPORT_VAR(long)
66 _nc_curr_file_pos = 0; /* file offset of current line */
67 NCURSES_EXPORT_VAR(long)
68 _nc_comment_start = 0; /* start of comment range before name */
69 NCURSES_EXPORT_VAR(long)
70 _nc_comment_end = 0; /* end of comment range before name */
71 NCURSES_EXPORT_VAR(long)
72 _nc_start_line = 0; /* start line of current entry */
74 NCURSES_EXPORT_VAR(struct token)
80 /*****************************************************************************
82 * Token-grabbing machinery
84 *****************************************************************************/
86 static bool first_column; /* See 'next_char()' below */
87 static char separator; /* capability separator */
88 static int pushtype; /* type of pushback token */
89 static char *pushname;
92 NCURSES_EXPORT_VAR(bool)
93 _nc_disable_period = FALSE; /* used by tic -a option */
96 static int last_char(void);
97 static int next_char(void);
98 static long stream_pos(void);
99 static bool end_of_stream(void);
100 static void push_back(char c);
102 /* Assume we may be looking at a termcap-style continuation */
104 eat_escaped_newline(int ch)
107 while ((ch = next_char()) == '\n' || iswhite(ch))
116 * Scans the input for the next token, storing the specifics in the
117 * global structure 'curr_token' and returning one of the following:
119 * NAMES A line beginning in column 1. 'name'
120 * will be set to point to everything up to but
121 * not including the first separator on the line.
122 * BOOLEAN An entry consisting of a name followed by
123 * a separator. 'name' will be set to point to
124 * the name of the capability.
125 * NUMBER An entry of the form
127 * 'name' will be set to point to the capability
128 * name and 'valnumber' to the number given.
129 * STRING An entry of the form
131 * 'name' is set to the capability name and
132 * 'valstring' to the string of characters, with
133 * input translations done.
134 * CANCEL An entry of the form
136 * 'name' is set to the capability name and
138 * EOF The end of the file has been reached.
140 * A `separator' is either a comma or a semicolon, depending on whether
141 * we are in termcap or terminfo mode.
146 _nc_get_token(bool silent)
148 static const char terminfo_punct[] = "@%&*!#";
155 int dot_flag = FALSE;
161 if (pushtype != NO_PUSHBACK) {
162 int retval = pushtype;
164 _nc_set_type(pushname != 0 ? pushname : "");
165 DEBUG(3, ("pushed-back token: `%s', class %d",
166 _nc_curr_token.tk_name, pushtype));
168 pushtype = NO_PUSHBACK;
172 /* currtok wasn't altered by _nc_push_token() */
180 token_start = stream_pos();
181 while ((ch = next_char()) == '\n' || iswhite(ch))
184 ch = eat_escaped_newline(ch);
189 /* if this is a termcap entry, skip a leading separator */
190 if (separator == ':' && ch == ':')
194 #if NCURSES_EXT_FUNCS
195 && !_nc_disable_period
199 DEBUG(8, ("dot-flag set"));
201 while ((ch = next_char()) == '.' || iswhite(ch))
210 /* have to make some punctuation chars legal for terminfo */
212 #if NCURSES_EXT_FUNCS
213 && !(ch == '.' && _nc_disable_period)
215 && !strchr(terminfo_punct, (char) ch)) {
217 _nc_warning("Illegal character (expected alphanumeric or %s) - %s",
218 terminfo_punct, unctrl((chtype) ch));
219 _nc_panic_mode(separator);
224 buffer = typeMalloc(char, MAX_ENTRY_SIZE);
232 _nc_comment_start = token_start;
233 _nc_comment_end = _nc_curr_file_pos;
234 _nc_start_line = _nc_curr_line;
237 while ((ch = next_char()) != '\n') {
239 _nc_err_abort(MSG_NO_MEMORY);
240 else if (ch == ':' && last_char() != ',') {
241 _nc_syntax = SYN_TERMCAP;
244 } else if (ch == ',') {
245 _nc_syntax = SYN_TERMINFO;
248 * Fall-through here is not an accident. The idea is that
249 * if we see a comma, we figure this is terminfo unless we
250 * subsequently run into a colon -- but we don't stop
251 * looking for that colon until hitting a newline. This
252 * allows commas to be embedded in description fields of
257 ch = eat_escaped_newline(ch);
262 if (_nc_syntax == ERR) {
264 * Grrr...what we ought to do here is barf, complaining that
265 * the entry is malformed. But because a couple of name fields
266 * in the 8.2 termcap file end with |\, we just have to assume
267 * it's termcap syntax.
269 _nc_syntax = SYN_TERMCAP;
271 } else if (_nc_syntax == SYN_TERMINFO) {
272 /* throw away trailing /, *$/ */
273 for (--ptr; iswhite(*ptr) || *ptr == ','; ptr--)
279 * This is the soonest we have the terminal name fetched. Set up
280 * for following warning messages.
282 ptr = strchr(buffer, '|');
283 if (ptr == (char *) NULL)
284 ptr = buffer + strlen(buffer);
287 _nc_set_type(buffer);
291 * Compute the boundary between the aliases and the description
292 * field for syntax-checking purposes.
294 desc = strrchr(buffer, '|');
295 if (!silent && desc) {
297 _nc_warning("empty longname field");
298 else if (strchr(desc, ' ') == (char *) NULL)
299 _nc_warning("older tic versions may treat the description field as an alias");
302 desc = buffer + strlen(buffer);
305 * Whitespace in a name field other than the long name can confuse
306 * rdist and some termcap tools. Slashes are a no-no. Other
307 * special characters can be dangerous due to shell expansion.
309 for (ptr = buffer; ptr < desc; ptr++) {
310 if (isspace(UChar(*ptr))) {
312 _nc_warning("whitespace in name or alias field");
314 } else if (*ptr == '/') {
316 _nc_warning("slashes aren't allowed in names or aliases");
318 } else if (strchr("$[]!*?", *ptr)) {
320 _nc_warning("dubious character `%c' in name or alias field", *ptr);
327 _nc_curr_token.tk_name = buffer;
330 while ((ch = next_char()) != EOF) {
332 if (_nc_syntax == SYN_TERMINFO) {
335 } else { /* allow ';' for "k;" */
348 _nc_err_abort("Separator inconsistent with syntax");
349 _nc_curr_token.tk_name = buffer;
353 if ((ch = next_char()) != separator && !silent)
354 _nc_warning("Missing separator after `%s', have %s",
355 buffer, unctrl((chtype) ch));
356 _nc_curr_token.tk_name = buffer;
362 while (isalnum(ch = next_char())) {
363 numbuf[found++] = ch;
364 if (found >= sizeof(numbuf) - 1)
367 numbuf[found] = '\0';
368 number = strtol(numbuf, &numchk, 0);
370 if (numchk == numbuf)
371 _nc_warning("no value given for `%s'", buffer);
372 if ((*numchk != '\0') || (ch != separator))
373 _nc_warning("Missing separator");
375 _nc_curr_token.tk_name = buffer;
376 _nc_curr_token.tk_valnumber = number;
381 ch = _nc_trans_string(ptr, buffer + MAX_ENTRY_SIZE);
382 if (!silent && ch != separator)
383 _nc_warning("Missing separator");
384 _nc_curr_token.tk_name = buffer;
385 _nc_curr_token.tk_valstring = ptr;
393 /* just to get rid of the compiler warning */
396 _nc_warning("Illegal character - %s", unctrl((chtype) ch));
398 } /* end else (first_column == FALSE) */
399 } /* end else (ch != EOF) */
404 if (dot_flag == TRUE)
405 DEBUG(8, ("Commented out "));
407 if (_nc_tracing >= DEBUG_LEVEL(7)) {
410 _tracef("Token: Boolean; name='%s'",
411 _nc_curr_token.tk_name);
415 _tracef("Token: Number; name='%s', value=%d",
416 _nc_curr_token.tk_name,
417 _nc_curr_token.tk_valnumber);
421 _tracef("Token: String; name='%s', value=%s",
422 _nc_curr_token.tk_name,
423 _nc_visbuf(_nc_curr_token.tk_valstring));
427 _tracef("Token: Cancel; name='%s'",
428 _nc_curr_token.tk_name);
433 _tracef("Token: Names; value='%s'",
434 _nc_curr_token.tk_name);
438 _tracef("Token: End of file");
442 _nc_warning("Bad token type");
447 if (dot_flag == TRUE) /* if commented out, use the next one */
448 type = _nc_get_token(silent);
450 DEBUG(3, ("token: `%s', class %d",
451 _nc_curr_token.tk_name != 0 ? _nc_curr_token.tk_name :
462 * Reads characters using next_char() until encountering a separator, nl,
463 * or end-of-file. The returned value is the character which caused
464 * reading to stop. The following translations are done on the input:
466 * ^X goes to ctrl-X (i.e. X & 037)
467 * {\E,\n,\r,\b,\t,\f} go to
468 * {ESCAPE,newline,carriage-return,backspace,tab,formfeed}
469 * {\^,\\} go to {carat,backslash}
470 * \ddd (for ddd = up to three octal digits) goes to the character ddd
478 _nc_trans_string(char *ptr, char *last)
483 chtype ch, last_ch = '\0';
484 bool ignored = FALSE;
485 bool long_warning = FALSE;
487 while ((ch = c = next_char()) != (chtype) separator && c != EOF) {
488 if (ptr == (last - 1))
490 if ((_nc_syntax == SYN_TERMCAP) && c == '\n')
492 if (ch == '^' && last_ch != '%') {
493 ch = c = next_char();
495 _nc_err_abort(MSG_NO_INPUTS);
497 if (!(is7bits(ch) && isprint(ch))) {
498 _nc_warning("Illegal ^ character - %s", unctrl(ch));
503 _nc_warning("Allow ^? as synonym for \\177");
505 if ((ch &= 037) == 0)
507 *(ptr++) = (char) (ch);
509 } else if (ch == '\\') {
510 ch = c = next_char();
512 _nc_err_abort(MSG_NO_INPUTS);
514 if (ch >= '0' && ch <= '7') {
516 for (i = 0; i < 2; i++) {
517 ch = c = next_char();
519 _nc_err_abort(MSG_NO_INPUTS);
521 if (c < '0' || c > '7') {
523 _nc_warning("Non-octal digit `%c' in \\ sequence", c);
524 /* allow the digit; it'll do less harm */
531 number = number * 8 + c - '0';
536 *(ptr++) = (char) number;
593 _nc_warning("Illegal character %s in \\ sequence",
595 *(ptr++) = (char) ch;
596 } /* endswitch (ch) */
597 } /* endelse (ch < '0' || ch > '7') */
599 /* end else if (ch == '\\') */
600 else if (ch == '\n' && (_nc_syntax == SYN_TERMINFO)) {
601 /* newlines embedded in a terminfo string are ignored */
604 *(ptr++) = (char) ch;
613 if (count > MAXCAPLEN && !long_warning) {
614 _nc_warning("Very long string found. Missing separator?");
627 * Push a token of given type so that it will be reread by the next
632 _nc_push_token(int tokclass)
635 * This implementation is kind of bogus, it will fail if we ever do more
636 * than one pushback at a time between get_token() calls. It relies on the
637 * fact that _nc_curr_token is static storage that nothing but
638 * _nc_get_token() touches.
642 pushname = typeMalloc(char, MAX_NAME_SIZE + 1);
643 _nc_get_type(pushname);
645 DEBUG(3, ("pushing token: `%s', class %d",
646 _nc_curr_token.tk_name, pushtype));
650 * Panic mode error recovery - skip everything until a "ch" is found.
653 _nc_panic_mode(char ch)
666 /*****************************************************************************
668 * Character-stream handling
670 *****************************************************************************/
672 #define LEXBUFSIZ 1024
674 static char *bufptr; /* otherwise, the input buffer pointer */
675 static char *bufstart; /* start of buffer so we can compute offsets */
676 static FILE *yyin; /* scanner's input file descriptor */
681 * Resets the input-reading routines. Used on initialization,
682 * or after a seek has been done. Exactly one argument must be
687 _nc_reset_input(FILE * fp, char *buf)
689 pushtype = NO_PUSHBACK;
693 bufstart = bufptr = buf;
694 _nc_curr_file_pos = 0L;
703 * Returns the final nonblank character on the current input buffer
708 size_t len = strlen(bufptr);
710 if (!isspace(UChar(bufptr[len])))
719 * Returns the next character in the input stream. Comments and leading
720 * white space are stripped.
722 * The global state variable 'firstcolumn' is set TRUE if the character
723 * returned is from the first column of the input line.
725 * The global variable _nc_curr_line is incremented for each new line.
726 * The global variable _nc_curr_file_pos is set to the file offset of the
727 * beginning of each line.
735 * An string with an embedded null will truncate the input. This is
736 * intentional (we don't read binary files here).
740 if (*bufptr == '\n') {
744 } else if (!bufptr || !*bufptr) {
746 * In theory this could be recoded to do its I/O one character at a
747 * time, saving the buffer space. In practice, this turns out to be
748 * quite hard to get completely right. Try it and see. If you
749 * succeed, don't forget to hack push_back() correspondingly.
752 static size_t allocated;
760 if (used + (LEXBUFSIZ / 4) >= allocated) {
761 allocated += (allocated + LEXBUFSIZ);
762 result = typeRealloc(char, allocated, result);
767 _nc_curr_file_pos = ftell(yyin);
769 if (fgets(result + used, allocated - used, yyin) != NULL) {
777 strcat(result, "\n");
779 if ((bufptr = bufstart) != 0) {
780 used = strlen(bufptr);
781 while (iswhite(*bufptr))
785 * Treat a trailing <cr><lf> the same as a <newline> so we
786 * can read files on OS/2, etc.
788 if ((len = strlen(bufptr)) > 1) {
789 if (bufptr[len - 1] == '\n'
790 && bufptr[len - 2] == '\r') {
792 bufptr[len - 1] = '\n';
799 } while (bufptr[len - 1] != '\n'); /* complete a line */
800 } while (result[0] == '#'); /* ignore comments */
803 first_column = (bufptr == bufstart);
811 /* push a character back onto the input stream */
813 if (bufptr == bufstart)
814 _nc_syserr_abort("Can't backspace off beginning of line");
820 /* return our current character position in the input stream */
822 return (yyin ? ftell(yyin) : (bufptr ? bufptr - bufstart : 0));
827 /* are we at end of input? */
829 return ((yyin ? feof(yyin) : (bufptr && *bufptr == '\0'))