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 ****************************************************************************/
34 /* $FreeBSD: src/contrib/ncurses/ncurses/tinfo/comp_scan.c,v 1.2.2.2 2000/10/12 18:40:51 peter Exp $ */
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.47 2000/09/24 01:15:17 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 int _nc_syntax = 0; /* termcap or terminfo? */
66 long _nc_curr_file_pos = 0; /* file offset of current line */
67 long _nc_comment_start = 0; /* start of comment range before name */
68 long _nc_comment_end = 0; /* end of comment range before name */
69 long _nc_start_line = 0; /* start line of current entry */
71 struct token _nc_curr_token =
74 /*****************************************************************************
76 * Token-grabbing machinery
78 *****************************************************************************/
80 static bool first_column; /* See 'next_char()' below */
81 static char separator; /* capability separator */
82 static int pushtype; /* type of pushback token */
83 static char pushname[MAX_NAME_SIZE + 1];
86 bool _nc_disable_period = FALSE; /* used by tic -a option */
89 static int last_char(void);
90 static int next_char(void);
91 static long stream_pos(void);
92 static bool end_of_stream(void);
93 static void push_back(char c);
95 /* Assume we may be looking at a termcap-style continuation */
97 eat_escaped_newline(int ch)
100 while ((ch = next_char()) == '\n' || iswhite(ch))
109 * Scans the input for the next token, storing the specifics in the
110 * global structure 'curr_token' and returning one of the following:
112 * NAMES A line beginning in column 1. 'name'
113 * will be set to point to everything up to but
114 * not including the first separator on the line.
115 * BOOLEAN An entry consisting of a name followed by
116 * a separator. 'name' will be set to point to
117 * the name of the capability.
118 * NUMBER An entry of the form
120 * 'name' will be set to point to the capability
121 * name and 'valnumber' to the number given.
122 * STRING An entry of the form
124 * 'name' is set to the capability name and
125 * 'valstring' to the string of characters, with
126 * input translations done.
127 * CANCEL An entry of the form
129 * 'name' is set to the capability name and
131 * EOF The end of the file has been reached.
133 * A `separator' is either a comma or a semicolon, depending on whether
134 * we are in termcap or terminfo mode.
141 static const char terminfo_punct[] = "@%&*!#";
148 static char buffer[MAX_ENTRY_SIZE];
150 int dot_flag = FALSE;
153 if (pushtype != NO_PUSHBACK) {
154 int retval = pushtype;
156 _nc_set_type(pushname);
157 DEBUG(3, ("pushed-back token: `%s', class %d",
158 _nc_curr_token.tk_name, pushtype));
160 pushtype = NO_PUSHBACK;
163 /* currtok wasn't altered by _nc_push_token() */
171 token_start = stream_pos();
172 while ((ch = next_char()) == '\n' || iswhite(ch))
175 ch = eat_escaped_newline(ch);
180 /* if this is a termcap entry, skip a leading separator */
181 if (separator == ':' && ch == ':')
185 #if NCURSES_EXT_FUNCS
186 && !_nc_disable_period
190 DEBUG(8, ("dot-flag set"));
192 while ((ch = next_char()) == '.' || iswhite(ch))
201 /* have to make some punctuation chars legal for terminfo */
203 #if NCURSES_EXT_FUNCS
204 && !(ch == '.' && _nc_disable_period)
206 && !strchr(terminfo_punct, (char) ch)) {
207 _nc_warning("Illegal character (expected alphanumeric or %s) - %s",
208 terminfo_punct, unctrl(ch));
209 _nc_panic_mode(separator);
219 _nc_comment_start = token_start;
220 _nc_comment_end = _nc_curr_file_pos;
221 _nc_start_line = _nc_curr_line;
224 while ((ch = next_char()) != '\n') {
226 _nc_err_abort("premature EOF");
227 else if (ch == ':' && last_char() != ',') {
228 _nc_syntax = SYN_TERMCAP;
231 } else if (ch == ',') {
232 _nc_syntax = SYN_TERMINFO;
235 * Fall-through here is not an accident.
236 * The idea is that if we see a comma, we
237 * figure this is terminfo unless we
238 * subsequently run into a colon -- but
239 * we don't stop looking for that colon until
240 * hitting a newline. This allows commas to
241 * be embedded in description fields of
246 ch = eat_escaped_newline(ch);
251 if (_nc_syntax == ERR) {
253 * Grrr...what we ought to do here is barf,
254 * complaining that the entry is malformed.
255 * But because a couple of name fields in the
256 * 8.2 termcap file end with |\, we just have
257 * to assume it's termcap syntax.
259 _nc_syntax = SYN_TERMCAP;
261 } else if (_nc_syntax == SYN_TERMINFO) {
262 /* throw away trailing /, *$/ */
263 for (--ptr; iswhite(*ptr) || *ptr == ','; ptr--)
269 * This is the soonest we have the terminal name
270 * fetched. Set up for following warning messages.
272 ptr = strchr(buffer, '|');
273 if (ptr == (char *) NULL)
274 ptr = buffer + strlen(buffer);
277 _nc_set_type(buffer);
281 * Compute the boundary between the aliases and the
282 * description field for syntax-checking purposes.
284 desc = strrchr(buffer, '|');
287 _nc_warning("empty longname field");
288 #ifndef FREEBSD_NATIVE
289 else if (strchr(desc, ' ') == (char *) NULL)
290 _nc_warning("older tic versions may treat the description field as an alias");
294 desc = buffer + strlen(buffer);
297 * Whitespace in a name field other than the long name
298 * can confuse rdist and some termcap tools. Slashes
299 * are a no-no. Other special characters can be
300 * dangerous due to shell expansion.
302 for (ptr = buffer; ptr < desc; ptr++) {
304 _nc_warning("whitespace in name or alias field");
306 } else if (*ptr == '/') {
307 _nc_warning("slashes aren't allowed in names or aliases");
309 } else if (strchr("$[]!*?", *ptr)) {
310 _nc_warning("dubious character `%c' in name or alias field", *ptr);
317 _nc_curr_token.tk_name = buffer;
320 while ((ch = next_char()) != EOF) {
322 if (_nc_syntax == SYN_TERMINFO) {
325 } else { /* allow ';' for "k;" */
338 _nc_err_abort("Separator inconsistent with syntax");
339 _nc_curr_token.tk_name = buffer;
343 if ((ch = next_char()) != separator)
344 _nc_warning("Missing separator after `%s', have %s",
346 _nc_curr_token.tk_name = buffer;
352 while (isalnum(ch = next_char())) {
353 numbuf[found++] = ch;
354 if (found >= sizeof(numbuf) - 1)
357 numbuf[found] = '\0';
358 number = strtol(numbuf, &numchk, 0);
359 if (numchk == numbuf)
360 _nc_warning("no value given for `%s'", buffer);
361 if ((*numchk != '\0') || (ch != separator))
362 _nc_warning("Missing separator");
363 _nc_curr_token.tk_name = buffer;
364 _nc_curr_token.tk_valnumber = number;
369 ch = _nc_trans_string(ptr, buffer + sizeof(buffer));
371 _nc_warning("Missing separator");
372 _nc_curr_token.tk_name = buffer;
373 _nc_curr_token.tk_valstring = ptr;
381 /* just to get rid of the compiler warning */
383 _nc_warning("Illegal character - %s", unctrl(ch));
385 } /* end else (first_column == FALSE) */
386 } /* end else (ch != EOF) */
391 if (dot_flag == TRUE)
392 DEBUG(8, ("Commented out "));
394 if (_nc_tracing >= DEBUG_LEVEL(7)) {
397 _tracef("Token: Boolean; name='%s'",
398 _nc_curr_token.tk_name);
402 _tracef("Token: Number; name='%s', value=%d",
403 _nc_curr_token.tk_name,
404 _nc_curr_token.tk_valnumber);
408 _tracef("Token: String; name='%s', value=%s",
409 _nc_curr_token.tk_name,
410 _nc_visbuf(_nc_curr_token.tk_valstring));
414 _tracef("Token: Cancel; name='%s'",
415 _nc_curr_token.tk_name);
420 _tracef("Token: Names; value='%s'",
421 _nc_curr_token.tk_name);
425 _tracef("Token: End of file");
429 _nc_warning("Bad token type");
434 if (dot_flag == TRUE) /* if commented out, use the next one */
435 type = _nc_get_token();
437 DEBUG(3, ("token: `%s', class %d", _nc_curr_token.tk_name, type));
446 * Reads characters using next_char() until encountering a separator, nl,
447 * or end-of-file. The returned value is the character which caused
448 * reading to stop. The following translations are done on the input:
450 * ^X goes to ctrl-X (i.e. X & 037)
451 * {\E,\n,\r,\b,\t,\f} go to
452 * {ESCAPE,newline,carriage-return,backspace,tab,formfeed}
453 * {\^,\\} go to {carat,backslash}
454 * \ddd (for ddd = up to three octal digits) goes to the character ddd
462 _nc_trans_string(char *ptr, char *last)
467 chtype ch, last_ch = '\0';
468 bool ignored = FALSE;
469 bool long_warning = FALSE;
471 while ((ch = c = next_char()) != (chtype) separator && c != EOF) {
472 if (ptr == (last - 1))
474 if ((_nc_syntax == SYN_TERMCAP) && c == '\n')
476 if (ch == '^' && last_ch != '%') {
477 ch = c = next_char();
479 _nc_err_abort("Premature EOF");
481 if (!(is7bits(ch) && isprint(ch))) {
482 _nc_warning("Illegal ^ character - %s", unctrl(ch));
487 _nc_warning("Allow ^? as synonym for \\177");
489 if ((ch &= 037) == 0)
491 *(ptr++) = (char) (ch);
493 } else if (ch == '\\') {
494 ch = c = next_char();
496 _nc_err_abort("Premature EOF");
498 if (ch >= '0' && ch <= '7') {
500 for (i = 0; i < 2; i++) {
501 ch = c = next_char();
503 _nc_err_abort("Premature EOF");
505 if (c < '0' || c > '7') {
507 _nc_warning("Non-octal digit `%c' in \\ sequence", c);
508 /* allow the digit; it'll do less harm */
515 number = number * 8 + c - '0';
520 *(ptr++) = (char) number;
577 _nc_warning("Illegal character %s in \\ sequence",
579 *(ptr++) = (char) ch;
580 } /* endswitch (ch) */
581 } /* endelse (ch < '0' || ch > '7') */
583 /* end else if (ch == '\\') */
584 else if (ch == '\n' && (_nc_syntax == SYN_TERMINFO)) {
585 /* newlines embedded in a terminfo string are ignored */
588 *(ptr++) = (char) ch;
597 if (count > MAXCAPLEN && !long_warning) {
598 _nc_warning("Very long string found. Missing separator?");
611 * Push a token of given type so that it will be reread by the next
616 _nc_push_token(int tokclass)
619 * This implementation is kind of bogus, it will fail if we ever do
620 * more than one pushback at a time between get_token() calls. It
621 * relies on the fact that curr_tok is static storage that nothing
622 * but get_token() touches.
625 _nc_get_type(pushname);
627 DEBUG(3, ("pushing token: `%s', class %d",
628 _nc_curr_token.tk_name, pushtype));
632 * Panic mode error recovery - skip everything until a "ch" is found.
635 _nc_panic_mode(char ch)
648 /*****************************************************************************
650 * Character-stream handling
652 *****************************************************************************/
654 #define LEXBUFSIZ 1024
656 static char *bufptr; /* otherwise, the input buffer pointer */
657 static char *bufstart; /* start of buffer so we can compute offsets */
658 static FILE *yyin; /* scanner's input file descriptor */
663 * Resets the input-reading routines. Used on initialization,
664 * or after a seek has been done. Exactly one argument must be
669 _nc_reset_input(FILE * fp, char *buf)
671 pushtype = NO_PUSHBACK;
674 bufstart = bufptr = buf;
675 _nc_curr_file_pos = 0L;
684 * Returns the final nonblank character on the current input buffer
689 size_t len = strlen(bufptr);
691 if (!isspace(bufptr[len]))
700 * Returns the next character in the input stream. Comments and leading
701 * white space are stripped.
703 * The global state variable 'firstcolumn' is set TRUE if the character
704 * returned is from the first column of the input line.
706 * The global variable _nc_curr_line is incremented for each new line.
707 * The global variable _nc_curr_file_pos is set to the file offset of the
708 * beginning of each line.
717 if (*bufptr == '\n') {
721 } else if (!bufptr || !*bufptr) {
723 * In theory this could be recoded to do its I/O one
724 * character at a time, saving the buffer space. In
725 * practice, this turns out to be quite hard to get
726 * completely right. Try it and see. If you succeed,
727 * don't forget to hack push_back() correspondingly.
729 static char line[LEXBUFSIZ];
733 _nc_curr_file_pos = ftell(yyin);
735 if ((bufstart = fgets(line, LEXBUFSIZ, yyin)) != NULL) {
741 (bufstart != NULL && line[0] == '#');
743 if (bufstart == NULL || *bufstart == 0)
746 while (iswhite(*bufptr))
750 * Treat a trailing <cr><lf> the same as a <newline> so we can read
751 * files on OS/2, etc.
753 if ((len = strlen(bufptr)) > 1) {
754 if (bufptr[len - 1] == '\n'
755 && bufptr[len - 2] == '\r') {
757 bufptr[len - 1] = '\n';
763 * If we don't have a trailing newline, it's because the line is simply
764 * too long. Give up. (FIXME: We could instead reallocate the line
765 * buffer and allow arbitrary-length lines).
767 if (len == 0 || (bufptr[len - 1] != '\n'))
771 first_column = (bufptr == bufstart);
779 /* push a character back onto the input stream */
781 if (bufptr == bufstart)
782 _nc_syserr_abort("Can't backspace off beginning of line");
788 /* return our current character position in the input stream */
790 return (yyin ? ftell(yyin) : (bufptr ? bufptr - bufstart : 0));
795 /* are we at end of input? */
797 return ((yyin ? feof(yyin) : (bufptr && *bufptr == '\0'))
801 /* comp_scan.c ends here */