]> ncurses.scripts.mit.edu Git - ncurses.git/blob - ncurses/tinfo/comp_scan.c
ncurses 6.3 - patch 20220806
[ncurses.git] / ncurses / tinfo / comp_scan.c
1 /****************************************************************************
2 ,* Copyright 2020-2021,2022 Thomas E. Dickey                                *
3  * Copyright 1998-2016,2017 Free Software Foundation, Inc.                  *
4  *                                                                          *
5  * Permission is hereby granted, free of charge, to any person obtaining a  *
6  * copy of this software and associated documentation files (the            *
7  * "Software"), to deal in the Software without restriction, including      *
8  * without limitation the rights to use, copy, modify, merge, publish,      *
9  * distribute, distribute with modifications, sublicense, and/or sell       *
10  * copies of the Software, and to permit persons to whom the Software is    *
11  * furnished to do so, subject to the following conditions:                 *
12  *                                                                          *
13  * The above copyright notice and this permission notice shall be included  *
14  * in all copies or substantial portions of the Software.                   *
15  *                                                                          *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS  *
17  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF               *
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.   *
19  * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,   *
20  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR    *
21  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR    *
22  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.                               *
23  *                                                                          *
24  * Except as contained in this notice, the name(s) of the above copyright   *
25  * holders shall not be used in advertising or otherwise to promote the     *
26  * sale, use or other dealings in this Software without prior written       *
27  * authorization.                                                           *
28  ****************************************************************************/
29
30 /****************************************************************************
31  *  Author: Zeyd M. Ben-Halim <zmbenhal@netcom.com> 1992,1995               *
32  *     and: Eric S. Raymond <esr@snark.thyrsus.com>                         *
33  *     and: Thomas E. Dickey                        1996 on                 *
34  ****************************************************************************/
35
36 /*
37  *      comp_scan.c --- Lexical scanner for terminfo compiler.
38  *
39  *      _nc_reset_input()
40  *      _nc_get_token()
41  *      _nc_panic_mode()
42  *      int _nc_syntax;
43  *      int _nc_curr_line;
44  *      long _nc_curr_file_pos;
45  *      long _nc_comment_start;
46  *      long _nc_comment_end;
47  */
48
49 #include <curses.priv.h>
50
51 #include <ctype.h>
52 #include <tic.h>
53
54 MODULE_ID("$Id: comp_scan.c,v 1.119 2022/08/07 00:20:26 tom Exp $")
55
56 /*
57  * Maximum length of string capability we'll accept before raising an error.
58  * Yes, there is a real capability in /etc/termcap this long, an "is".
59  */
60 #define MAXCAPLEN       600
61
62 #define iswhite(ch)     (ch == ' '  ||  ch == '\t')
63
64 NCURSES_EXPORT_VAR (int) _nc_syntax = 0;         /* termcap or terminfo? */
65 NCURSES_EXPORT_VAR (int) _nc_strict_bsd = 1;  /* ncurses extended termcap? */
66 NCURSES_EXPORT_VAR (long) _nc_curr_file_pos = 0; /* file offset of current line */
67 NCURSES_EXPORT_VAR (long) _nc_comment_start = 0; /* start of comment range before name */
68 NCURSES_EXPORT_VAR (long) _nc_comment_end = 0;   /* end of comment range before name */
69 NCURSES_EXPORT_VAR (long) _nc_start_line = 0;    /* start line of current entry */
70
71 NCURSES_EXPORT_VAR (struct token) _nc_curr_token =
72 {
73     0, 0, 0
74 };
75
76 /*****************************************************************************
77  *
78  * Token-grabbing machinery
79  *
80  *****************************************************************************/
81
82 static bool first_column;       /* See 'next_char()' below */
83 static bool had_newline;
84 static char separator;          /* capability separator */
85 static int pushtype;            /* type of pushback token */
86 static char *pushname;
87
88 #if NCURSES_EXT_FUNCS
89 NCURSES_EXPORT_VAR (bool) _nc_disable_period = FALSE; /* used by tic -a option */
90 #endif
91
92 /*****************************************************************************
93  *
94  * Character-stream handling
95  *
96  *****************************************************************************/
97
98 #define LEXBUFSIZ       1024
99
100 static char *bufptr;            /* otherwise, the input buffer pointer */
101 static char *bufstart;          /* start of buffer so we can compute offsets */
102 static FILE *yyin;              /* scanner's input file descriptor */
103
104 /*
105  *      _nc_reset_input()
106  *
107  *      Resets the input-reading routines.  Used on initialization,
108  *      or after a seek has been done.  Exactly one argument must be
109  *      non-null.
110  */
111
112 NCURSES_EXPORT(void)
113 _nc_reset_input(FILE *fp, char *buf)
114 {
115     TR(TRACE_DATABASE,
116        (T_CALLED("_nc_reset_input(fp=%p, buf=%p)"), (void *) fp, buf));
117
118     pushtype = NO_PUSHBACK;
119     if (pushname != 0)
120         pushname[0] = '\0';
121     yyin = fp;
122     bufstart = bufptr = buf;
123     _nc_curr_file_pos = 0L;
124     if (fp != 0)
125         _nc_curr_line = 0;
126     _nc_curr_col = 0;
127
128     returnVoidDB;
129 }
130
131 /*
132  *      int last_char()
133  *
134  *      Returns the final nonblank character on the current input buffer
135  */
136 static int
137 last_char(int from_end)
138 {
139     size_t len = strlen(bufptr);
140     int result = 0;
141
142     while (len--) {
143         if (!isspace(UChar(bufptr[len]))) {
144             if (from_end <= (int) len)
145                 result = bufptr[(int) len - from_end];
146             break;
147         }
148     }
149     return result;
150 }
151
152 /*
153  * Read, like fgets(), but error-out if the input contains nulls.
154  */
155 static int
156 get_text(char *buffer, int length)
157 {
158     int count = 0;
159     int limit = length - 1;
160
161     while (limit-- > 0) {
162         int ch = fgetc(yyin);
163
164         if (ch == '\0') {
165             _nc_err_abort("This is not a text-file");
166         } else if (ch == EOF) {
167             break;
168         }
169         ++count;
170         *buffer++ = (char) ch;
171         if (ch == '\n')
172             break;
173     }
174     *buffer = '\0';
175     return count;
176 }
177
178 /*
179  *      int next_char()
180  *
181  *      Returns the next character in the input stream.  Comments and leading
182  *      white space are stripped.
183  *
184  *      The global state variable 'firstcolumn' is set TRUE if the character
185  *      returned is from the first column of the input line.
186  *
187  *      The global variable _nc_curr_line is incremented for each new line.
188  *      The global variable _nc_curr_file_pos is set to the file offset of the
189  *      beginning of each line.
190  */
191
192 static int
193 next_char(void)
194 {
195     static char *result;
196     static size_t allocated;
197     int the_char;
198
199     if (!yyin) {
200         if (result != 0) {
201             FreeAndNull(result);
202             FreeAndNull(pushname);
203             bufptr = 0;
204             bufstart = 0;
205             allocated = 0;
206         }
207         /*
208          * An string with an embedded null will truncate the input.  This is
209          * intentional (we don't read binary files here).
210          */
211         if (bufptr == 0 || *bufptr == '\0')
212             return (EOF);
213         if (*bufptr == '\n') {
214             _nc_curr_line++;
215             _nc_curr_col = 0;
216         } else if (*bufptr == '\t') {
217             _nc_curr_col = (_nc_curr_col | 7);
218         }
219     } else if (!bufptr || !*bufptr) {
220         /*
221          * In theory this could be recoded to do its I/O one character at a
222          * time, saving the buffer space.  In practice, this turns out to be
223          * quite hard to get completely right.  Try it and see.  If you
224          * succeed, don't forget to hack push_back() correspondingly.
225          */
226         size_t len;
227
228         do {
229             size_t used = 0;
230             bufstart = 0;
231             do {
232                 if (used + (LEXBUFSIZ / 4) >= allocated) {
233                     allocated += (allocated + LEXBUFSIZ);
234                     result = typeRealloc(char, allocated, result);
235                     if (result == 0)
236                         return (EOF);
237                     if (bufstart)
238                         bufstart = result;
239                 }
240                 if (used == 0)
241                     _nc_curr_file_pos = ftell(yyin);
242
243                 if (get_text(result + used, (int) (allocated - used))) {
244                     bufstart = result;
245                     if (used == 0) {
246                         if (_nc_curr_line == 0
247                             && IS_TIC_MAGIC(result)) {
248                             _nc_err_abort("This is a compiled terminal description, not a source");
249                         }
250                         _nc_curr_line++;
251                         _nc_curr_col = 0;
252                     }
253                 } else {
254                     if (used != 0)
255                         _nc_STRCAT(result, "\n", allocated);
256                 }
257                 if ((bufptr = bufstart) != 0) {
258                     used = strlen(bufptr);
259                     if (used == 0)
260                         return (EOF);
261                     while (iswhite(*bufptr)) {
262                         if (*bufptr == '\t') {
263                             _nc_curr_col = (_nc_curr_col | 7) + 1;
264                         } else {
265                             _nc_curr_col++;
266                         }
267                         bufptr++;
268                     }
269
270                     /*
271                      * Treat a trailing <cr><lf> the same as a <newline> so we
272                      * can read files on OS/2, etc.
273                      */
274                     if ((len = strlen(bufptr)) > 1) {
275                         if (bufptr[len - 1] == '\n'
276                             && bufptr[len - 2] == '\r') {
277                             len--;
278                             bufptr[len - 1] = '\n';
279                             bufptr[len] = '\0';
280                         }
281                     }
282                 } else {
283                     return (EOF);
284                 }
285             } while (bufptr[len - 1] != '\n');  /* complete a line */
286         } while (result[0] == '#');     /* ignore comments */
287     } else if (*bufptr == '\t') {
288         _nc_curr_col = (_nc_curr_col | 7);
289     }
290
291     first_column = (bufptr == bufstart);
292     if (first_column)
293         had_newline = FALSE;
294
295     _nc_curr_col++;
296     the_char = *bufptr++;
297     return UChar(the_char);
298 }
299
300 static void
301 push_back(int c)
302 /* push a character back onto the input stream */
303 {
304     if (bufptr == bufstart)
305         _nc_syserr_abort("Can't backspace off beginning of line");
306     *--bufptr = (char) c;
307     _nc_curr_col--;
308 }
309
310 static long
311 stream_pos(void)
312 /* return our current character position in the input stream */
313 {
314     return (yyin ? ftell(yyin) : (bufptr ? bufptr - bufstart : 0));
315 }
316
317 static bool
318 end_of_stream(void)
319 /* are we at end of input? */
320 {
321     return ((yyin
322              ? (feof(yyin) && (bufptr == NULL || *bufptr == '\0'))
323              : (bufptr && *bufptr == '\0'))
324             ? TRUE : FALSE);
325 }
326
327 /* Assume we may be looking at a termcap-style continuation */
328 static NCURSES_INLINE int
329 eat_escaped_newline(int ch)
330 {
331     if (ch == '\\')
332         while ((ch = next_char()) == '\n' || iswhite(ch))
333             continue;
334     return ch;
335 }
336
337 #define TOK_BUF_SIZE MAX_ENTRY_SIZE
338
339 #define OkToAdd() \
340         ((tok_ptr - tok_buf) < (TOK_BUF_SIZE - 2))
341
342 #define AddCh(ch) \
343         *tok_ptr++ = (char) ch; \
344         *tok_ptr = '\0'
345
346 static char *tok_buf;
347
348 /*
349  *      int
350  *      get_token()
351  *
352  *      Scans the input for the next token, storing the specifics in the
353  *      global structure 'curr_token' and returning one of the following:
354  *
355  *              NAMES           A line beginning in column 1.  'name'
356  *                              will be set to point to everything up to but
357  *                              not including the first separator on the line.
358  *              BOOLEAN         An entry consisting of a name followed by
359  *                              a separator.  'name' will be set to point to
360  *                              the name of the capability.
361  *              NUMBER          An entry of the form
362  *                                      name#digits,
363  *                              'name' will be set to point to the capability
364  *                              name and 'valnumber' to the number given.
365  *              STRING          An entry of the form
366  *                                      name=characters,
367  *                              'name' is set to the capability name and
368  *                              'valstring' to the string of characters, with
369  *                              input translations done.
370  *              CANCEL          An entry of the form
371  *                                      name@,
372  *                              'name' is set to the capability name and
373  *                              'valnumber' to -1.
374  *              EOF             The end of the file has been reached.
375  *
376  *      A `separator' is either a comma or a semicolon, depending on whether
377  *      we are in termcap or terminfo mode.
378  *
379  */
380
381 NCURSES_EXPORT(int)
382 _nc_get_token(bool silent)
383 {
384     static const char terminfo_punct[] = "@%&*!#";
385
386     char *after_name;           /* after primary name */
387     char *after_list;           /* after primary and alias list */
388     char *numchk;
389     char *tok_ptr;
390     char *s;
391     char numbuf[80];
392     int ch, c0, c1;
393     int dot_flag = FALSE;
394     int type;
395     long number;
396     long token_start;
397     unsigned found;
398 #ifdef TRACE
399     int old_line;
400     int old_col;
401 #endif
402
403     DEBUG(3, (T_CALLED("_nc_get_token(silent=%d)"), silent));
404
405     if (pushtype != NO_PUSHBACK) {
406         int retval = pushtype;
407
408         _nc_set_type(pushname != 0 ? pushname : "");
409         DEBUG(3, ("pushed-back token: `%s', class %d",
410                   _nc_curr_token.tk_name, pushtype));
411
412         pushtype = NO_PUSHBACK;
413         if (pushname != 0)
414             pushname[0] = '\0';
415
416         /* currtok wasn't altered by _nc_push_token() */
417         DEBUG(3, (T_RETURN("%d"), retval));
418         return (retval);
419     }
420
421     if (end_of_stream()) {
422         yyin = 0;
423         (void) next_char();     /* frees its allocated memory */
424         if (tok_buf != 0) {
425             if (_nc_curr_token.tk_name == tok_buf)
426                 _nc_curr_token.tk_name = 0;
427         }
428         DEBUG(3, (T_RETURN("%d"), EOF));
429         return (EOF);
430     }
431
432   start_token:
433     token_start = stream_pos();
434     while ((ch = next_char()) == '\n' || iswhite(ch)) {
435         if (ch == '\n')
436             had_newline = TRUE;
437         continue;
438     }
439
440     ch = eat_escaped_newline(ch);
441     _nc_curr_token.tk_valstring = 0;
442
443 #ifdef TRACE
444     old_line = _nc_curr_line;
445     old_col = _nc_curr_col;
446 #endif
447     if (ch == EOF)
448         type = EOF;
449     else {
450         /* if this is a termcap entry, skip a leading separator */
451         if (separator == ':' && ch == ':')
452             ch = next_char();
453
454         if (ch == '.'
455 #if NCURSES_EXT_FUNCS
456             && !_nc_disable_period
457 #endif
458             ) {
459             dot_flag = TRUE;
460             DEBUG(8, ("dot-flag set"));
461
462             while ((ch = next_char()) == '.' || iswhite(ch))
463                 continue;
464         }
465
466         if (ch == EOF) {
467             type = EOF;
468             goto end_of_token;
469         }
470
471         /* have to make some punctuation chars legal for terminfo */
472         if (!isalnum(UChar(ch))
473 #if NCURSES_EXT_FUNCS
474             && !(ch == '.' && _nc_disable_period)
475 #endif
476             && ((strchr) (terminfo_punct, (char) ch) == 0)) {
477             if (!silent)
478                 _nc_warning("Illegal character (expected alphanumeric or %s) - '%s'",
479                             terminfo_punct, unctrl(UChar(ch)));
480             _nc_panic_mode(separator);
481             goto start_token;
482         }
483
484         if (tok_buf == 0)
485             tok_buf = typeMalloc(char, TOK_BUF_SIZE);
486
487 #ifdef TRACE
488         old_line = _nc_curr_line;
489         old_col = _nc_curr_col;
490 #endif
491         tok_ptr = tok_buf;
492         AddCh(ch);
493
494         if (first_column) {
495             _nc_comment_start = token_start;
496             _nc_comment_end = _nc_curr_file_pos;
497             _nc_start_line = _nc_curr_line;
498
499             _nc_syntax = ERR;
500             after_name = 0;
501             after_list = 0;
502             while ((ch = next_char()) != '\n') {
503                 if (ch == EOF) {
504                     _nc_err_abort(MSG_NO_INPUTS);
505                 } else if (ch == '|') {
506                     after_list = tok_ptr;
507                     if (after_name == 0)
508                         after_name = tok_ptr;
509                 } else if (ch == ':' && last_char(0) != ',') {
510                     _nc_syntax = SYN_TERMCAP;
511                     separator = ':';
512                     break;
513                 } else if (ch == ',') {
514                     _nc_syntax = SYN_TERMINFO;
515                     separator = ',';
516                     /*
517                      * If we did not see a '|', then we found a name with no
518                      * aliases or description.
519                      */
520                     if (after_name == 0)
521                         break;
522                     /*
523                      * We saw a comma, but are not entirely sure this is
524                      * terminfo format, since we can still be parsing the
525                      * description field (for either syntax).
526                      *
527                      * A properly formatted termcap line ends with either a
528                      * colon, or a backslash after a colon.  It is possible
529                      * to have a backslash in the middle of a capability, but
530                      * then there would be no leading whitespace on the next
531                      * line - something we want to discourage.
532                      */
533                     c0 = last_char(0);
534                     c1 = last_char(1);
535                     if (c1 != ':' && c0 != '\\' && c0 != ':') {
536                         bool capability = FALSE;
537
538                         /*
539                          * Since it is not termcap, assume the line is terminfo
540                          * format.  However, the comma can be embedded in a
541                          * description field.  It also can be a separator
542                          * between a description field and a capability.
543                          *
544                          * Improve the guess by checking if the next word after
545                          * the comma does not look like a capability.  In that
546                          * case, extend the description past the comma.
547                          */
548                         for (s = bufptr; isspace(UChar(*s)); ++s) {
549                             ;
550                         }
551                         if (islower(UChar(*s))) {
552                             char *name = s;
553                             while (isalnum(UChar(*s))) {
554                                 ++s;
555                             }
556                             if (*s == '#' || *s == '=' || *s == '@') {
557                                 /*
558                                  * Checking solely with syntax allows us to
559                                  * support extended capabilities with string
560                                  * values.
561                                  */
562                                 capability = TRUE;
563                             } else if (*s == ',') {
564                                 c0 = *s;
565                                 *s = '\0';
566                                 /*
567                                  * Otherwise, we can handle predefined boolean
568                                  * capabilities, still aided by syntax.
569                                  */
570                                 if (_nc_find_entry(name,
571                                                    _nc_get_hash_table(FALSE))) {
572                                     capability = TRUE;
573                                 }
574                                 *s = (char) c0;
575                             }
576                         }
577                         if (capability) {
578                             break;
579                         }
580                     }
581                 } else
582                     ch = eat_escaped_newline(ch);
583
584                 if (OkToAdd()) {
585                     AddCh(ch);
586                 } else {
587                     break;
588                 }
589             }
590             *tok_ptr = '\0';
591             if (_nc_syntax == ERR) {
592                 /*
593                  * Grrr...what we ought to do here is barf, complaining that
594                  * the entry is malformed.  But because a couple of name fields
595                  * in the 8.2 termcap file end with |\, we just have to assume
596                  * it is termcap syntax.
597                  */
598                 _nc_syntax = SYN_TERMCAP;
599                 separator = ':';
600             } else if (_nc_syntax == SYN_TERMINFO) {
601                 /* throw away trailing /, *$/ */
602                 for (--tok_ptr;
603                      iswhite(*tok_ptr) || *tok_ptr == ',';
604                      tok_ptr--)
605                     continue;
606                 tok_ptr[1] = '\0';
607             }
608
609             /*
610              * This is the soonest we have the terminal name fetched.  Set up
611              * for following warning messages.  If there's no '|', then there
612              * is no description.
613              */
614             if (after_name != 0) {
615                 ch = *after_name;
616                 *after_name = '\0';
617                 _nc_set_type(tok_buf);
618                 *after_name = (char) ch;
619             }
620
621             /*
622              * Compute the boundary between the aliases and the description
623              * field for syntax-checking purposes.
624              */
625             if (after_list != 0) {
626                 if (!silent) {
627                     if (*after_list == '\0' || strchr("|", after_list[1]) != NULL) {
628                         _nc_warning("empty longname field");
629                     } else if (strchr(after_list, ' ') == 0) {
630                         _nc_warning("older tic versions may treat the description field as an alias");
631                     }
632                 }
633             } else {
634                 after_list = tok_buf + strlen(tok_buf);
635                 DEBUG(2, ("missing description"));
636             }
637
638             /*
639              * Whitespace in a name field other than the long name can confuse
640              * rdist and some termcap tools.  Slashes are a no-no.  Other
641              * special characters can be dangerous due to shell expansion.
642              */
643             for (s = tok_buf; s < after_list; ++s) {
644                 if (isspace(UChar(*s))) {
645                     if (!silent)
646                         _nc_warning("whitespace in name or alias field");
647                     break;
648                 } else if (*s == '/') {
649                     if (!silent)
650                         _nc_warning("slashes aren't allowed in names or aliases");
651                     break;
652                 } else if (strchr("$[]!*?", *s)) {
653                     if (!silent)
654                         _nc_warning("dubious character `%c' in name or alias field", *s);
655                     break;
656                 }
657             }
658
659             _nc_curr_token.tk_name = tok_buf;
660             type = NAMES;
661         } else {
662             if (had_newline && _nc_syntax == SYN_TERMCAP) {
663                 _nc_warning("Missing backslash before newline");
664                 had_newline = FALSE;
665             }
666             while ((ch = next_char()) != EOF) {
667                 if (!isalnum(UChar(ch))) {
668                     if (_nc_syntax == SYN_TERMINFO) {
669                         if (ch != '_')
670                             break;
671                     } else {    /* allow ';' for "k;" */
672                         if (ch != ';')
673                             break;
674                     }
675                 }
676                 if (OkToAdd()) {
677                     AddCh(ch);
678                 } else {
679                     ch = EOF;
680                     break;
681                 }
682             }
683
684             *tok_ptr++ = '\0';  /* separate name/value in buffer */
685             switch (ch) {
686             case ',':
687             case ':':
688                 if (ch != separator)
689                     _nc_err_abort("Separator inconsistent with syntax");
690                 _nc_curr_token.tk_name = tok_buf;
691                 type = BOOLEAN;
692                 break;
693             case '@':
694                 if ((ch = next_char()) != separator && !silent)
695                     _nc_warning("Missing separator after `%s', have %s",
696                                 tok_buf, unctrl(UChar(ch)));
697                 _nc_curr_token.tk_name = tok_buf;
698                 type = CANCEL;
699                 break;
700
701             case '#':
702                 found = 0;
703                 while (isalnum(ch = next_char())) {
704                     numbuf[found++] = (char) ch;
705                     if (found >= sizeof(numbuf) - 1)
706                         break;
707                 }
708                 numbuf[found] = '\0';
709                 number = strtol(numbuf, &numchk, 0);
710                 if (!silent) {
711                     if (numchk == numbuf)
712                         _nc_warning("no value given for `%s'", tok_buf);
713                     if ((*numchk != '\0') || (ch != separator))
714                         _nc_warning("Missing separator for `%s'", tok_buf);
715                     if (number < 0)
716                         _nc_warning("value of `%s' cannot be negative", tok_buf);
717                     if (number > MAX_OF_TYPE(NCURSES_INT2)) {
718                         _nc_warning("limiting value of `%s' from %#lx to %#x",
719                                     tok_buf,
720                                     number, MAX_OF_TYPE(NCURSES_INT2));
721                         number = MAX_OF_TYPE(NCURSES_INT2);
722                     }
723                 }
724                 _nc_curr_token.tk_name = tok_buf;
725                 _nc_curr_token.tk_valnumber = (int) number;
726                 type = NUMBER;
727                 break;
728
729             case '=':
730                 ch = _nc_trans_string(tok_ptr, tok_buf + TOK_BUF_SIZE);
731                 if (!silent && ch != separator)
732                     _nc_warning("Missing separator");
733                 _nc_curr_token.tk_name = tok_buf;
734                 _nc_curr_token.tk_valstring = tok_ptr;
735                 type = STRING;
736                 break;
737
738             case EOF:
739                 type = EOF;
740                 break;
741             default:
742                 /* just to get rid of the compiler warning */
743                 type = UNDEF;
744                 if (!silent)
745                     _nc_warning("Illegal character - '%s'", unctrl(UChar(ch)));
746             }
747         }                       /* end else (first_column == FALSE) */
748     }                           /* end else (ch != EOF) */
749
750   end_of_token:
751
752 #ifdef TRACE
753     if (dot_flag == TRUE)
754         DEBUG(8, ("Commented out "));
755
756     if (_nc_tracing >= DEBUG_LEVEL(8)) {
757         _tracef("parsed %d.%d to %d.%d",
758                 old_line, old_col,
759                 _nc_curr_line, _nc_curr_col);
760     }
761     if (_nc_tracing >= DEBUG_LEVEL(7)) {
762         switch (type) {
763         case BOOLEAN:
764             _tracef("Token: Boolean; name='%s'",
765                     _nc_curr_token.tk_name);
766             break;
767
768         case NUMBER:
769             _tracef("Token: Number;  name='%s', value=%d",
770                     _nc_curr_token.tk_name,
771                     _nc_curr_token.tk_valnumber);
772             break;
773
774         case STRING:
775             _tracef("Token: String;  name='%s', value=%s",
776                     _nc_curr_token.tk_name,
777                     _nc_visbuf(_nc_curr_token.tk_valstring));
778             break;
779
780         case CANCEL:
781             _tracef("Token: Cancel; name='%s'",
782                     _nc_curr_token.tk_name);
783             break;
784
785         case NAMES:
786
787             _tracef("Token: Names; value='%s'",
788                     _nc_curr_token.tk_name);
789             break;
790
791         case EOF:
792             _tracef("Token: End of file");
793             break;
794
795         default:
796             _nc_warning("Bad token type");
797         }
798     }
799 #endif
800
801     if (dot_flag == TRUE)       /* if commented out, use the next one */
802         type = _nc_get_token(silent);
803
804     DEBUG(3, ("token: `%s', class %d",
805               ((_nc_curr_token.tk_name != 0)
806                ? _nc_curr_token.tk_name
807                : "<null>"),
808               type));
809
810     DEBUG(3, (T_RETURN("%d"), type));
811     return (type);
812 }
813
814 /*
815  *      char
816  *      trans_string(ptr)
817  *
818  *      Reads characters using next_char() until encountering a separator, nl,
819  *      or end-of-file.  The returned value is the character which caused
820  *      reading to stop.  The following translations are done on the input:
821  *
822  *              ^X  goes to  ctrl-X (i.e. X & 037)
823  *              {\E,\n,\r,\b,\t,\f}  go to
824  *                      {ESCAPE,newline,carriage-return,backspace,tab,formfeed}
825  *              {\^,\\}  go to  {carat,backslash}
826  *              \ddd (for ddd = up to three octal digits)  goes to the character ddd
827  *
828  *              \e == \E
829  *              \0 == \200
830  *
831  */
832
833 NCURSES_EXPORT(int)
834 _nc_trans_string(char *ptr, char *last)
835 {
836     int count = 0;
837     int number = 0;
838     int i, c;
839     int last_ch = '\0';
840     bool ignored = FALSE;
841     bool long_warning = FALSE;
842
843     while ((c = next_char()) != separator && c != EOF) {
844         if (ptr >= (last - 1)) {
845             if (c != EOF) {
846                 while ((c = next_char()) != separator && c != EOF) {
847                     ;
848                 }
849             }
850             break;
851         }
852         if ((_nc_syntax == SYN_TERMCAP) && c == '\n')
853             break;
854         if (c == '^' && last_ch != '%') {
855             c = next_char();
856             if (c == EOF)
857                 _nc_err_abort(MSG_NO_INPUTS);
858
859             if (!(is7bits(c) && isprint(c))) {
860                 _nc_warning("Illegal ^ character - '%s'", unctrl(UChar(c)));
861             }
862             if (c == '?' && (_nc_syntax != SYN_TERMCAP)) {
863                 *(ptr++) = '\177';
864             } else {
865                 if ((c &= 037) == 0)
866                     c = 128;
867                 *(ptr++) = (char) (c);
868             }
869         } else if (c == '\\') {
870             bool strict_bsd = ((_nc_syntax == SYN_TERMCAP) && _nc_strict_bsd);
871
872             c = next_char();
873             if (c == EOF)
874                 _nc_err_abort(MSG_NO_INPUTS);
875
876             if (isoctal(c) || (strict_bsd && isdigit(c))) {
877                 number = c - '0';
878                 for (i = 0; i < 2; i++) {
879                     c = next_char();
880                     if (c == EOF)
881                         _nc_err_abort(MSG_NO_INPUTS);
882
883                     if (!isoctal(c)) {
884                         if (isdigit(c)) {
885                             if (!strict_bsd) {
886                                 _nc_warning("Non-octal digit `%c' in \\ sequence", c);
887                                 /* allow the digit; it'll do less harm */
888                             }
889                         } else {
890                             push_back(c);
891                             break;
892                         }
893                     }
894
895                     number = number * 8 + c - '0';
896                 }
897
898                 number = UChar(number);
899                 if (number == 0 && !strict_bsd)
900                     number = 0200;
901                 *(ptr++) = (char) number;
902             } else {
903                 switch (c) {
904                 case 'E':
905                     *(ptr++) = '\033';
906                     break;
907
908                 case 'n':
909                     *(ptr++) = '\n';
910                     break;
911
912                 case 'r':
913                     *(ptr++) = '\r';
914                     break;
915
916                 case 'b':
917                     *(ptr++) = '\010';
918                     break;
919
920                 case 'f':
921                     *(ptr++) = '\014';
922                     break;
923
924                 case 't':
925                     *(ptr++) = '\t';
926                     break;
927
928                 case '\\':
929                     *(ptr++) = '\\';
930                     break;
931
932                 case '^':
933                     *(ptr++) = '^';
934                     break;
935
936                 case ',':
937                     *(ptr++) = ',';
938                     break;
939
940                 case '\n':
941                     continue;
942
943                 default:
944                     if ((_nc_syntax == SYN_TERMINFO) || !_nc_strict_bsd) {
945                         switch (c) {
946                         case 'a':
947                             c = '\007';
948                             break;
949                         case 'e':
950                             c = '\033';
951                             break;
952                         case 'l':
953                             c = '\n';
954                             break;
955                         case 's':
956                             c = ' ';
957                             break;
958                         case ':':
959                             c = ':';
960                             break;
961                         default:
962                             _nc_warning("Illegal character '%s' in \\ sequence",
963                                         unctrl(UChar(c)));
964                             break;
965                         }
966                     }
967                     /* FALLTHRU */
968                 case '|':
969                     *(ptr++) = (char) c;
970                 }               /* endswitch (c) */
971             }                   /* endelse (c < '0' ||  c > '7') */
972         }
973         /* end else if (c == '\\') */
974         else if (c == '\n' && (_nc_syntax == SYN_TERMINFO)) {
975             /*
976              * Newlines embedded in a terminfo string are ignored, provided
977              * that the next line begins with whitespace.
978              */
979             ignored = TRUE;
980         } else {
981             *(ptr++) = (char) c;
982         }
983
984         if (!ignored) {
985             if (_nc_curr_col <= 1) {
986                 push_back(c);
987                 c = '\n';
988                 break;
989             }
990             last_ch = c;
991             count++;
992         }
993         ignored = FALSE;
994
995         if (count > MAXCAPLEN && !long_warning) {
996             _nc_warning("Very long string found.  Missing separator?");
997             long_warning = TRUE;
998         }
999     }                           /* end while */
1000
1001     *ptr = '\0';
1002
1003     return (c);
1004 }
1005
1006 /*
1007  *      _nc_push_token()
1008  *
1009  *      Push a token of given type so that it will be reread by the next
1010  *      get_token() call.
1011  */
1012
1013 NCURSES_EXPORT(void)
1014 _nc_push_token(int tokclass)
1015 {
1016     /*
1017      * This implementation is kind of bogus, it will fail if we ever do more
1018      * than one pushback at a time between get_token() calls.  It relies on the
1019      * fact that _nc_curr_token is static storage that nothing but
1020      * _nc_get_token() touches.
1021      */
1022     pushtype = tokclass;
1023     if (pushname == 0)
1024         pushname = typeMalloc(char, MAX_NAME_SIZE + 1);
1025     _nc_get_type(pushname);
1026
1027     DEBUG(3, ("pushing token: `%s', class %d",
1028               ((_nc_curr_token.tk_name != 0)
1029                ? _nc_curr_token.tk_name
1030                : "<null>"),
1031               pushtype));
1032 }
1033
1034 /*
1035  * Panic mode error recovery - skip everything until a "ch" is found.
1036  */
1037 NCURSES_EXPORT(void)
1038 _nc_panic_mode(char ch)
1039 {
1040     for (;;) {
1041         int c = next_char();
1042         if (c == ch)
1043             return;
1044         if (c == EOF)
1045             return;
1046     }
1047 }
1048
1049 #if NO_LEAKS
1050 NCURSES_EXPORT(void)
1051 _nc_comp_scan_leaks(void)
1052 {
1053     if (pushname != 0) {
1054         FreeAndNull(pushname);
1055     }
1056     if (tok_buf != 0) {
1057         FreeAndNull(tok_buf);
1058     }
1059 }
1060 #endif