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