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