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