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