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 $ */
35 /* $DragonFly: src/contrib/ncurses/ncurses/tinfo/Attic/comp_scan.c,v 1.2 2003/06/17 04:24:03 dillon Exp $ */
38 * comp_scan.c --- Lexical scanner for terminfo compiler.
45 * long _nc_curr_file_pos;
46 * long _nc_comment_start;
47 * long _nc_comment_end;
50 #include <curses.priv.h>
53 #include <term_entry.h>
56 MODULE_ID("$Id: comp_scan.c,v 1.47 2000/09/24 01:15:17 tom Exp $")
59 * Maximum length of string capability we'll accept before raising an error.
60 * Yes, there is a real capability in /etc/termcap this long, an "is".
64 #define iswhite(ch) (ch == ' ' || ch == '\t')
66 int _nc_syntax = 0; /* termcap or terminfo? */
67 long _nc_curr_file_pos = 0; /* file offset of current line */
68 long _nc_comment_start = 0; /* start of comment range before name */
69 long _nc_comment_end = 0; /* end of comment range before name */
70 long _nc_start_line = 0; /* start line of current entry */
72 struct token _nc_curr_token =
75 /*****************************************************************************
77 * Token-grabbing machinery
79 *****************************************************************************/
81 static bool first_column; /* See 'next_char()' below */
82 static char separator; /* capability separator */
83 static int pushtype; /* type of pushback token */
84 static char pushname[MAX_NAME_SIZE + 1];
87 bool _nc_disable_period = FALSE; /* used by tic -a option */
90 static int last_char(void);
91 static int next_char(void);
92 static long stream_pos(void);
93 static bool end_of_stream(void);
94 static void push_back(char c);
96 /* Assume we may be looking at a termcap-style continuation */
98 eat_escaped_newline(int ch)
101 while ((ch = next_char()) == '\n' || iswhite(ch))
110 * Scans the input for the next token, storing the specifics in the
111 * global structure 'curr_token' and returning one of the following:
113 * NAMES A line beginning in column 1. 'name'
114 * will be set to point to everything up to but
115 * not including the first separator on the line.
116 * BOOLEAN An entry consisting of a name followed by
117 * a separator. 'name' will be set to point to
118 * the name of the capability.
119 * NUMBER An entry of the form
121 * 'name' will be set to point to the capability
122 * name and 'valnumber' to the number given.
123 * STRING An entry of the form
125 * 'name' is set to the capability name and
126 * 'valstring' to the string of characters, with
127 * input translations done.
128 * CANCEL An entry of the form
130 * 'name' is set to the capability name and
132 * EOF The end of the file has been reached.
134 * A `separator' is either a comma or a semicolon, depending on whether
135 * we are in termcap or terminfo mode.
142 static const char terminfo_punct[] = "@%&*!#";
149 static char buffer[MAX_ENTRY_SIZE];
151 int dot_flag = FALSE;
154 if (pushtype != NO_PUSHBACK) {
155 int retval = pushtype;
157 _nc_set_type(pushname);
158 DEBUG(3, ("pushed-back token: `%s', class %d",
159 _nc_curr_token.tk_name, pushtype));
161 pushtype = NO_PUSHBACK;
164 /* currtok wasn't altered by _nc_push_token() */
172 token_start = stream_pos();
173 while ((ch = next_char()) == '\n' || iswhite(ch))
176 ch = eat_escaped_newline(ch);
181 /* if this is a termcap entry, skip a leading separator */
182 if (separator == ':' && ch == ':')
186 #if NCURSES_EXT_FUNCS
187 && !_nc_disable_period
191 DEBUG(8, ("dot-flag set"));
193 while ((ch = next_char()) == '.' || iswhite(ch))
202 /* have to make some punctuation chars legal for terminfo */
204 #if NCURSES_EXT_FUNCS
205 && !(ch == '.' && _nc_disable_period)
207 && !strchr(terminfo_punct, (char) ch)) {
208 _nc_warning("Illegal character (expected alphanumeric or %s) - %s",
209 terminfo_punct, unctrl(ch));
210 _nc_panic_mode(separator);
220 _nc_comment_start = token_start;
221 _nc_comment_end = _nc_curr_file_pos;
222 _nc_start_line = _nc_curr_line;
225 while ((ch = next_char()) != '\n') {
227 _nc_err_abort("premature EOF");
228 else if (ch == ':' && last_char() != ',') {
229 _nc_syntax = SYN_TERMCAP;
232 } else if (ch == ',') {
233 _nc_syntax = SYN_TERMINFO;
236 * Fall-through here is not an accident.
237 * The idea is that if we see a comma, we
238 * figure this is terminfo unless we
239 * subsequently run into a colon -- but
240 * we don't stop looking for that colon until
241 * hitting a newline. This allows commas to
242 * be embedded in description fields of
247 ch = eat_escaped_newline(ch);
252 if (_nc_syntax == ERR) {
254 * Grrr...what we ought to do here is barf,
255 * complaining that the entry is malformed.
256 * But because a couple of name fields in the
257 * 8.2 termcap file end with |\, we just have
258 * to assume it's termcap syntax.
260 _nc_syntax = SYN_TERMCAP;
262 } else if (_nc_syntax == SYN_TERMINFO) {
263 /* throw away trailing /, *$/ */
264 for (--ptr; iswhite(*ptr) || *ptr == ','; ptr--)
270 * This is the soonest we have the terminal name
271 * fetched. Set up for following warning messages.
273 ptr = strchr(buffer, '|');
274 if (ptr == (char *) NULL)
275 ptr = buffer + strlen(buffer);
278 _nc_set_type(buffer);
282 * Compute the boundary between the aliases and the
283 * description field for syntax-checking purposes.
285 desc = strrchr(buffer, '|');
288 _nc_warning("empty longname field");
289 #ifndef FREEBSD_NATIVE
290 else if (strchr(desc, ' ') == (char *) NULL)
291 _nc_warning("older tic versions may treat the description field as an alias");
295 desc = buffer + strlen(buffer);
298 * Whitespace in a name field other than the long name
299 * can confuse rdist and some termcap tools. Slashes
300 * are a no-no. Other special characters can be
301 * dangerous due to shell expansion.
303 for (ptr = buffer; ptr < desc; ptr++) {
305 _nc_warning("whitespace in name or alias field");
307 } else if (*ptr == '/') {
308 _nc_warning("slashes aren't allowed in names or aliases");
310 } else if (strchr("$[]!*?", *ptr)) {
311 _nc_warning("dubious character `%c' in name or alias field", *ptr);
318 _nc_curr_token.tk_name = buffer;
321 while ((ch = next_char()) != EOF) {
323 if (_nc_syntax == SYN_TERMINFO) {
326 } else { /* allow ';' for "k;" */
339 _nc_err_abort("Separator inconsistent with syntax");
340 _nc_curr_token.tk_name = buffer;
344 if ((ch = next_char()) != separator)
345 _nc_warning("Missing separator after `%s', have %s",
347 _nc_curr_token.tk_name = buffer;
353 while (isalnum(ch = next_char())) {
354 numbuf[found++] = ch;
355 if (found >= sizeof(numbuf) - 1)
358 numbuf[found] = '\0';
359 number = strtol(numbuf, &numchk, 0);
360 if (numchk == numbuf)
361 _nc_warning("no value given for `%s'", buffer);
362 if ((*numchk != '\0') || (ch != separator))
363 _nc_warning("Missing separator");
364 _nc_curr_token.tk_name = buffer;
365 _nc_curr_token.tk_valnumber = number;
370 ch = _nc_trans_string(ptr, buffer + sizeof(buffer));
372 _nc_warning("Missing separator");
373 _nc_curr_token.tk_name = buffer;
374 _nc_curr_token.tk_valstring = ptr;
382 /* just to get rid of the compiler warning */
384 _nc_warning("Illegal character - %s", unctrl(ch));
386 } /* end else (first_column == FALSE) */
387 } /* end else (ch != EOF) */
392 if (dot_flag == TRUE)
393 DEBUG(8, ("Commented out "));
395 if (_nc_tracing >= DEBUG_LEVEL(7)) {
398 _tracef("Token: Boolean; name='%s'",
399 _nc_curr_token.tk_name);
403 _tracef("Token: Number; name='%s', value=%d",
404 _nc_curr_token.tk_name,
405 _nc_curr_token.tk_valnumber);
409 _tracef("Token: String; name='%s', value=%s",
410 _nc_curr_token.tk_name,
411 _nc_visbuf(_nc_curr_token.tk_valstring));
415 _tracef("Token: Cancel; name='%s'",
416 _nc_curr_token.tk_name);
421 _tracef("Token: Names; value='%s'",
422 _nc_curr_token.tk_name);
426 _tracef("Token: End of file");
430 _nc_warning("Bad token type");
435 if (dot_flag == TRUE) /* if commented out, use the next one */
436 type = _nc_get_token();
438 DEBUG(3, ("token: `%s', class %d", _nc_curr_token.tk_name, type));
447 * Reads characters using next_char() until encountering a separator, nl,
448 * or end-of-file. The returned value is the character which caused
449 * reading to stop. The following translations are done on the input:
451 * ^X goes to ctrl-X (i.e. X & 037)
452 * {\E,\n,\r,\b,\t,\f} go to
453 * {ESCAPE,newline,carriage-return,backspace,tab,formfeed}
454 * {\^,\\} go to {carat,backslash}
455 * \ddd (for ddd = up to three octal digits) goes to the character ddd
463 _nc_trans_string(char *ptr, char *last)
468 chtype ch, last_ch = '\0';
469 bool ignored = FALSE;
470 bool long_warning = FALSE;
472 while ((ch = c = next_char()) != (chtype) separator && c != EOF) {
473 if (ptr == (last - 1))
475 if ((_nc_syntax == SYN_TERMCAP) && c == '\n')
477 if (ch == '^' && last_ch != '%') {
478 ch = c = next_char();
480 _nc_err_abort("Premature EOF");
482 if (!(is7bits(ch) && isprint(ch))) {
483 _nc_warning("Illegal ^ character - %s", unctrl(ch));
488 _nc_warning("Allow ^? as synonym for \\177");
490 if ((ch &= 037) == 0)
492 *(ptr++) = (char) (ch);
494 } else if (ch == '\\') {
495 ch = c = next_char();
497 _nc_err_abort("Premature EOF");
499 if (ch >= '0' && ch <= '7') {
501 for (i = 0; i < 2; i++) {
502 ch = c = next_char();
504 _nc_err_abort("Premature EOF");
506 if (c < '0' || c > '7') {
508 _nc_warning("Non-octal digit `%c' in \\ sequence", c);
509 /* allow the digit; it'll do less harm */
516 number = number * 8 + c - '0';
521 *(ptr++) = (char) number;
578 _nc_warning("Illegal character %s in \\ sequence",
580 *(ptr++) = (char) ch;
581 } /* endswitch (ch) */
582 } /* endelse (ch < '0' || ch > '7') */
584 /* end else if (ch == '\\') */
585 else if (ch == '\n' && (_nc_syntax == SYN_TERMINFO)) {
586 /* newlines embedded in a terminfo string are ignored */
589 *(ptr++) = (char) ch;
598 if (count > MAXCAPLEN && !long_warning) {
599 _nc_warning("Very long string found. Missing separator?");
612 * Push a token of given type so that it will be reread by the next
617 _nc_push_token(int tokclass)
620 * This implementation is kind of bogus, it will fail if we ever do
621 * more than one pushback at a time between get_token() calls. It
622 * relies on the fact that curr_tok is static storage that nothing
623 * but get_token() touches.
626 _nc_get_type(pushname);
628 DEBUG(3, ("pushing token: `%s', class %d",
629 _nc_curr_token.tk_name, pushtype));
633 * Panic mode error recovery - skip everything until a "ch" is found.
636 _nc_panic_mode(char ch)
649 /*****************************************************************************
651 * Character-stream handling
653 *****************************************************************************/
655 #define LEXBUFSIZ 1024
657 static char *bufptr; /* otherwise, the input buffer pointer */
658 static char *bufstart; /* start of buffer so we can compute offsets */
659 static FILE *yyin; /* scanner's input file descriptor */
664 * Resets the input-reading routines. Used on initialization,
665 * or after a seek has been done. Exactly one argument must be
670 _nc_reset_input(FILE * fp, char *buf)
672 pushtype = NO_PUSHBACK;
675 bufstart = bufptr = buf;
676 _nc_curr_file_pos = 0L;
685 * Returns the final nonblank character on the current input buffer
690 size_t len = strlen(bufptr);
692 if (!isspace(bufptr[len]))
701 * Returns the next character in the input stream. Comments and leading
702 * white space are stripped.
704 * The global state variable 'firstcolumn' is set TRUE if the character
705 * returned is from the first column of the input line.
707 * The global variable _nc_curr_line is incremented for each new line.
708 * The global variable _nc_curr_file_pos is set to the file offset of the
709 * beginning of each line.
718 if (*bufptr == '\n') {
722 } else if (!bufptr || !*bufptr) {
724 * In theory this could be recoded to do its I/O one
725 * character at a time, saving the buffer space. In
726 * practice, this turns out to be quite hard to get
727 * completely right. Try it and see. If you succeed,
728 * don't forget to hack push_back() correspondingly.
730 static char line[LEXBUFSIZ];
734 _nc_curr_file_pos = ftell(yyin);
736 if ((bufstart = fgets(line, LEXBUFSIZ, yyin)) != NULL) {
742 (bufstart != NULL && line[0] == '#');
744 if (bufstart == NULL || *bufstart == 0)
747 while (iswhite(*bufptr))
751 * Treat a trailing <cr><lf> the same as a <newline> so we can read
752 * files on OS/2, etc.
754 if ((len = strlen(bufptr)) > 1) {
755 if (bufptr[len - 1] == '\n'
756 && bufptr[len - 2] == '\r') {
758 bufptr[len - 1] = '\n';
764 * If we don't have a trailing newline, it's because the line is simply
765 * too long. Give up. (FIXME: We could instead reallocate the line
766 * buffer and allow arbitrary-length lines).
768 if (len == 0 || (bufptr[len - 1] != '\n'))
772 first_column = (bufptr == bufstart);
780 /* push a character back onto the input stream */
782 if (bufptr == bufstart)
783 _nc_syserr_abort("Can't backspace off beginning of line");
789 /* return our current character position in the input stream */
791 return (yyin ? ftell(yyin) : (bufptr ? bufptr - bufstart : 0));
796 /* are we at end of input? */
798 return ((yyin ? feof(yyin) : (bufptr && *bufptr == '\0'))
802 /* comp_scan.c ends here */