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