Home | History | Annotate | Line # | Download | only in more
line.c revision 1.2
      1  1.2  perry /*	$NetBSD: line.c,v 1.2 1998/01/09 08:03:29 perry Exp $	*/
      2  1.2  perry 
      3  1.1    cjs /*
      4  1.1    cjs  * Copyright (c) 1988 Mark Nudleman
      5  1.1    cjs  * Copyright (c) 1988, 1993
      6  1.1    cjs  *	The Regents of the University of California.  All rights reserved.
      7  1.1    cjs  *
      8  1.1    cjs  * Redistribution and use in source and binary forms, with or without
      9  1.1    cjs  * modification, are permitted provided that the following conditions
     10  1.1    cjs  * are met:
     11  1.1    cjs  * 1. Redistributions of source code must retain the above copyright
     12  1.1    cjs  *    notice, this list of conditions and the following disclaimer.
     13  1.1    cjs  * 2. Redistributions in binary form must reproduce the above copyright
     14  1.1    cjs  *    notice, this list of conditions and the following disclaimer in the
     15  1.1    cjs  *    documentation and/or other materials provided with the distribution.
     16  1.1    cjs  * 3. All advertising materials mentioning features or use of this software
     17  1.1    cjs  *    must display the following acknowledgement:
     18  1.1    cjs  *	This product includes software developed by the University of
     19  1.1    cjs  *	California, Berkeley and its contributors.
     20  1.1    cjs  * 4. Neither the name of the University nor the names of its contributors
     21  1.1    cjs  *    may be used to endorse or promote products derived from this software
     22  1.1    cjs  *    without specific prior written permission.
     23  1.1    cjs  *
     24  1.1    cjs  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     25  1.1    cjs  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     26  1.1    cjs  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     27  1.1    cjs  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     28  1.1    cjs  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     29  1.1    cjs  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     30  1.1    cjs  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     31  1.1    cjs  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     32  1.1    cjs  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     33  1.1    cjs  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     34  1.1    cjs  * SUCH DAMAGE.
     35  1.1    cjs  */
     36  1.1    cjs 
     37  1.1    cjs #ifndef lint
     38  1.1    cjs static char sccsid[] = "@(#)line.c	8.1 (Berkeley) 6/6/93";
     39  1.1    cjs #endif /* not lint */
     40  1.1    cjs 
     41  1.1    cjs /*
     42  1.1    cjs  * Routines to manipulate the "line buffer".
     43  1.1    cjs  * The line buffer holds a line of output as it is being built
     44  1.1    cjs  * in preparation for output to the screen.
     45  1.1    cjs  * We keep track of the PRINTABLE length of the line as it is being built.
     46  1.1    cjs  */
     47  1.1    cjs 
     48  1.1    cjs #include <sys/types.h>
     49  1.1    cjs #include <ctype.h>
     50  1.1    cjs #include <less.h>
     51  1.1    cjs 
     52  1.1    cjs static char linebuf[1024];	/* Buffer which holds the current output line */
     53  1.1    cjs static char *curr;		/* Pointer into linebuf */
     54  1.1    cjs static int column;		/* Printable length, accounting for
     55  1.1    cjs 				   backspaces, etc. */
     56  1.1    cjs /*
     57  1.1    cjs  * A ridiculously complex state machine takes care of backspaces.  The
     58  1.1    cjs  * complexity arises from the attempt to deal with all cases, especially
     59  1.1    cjs  * involving long lines with underlining, boldfacing or whatever.  There
     60  1.1    cjs  * are still some cases which will break it.
     61  1.1    cjs  *
     62  1.1    cjs  * There are four states:
     63  1.1    cjs  *	LN_NORMAL is the normal state (not in underline mode).
     64  1.1    cjs  *	LN_UNDERLINE means we are in underline mode.  We expect to get
     65  1.1    cjs  *		either a sequence like "_\bX" or "X\b_" to continue
     66  1.1    cjs  *		underline mode, or anything else to end underline mode.
     67  1.1    cjs  *	LN_BOLDFACE means we are in boldface mode.  We expect to get sequences
     68  1.1    cjs  *		like "X\bX\b...X\bX" to continue boldface mode, or anything
     69  1.1    cjs  *		else to end boldface mode.
     70  1.1    cjs  *	LN_UL_X means we are one character after LN_UNDERLINE
     71  1.1    cjs  *		(we have gotten the '_' in "_\bX" or the 'X' in "X\b_").
     72  1.1    cjs  *	LN_UL_XB means we are one character after LN_UL_X
     73  1.1    cjs  *		(we have gotten the backspace in "_\bX" or "X\b_";
     74  1.1    cjs  *		we expect one more ordinary character,
     75  1.1    cjs  *		which will put us back in state LN_UNDERLINE).
     76  1.1    cjs  *	LN_BO_X means we are one character after LN_BOLDFACE
     77  1.1    cjs  *		(we have gotten the 'X' in "X\bX").
     78  1.1    cjs  *	LN_BO_XB means we are one character after LN_BO_X
     79  1.1    cjs  *		(we have gotten the backspace in "X\bX";
     80  1.1    cjs  *		we expect one more 'X' which will put us back
     81  1.1    cjs  *		in LN_BOLDFACE).
     82  1.1    cjs  */
     83  1.1    cjs static int ln_state;		/* Currently in normal/underline/bold/etc mode? */
     84  1.1    cjs #define	LN_NORMAL	0	/* Not in underline, boldface or whatever mode */
     85  1.1    cjs #define	LN_UNDERLINE	1	/* In underline, need next char */
     86  1.1    cjs #define	LN_UL_X		2	/* In underline, got char, need \b */
     87  1.1    cjs #define	LN_UL_XB	3	/* In underline, got char & \b, need one more */
     88  1.1    cjs #define	LN_BOLDFACE	4	/* In boldface, need next char */
     89  1.1    cjs #define	LN_BO_X		5	/* In boldface, got char, need \b */
     90  1.1    cjs #define	LN_BO_XB	6	/* In boldface, got char & \b, need same char */
     91  1.1    cjs 
     92  1.1    cjs char *line;			/* Pointer to the current line.
     93  1.1    cjs 				   Usually points to linebuf. */
     94  1.1    cjs 
     95  1.1    cjs extern int bs_mode;
     96  1.1    cjs extern int tabstop;
     97  1.1    cjs extern int bo_width, be_width;
     98  1.1    cjs extern int ul_width, ue_width;
     99  1.1    cjs extern int sc_width, sc_height;
    100  1.1    cjs 
    101  1.1    cjs /*
    102  1.1    cjs  * Rewind the line buffer.
    103  1.1    cjs  */
    104  1.1    cjs prewind()
    105  1.1    cjs {
    106  1.1    cjs 	line = curr = linebuf;
    107  1.1    cjs 	ln_state = LN_NORMAL;
    108  1.1    cjs 	column = 0;
    109  1.1    cjs }
    110  1.1    cjs 
    111  1.1    cjs /*
    112  1.1    cjs  * Append a character to the line buffer.
    113  1.1    cjs  * Expand tabs into spaces, handle underlining, boldfacing, etc.
    114  1.1    cjs  * Returns 0 if ok, 1 if couldn't fit in buffer.
    115  1.1    cjs  */
    116  1.1    cjs #define	NEW_COLUMN(addon) \
    117  1.1    cjs 	if (column + addon + (ln_state ? ue_width : 0) > sc_width) \
    118  1.1    cjs 		return(1); \
    119  1.1    cjs 	else \
    120  1.1    cjs 		column += addon
    121  1.1    cjs 
    122  1.1    cjs pappend(c)
    123  1.1    cjs 	int c;
    124  1.1    cjs {
    125  1.1    cjs 	if (c == '\0') {
    126  1.1    cjs 		/*
    127  1.1    cjs 		 * Terminate any special modes, if necessary.
    128  1.1    cjs 		 * Append a '\0' to the end of the line.
    129  1.1    cjs 		 */
    130  1.1    cjs 		switch (ln_state) {
    131  1.1    cjs 		case LN_UL_X:
    132  1.1    cjs 			curr[0] = curr[-1];
    133  1.1    cjs 			curr[-1] = UE_CHAR;
    134  1.1    cjs 			curr++;
    135  1.1    cjs 			break;
    136  1.1    cjs 		case LN_BO_X:
    137  1.1    cjs 			curr[0] = curr[-1];
    138  1.1    cjs 			curr[-1] = BE_CHAR;
    139  1.1    cjs 			curr++;
    140  1.1    cjs 			break;
    141  1.1    cjs 		case LN_UL_XB:
    142  1.1    cjs 		case LN_UNDERLINE:
    143  1.1    cjs 			*curr++ = UE_CHAR;
    144  1.1    cjs 			break;
    145  1.1    cjs 		case LN_BO_XB:
    146  1.1    cjs 		case LN_BOLDFACE:
    147  1.1    cjs 			*curr++ = BE_CHAR;
    148  1.1    cjs 			break;
    149  1.1    cjs 		}
    150  1.1    cjs 		ln_state = LN_NORMAL;
    151  1.1    cjs 		*curr = '\0';
    152  1.1    cjs 		return(0);
    153  1.1    cjs 	}
    154  1.1    cjs 
    155  1.1    cjs 	if (curr > linebuf + sizeof(linebuf) - 12)
    156  1.1    cjs 		/*
    157  1.1    cjs 		 * Almost out of room in the line buffer.
    158  1.1    cjs 		 * Don't take any chances.
    159  1.1    cjs 		 * {{ Linebuf is supposed to be big enough that this
    160  1.1    cjs 		 *    will never happen, but may need to be made
    161  1.1    cjs 		 *    bigger for wide screens or lots of backspaces. }}
    162  1.1    cjs 		 */
    163  1.1    cjs 		return(1);
    164  1.1    cjs 
    165  1.1    cjs 	if (!bs_mode) {
    166  1.1    cjs 		/*
    167  1.1    cjs 		 * Advance the state machine.
    168  1.1    cjs 		 */
    169  1.1    cjs 		switch (ln_state) {
    170  1.1    cjs 		case LN_NORMAL:
    171  1.1    cjs 			if (curr <= linebuf + 1
    172  1.1    cjs 			    || curr[-1] != (char)('H' | 0200))
    173  1.1    cjs 				break;
    174  1.1    cjs 			column -= 2;
    175  1.1    cjs 			if (c == curr[-2])
    176  1.1    cjs 				goto enter_boldface;
    177  1.1    cjs 			if (c == '_' || curr[-2] == '_')
    178  1.1    cjs 				goto enter_underline;
    179  1.1    cjs 			curr -= 2;
    180  1.1    cjs 			break;
    181  1.1    cjs 
    182  1.1    cjs enter_boldface:
    183  1.1    cjs 			/*
    184  1.1    cjs 			 * We have "X\bX" (including the current char).
    185  1.1    cjs 			 * Switch into boldface mode.
    186  1.1    cjs 			 */
    187  1.1    cjs 			column--;
    188  1.1    cjs 			if (column + bo_width + be_width + 1 >= sc_width)
    189  1.1    cjs 				/*
    190  1.1    cjs 				 * Not enough room left on the screen to
    191  1.1    cjs 				 * enter and exit boldface mode.
    192  1.1    cjs 				 */
    193  1.1    cjs 				return (1);
    194  1.1    cjs 
    195  1.1    cjs 			if (bo_width > 0 && curr > linebuf + 2
    196  1.1    cjs 			    && curr[-3] == ' ') {
    197  1.1    cjs 				/*
    198  1.1    cjs 				 * Special case for magic cookie terminals:
    199  1.1    cjs 				 * if the previous char was a space, replace
    200  1.1    cjs 				 * it with the "enter boldface" sequence.
    201  1.1    cjs 				 */
    202  1.1    cjs 				curr[-3] = BO_CHAR;
    203  1.1    cjs 				column += bo_width-1;
    204  1.1    cjs 			} else {
    205  1.1    cjs 				curr[-1] = curr[-2];
    206  1.1    cjs 				curr[-2] = BO_CHAR;
    207  1.1    cjs 				column += bo_width;
    208  1.1    cjs 				curr++;
    209  1.1    cjs 			}
    210  1.1    cjs 			goto ln_bo_xb_case;
    211  1.1    cjs 
    212  1.1    cjs enter_underline:
    213  1.1    cjs 			/*
    214  1.1    cjs 			 * We have either "_\bX" or "X\b_" (including
    215  1.1    cjs 			 * the current char).  Switch into underline mode.
    216  1.1    cjs 			 */
    217  1.1    cjs 			column--;
    218  1.1    cjs 			if (column + ul_width + ue_width + 1 >= sc_width)
    219  1.1    cjs 				/*
    220  1.1    cjs 				 * Not enough room left on the screen to
    221  1.1    cjs 				 * enter and exit underline mode.
    222  1.1    cjs 				 */
    223  1.1    cjs 				return (1);
    224  1.1    cjs 
    225  1.1    cjs 			if (ul_width > 0 &&
    226  1.1    cjs 			    curr > linebuf + 2 && curr[-3] == ' ')
    227  1.1    cjs 			{
    228  1.1    cjs 				/*
    229  1.1    cjs 				 * Special case for magic cookie terminals:
    230  1.1    cjs 				 * if the previous char was a space, replace
    231  1.1    cjs 				 * it with the "enter underline" sequence.
    232  1.1    cjs 				 */
    233  1.1    cjs 				curr[-3] = UL_CHAR;
    234  1.1    cjs 				column += ul_width-1;
    235  1.1    cjs 			} else
    236  1.1    cjs 			{
    237  1.1    cjs 				curr[-1] = curr[-2];
    238  1.1    cjs 				curr[-2] = UL_CHAR;
    239  1.1    cjs 				column += ul_width;
    240  1.1    cjs 				curr++;
    241  1.1    cjs 			}
    242  1.1    cjs 			goto ln_ul_xb_case;
    243  1.1    cjs 			/*NOTREACHED*/
    244  1.1    cjs 		case LN_UL_XB:
    245  1.1    cjs 			/*
    246  1.1    cjs 			 * Termination of a sequence "_\bX" or "X\b_".
    247  1.1    cjs 			 */
    248  1.1    cjs 			if (c != '_' && curr[-2] != '_' && c == curr[-2])
    249  1.1    cjs 			{
    250  1.1    cjs 				/*
    251  1.1    cjs 				 * We seem to have run on from underlining
    252  1.1    cjs 				 * into boldfacing - this is a nasty fix, but
    253  1.1    cjs 				 * until this whole routine is rewritten as a
    254  1.1    cjs 				 * real DFA, ...  well ...
    255  1.1    cjs 				 */
    256  1.1    cjs 				curr[0] = curr[-2];
    257  1.1    cjs 				curr[-2] = UE_CHAR;
    258  1.1    cjs 				curr[-1] = BO_CHAR;
    259  1.1    cjs 				curr += 2; /* char & non-existent backspace */
    260  1.1    cjs 				ln_state = LN_BO_XB;
    261  1.1    cjs 				goto ln_bo_xb_case;
    262  1.1    cjs 			}
    263  1.1    cjs ln_ul_xb_case:
    264  1.1    cjs 			if (c == '_')
    265  1.1    cjs 				c = curr[-2];
    266  1.1    cjs 			curr -= 2;
    267  1.1    cjs 			ln_state = LN_UNDERLINE;
    268  1.1    cjs 			break;
    269  1.1    cjs 		case LN_BO_XB:
    270  1.1    cjs 			/*
    271  1.1    cjs 			 * Termination of a sequnce "X\bX".
    272  1.1    cjs 			 */
    273  1.1    cjs 			if (c != curr[-2] && (c == '_' || curr[-2] == '_'))
    274  1.1    cjs 			{
    275  1.1    cjs 				/*
    276  1.1    cjs 				 * We seem to have run on from
    277  1.1    cjs 				 * boldfacing into underlining.
    278  1.1    cjs 				 */
    279  1.1    cjs 				curr[0] = curr[-2];
    280  1.1    cjs 				curr[-2] = BE_CHAR;
    281  1.1    cjs 				curr[-1] = UL_CHAR;
    282  1.1    cjs 				curr += 2; /* char & non-existent backspace */
    283  1.1    cjs 				ln_state = LN_UL_XB;
    284  1.1    cjs 				goto ln_ul_xb_case;
    285  1.1    cjs 			}
    286  1.1    cjs ln_bo_xb_case:
    287  1.1    cjs 			curr -= 2;
    288  1.1    cjs 			ln_state = LN_BOLDFACE;
    289  1.1    cjs 			break;
    290  1.1    cjs 		case LN_UNDERLINE:
    291  1.1    cjs 			if (column + ue_width + bo_width + 1 + be_width >= sc_width)
    292  1.1    cjs 				/*
    293  1.1    cjs 				 * We have just barely enough room to
    294  1.1    cjs 				 * exit underline mode and handle a possible
    295  1.1    cjs 				 * underline/boldface run on mixup.
    296  1.1    cjs 				 */
    297  1.1    cjs 				return (1);
    298  1.1    cjs 			ln_state = LN_UL_X;
    299  1.1    cjs 			break;
    300  1.1    cjs 		case LN_BOLDFACE:
    301  1.1    cjs 			if (c == '\b')
    302  1.1    cjs 			{
    303  1.1    cjs 				ln_state = LN_BO_XB;
    304  1.1    cjs 				break;
    305  1.1    cjs 			}
    306  1.1    cjs 			if (column + be_width + ul_width + 1 + ue_width >= sc_width)
    307  1.1    cjs 				/*
    308  1.1    cjs 				 * We have just barely enough room to
    309  1.1    cjs 				 * exit underline mode and handle a possible
    310  1.1    cjs 				 * underline/boldface run on mixup.
    311  1.1    cjs 				 */
    312  1.1    cjs 				return (1);
    313  1.1    cjs 			ln_state = LN_BO_X;
    314  1.1    cjs 			break;
    315  1.1    cjs 		case LN_UL_X:
    316  1.1    cjs 			if (c == '\b')
    317  1.1    cjs 				ln_state = LN_UL_XB;
    318  1.1    cjs 			else
    319  1.1    cjs 			{
    320  1.1    cjs 				/*
    321  1.1    cjs 				 * Exit underline mode.
    322  1.1    cjs 				 * We have to shuffle the chars a bit
    323  1.1    cjs 				 * to make this work.
    324  1.1    cjs 				 */
    325  1.1    cjs 				curr[0] = curr[-1];
    326  1.1    cjs 				curr[-1] = UE_CHAR;
    327  1.1    cjs 				column += ue_width;
    328  1.1    cjs 				if (ue_width > 0 && curr[0] == ' ')
    329  1.1    cjs 					/*
    330  1.1    cjs 					 * Another special case for magic
    331  1.1    cjs 					 * cookie terminals: if the next
    332  1.1    cjs 					 * char is a space, replace it
    333  1.1    cjs 					 * with the "exit underline" sequence.
    334  1.1    cjs 					 */
    335  1.1    cjs 					column--;
    336  1.1    cjs 				else
    337  1.1    cjs 					curr++;
    338  1.1    cjs 				ln_state = LN_NORMAL;
    339  1.1    cjs 			}
    340  1.1    cjs 			break;
    341  1.1    cjs 		case LN_BO_X:
    342  1.1    cjs 			if (c == '\b')
    343  1.1    cjs 				ln_state = LN_BO_XB;
    344  1.1    cjs 			else
    345  1.1    cjs 			{
    346  1.1    cjs 				/*
    347  1.1    cjs 				 * Exit boldface mode.
    348  1.1    cjs 				 * We have to shuffle the chars a bit
    349  1.1    cjs 				 * to make this work.
    350  1.1    cjs 				 */
    351  1.1    cjs 				curr[0] = curr[-1];
    352  1.1    cjs 				curr[-1] = BE_CHAR;
    353  1.1    cjs 				column += be_width;
    354  1.1    cjs 				if (be_width > 0 && curr[0] == ' ')
    355  1.1    cjs 					/*
    356  1.1    cjs 					 * Another special case for magic
    357  1.1    cjs 					 * cookie terminals: if the next
    358  1.1    cjs 					 * char is a space, replace it
    359  1.1    cjs 					 * with the "exit boldface" sequence.
    360  1.1    cjs 					 */
    361  1.1    cjs 					column--;
    362  1.1    cjs 				else
    363  1.1    cjs 					curr++;
    364  1.1    cjs 				ln_state = LN_NORMAL;
    365  1.1    cjs 			}
    366  1.1    cjs 			break;
    367  1.1    cjs 		}
    368  1.1    cjs 	}
    369  1.1    cjs 
    370  1.1    cjs 	if (c == '\t') {
    371  1.1    cjs 		/*
    372  1.1    cjs 		 * Expand a tab into spaces.
    373  1.1    cjs 		 */
    374  1.1    cjs 		do {
    375  1.1    cjs 			NEW_COLUMN(1);
    376  1.1    cjs 		} while ((column % tabstop) != 0);
    377  1.1    cjs 		*curr++ = '\t';
    378  1.1    cjs 		return (0);
    379  1.1    cjs 	}
    380  1.1    cjs 
    381  1.1    cjs 	if (c == '\b') {
    382  1.1    cjs 		if (ln_state == LN_NORMAL)
    383  1.1    cjs 			NEW_COLUMN(2);
    384  1.1    cjs 		else
    385  1.1    cjs 			column--;
    386  1.1    cjs 		*curr++ = ('H' | 0200);
    387  1.1    cjs 		return(0);
    388  1.1    cjs 	}
    389  1.1    cjs 
    390  1.1    cjs 	if (CONTROL_CHAR(c)) {
    391  1.1    cjs 		/*
    392  1.1    cjs 		 * Put a "^X" into the buffer.  The 0200 bit is used to tell
    393  1.1    cjs 		 * put_line() to prefix the char with a ^.  We don't actually
    394  1.1    cjs 		 * put the ^ in the buffer because we sometimes need to move
    395  1.1    cjs 		 * chars around, and such movement might separate the ^ from
    396  1.1    cjs 		 * its following character.
    397  1.1    cjs 		 */
    398  1.1    cjs 		NEW_COLUMN(2);
    399  1.1    cjs 		*curr++ = (CARAT_CHAR(c) | 0200);
    400  1.1    cjs 		return(0);
    401  1.1    cjs 	}
    402  1.1    cjs 
    403  1.1    cjs 	/*
    404  1.1    cjs 	 * Ordinary character.  Just put it in the buffer.
    405  1.1    cjs 	 */
    406  1.1    cjs 	NEW_COLUMN(1);
    407  1.1    cjs 	*curr++ = c;
    408  1.1    cjs 	return (0);
    409  1.1    cjs }
    410  1.1    cjs 
    411  1.1    cjs /*
    412  1.1    cjs  * Analogous to forw_line(), but deals with "raw lines":
    413  1.1    cjs  * lines which are not split for screen width.
    414  1.1    cjs  * {{ This is supposed to be more efficient than forw_line(). }}
    415  1.1    cjs  */
    416  1.1    cjs off_t
    417  1.1    cjs forw_raw_line(curr_pos)
    418  1.1    cjs 	off_t curr_pos;
    419  1.1    cjs {
    420  1.1    cjs 	register char *p;
    421  1.1    cjs 	register int c;
    422  1.1    cjs 	off_t new_pos, ch_tell();
    423  1.1    cjs 
    424  1.1    cjs 	if (curr_pos == NULL_POSITION || ch_seek(curr_pos) ||
    425  1.1    cjs 		(c = ch_forw_get()) == EOI)
    426  1.1    cjs 		return (NULL_POSITION);
    427  1.1    cjs 
    428  1.1    cjs 	p = linebuf;
    429  1.1    cjs 
    430  1.1    cjs 	for (;;)
    431  1.1    cjs 	{
    432  1.1    cjs 		if (c == '\n' || c == EOI)
    433  1.1    cjs 		{
    434  1.1    cjs 			new_pos = ch_tell();
    435  1.1    cjs 			break;
    436  1.1    cjs 		}
    437  1.1    cjs 		if (p >= &linebuf[sizeof(linebuf)-1])
    438  1.1    cjs 		{
    439  1.1    cjs 			/*
    440  1.1    cjs 			 * Overflowed the input buffer.
    441  1.1    cjs 			 * Pretend the line ended here.
    442  1.1    cjs 			 * {{ The line buffer is supposed to be big
    443  1.1    cjs 			 *    enough that this never happens. }}
    444  1.1    cjs 			 */
    445  1.1    cjs 			new_pos = ch_tell() - 1;
    446  1.1    cjs 			break;
    447  1.1    cjs 		}
    448  1.1    cjs 		*p++ = c;
    449  1.1    cjs 		c = ch_forw_get();
    450  1.1    cjs 	}
    451  1.1    cjs 	*p = '\0';
    452  1.1    cjs 	line = linebuf;
    453  1.1    cjs 	return (new_pos);
    454  1.1    cjs }
    455  1.1    cjs 
    456  1.1    cjs /*
    457  1.1    cjs  * Analogous to back_line(), but deals with "raw lines".
    458  1.1    cjs  * {{ This is supposed to be more efficient than back_line(). }}
    459  1.1    cjs  */
    460  1.1    cjs off_t
    461  1.1    cjs back_raw_line(curr_pos)
    462  1.1    cjs 	off_t curr_pos;
    463  1.1    cjs {
    464  1.1    cjs 	register char *p;
    465  1.1    cjs 	register int c;
    466  1.1    cjs 	off_t new_pos, ch_tell();
    467  1.1    cjs 
    468  1.1    cjs 	if (curr_pos == NULL_POSITION || curr_pos <= (off_t)0 ||
    469  1.1    cjs 		ch_seek(curr_pos-1))
    470  1.1    cjs 		return (NULL_POSITION);
    471  1.1    cjs 
    472  1.1    cjs 	p = &linebuf[sizeof(linebuf)];
    473  1.1    cjs 	*--p = '\0';
    474  1.1    cjs 
    475  1.1    cjs 	for (;;)
    476  1.1    cjs 	{
    477  1.1    cjs 		c = ch_back_get();
    478  1.1    cjs 		if (c == '\n')
    479  1.1    cjs 		{
    480  1.1    cjs 			/*
    481  1.1    cjs 			 * This is the newline ending the previous line.
    482  1.1    cjs 			 * We have hit the beginning of the line.
    483  1.1    cjs 			 */
    484  1.1    cjs 			new_pos = ch_tell() + 1;
    485  1.1    cjs 			break;
    486  1.1    cjs 		}
    487  1.1    cjs 		if (c == EOI)
    488  1.1    cjs 		{
    489  1.1    cjs 			/*
    490  1.1    cjs 			 * We have hit the beginning of the file.
    491  1.1    cjs 			 * This must be the first line in the file.
    492  1.1    cjs 			 * This must, of course, be the beginning of the line.
    493  1.1    cjs 			 */
    494  1.1    cjs 			new_pos = (off_t)0;
    495  1.1    cjs 			break;
    496  1.1    cjs 		}
    497  1.1    cjs 		if (p <= linebuf)
    498  1.1    cjs 		{
    499  1.1    cjs 			/*
    500  1.1    cjs 			 * Overflowed the input buffer.
    501  1.1    cjs 			 * Pretend the line ended here.
    502  1.1    cjs 			 */
    503  1.1    cjs 			new_pos = ch_tell() + 1;
    504  1.1    cjs 			break;
    505  1.1    cjs 		}
    506  1.1    cjs 		*--p = c;
    507  1.1    cjs 	}
    508  1.1    cjs 	line = p;
    509  1.1    cjs 	return (new_pos);
    510  1.1    cjs }
    511