("" < 3.4) always evaluates to true, which unconditionally
[dragonfly.git] / contrib / less-381 / input.c
bloba20ce051e04ba10c431211f0c04a77f94fe173ad
1 /*
2 * Copyright (C) 1984-2002 Mark Nudelman
4 * You may distribute under the terms of either the GNU General Public
5 * License or the Less License, as specified in the README file.
7 * For more information about less, or for information on how to
8 * contact the author, see the README file.
9 */
13 * High level routines dealing with getting lines of input
14 * from the file being viewed.
16 * When we speak of "lines" here, we mean PRINTABLE lines;
17 * lines processed with respect to the screen width.
18 * We use the term "raw line" to refer to lines simply
19 * delimited by newlines; not processed with respect to screen width.
22 #include "less.h"
24 extern int squeeze;
25 extern int chopline;
26 extern int hshift;
27 extern int quit_if_one_screen;
28 extern int sigs;
29 extern int ignore_eoi;
30 extern POSITION start_attnpos;
31 extern POSITION end_attnpos;
32 #if HILITE_SEARCH
33 extern int hilite_search;
34 extern int size_linebuf;
35 #endif
38 * Get the next line.
39 * A "current" position is passed and a "new" position is returned.
40 * The current position is the position of the first character of
41 * a line. The new position is the position of the first character
42 * of the NEXT line. The line obtained is the line starting at curr_pos.
44 public POSITION
45 forw_line(curr_pos)
46 POSITION curr_pos;
48 POSITION new_pos;
49 register int c;
50 int blankline;
51 int endline;
53 if (curr_pos == NULL_POSITION)
55 null_line();
56 return (NULL_POSITION);
58 #if HILITE_SEARCH
59 if (hilite_search == OPT_ONPLUS)
61 * If we are ignoring EOI (command F), only prepare
62 * one line ahead, to avoid getting stuck waiting for
63 * slow data without displaying the data we already have.
64 * If we're not ignoring EOI, we *could* do the same, but
65 * for efficiency we prepare several lines ahead at once.
67 prep_hilite(curr_pos, curr_pos + 3*size_linebuf,
68 ignore_eoi ? 1 : -1);
69 #endif
70 if (ch_seek(curr_pos))
72 null_line();
73 return (NULL_POSITION);
76 prewind();
77 plinenum(curr_pos);
78 (void) ch_seek(curr_pos);
80 c = ch_forw_get();
81 if (c == EOI)
83 null_line();
84 return (NULL_POSITION);
86 blankline = (c == '\n' || c == '\r');
88 for (;;)
90 if (ABORT_SIGS())
92 null_line();
93 return (NULL_POSITION);
95 if (c == '\n' || c == EOI)
98 * End of the line.
100 new_pos = ch_tell();
101 endline = TRUE;
102 break;
106 * Append the char to the line and get the next char.
108 if (pappend(c, ch_tell()-1))
111 * The char won't fit in the line; the line
112 * is too long to print in the screen width.
113 * End the line here.
115 if (chopline || hshift > 0)
119 c = ch_forw_get();
120 } while (c != '\n' && c != EOI);
121 new_pos = ch_tell();
122 endline = TRUE;
123 quit_if_one_screen = FALSE;
124 } else
126 new_pos = ch_tell() - 1;
127 endline = FALSE;
129 break;
131 c = ch_forw_get();
133 pdone(endline);
135 if (squeeze && blankline)
138 * This line is blank.
139 * Skip down to the last contiguous blank line
140 * and pretend it is the one which we are returning.
142 while ((c = ch_forw_get()) == '\n' || c == '\r')
143 if (ABORT_SIGS())
145 null_line();
146 return (NULL_POSITION);
148 if (c != EOI)
149 (void) ch_back_get();
150 new_pos = ch_tell();
153 return (new_pos);
157 * Get the previous line.
158 * A "current" position is passed and a "new" position is returned.
159 * The current position is the position of the first character of
160 * a line. The new position is the position of the first character
161 * of the PREVIOUS line. The line obtained is the one starting at new_pos.
163 public POSITION
164 back_line(curr_pos)
165 POSITION curr_pos;
167 POSITION new_pos, begin_new_pos;
168 int c;
169 int endline;
171 if (curr_pos == NULL_POSITION || curr_pos <= ch_zero())
173 null_line();
174 return (NULL_POSITION);
176 #if HILITE_SEARCH
177 if (hilite_search == OPT_ONPLUS)
178 prep_hilite((curr_pos < 3*size_linebuf) ?
179 0 : curr_pos - 3*size_linebuf, curr_pos, -1);
180 #endif
181 if (ch_seek(curr_pos-1))
183 null_line();
184 return (NULL_POSITION);
187 if (squeeze)
190 * Find out if the "current" line was blank.
192 (void) ch_forw_get(); /* Skip the newline */
193 c = ch_forw_get(); /* First char of "current" line */
194 (void) ch_back_get(); /* Restore our position */
195 (void) ch_back_get();
197 if (c == '\n' || c == '\r')
200 * The "current" line was blank.
201 * Skip over any preceding blank lines,
202 * since we skipped them in forw_line().
204 while ((c = ch_back_get()) == '\n' || c == '\r')
205 if (ABORT_SIGS())
207 null_line();
208 return (NULL_POSITION);
210 if (c == EOI)
212 null_line();
213 return (NULL_POSITION);
215 (void) ch_forw_get();
220 * Scan backwards until we hit the beginning of the line.
222 for (;;)
224 if (ABORT_SIGS())
226 null_line();
227 return (NULL_POSITION);
229 c = ch_back_get();
230 if (c == '\n')
233 * This is the newline ending the previous line.
234 * We have hit the beginning of the line.
236 new_pos = ch_tell() + 1;
237 break;
239 if (c == EOI)
242 * We have hit the beginning of the file.
243 * This must be the first line in the file.
244 * This must, of course, be the beginning of the line.
246 new_pos = ch_tell();
247 break;
252 * Now scan forwards from the beginning of this line.
253 * We keep discarding "printable lines" (based on screen width)
254 * until we reach the curr_pos.
256 * {{ This algorithm is pretty inefficient if the lines
257 * are much longer than the screen width,
258 * but I don't know of any better way. }}
260 if (ch_seek(new_pos))
262 null_line();
263 return (NULL_POSITION);
265 endline = FALSE;
266 loop:
267 begin_new_pos = new_pos;
268 prewind();
269 plinenum(new_pos);
270 (void) ch_seek(new_pos);
274 c = ch_forw_get();
275 if (c == EOI || ABORT_SIGS())
277 null_line();
278 return (NULL_POSITION);
280 new_pos++;
281 if (c == '\n')
283 endline = TRUE;
284 break;
286 if (pappend(c, ch_tell()-1))
289 * Got a full printable line, but we haven't
290 * reached our curr_pos yet. Discard the line
291 * and start a new one.
293 if (chopline || hshift > 0)
295 endline = TRUE;
296 quit_if_one_screen = FALSE;
297 break;
299 pdone(0);
300 (void) ch_back_get();
301 new_pos--;
302 goto loop;
304 } while (new_pos < curr_pos);
306 pdone(endline);
308 return (begin_new_pos);
312 * Set attnpos.
314 public void
315 set_attnpos(pos)
316 POSITION pos;
318 int c;
320 if (pos != NULL_POSITION)
322 if (ch_seek(pos))
323 return;
324 for (;;)
326 c = ch_forw_get();
327 if (c == EOI)
328 return;
329 if (c != '\n' && c != '\r')
330 break;
331 pos++;
334 start_attnpos = pos;
335 for (;;)
337 c = ch_forw_get();
338 pos++;
339 if (c == EOI || c == '\n' || c == '\r')
340 break;
342 end_attnpos = pos;