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