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