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