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