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