]> CyberLeo.Net >> Repos - FreeBSD/releng/7.2.git/blob - contrib/ncurses/ncurses/tinfo/comp_scan.c
Create releng/7.2 from stable/7 in preparation for 7.2-RELEASE.
[FreeBSD/releng/7.2.git] / contrib / ncurses / ncurses / tinfo / comp_scan.c
1 /****************************************************************************
2  * Copyright (c) 1998-2005,2006 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 /* $FreeBSD$ */
36
37 /*
38  *      comp_scan.c --- Lexical scanner for terminfo compiler.
39  *
40  *      _nc_reset_input()
41  *      _nc_get_token()
42  *      _nc_panic_mode()
43  *      int _nc_syntax;
44  *      int _nc_curr_line;
45  *      long _nc_curr_file_pos;
46  *      long _nc_comment_start;
47  *      long _nc_comment_end;
48  */
49
50 #include <curses.priv.h>
51
52 #include <ctype.h>
53 #include <term_entry.h>
54 #include <tic.h>
55
56 MODULE_ID("$Id: comp_scan.c,v 1.78 2006/12/16 19:17:01 tom Exp $")
57
58 /*
59  * Maximum length of string capability we'll accept before raising an error.
60  * Yes, there is a real capability in /etc/termcap this long, an "is".
61  */
62 #define MAXCAPLEN       600
63
64 #define iswhite(ch)     (ch == ' '  ||  ch == '\t')
65
66 NCURSES_EXPORT_VAR(int)
67 _nc_syntax = 0;                 /* termcap or terminfo? */
68 NCURSES_EXPORT_VAR(long)
69 _nc_curr_file_pos = 0;          /* file offset of current line */
70 NCURSES_EXPORT_VAR(long)
71 _nc_comment_start = 0;          /* start of comment range before name */
72 NCURSES_EXPORT_VAR(long)
73 _nc_comment_end = 0;            /* end of comment range before name */
74 NCURSES_EXPORT_VAR(long)
75 _nc_start_line = 0;             /* start line of current entry */
76
77 NCURSES_EXPORT_VAR(struct token)
78 _nc_curr_token =
79 {
80     0, 0, 0
81 };
82
83 /*****************************************************************************
84  *
85  * Token-grabbing machinery
86  *
87  *****************************************************************************/
88
89 static bool first_column;       /* See 'next_char()' below */
90 static bool had_newline;
91 static char separator;          /* capability separator */
92 static int pushtype;            /* type of pushback token */
93 static char *pushname;
94
95 #if NCURSES_EXT_FUNCS
96 NCURSES_EXPORT_VAR(bool)
97 _nc_disable_period = FALSE;     /* used by tic -a option */
98 #endif
99
100 /*****************************************************************************
101  *
102  * Character-stream handling
103  *
104  *****************************************************************************/
105
106 #define LEXBUFSIZ       1024
107
108 static char *bufptr;            /* otherwise, the input buffer pointer */
109 static char *bufstart;          /* start of buffer so we can compute offsets */
110 static FILE *yyin;              /* scanner's input file descriptor */
111
112 /*
113  *      _nc_reset_input()
114  *
115  *      Resets the input-reading routines.  Used on initialization,
116  *      or after a seek has been done.  Exactly one argument must be
117  *      non-null.
118  */
119
120 NCURSES_EXPORT(void)
121 _nc_reset_input(FILE *fp, char *buf)
122 {
123     pushtype = NO_PUSHBACK;
124     if (pushname != 0)
125         pushname[0] = '\0';
126     yyin = fp;
127     bufstart = bufptr = buf;
128     _nc_curr_file_pos = 0L;
129     if (fp != 0)
130         _nc_curr_line = 0;
131     _nc_curr_col = 0;
132 }
133
134 /*
135  *      int last_char()
136  *
137  *      Returns the final nonblank character on the current input buffer
138  */
139 static int
140 last_char(void)
141 {
142     size_t len = strlen(bufptr);
143     while (len--) {
144         if (!isspace(UChar(bufptr[len])))
145             return bufptr[len];
146     }
147     return 0;
148 }
149
150 /*
151  *      int next_char()
152  *
153  *      Returns the next character in the input stream.  Comments and leading
154  *      white space are stripped.
155  *
156  *      The global state variable 'firstcolumn' is set TRUE if the character
157  *      returned is from the first column of the input line.
158  *
159  *      The global variable _nc_curr_line is incremented for each new line.
160  *      The global variable _nc_curr_file_pos is set to the file offset of the
161  *      beginning of each line.
162  */
163
164 static int
165 next_char(void)
166 {
167     static char *result;
168     static size_t allocated;
169     int the_char;
170
171     if (!yyin) {
172         if (result != 0) {
173             FreeAndNull(result);
174             FreeAndNull(pushname);
175             allocated = 0;
176         }
177         /*
178          * An string with an embedded null will truncate the input.  This is
179          * intentional (we don't read binary files here).
180          */
181         if (bufptr == 0 || *bufptr == '\0')
182             return (EOF);
183         if (*bufptr == '\n') {
184             _nc_curr_line++;
185             _nc_curr_col = 0;
186         } else if (*bufptr == '\t') {
187             _nc_curr_col = (_nc_curr_col | 7);
188         }
189     } else if (!bufptr || !*bufptr) {
190         /*
191          * In theory this could be recoded to do its I/O one character at a
192          * time, saving the buffer space.  In practice, this turns out to be
193          * quite hard to get completely right.  Try it and see.  If you
194          * succeed, don't forget to hack push_back() correspondingly.
195          */
196         size_t used;
197         size_t len;
198
199         do {
200             bufstart = 0;
201             used = 0;
202             do {
203                 if (used + (LEXBUFSIZ / 4) >= allocated) {
204                     allocated += (allocated + LEXBUFSIZ);
205                     result = typeRealloc(char, allocated, result);
206                     if (result == 0)
207                         return (EOF);
208                 }
209                 if (used == 0)
210                     _nc_curr_file_pos = ftell(yyin);
211
212                 if (fgets(result + used, (int) (allocated - used), yyin) != 0) {
213                     bufstart = result;
214                     if (used == 0) {
215                         _nc_curr_line++;
216                         _nc_curr_col = 0;
217                     }
218                 } else {
219                     if (used != 0)
220                         strcat(result, "\n");
221                 }
222                 if ((bufptr = bufstart) != 0) {
223                     used = strlen(bufptr);
224                     while (iswhite(*bufptr)) {
225                         if (*bufptr == '\t') {
226                             _nc_curr_col = (_nc_curr_col | 7) + 1;
227                         } else {
228                             _nc_curr_col++;
229                         }
230                         bufptr++;
231                     }
232
233                     /*
234                      * Treat a trailing <cr><lf> the same as a <newline> so we
235                      * can read files on OS/2, etc.
236                      */
237                     if ((len = strlen(bufptr)) > 1) {
238                         if (bufptr[len - 1] == '\n'
239                             && bufptr[len - 2] == '\r') {
240                             len--;
241                             bufptr[len - 1] = '\n';
242                             bufptr[len] = '\0';
243                         }
244                     }
245                 } else {
246                     return (EOF);
247                 }
248             } while (bufptr[len - 1] != '\n');  /* complete a line */
249         } while (result[0] == '#');     /* ignore comments */
250     } else if (*bufptr == '\t') {
251         _nc_curr_col = (_nc_curr_col | 7);
252     }
253
254     first_column = (bufptr == bufstart);
255     if (first_column)
256         had_newline = FALSE;
257
258     _nc_curr_col++;
259     the_char = *bufptr++;
260     return UChar(the_char);
261 }
262
263 static void
264 push_back(char c)
265 /* push a character back onto the input stream */
266 {
267     if (bufptr == bufstart)
268         _nc_syserr_abort("Can't backspace off beginning of line");
269     *--bufptr = c;
270     _nc_curr_col--;
271 }
272
273 static long
274 stream_pos(void)
275 /* return our current character position in the input stream */
276 {
277     return (yyin ? ftell(yyin) : (bufptr ? bufptr - bufstart : 0));
278 }
279
280 static bool
281 end_of_stream(void)
282 /* are we at end of input? */
283 {
284     return ((yyin ? feof(yyin) : (bufptr && *bufptr == '\0'))
285             ? TRUE : FALSE);
286 }
287
288 /* Assume we may be looking at a termcap-style continuation */
289 static NCURSES_INLINE int
290 eat_escaped_newline(int ch)
291 {
292     if (ch == '\\')
293         while ((ch = next_char()) == '\n' || iswhite(ch))
294             continue;
295     return ch;
296 }
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 *buffer;
336
337     char *after_list;
338     char *after_name;
339     char *numchk;
340     char *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 (buffer != 0) {
373             if (_nc_curr_token.tk_name == buffer)
374                 _nc_curr_token.tk_name = 0;
375             FreeAndNull(buffer);
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((chtype) ch));
427             _nc_panic_mode(separator);
428             goto start_token;
429         }
430
431         if (buffer == 0)
432             buffer = typeMalloc(char, MAX_ENTRY_SIZE);
433
434 #ifdef TRACE
435         old_line = _nc_curr_line;
436         old_col = _nc_curr_col;
437 #endif
438         ptr = buffer;
439         *(ptr++) = 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 = ptr;
454                     if (after_name == 0)
455                         after_name = 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                 *ptr++ = ch;
480             }
481             ptr[0] = '\0';
482             if (_nc_syntax == ERR) {
483                 /*
484                  * Grrr...what we ought to do here is barf, complaining that
485                  * the entry is malformed.  But because a couple of name fields
486                  * in the 8.2 termcap file end with |\, we just have to assume
487                  * it's termcap syntax.
488                  */
489                 _nc_syntax = SYN_TERMCAP;
490                 separator = ':';
491             } else if (_nc_syntax == SYN_TERMINFO) {
492                 /* throw away trailing /, *$/ */
493                 for (--ptr; iswhite(*ptr) || *ptr == ','; ptr--)
494                     continue;
495                 ptr[1] = '\0';
496             }
497
498             /*
499              * This is the soonest we have the terminal name fetched.  Set up
500              * for following warning messages.  If there's no '|', then there
501              * is no description.
502              */
503             if (after_name != 0) {
504                 ch = *after_name;
505                 *after_name = '\0';
506                 _nc_set_type(buffer);
507                 *after_name = ch;
508             }
509
510             /*
511              * Compute the boundary between the aliases and the description
512              * field for syntax-checking purposes.
513              */
514             if (after_list != 0) {
515                 if (!silent) {
516                     if (*after_list == '\0')
517                         _nc_warning("empty longname field");
518 #ifndef FREEBSD_NATIVE
519                     else if (strchr(after_list, ' ') == 0)
520                         _nc_warning("older tic versions may treat the description field as an alias");
521 #endif
522                 }
523             } else {
524                 after_list = buffer + strlen(buffer);
525                 DEBUG(1, ("missing description"));
526             }
527
528             /*
529              * Whitespace in a name field other than the long name can confuse
530              * rdist and some termcap tools.  Slashes are a no-no.  Other
531              * special characters can be dangerous due to shell expansion.
532              */
533             for (s = buffer; s < after_list; ++s) {
534                 if (isspace(UChar(*s))) {
535                     if (!silent)
536                         _nc_warning("whitespace in name or alias field");
537                     break;
538                 } else if (*s == '/') {
539                     if (!silent)
540                         _nc_warning("slashes aren't allowed in names or aliases");
541                     break;
542                 } else if (strchr("$[]!*?", *s)) {
543                     if (!silent)
544                         _nc_warning("dubious character `%c' in name or alias field", *s);
545                     break;
546                 }
547             }
548
549             _nc_curr_token.tk_name = buffer;
550             type = NAMES;
551         } else {
552             if (had_newline && _nc_syntax == SYN_TERMCAP) {
553                 _nc_warning("Missing backslash before newline");
554                 had_newline = FALSE;
555             }
556             while ((ch = next_char()) != EOF) {
557                 if (!isalnum(UChar(ch))) {
558                     if (_nc_syntax == SYN_TERMINFO) {
559                         if (ch != '_')
560                             break;
561                     } else {    /* allow ';' for "k;" */
562                         if (ch != ';')
563                             break;
564                     }
565                 }
566                 *(ptr++) = ch;
567             }
568
569             *ptr++ = '\0';
570             switch (ch) {
571             case ',':
572             case ':':
573                 if (ch != separator)
574                     _nc_err_abort("Separator inconsistent with syntax");
575                 _nc_curr_token.tk_name = buffer;
576                 type = BOOLEAN;
577                 break;
578             case '@':
579                 if ((ch = next_char()) != separator && !silent)
580                     _nc_warning("Missing separator after `%s', have %s",
581                                 buffer, unctrl((chtype) ch));
582                 _nc_curr_token.tk_name = buffer;
583                 type = CANCEL;
584                 break;
585
586             case '#':
587                 found = 0;
588                 while (isalnum(ch = next_char())) {
589                     numbuf[found++] = ch;
590                     if (found >= sizeof(numbuf) - 1)
591                         break;
592                 }
593                 numbuf[found] = '\0';
594                 number = strtol(numbuf, &numchk, 0);
595                 if (!silent) {
596                     if (numchk == numbuf)
597                         _nc_warning("no value given for `%s'", buffer);
598                     if ((*numchk != '\0') || (ch != separator))
599                         _nc_warning("Missing separator");
600                 }
601                 _nc_curr_token.tk_name = buffer;
602                 _nc_curr_token.tk_valnumber = number;
603                 type = NUMBER;
604                 break;
605
606             case '=':
607                 ch = _nc_trans_string(ptr, buffer + MAX_ENTRY_SIZE);
608                 if (!silent && ch != separator)
609                     _nc_warning("Missing separator");
610                 _nc_curr_token.tk_name = buffer;
611                 _nc_curr_token.tk_valstring = ptr;
612                 type = STRING;
613                 break;
614
615             case EOF:
616                 type = EOF;
617                 break;
618             default:
619                 /* just to get rid of the compiler warning */
620                 type = UNDEF;
621                 if (!silent)
622                     _nc_warning("Illegal character - '%s'", unctrl((chtype) ch));
623             }
624         }                       /* end else (first_column == FALSE) */
625     }                           /* end else (ch != EOF) */
626
627   end_of_token:
628
629 #ifdef TRACE
630     if (dot_flag == TRUE)
631         DEBUG(8, ("Commented out "));
632
633     if (_nc_tracing >= DEBUG_LEVEL(8)) {
634         _tracef("parsed %d.%d to %d.%d",
635                 old_line, old_col,
636                 _nc_curr_line, _nc_curr_col);
637     }
638     if (_nc_tracing >= DEBUG_LEVEL(7)) {
639         switch (type) {
640         case BOOLEAN:
641             _tracef("Token: Boolean; name='%s'",
642                     _nc_curr_token.tk_name);
643             break;
644
645         case NUMBER:
646             _tracef("Token: Number;  name='%s', value=%d",
647                     _nc_curr_token.tk_name,
648                     _nc_curr_token.tk_valnumber);
649             break;
650
651         case STRING:
652             _tracef("Token: String;  name='%s', value=%s",
653                     _nc_curr_token.tk_name,
654                     _nc_visbuf(_nc_curr_token.tk_valstring));
655             break;
656
657         case CANCEL:
658             _tracef("Token: Cancel; name='%s'",
659                     _nc_curr_token.tk_name);
660             break;
661
662         case NAMES:
663
664             _tracef("Token: Names; value='%s'",
665                     _nc_curr_token.tk_name);
666             break;
667
668         case EOF:
669             _tracef("Token: End of file");
670             break;
671
672         default:
673             _nc_warning("Bad token type");
674         }
675     }
676 #endif
677
678     if (dot_flag == TRUE)       /* if commented out, use the next one */
679         type = _nc_get_token(silent);
680
681     DEBUG(3, ("token: `%s', class %d",
682               ((_nc_curr_token.tk_name != 0)
683                ? _nc_curr_token.tk_name
684                : "<null>"),
685               type));
686
687     return (type);
688 }
689
690 /*
691  *      char
692  *      trans_string(ptr)
693  *
694  *      Reads characters using next_char() until encountering a separator, nl,
695  *      or end-of-file.  The returned value is the character which caused
696  *      reading to stop.  The following translations are done on the input:
697  *
698  *              ^X  goes to  ctrl-X (i.e. X & 037)
699  *              {\E,\n,\r,\b,\t,\f}  go to
700  *                      {ESCAPE,newline,carriage-return,backspace,tab,formfeed}
701  *              {\^,\\}  go to  {carat,backslash}
702  *              \ddd (for ddd = up to three octal digits)  goes to the character ddd
703  *
704  *              \e == \E
705  *              \0 == \200
706  *
707  */
708
709 NCURSES_EXPORT(int)
710 _nc_trans_string(char *ptr, char *last)
711 {
712     int count = 0;
713     int number = 0;
714     int i, c;
715     chtype ch, last_ch = '\0';
716     bool ignored = FALSE;
717     bool long_warning = FALSE;
718
719     while ((ch = c = next_char()) != (chtype) separator && c != EOF) {
720         if (ptr == (last - 1))
721             break;
722         if ((_nc_syntax == SYN_TERMCAP) && c == '\n')
723             break;
724         if (ch == '^' && last_ch != '%') {
725             ch = c = next_char();
726             if (c == EOF)
727                 _nc_err_abort(MSG_NO_INPUTS);
728
729             if (!(is7bits(ch) && isprint(ch))) {
730                 _nc_warning("Illegal ^ character - '%s'", unctrl(ch));
731             }
732             if (ch == '?') {
733                 *(ptr++) = '\177';
734                 if (_nc_tracing)
735                     _nc_warning("Allow ^? as synonym for \\177");
736             } else {
737                 if ((ch &= 037) == 0)
738                     ch = 128;
739                 *(ptr++) = (char) (ch);
740             }
741         } else if (ch == '\\') {
742             ch = c = next_char();
743             if (c == EOF)
744                 _nc_err_abort(MSG_NO_INPUTS);
745
746             if (ch >= '0' && ch <= '7') {
747                 number = ch - '0';
748                 for (i = 0; i < 2; i++) {
749                     ch = c = next_char();
750                     if (c == EOF)
751                         _nc_err_abort(MSG_NO_INPUTS);
752
753                     if (c < '0' || c > '7') {
754                         if (isdigit(c)) {
755                             _nc_warning("Non-octal digit `%c' in \\ sequence", c);
756                             /* allow the digit; it'll do less harm */
757                         } else {
758                             push_back((char) c);
759                             break;
760                         }
761                     }
762
763                     number = number * 8 + c - '0';
764                 }
765
766                 if (number == 0)
767                     number = 0200;
768                 *(ptr++) = (char) number;
769             } else {
770                 switch (c) {
771                 case 'E':
772                 case 'e':
773                     *(ptr++) = '\033';
774                     break;
775
776                 case 'a':
777                     *(ptr++) = '\007';
778                     break;
779
780                 case 'l':
781                 case 'n':
782                     *(ptr++) = '\n';
783                     break;
784
785                 case 'r':
786                     *(ptr++) = '\r';
787                     break;
788
789                 case 'b':
790                     *(ptr++) = '\010';
791                     break;
792
793                 case 's':
794                     *(ptr++) = ' ';
795                     break;
796
797                 case 'f':
798                     *(ptr++) = '\014';
799                     break;
800
801                 case 't':
802                     *(ptr++) = '\t';
803                     break;
804
805                 case '\\':
806                     *(ptr++) = '\\';
807                     break;
808
809                 case '^':
810                     *(ptr++) = '^';
811                     break;
812
813                 case ',':
814                     *(ptr++) = ',';
815                     break;
816
817                 case ':':
818                     *(ptr++) = ':';
819                     break;
820
821                 case '\n':
822                     continue;
823
824                 default:
825                     _nc_warning("Illegal character '%s' in \\ sequence",
826                                 unctrl(ch));
827                     /* FALLTHRU */
828                 case '|':
829                     *(ptr++) = (char) ch;
830                 }               /* endswitch (ch) */
831             }                   /* endelse (ch < '0' ||  ch > '7') */
832         }
833         /* end else if (ch == '\\') */
834         else if (ch == '\n' && (_nc_syntax == SYN_TERMINFO)) {
835             /*
836              * Newlines embedded in a terminfo string are ignored, provided
837              * that the next line begins with whitespace.
838              */
839             ignored = TRUE;
840         } else {
841             *(ptr++) = (char) ch;
842         }
843
844         if (!ignored) {
845             if (_nc_curr_col <= 1) {
846                 push_back(ch);
847                 ch = '\n';
848                 break;
849             }
850             last_ch = ch;
851             count++;
852         }
853         ignored = FALSE;
854
855         if (count > MAXCAPLEN && !long_warning) {
856             _nc_warning("Very long string found.  Missing separator?");
857             long_warning = TRUE;
858         }
859     }                           /* end while */
860
861     *ptr = '\0';
862
863     return (ch);
864 }
865
866 /*
867  *      _nc_push_token()
868  *
869  *      Push a token of given type so that it will be reread by the next
870  *      get_token() call.
871  */
872
873 NCURSES_EXPORT(void)
874 _nc_push_token(int tokclass)
875 {
876     /*
877      * This implementation is kind of bogus, it will fail if we ever do more
878      * than one pushback at a time between get_token() calls.  It relies on the
879      * fact that _nc_curr_token is static storage that nothing but
880      * _nc_get_token() touches.
881      */
882     pushtype = tokclass;
883     if (pushname == 0)
884         pushname = typeMalloc(char, MAX_NAME_SIZE + 1);
885     _nc_get_type(pushname);
886
887     DEBUG(3, ("pushing token: `%s', class %d",
888               ((_nc_curr_token.tk_name != 0)
889                ? _nc_curr_token.tk_name
890                : "<null>"),
891               pushtype));
892 }
893
894 /*
895  * Panic mode error recovery - skip everything until a "ch" is found.
896  */
897 NCURSES_EXPORT(void)
898 _nc_panic_mode(char ch)
899 {
900     int c;
901
902     for (;;) {
903         c = next_char();
904         if (c == ch)
905             return;
906         if (c == EOF)
907             return;
908     }
909 }
910
911 #if NO_LEAKS
912 NCURSES_EXPORT(void)
913 _nc_comp_scan_leaks(void)
914 {
915     if (pushname != 0) {
916         FreeAndNull(pushname);
917     }
918 }
919 #endif