blob: 3ba0835e2fb807aed6d27d6e9a2625c909d76f55 [file] [log] [blame]
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +05301/****************************************************************************
micky3879b9f5e72025-07-08 18:04:53 -04002,* Copyright 2020-2022,2023 Thomas E. Dickey *
3 * Copyright 1998-2016,2017 Free Software Foundation, Inc. *
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +05304 * *
5 * Permission is hereby granted, free of charge, to any person obtaining a *
6 * copy of this software and associated documentation files (the *
7 * "Software"), to deal in the Software without restriction, including *
8 * without limitation the rights to use, copy, modify, merge, publish, *
9 * distribute, distribute with modifications, sublicense, and/or sell *
10 * copies of the Software, and to permit persons to whom the Software is *
11 * furnished to do so, subject to the following conditions: *
12 * *
13 * The above copyright notice and this permission notice shall be included *
14 * in all copies or substantial portions of the Software. *
15 * *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS *
17 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF *
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. *
19 * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, *
20 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR *
21 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR *
22 * THE USE OR OTHER DEALINGS IN THE SOFTWARE. *
23 * *
24 * Except as contained in this notice, the name(s) of the above copyright *
25 * holders shall not be used in advertising or otherwise to promote the *
26 * sale, use or other dealings in this Software without prior written *
27 * authorization. *
28 ****************************************************************************/
29
30/****************************************************************************
31 * Author: Zeyd M. Ben-Halim <zmbenhal@netcom.com> 1992,1995 *
32 * and: Eric S. Raymond <esr@snark.thyrsus.com> *
33 * and: Thomas E. Dickey 1996 on *
34 ****************************************************************************/
35
36/*
37 * comp_scan.c --- Lexical scanner for terminfo compiler.
38 *
39 * _nc_reset_input()
40 * _nc_get_token()
41 * _nc_panic_mode()
42 * int _nc_syntax;
43 * int _nc_curr_line;
44 * long _nc_curr_file_pos;
45 * long _nc_comment_start;
46 * long _nc_comment_end;
47 */
48
49#include <curses.priv.h>
50
51#include <ctype.h>
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +053052#include <tic.h>
53
micky3879b9f5e72025-07-08 18:04:53 -040054MODULE_ID("$Id: comp_scan.c,v 1.122 2023/05/27 20:13:10 tom Exp $")
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +053055
56/*
57 * Maximum length of string capability we'll accept before raising an error.
58 * Yes, there is a real capability in /etc/termcap this long, an "is".
59 */
60#define MAXCAPLEN 600
61
62#define iswhite(ch) (ch == ' ' || ch == '\t')
63
Steve Kondikae271bc2015-11-15 02:50:53 +010064NCURSES_EXPORT_VAR (int) _nc_syntax = 0; /* termcap or terminfo? */
65NCURSES_EXPORT_VAR (int) _nc_strict_bsd = 1; /* ncurses extended termcap? */
66NCURSES_EXPORT_VAR (long) _nc_curr_file_pos = 0; /* file offset of current line */
67NCURSES_EXPORT_VAR (long) _nc_comment_start = 0; /* start of comment range before name */
68NCURSES_EXPORT_VAR (long) _nc_comment_end = 0; /* end of comment range before name */
69NCURSES_EXPORT_VAR (long) _nc_start_line = 0; /* start line of current entry */
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +053070
Steve Kondikae271bc2015-11-15 02:50:53 +010071NCURSES_EXPORT_VAR (struct token) _nc_curr_token =
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +053072{
73 0, 0, 0
74};
75
76/*****************************************************************************
77 *
78 * Token-grabbing machinery
79 *
80 *****************************************************************************/
81
82static bool first_column; /* See 'next_char()' below */
83static bool had_newline;
84static char separator; /* capability separator */
85static int pushtype; /* type of pushback token */
86static char *pushname;
87
88#if NCURSES_EXT_FUNCS
Steve Kondikae271bc2015-11-15 02:50:53 +010089NCURSES_EXPORT_VAR (bool) _nc_disable_period = FALSE; /* used by tic -a option */
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +053090#endif
91
92/*****************************************************************************
93 *
94 * Character-stream handling
95 *
96 *****************************************************************************/
97
98#define LEXBUFSIZ 1024
99
100static char *bufptr; /* otherwise, the input buffer pointer */
101static char *bufstart; /* start of buffer so we can compute offsets */
102static FILE *yyin; /* scanner's input file descriptor */
103
104/*
105 * _nc_reset_input()
106 *
107 * Resets the input-reading routines. Used on initialization,
108 * or after a seek has been done. Exactly one argument must be
109 * non-null.
110 */
111
112NCURSES_EXPORT(void)
113_nc_reset_input(FILE *fp, char *buf)
114{
micky3879b9f5e72025-07-08 18:04:53 -0400115 TR(TRACE_DATABASE,
116 (T_CALLED("_nc_reset_input(fp=%p, buf=%p)"), (void *) fp, buf));
117
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530118 pushtype = NO_PUSHBACK;
119 if (pushname != 0)
120 pushname[0] = '\0';
121 yyin = fp;
122 bufstart = bufptr = buf;
123 _nc_curr_file_pos = 0L;
124 if (fp != 0)
125 _nc_curr_line = 0;
126 _nc_curr_col = 0;
micky3879b9f5e72025-07-08 18:04:53 -0400127
128 returnVoidDB;
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530129}
130
131/*
132 * int last_char()
133 *
134 * Returns the final nonblank character on the current input buffer
135 */
136static int
Steve Kondikae271bc2015-11-15 02:50:53 +0100137last_char(int from_end)
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530138{
139 size_t len = strlen(bufptr);
Steve Kondikae271bc2015-11-15 02:50:53 +0100140 int result = 0;
141
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530142 while (len--) {
Steve Kondikae271bc2015-11-15 02:50:53 +0100143 if (!isspace(UChar(bufptr[len]))) {
micky3879b9f5e72025-07-08 18:04:53 -0400144 if (from_end <= (int) len)
Steve Kondikae271bc2015-11-15 02:50:53 +0100145 result = bufptr[(int) len - from_end];
146 break;
147 }
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530148 }
Steve Kondikae271bc2015-11-15 02:50:53 +0100149 return result;
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530150}
151
152/*
micky3879b9f5e72025-07-08 18:04:53 -0400153 * Read, like fgets(), but error-out if the input contains nulls.
154 */
155static int
156get_text(char *buffer, int length)
157{
158 int count = 0;
159 int limit = length - 1;
160
161 while (limit-- > 0) {
162 int ch = fgetc(yyin);
163
164 if (ch == '\0') {
165 _nc_err_abort("This is not a text-file");
166 } else if (ch == EOF) {
167 break;
168 }
169 ++count;
170 *buffer++ = (char) ch;
171 if (ch == '\n')
172 break;
173 }
174 *buffer = '\0';
175 return count;
176}
177
178/*
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530179 * int next_char()
180 *
181 * Returns the next character in the input stream. Comments and leading
182 * white space are stripped.
183 *
184 * The global state variable 'firstcolumn' is set TRUE if the character
185 * returned is from the first column of the input line.
186 *
187 * The global variable _nc_curr_line is incremented for each new line.
188 * The global variable _nc_curr_file_pos is set to the file offset of the
189 * beginning of each line.
190 */
191
192static int
193next_char(void)
194{
195 static char *result;
196 static size_t allocated;
197 int the_char;
198
199 if (!yyin) {
200 if (result != 0) {
201 FreeAndNull(result);
202 FreeAndNull(pushname);
micky3879b9f5e72025-07-08 18:04:53 -0400203 bufptr = 0;
204 bufstart = 0;
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530205 allocated = 0;
206 }
207 /*
208 * An string with an embedded null will truncate the input. This is
209 * intentional (we don't read binary files here).
210 */
211 if (bufptr == 0 || *bufptr == '\0')
212 return (EOF);
213 if (*bufptr == '\n') {
214 _nc_curr_line++;
215 _nc_curr_col = 0;
216 } else if (*bufptr == '\t') {
217 _nc_curr_col = (_nc_curr_col | 7);
218 }
219 } else if (!bufptr || !*bufptr) {
220 /*
221 * In theory this could be recoded to do its I/O one character at a
222 * time, saving the buffer space. In practice, this turns out to be
223 * quite hard to get completely right. Try it and see. If you
224 * succeed, don't forget to hack push_back() correspondingly.
225 */
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530226 size_t len;
227
228 do {
micky3879b9f5e72025-07-08 18:04:53 -0400229 size_t used = 0;
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530230 bufstart = 0;
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530231 do {
232 if (used + (LEXBUFSIZ / 4) >= allocated) {
233 allocated += (allocated + LEXBUFSIZ);
234 result = typeRealloc(char, allocated, result);
235 if (result == 0)
236 return (EOF);
Steve Kondikae271bc2015-11-15 02:50:53 +0100237 if (bufstart)
238 bufstart = result;
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530239 }
240 if (used == 0)
241 _nc_curr_file_pos = ftell(yyin);
242
micky3879b9f5e72025-07-08 18:04:53 -0400243 if (get_text(result + used, (int) (allocated - used))) {
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530244 bufstart = result;
245 if (used == 0) {
Steve Kondikae271bc2015-11-15 02:50:53 +0100246 if (_nc_curr_line == 0
247 && IS_TIC_MAGIC(result)) {
248 _nc_err_abort("This is a compiled terminal description, not a source");
249 }
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530250 _nc_curr_line++;
251 _nc_curr_col = 0;
252 }
253 } else {
254 if (used != 0)
Steve Kondikae271bc2015-11-15 02:50:53 +0100255 _nc_STRCAT(result, "\n", allocated);
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530256 }
257 if ((bufptr = bufstart) != 0) {
258 used = strlen(bufptr);
micky3879b9f5e72025-07-08 18:04:53 -0400259 if (used == 0)
260 return (EOF);
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530261 while (iswhite(*bufptr)) {
262 if (*bufptr == '\t') {
263 _nc_curr_col = (_nc_curr_col | 7) + 1;
264 } else {
265 _nc_curr_col++;
266 }
267 bufptr++;
268 }
269
270 /*
271 * Treat a trailing <cr><lf> the same as a <newline> so we
272 * can read files on OS/2, etc.
273 */
274 if ((len = strlen(bufptr)) > 1) {
275 if (bufptr[len - 1] == '\n'
276 && bufptr[len - 2] == '\r') {
277 len--;
278 bufptr[len - 1] = '\n';
279 bufptr[len] = '\0';
280 }
281 }
282 } else {
283 return (EOF);
284 }
285 } while (bufptr[len - 1] != '\n'); /* complete a line */
286 } while (result[0] == '#'); /* ignore comments */
287 } else if (*bufptr == '\t') {
288 _nc_curr_col = (_nc_curr_col | 7);
289 }
290
291 first_column = (bufptr == bufstart);
292 if (first_column)
293 had_newline = FALSE;
294
295 _nc_curr_col++;
296 the_char = *bufptr++;
297 return UChar(the_char);
298}
299
300static void
Steve Kondikae271bc2015-11-15 02:50:53 +0100301push_back(int c)
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530302/* push a character back onto the input stream */
303{
304 if (bufptr == bufstart)
micky3879b9f5e72025-07-08 18:04:53 -0400305 _nc_syserr_abort("cannot backspace off beginning of line");
Steve Kondikae271bc2015-11-15 02:50:53 +0100306 *--bufptr = (char) c;
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530307 _nc_curr_col--;
308}
309
310static long
311stream_pos(void)
312/* return our current character position in the input stream */
313{
micky3879b9f5e72025-07-08 18:04:53 -0400314 return (yyin ? ftell(yyin) : (bufptr ? (long) (bufptr - bufstart) : 0));
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530315}
316
317static bool
318end_of_stream(void)
319/* are we at end of input? */
320{
micky3879b9f5e72025-07-08 18:04:53 -0400321 return ((yyin
322 ? (feof(yyin) && (bufptr == NULL || *bufptr == '\0'))
323 : (bufptr && *bufptr == '\0'))
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530324 ? TRUE : FALSE);
325}
326
327/* Assume we may be looking at a termcap-style continuation */
328static NCURSES_INLINE int
329eat_escaped_newline(int ch)
330{
micky3879b9f5e72025-07-08 18:04:53 -0400331 if (ch == '\\') {
332 while ((ch = next_char()) == '\n' || iswhite(ch)) {
333 /* EMPTY */ ;
334 }
335 }
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530336 return ch;
337}
338
339#define TOK_BUF_SIZE MAX_ENTRY_SIZE
340
341#define OkToAdd() \
342 ((tok_ptr - tok_buf) < (TOK_BUF_SIZE - 2))
343
344#define AddCh(ch) \
345 *tok_ptr++ = (char) ch; \
346 *tok_ptr = '\0'
347
Steve Kondikae271bc2015-11-15 02:50:53 +0100348static char *tok_buf;
349
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530350/*
351 * int
352 * get_token()
353 *
354 * Scans the input for the next token, storing the specifics in the
355 * global structure 'curr_token' and returning one of the following:
356 *
357 * NAMES A line beginning in column 1. 'name'
358 * will be set to point to everything up to but
359 * not including the first separator on the line.
360 * BOOLEAN An entry consisting of a name followed by
361 * a separator. 'name' will be set to point to
362 * the name of the capability.
363 * NUMBER An entry of the form
364 * name#digits,
365 * 'name' will be set to point to the capability
366 * name and 'valnumber' to the number given.
367 * STRING An entry of the form
368 * name=characters,
369 * 'name' is set to the capability name and
370 * 'valstring' to the string of characters, with
371 * input translations done.
372 * CANCEL An entry of the form
373 * name@,
374 * 'name' is set to the capability name and
375 * 'valnumber' to -1.
376 * EOF The end of the file has been reached.
377 *
378 * A `separator' is either a comma or a semicolon, depending on whether
379 * we are in termcap or terminfo mode.
380 *
381 */
382
383NCURSES_EXPORT(int)
384_nc_get_token(bool silent)
385{
386 static const char terminfo_punct[] = "@%&*!#";
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530387
Steve Kondikae271bc2015-11-15 02:50:53 +0100388 char *after_name; /* after primary name */
389 char *after_list; /* after primary and alias list */
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530390 char *numchk;
391 char *tok_ptr;
392 char *s;
393 char numbuf[80];
Steve Kondikae271bc2015-11-15 02:50:53 +0100394 int ch, c0, c1;
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530395 int dot_flag = FALSE;
396 int type;
397 long number;
398 long token_start;
399 unsigned found;
400#ifdef TRACE
401 int old_line;
402 int old_col;
403#endif
404
micky3879b9f5e72025-07-08 18:04:53 -0400405 DEBUG(3, (T_CALLED("_nc_get_token(silent=%d)"), silent));
406
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530407 if (pushtype != NO_PUSHBACK) {
408 int retval = pushtype;
409
410 _nc_set_type(pushname != 0 ? pushname : "");
411 DEBUG(3, ("pushed-back token: `%s', class %d",
412 _nc_curr_token.tk_name, pushtype));
413
414 pushtype = NO_PUSHBACK;
415 if (pushname != 0)
416 pushname[0] = '\0';
417
418 /* currtok wasn't altered by _nc_push_token() */
micky3879b9f5e72025-07-08 18:04:53 -0400419 DEBUG(3, (T_RETURN("%d"), retval));
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530420 return (retval);
421 }
422
423 if (end_of_stream()) {
424 yyin = 0;
Steve Kondikae271bc2015-11-15 02:50:53 +0100425 (void) next_char(); /* frees its allocated memory */
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530426 if (tok_buf != 0) {
427 if (_nc_curr_token.tk_name == tok_buf)
428 _nc_curr_token.tk_name = 0;
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530429 }
micky3879b9f5e72025-07-08 18:04:53 -0400430 DEBUG(3, (T_RETURN("%d"), EOF));
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530431 return (EOF);
432 }
433
434 start_token:
435 token_start = stream_pos();
436 while ((ch = next_char()) == '\n' || iswhite(ch)) {
437 if (ch == '\n')
438 had_newline = TRUE;
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530439 }
440
441 ch = eat_escaped_newline(ch);
Steve Kondikae271bc2015-11-15 02:50:53 +0100442 _nc_curr_token.tk_valstring = 0;
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530443
444#ifdef TRACE
445 old_line = _nc_curr_line;
446 old_col = _nc_curr_col;
447#endif
448 if (ch == EOF)
449 type = EOF;
450 else {
451 /* if this is a termcap entry, skip a leading separator */
452 if (separator == ':' && ch == ':')
453 ch = next_char();
454
455 if (ch == '.'
456#if NCURSES_EXT_FUNCS
457 && !_nc_disable_period
458#endif
459 ) {
460 dot_flag = TRUE;
461 DEBUG(8, ("dot-flag set"));
462
micky3879b9f5e72025-07-08 18:04:53 -0400463 while ((ch = next_char()) == '.' || iswhite(ch)) {
464 /* EMPTY */ ;
465 }
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530466 }
467
468 if (ch == EOF) {
469 type = EOF;
470 goto end_of_token;
471 }
472
473 /* have to make some punctuation chars legal for terminfo */
474 if (!isalnum(UChar(ch))
475#if NCURSES_EXT_FUNCS
476 && !(ch == '.' && _nc_disable_period)
477#endif
Steve Kondikae271bc2015-11-15 02:50:53 +0100478 && ((strchr) (terminfo_punct, (char) ch) == 0)) {
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530479 if (!silent)
480 _nc_warning("Illegal character (expected alphanumeric or %s) - '%s'",
Steve Kondikae271bc2015-11-15 02:50:53 +0100481 terminfo_punct, unctrl(UChar(ch)));
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530482 _nc_panic_mode(separator);
483 goto start_token;
484 }
485
486 if (tok_buf == 0)
487 tok_buf = typeMalloc(char, TOK_BUF_SIZE);
488
489#ifdef TRACE
490 old_line = _nc_curr_line;
491 old_col = _nc_curr_col;
492#endif
493 tok_ptr = tok_buf;
494 AddCh(ch);
495
496 if (first_column) {
497 _nc_comment_start = token_start;
498 _nc_comment_end = _nc_curr_file_pos;
499 _nc_start_line = _nc_curr_line;
500
501 _nc_syntax = ERR;
502 after_name = 0;
503 after_list = 0;
504 while ((ch = next_char()) != '\n') {
505 if (ch == EOF) {
506 _nc_err_abort(MSG_NO_INPUTS);
507 } else if (ch == '|') {
508 after_list = tok_ptr;
509 if (after_name == 0)
510 after_name = tok_ptr;
Steve Kondikae271bc2015-11-15 02:50:53 +0100511 } else if (ch == ':' && last_char(0) != ',') {
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530512 _nc_syntax = SYN_TERMCAP;
513 separator = ':';
514 break;
515 } else if (ch == ',') {
516 _nc_syntax = SYN_TERMINFO;
517 separator = ',';
518 /*
519 * If we did not see a '|', then we found a name with no
520 * aliases or description.
521 */
522 if (after_name == 0)
523 break;
524 /*
Steve Kondikae271bc2015-11-15 02:50:53 +0100525 * We saw a comma, but are not entirely sure this is
526 * terminfo format, since we can still be parsing the
527 * description field (for either syntax).
528 *
529 * A properly formatted termcap line ends with either a
530 * colon, or a backslash after a colon. It is possible
531 * to have a backslash in the middle of a capability, but
532 * then there would be no leading whitespace on the next
533 * line - something we want to discourage.
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530534 */
Steve Kondikae271bc2015-11-15 02:50:53 +0100535 c0 = last_char(0);
536 c1 = last_char(1);
537 if (c1 != ':' && c0 != '\\' && c0 != ':') {
538 bool capability = FALSE;
539
540 /*
541 * Since it is not termcap, assume the line is terminfo
542 * format. However, the comma can be embedded in a
543 * description field. It also can be a separator
544 * between a description field and a capability.
545 *
546 * Improve the guess by checking if the next word after
547 * the comma does not look like a capability. In that
548 * case, extend the description past the comma.
549 */
550 for (s = bufptr; isspace(UChar(*s)); ++s) {
551 ;
552 }
553 if (islower(UChar(*s))) {
554 char *name = s;
555 while (isalnum(UChar(*s))) {
556 ++s;
557 }
558 if (*s == '#' || *s == '=' || *s == '@') {
559 /*
560 * Checking solely with syntax allows us to
561 * support extended capabilities with string
562 * values.
563 */
564 capability = TRUE;
565 } else if (*s == ',') {
566 c0 = *s;
567 *s = '\0';
568 /*
569 * Otherwise, we can handle predefined boolean
570 * capabilities, still aided by syntax.
571 */
572 if (_nc_find_entry(name,
573 _nc_get_hash_table(FALSE))) {
574 capability = TRUE;
575 }
576 *s = (char) c0;
577 }
578 }
579 if (capability) {
580 break;
581 }
582 }
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530583 } else
584 ch = eat_escaped_newline(ch);
585
586 if (OkToAdd()) {
587 AddCh(ch);
588 } else {
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530589 break;
590 }
591 }
592 *tok_ptr = '\0';
593 if (_nc_syntax == ERR) {
594 /*
595 * Grrr...what we ought to do here is barf, complaining that
596 * the entry is malformed. But because a couple of name fields
597 * in the 8.2 termcap file end with |\, we just have to assume
micky3879b9f5e72025-07-08 18:04:53 -0400598 * it is termcap syntax.
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530599 */
600 _nc_syntax = SYN_TERMCAP;
601 separator = ':';
602 } else if (_nc_syntax == SYN_TERMINFO) {
603 /* throw away trailing /, *$/ */
604 for (--tok_ptr;
605 iswhite(*tok_ptr) || *tok_ptr == ',';
micky3879b9f5e72025-07-08 18:04:53 -0400606 tok_ptr--) {
607 /* EMPTY */ ;
608 }
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530609 tok_ptr[1] = '\0';
610 }
611
612 /*
613 * This is the soonest we have the terminal name fetched. Set up
614 * for following warning messages. If there's no '|', then there
615 * is no description.
616 */
617 if (after_name != 0) {
618 ch = *after_name;
619 *after_name = '\0';
620 _nc_set_type(tok_buf);
621 *after_name = (char) ch;
622 }
623
624 /*
625 * Compute the boundary between the aliases and the description
626 * field for syntax-checking purposes.
627 */
628 if (after_list != 0) {
629 if (!silent) {
micky3879b9f5e72025-07-08 18:04:53 -0400630 if (*after_list == '\0' || strchr("|", after_list[1]) != NULL) {
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530631 _nc_warning("empty longname field");
micky3879b9f5e72025-07-08 18:04:53 -0400632 } else if (strchr(after_list, ' ') == 0) {
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530633 _nc_warning("older tic versions may treat the description field as an alias");
micky3879b9f5e72025-07-08 18:04:53 -0400634 }
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530635 }
636 } else {
637 after_list = tok_buf + strlen(tok_buf);
micky3879b9f5e72025-07-08 18:04:53 -0400638 DEBUG(2, ("missing description"));
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530639 }
640
641 /*
642 * Whitespace in a name field other than the long name can confuse
643 * rdist and some termcap tools. Slashes are a no-no. Other
644 * special characters can be dangerous due to shell expansion.
645 */
646 for (s = tok_buf; s < after_list; ++s) {
647 if (isspace(UChar(*s))) {
648 if (!silent)
649 _nc_warning("whitespace in name or alias field");
650 break;
651 } else if (*s == '/') {
652 if (!silent)
653 _nc_warning("slashes aren't allowed in names or aliases");
654 break;
655 } else if (strchr("$[]!*?", *s)) {
656 if (!silent)
657 _nc_warning("dubious character `%c' in name or alias field", *s);
658 break;
659 }
660 }
661
662 _nc_curr_token.tk_name = tok_buf;
663 type = NAMES;
664 } else {
665 if (had_newline && _nc_syntax == SYN_TERMCAP) {
666 _nc_warning("Missing backslash before newline");
667 had_newline = FALSE;
668 }
669 while ((ch = next_char()) != EOF) {
670 if (!isalnum(UChar(ch))) {
671 if (_nc_syntax == SYN_TERMINFO) {
672 if (ch != '_')
673 break;
674 } else { /* allow ';' for "k;" */
675 if (ch != ';')
676 break;
677 }
678 }
679 if (OkToAdd()) {
680 AddCh(ch);
681 } else {
682 ch = EOF;
683 break;
684 }
685 }
686
687 *tok_ptr++ = '\0'; /* separate name/value in buffer */
688 switch (ch) {
689 case ',':
690 case ':':
691 if (ch != separator)
692 _nc_err_abort("Separator inconsistent with syntax");
693 _nc_curr_token.tk_name = tok_buf;
694 type = BOOLEAN;
695 break;
696 case '@':
697 if ((ch = next_char()) != separator && !silent)
698 _nc_warning("Missing separator after `%s', have %s",
Steve Kondikae271bc2015-11-15 02:50:53 +0100699 tok_buf, unctrl(UChar(ch)));
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530700 _nc_curr_token.tk_name = tok_buf;
701 type = CANCEL;
702 break;
703
704 case '#':
705 found = 0;
706 while (isalnum(ch = next_char())) {
707 numbuf[found++] = (char) ch;
708 if (found >= sizeof(numbuf) - 1)
709 break;
710 }
711 numbuf[found] = '\0';
712 number = strtol(numbuf, &numchk, 0);
713 if (!silent) {
714 if (numchk == numbuf)
715 _nc_warning("no value given for `%s'", tok_buf);
716 if ((*numchk != '\0') || (ch != separator))
micky3879b9f5e72025-07-08 18:04:53 -0400717 _nc_warning("Missing separator for `%s'", tok_buf);
718 if (number < 0)
719 _nc_warning("value of `%s' cannot be negative", tok_buf);
720 if (number > MAX_OF_TYPE(NCURSES_INT2)) {
721 _nc_warning("limiting value of `%s' from %#lx to %#x",
722 tok_buf,
723 number, MAX_OF_TYPE(NCURSES_INT2));
724 number = MAX_OF_TYPE(NCURSES_INT2);
725 }
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530726 }
727 _nc_curr_token.tk_name = tok_buf;
Steve Kondikae271bc2015-11-15 02:50:53 +0100728 _nc_curr_token.tk_valnumber = (int) number;
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530729 type = NUMBER;
730 break;
731
732 case '=':
733 ch = _nc_trans_string(tok_ptr, tok_buf + TOK_BUF_SIZE);
734 if (!silent && ch != separator)
735 _nc_warning("Missing separator");
736 _nc_curr_token.tk_name = tok_buf;
737 _nc_curr_token.tk_valstring = tok_ptr;
738 type = STRING;
739 break;
740
741 case EOF:
742 type = EOF;
743 break;
744 default:
745 /* just to get rid of the compiler warning */
746 type = UNDEF;
747 if (!silent)
Steve Kondikae271bc2015-11-15 02:50:53 +0100748 _nc_warning("Illegal character - '%s'", unctrl(UChar(ch)));
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530749 }
750 } /* end else (first_column == FALSE) */
751 } /* end else (ch != EOF) */
752
753 end_of_token:
754
755#ifdef TRACE
756 if (dot_flag == TRUE)
757 DEBUG(8, ("Commented out "));
758
759 if (_nc_tracing >= DEBUG_LEVEL(8)) {
760 _tracef("parsed %d.%d to %d.%d",
761 old_line, old_col,
762 _nc_curr_line, _nc_curr_col);
763 }
764 if (_nc_tracing >= DEBUG_LEVEL(7)) {
765 switch (type) {
766 case BOOLEAN:
767 _tracef("Token: Boolean; name='%s'",
768 _nc_curr_token.tk_name);
769 break;
770
771 case NUMBER:
772 _tracef("Token: Number; name='%s', value=%d",
773 _nc_curr_token.tk_name,
774 _nc_curr_token.tk_valnumber);
775 break;
776
777 case STRING:
778 _tracef("Token: String; name='%s', value=%s",
779 _nc_curr_token.tk_name,
780 _nc_visbuf(_nc_curr_token.tk_valstring));
781 break;
782
783 case CANCEL:
784 _tracef("Token: Cancel; name='%s'",
785 _nc_curr_token.tk_name);
786 break;
787
788 case NAMES:
789
790 _tracef("Token: Names; value='%s'",
791 _nc_curr_token.tk_name);
792 break;
793
794 case EOF:
795 _tracef("Token: End of file");
796 break;
797
798 default:
799 _nc_warning("Bad token type");
800 }
801 }
802#endif
803
804 if (dot_flag == TRUE) /* if commented out, use the next one */
805 type = _nc_get_token(silent);
806
807 DEBUG(3, ("token: `%s', class %d",
808 ((_nc_curr_token.tk_name != 0)
809 ? _nc_curr_token.tk_name
810 : "<null>"),
811 type));
812
micky3879b9f5e72025-07-08 18:04:53 -0400813 DEBUG(3, (T_RETURN("%d"), type));
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530814 return (type);
815}
816
817/*
818 * char
819 * trans_string(ptr)
820 *
821 * Reads characters using next_char() until encountering a separator, nl,
822 * or end-of-file. The returned value is the character which caused
823 * reading to stop. The following translations are done on the input:
824 *
825 * ^X goes to ctrl-X (i.e. X & 037)
826 * {\E,\n,\r,\b,\t,\f} go to
827 * {ESCAPE,newline,carriage-return,backspace,tab,formfeed}
828 * {\^,\\} go to {carat,backslash}
829 * \ddd (for ddd = up to three octal digits) goes to the character ddd
830 *
831 * \e == \E
832 * \0 == \200
833 *
834 */
835
836NCURSES_EXPORT(int)
micky3879b9f5e72025-07-08 18:04:53 -0400837_nc_trans_string(char *ptr, const char *const last)
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530838{
839 int count = 0;
840 int number = 0;
841 int i, c;
Steve Kondikae271bc2015-11-15 02:50:53 +0100842 int last_ch = '\0';
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530843 bool ignored = FALSE;
844 bool long_warning = FALSE;
845
Steve Kondikae271bc2015-11-15 02:50:53 +0100846 while ((c = next_char()) != separator && c != EOF) {
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530847 if (ptr >= (last - 1)) {
848 if (c != EOF) {
849 while ((c = next_char()) != separator && c != EOF) {
850 ;
851 }
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530852 }
853 break;
854 }
855 if ((_nc_syntax == SYN_TERMCAP) && c == '\n')
856 break;
Steve Kondikae271bc2015-11-15 02:50:53 +0100857 if (c == '^' && last_ch != '%') {
858 c = next_char();
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530859 if (c == EOF)
860 _nc_err_abort(MSG_NO_INPUTS);
861
Steve Kondikae271bc2015-11-15 02:50:53 +0100862 if (!(is7bits(c) && isprint(c))) {
863 _nc_warning("Illegal ^ character - '%s'", unctrl(UChar(c)));
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530864 }
Steve Kondikae271bc2015-11-15 02:50:53 +0100865 if (c == '?' && (_nc_syntax != SYN_TERMCAP)) {
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530866 *(ptr++) = '\177';
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530867 } else {
Steve Kondikae271bc2015-11-15 02:50:53 +0100868 if ((c &= 037) == 0)
869 c = 128;
870 *(ptr++) = (char) (c);
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530871 }
Steve Kondikae271bc2015-11-15 02:50:53 +0100872 } else if (c == '\\') {
873 bool strict_bsd = ((_nc_syntax == SYN_TERMCAP) && _nc_strict_bsd);
874
875 c = next_char();
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530876 if (c == EOF)
877 _nc_err_abort(MSG_NO_INPUTS);
878
Steve Kondikae271bc2015-11-15 02:50:53 +0100879 if (isoctal(c) || (strict_bsd && isdigit(c))) {
880 number = c - '0';
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530881 for (i = 0; i < 2; i++) {
Steve Kondikae271bc2015-11-15 02:50:53 +0100882 c = next_char();
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530883 if (c == EOF)
884 _nc_err_abort(MSG_NO_INPUTS);
885
Steve Kondikae271bc2015-11-15 02:50:53 +0100886 if (!isoctal(c)) {
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530887 if (isdigit(c)) {
Steve Kondikae271bc2015-11-15 02:50:53 +0100888 if (!strict_bsd) {
889 _nc_warning("Non-octal digit `%c' in \\ sequence", c);
890 /* allow the digit; it'll do less harm */
891 }
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530892 } else {
Steve Kondikae271bc2015-11-15 02:50:53 +0100893 push_back(c);
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530894 break;
895 }
896 }
897
898 number = number * 8 + c - '0';
899 }
900
Steve Kondikae271bc2015-11-15 02:50:53 +0100901 number = UChar(number);
902 if (number == 0 && !strict_bsd)
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530903 number = 0200;
904 *(ptr++) = (char) number;
905 } else {
906 switch (c) {
907 case 'E':
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530908 *(ptr++) = '\033';
909 break;
910
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530911 case 'n':
912 *(ptr++) = '\n';
913 break;
914
915 case 'r':
916 *(ptr++) = '\r';
917 break;
918
919 case 'b':
920 *(ptr++) = '\010';
921 break;
922
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530923 case 'f':
924 *(ptr++) = '\014';
925 break;
926
927 case 't':
928 *(ptr++) = '\t';
929 break;
930
931 case '\\':
932 *(ptr++) = '\\';
933 break;
934
935 case '^':
936 *(ptr++) = '^';
937 break;
938
939 case ',':
940 *(ptr++) = ',';
941 break;
942
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530943 case '\n':
944 continue;
945
946 default:
Steve Kondikae271bc2015-11-15 02:50:53 +0100947 if ((_nc_syntax == SYN_TERMINFO) || !_nc_strict_bsd) {
948 switch (c) {
949 case 'a':
950 c = '\007';
951 break;
952 case 'e':
953 c = '\033';
954 break;
955 case 'l':
956 c = '\n';
957 break;
958 case 's':
959 c = ' ';
960 break;
961 case ':':
962 c = ':';
963 break;
964 default:
965 _nc_warning("Illegal character '%s' in \\ sequence",
966 unctrl(UChar(c)));
967 break;
968 }
969 }
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530970 /* FALLTHRU */
971 case '|':
Steve Kondikae271bc2015-11-15 02:50:53 +0100972 *(ptr++) = (char) c;
973 } /* endswitch (c) */
974 } /* endelse (c < '0' || c > '7') */
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530975 }
Steve Kondikae271bc2015-11-15 02:50:53 +0100976 /* end else if (c == '\\') */
977 else if (c == '\n' && (_nc_syntax == SYN_TERMINFO)) {
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530978 /*
979 * Newlines embedded in a terminfo string are ignored, provided
980 * that the next line begins with whitespace.
981 */
982 ignored = TRUE;
983 } else {
Steve Kondikae271bc2015-11-15 02:50:53 +0100984 *(ptr++) = (char) c;
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530985 }
986
987 if (!ignored) {
988 if (_nc_curr_col <= 1) {
Steve Kondikae271bc2015-11-15 02:50:53 +0100989 push_back(c);
990 c = '\n';
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530991 break;
992 }
Steve Kondikae271bc2015-11-15 02:50:53 +0100993 last_ch = c;
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530994 count++;
995 }
996 ignored = FALSE;
997
998 if (count > MAXCAPLEN && !long_warning) {
999 _nc_warning("Very long string found. Missing separator?");
1000 long_warning = TRUE;
1001 }
1002 } /* end while */
1003
1004 *ptr = '\0';
1005
Steve Kondikae271bc2015-11-15 02:50:53 +01001006 return (c);
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +05301007}
1008
1009/*
1010 * _nc_push_token()
1011 *
1012 * Push a token of given type so that it will be reread by the next
1013 * get_token() call.
1014 */
1015
1016NCURSES_EXPORT(void)
1017_nc_push_token(int tokclass)
1018{
1019 /*
1020 * This implementation is kind of bogus, it will fail if we ever do more
1021 * than one pushback at a time between get_token() calls. It relies on the
1022 * fact that _nc_curr_token is static storage that nothing but
1023 * _nc_get_token() touches.
1024 */
1025 pushtype = tokclass;
1026 if (pushname == 0)
1027 pushname = typeMalloc(char, MAX_NAME_SIZE + 1);
1028 _nc_get_type(pushname);
1029
1030 DEBUG(3, ("pushing token: `%s', class %d",
1031 ((_nc_curr_token.tk_name != 0)
1032 ? _nc_curr_token.tk_name
1033 : "<null>"),
1034 pushtype));
1035}
1036
1037/*
1038 * Panic mode error recovery - skip everything until a "ch" is found.
1039 */
1040NCURSES_EXPORT(void)
1041_nc_panic_mode(char ch)
1042{
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +05301043 for (;;) {
micky3879b9f5e72025-07-08 18:04:53 -04001044 int c = next_char();
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +05301045 if (c == ch)
1046 return;
1047 if (c == EOF)
1048 return;
1049 }
1050}
1051
1052#if NO_LEAKS
1053NCURSES_EXPORT(void)
1054_nc_comp_scan_leaks(void)
1055{
1056 if (pushname != 0) {
1057 FreeAndNull(pushname);
1058 }
Steve Kondikae271bc2015-11-15 02:50:53 +01001059 if (tok_buf != 0) {
1060 FreeAndNull(tok_buf);
1061 }
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +05301062}
1063#endif