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