Drop main() prototype. Syncs with NetBSD-8
[minix.git] / external / bsd / less / dist / input.c
blob579063cfa7c28ba1812704aabd8d3f10143edcdd
1 /* $NetBSD: input.c,v 1.3 2013/09/04 19:44:21 tron Exp $ */
3 /*
4 * Copyright (C) 1984-2012 Mark Nudelman
6 * You may distribute under the terms of either the GNU General Public
7 * License or the Less License, as specified in the README file.
9 * For more information, see the README file.
14 * High level routines dealing with getting lines of input
15 * from the file being viewed.
17 * When we speak of "lines" here, we mean PRINTABLE lines;
18 * lines processed with respect to the screen width.
19 * We use the term "raw line" to refer to lines simply
20 * delimited by newlines; not processed with respect to screen width.
23 #include "less.h"
25 extern int squeeze;
26 extern int chopline;
27 extern int hshift;
28 extern int quit_if_one_screen;
29 extern int sigs;
30 extern int ignore_eoi;
31 extern int status_col;
32 extern POSITION start_attnpos;
33 extern POSITION end_attnpos;
34 #if HILITE_SEARCH
35 extern int hilite_search;
36 extern int size_linebuf;
37 #endif
40 * Get the next line.
41 * A "current" position is passed and a "new" position is returned.
42 * The current position is the position of the first character of
43 * a line. The new position is the position of the first character
44 * of the NEXT line. The line obtained is the line starting at curr_pos.
46 public POSITION
47 forw_line(curr_pos)
48 POSITION curr_pos;
50 POSITION base_pos;
51 POSITION new_pos;
52 register int c;
53 int blankline;
54 int endline;
55 int backchars;
57 get_forw_line:
58 if (curr_pos == NULL_POSITION)
60 null_line();
61 return (NULL_POSITION);
63 #if HILITE_SEARCH
64 if (hilite_search == OPT_ONPLUS || is_filtering() || status_col)
66 * If we are ignoring EOI (command F), only prepare
67 * one line ahead, to avoid getting stuck waiting for
68 * slow data without displaying the data we already have.
69 * If we're not ignoring EOI, we *could* do the same, but
70 * for efficiency we prepare several lines ahead at once.
72 prep_hilite(curr_pos, curr_pos + 3*size_linebuf,
73 ignore_eoi ? 1 : -1);
74 #endif
75 if (ch_seek(curr_pos))
77 null_line();
78 return (NULL_POSITION);
82 * Step back to the beginning of the line.
84 base_pos = curr_pos;
85 for (;;)
87 if (ABORT_SIGS())
89 null_line();
90 return (NULL_POSITION);
92 c = ch_back_get();
93 if (c == EOI)
94 break;
95 if (c == '\n')
97 (void) ch_forw_get();
98 break;
100 --base_pos;
104 * Read forward again to the position we should start at.
106 prewind();
107 plinenum(base_pos);
108 (void) ch_seek(base_pos);
109 new_pos = base_pos;
110 while (new_pos < curr_pos)
112 if (ABORT_SIGS())
114 null_line();
115 return (NULL_POSITION);
117 c = ch_forw_get();
118 backchars = pappend(c, new_pos);
119 new_pos++;
120 if (backchars > 0)
122 pshift_all();
123 new_pos -= backchars;
124 while (--backchars >= 0)
125 (void) ch_back_get();
128 (void) pflushmbc();
129 pshift_all();
132 * Read the first character to display.
134 c = ch_forw_get();
135 if (c == EOI)
137 null_line();
138 return (NULL_POSITION);
140 blankline = (c == '\n' || c == '\r');
143 * Read each character in the line and append to the line buffer.
145 for (;;)
147 if (ABORT_SIGS())
149 null_line();
150 return (NULL_POSITION);
152 if (c == '\n' || c == EOI)
155 * End of the line.
157 backchars = pflushmbc();
158 new_pos = ch_tell();
159 if (backchars > 0 && !chopline && hshift == 0)
161 new_pos -= backchars + 1;
162 endline = FALSE;
163 } else
164 endline = TRUE;
165 break;
167 if (c != '\r')
168 blankline = 0;
171 * Append the char to the line and get the next char.
173 backchars = pappend(c, ch_tell()-1);
174 if (backchars > 0)
177 * The char won't fit in the line; the line
178 * is too long to print in the screen width.
179 * End the line here.
181 if (chopline || hshift > 0)
185 if (ABORT_SIGS())
187 null_line();
188 return (NULL_POSITION);
190 c = ch_forw_get();
191 } while (c != '\n' && c != EOI);
192 new_pos = ch_tell();
193 endline = TRUE;
194 quit_if_one_screen = FALSE;
195 } else
197 new_pos = ch_tell() - backchars;
198 endline = FALSE;
200 break;
202 c = ch_forw_get();
205 pdone(endline, 1);
207 #if HILITE_SEARCH
208 if (is_filtered(base_pos))
211 * We don't want to display this line.
212 * Get the next line.
214 curr_pos = new_pos;
215 goto get_forw_line;
218 if (status_col && is_hilited(base_pos, ch_tell()-1, 1, NULL))
219 set_status_col('*');
220 #endif
222 if (squeeze && blankline)
225 * This line is blank.
226 * Skip down to the last contiguous blank line
227 * and pretend it is the one which we are returning.
229 while ((c = ch_forw_get()) == '\n' || c == '\r')
230 if (ABORT_SIGS())
232 null_line();
233 return (NULL_POSITION);
235 if (c != EOI)
236 (void) ch_back_get();
237 new_pos = ch_tell();
240 return (new_pos);
244 * Get the previous line.
245 * A "current" position is passed and a "new" position is returned.
246 * The current position is the position of the first character of
247 * a line. The new position is the position of the first character
248 * of the PREVIOUS line. The line obtained is the one starting at new_pos.
250 public POSITION
251 back_line(curr_pos)
252 POSITION curr_pos;
254 POSITION new_pos, begin_new_pos, base_pos;
255 int c;
256 int endline;
257 int backchars;
259 get_back_line:
260 if (curr_pos == NULL_POSITION || curr_pos <= ch_zero())
262 null_line();
263 return (NULL_POSITION);
265 #if HILITE_SEARCH
266 if (hilite_search == OPT_ONPLUS || is_filtering() || status_col)
267 prep_hilite((curr_pos < 3*size_linebuf) ?
268 0 : curr_pos - 3*size_linebuf, curr_pos, -1);
269 #endif
270 if (ch_seek(curr_pos-1))
272 null_line();
273 return (NULL_POSITION);
276 if (squeeze)
279 * Find out if the "current" line was blank.
281 (void) ch_forw_get(); /* Skip the newline */
282 c = ch_forw_get(); /* First char of "current" line */
283 (void) ch_back_get(); /* Restore our position */
284 (void) ch_back_get();
286 if (c == '\n' || c == '\r')
289 * The "current" line was blank.
290 * Skip over any preceding blank lines,
291 * since we skipped them in forw_line().
293 while ((c = ch_back_get()) == '\n' || c == '\r')
294 if (ABORT_SIGS())
296 null_line();
297 return (NULL_POSITION);
299 if (c == EOI)
301 null_line();
302 return (NULL_POSITION);
304 (void) ch_forw_get();
309 * Scan backwards until we hit the beginning of the line.
311 for (;;)
313 if (ABORT_SIGS())
315 null_line();
316 return (NULL_POSITION);
318 c = ch_back_get();
319 if (c == '\n')
322 * This is the newline ending the previous line.
323 * We have hit the beginning of the line.
325 base_pos = ch_tell() + 1;
326 break;
328 if (c == EOI)
331 * We have hit the beginning of the file.
332 * This must be the first line in the file.
333 * This must, of course, be the beginning of the line.
335 base_pos = ch_tell();
336 break;
341 * Now scan forwards from the beginning of this line.
342 * We keep discarding "printable lines" (based on screen width)
343 * until we reach the curr_pos.
345 * {{ This algorithm is pretty inefficient if the lines
346 * are much longer than the screen width,
347 * but I don't know of any better way. }}
349 new_pos = base_pos;
350 if (ch_seek(new_pos))
352 null_line();
353 return (NULL_POSITION);
355 endline = FALSE;
356 prewind();
357 plinenum(new_pos);
358 loop:
359 begin_new_pos = new_pos;
360 (void) ch_seek(new_pos);
364 c = ch_forw_get();
365 if (c == EOI || ABORT_SIGS())
367 null_line();
368 return (NULL_POSITION);
370 new_pos++;
371 if (c == '\n')
373 backchars = pflushmbc();
374 if (backchars > 0 && !chopline && hshift == 0)
376 backchars++;
377 goto shift;
379 endline = TRUE;
380 break;
382 backchars = pappend(c, ch_tell()-1);
383 if (backchars > 0)
386 * Got a full printable line, but we haven't
387 * reached our curr_pos yet. Discard the line
388 * and start a new one.
390 if (chopline || hshift > 0)
392 endline = TRUE;
393 quit_if_one_screen = FALSE;
394 break;
396 shift:
397 pshift_all();
398 while (backchars-- > 0)
400 (void) ch_back_get();
401 new_pos--;
403 goto loop;
405 } while (new_pos < curr_pos);
407 pdone(endline, 0);
409 #if HILITE_SEARCH
410 if (is_filtered(base_pos))
413 * We don't want to display this line.
414 * Get the previous line.
416 curr_pos = begin_new_pos;
417 goto get_back_line;
420 if (status_col && curr_pos > 0 && is_hilited(base_pos, curr_pos-1, 1, NULL))
421 set_status_col('*');
422 #endif
424 return (begin_new_pos);
428 * Set attnpos.
430 public void
431 set_attnpos(pos)
432 POSITION pos;
434 int c;
436 if (pos != NULL_POSITION)
438 if (ch_seek(pos))
439 return;
440 for (;;)
442 c = ch_forw_get();
443 if (c == EOI)
444 return;
445 if (c != '\n' && c != '\r')
446 break;
447 pos++;
450 start_attnpos = pos;
451 for (;;)
453 c = ch_forw_get();
454 pos++;
455 if (c == EOI || c == '\n' || c == '\r')
456 break;
458 end_attnpos = pos;