+ while (1) {
+ again:
+ *p = '\0';
+ terminated = 0;
+ while (1) {
+ char c;
+ if (readpos >= readeof) {
+ ndelay_on(0);
+ eof_error = safe_read(0, readbuf, sizeof(readbuf));
+ ndelay_off(0);
+ readpos = 0;
+ readeof = eof_error;
+ if (eof_error < 0) {
+ readeof = 0;
+ if (errno != EAGAIN)
+ print_statusline("read error");
+ }
+ if (eof_error <= 0) {
+ goto reached_eof;
+ }
+ }
+ c = readbuf[readpos];
+ /* backspace? [needed for manpages] */
+ /* <tab><bs> is (a) insane and */
+ /* (b) harder to do correctly, so we refuse to do it */
+ if (c == '\x8' && linepos && p[-1] != '\t') {
+ readpos++; /* eat it */
+ linepos--;
+ *--p = '\0';
+ continue;
+ }
+ if (c == '\t')
+ linepos += (linepos^7) & 7;
+ linepos++;
+ if (linepos >= w)
+ break;
+ /* ok, we will eat this char */
+ readpos++;
+ if (c == '\n') { terminated = 1; break; }
+ /* NUL is substituted by '\n'! */
+ if (c == '\0') c = '\n';
+ *p++ = c;
+ *p = '\0';
+ }
+ /* Corner case: linewrap with only "" wrapping to next line */
+ /* Looks ugly on screen, so we do not store this empty line */
+ if (!last_terminated && !current_line[0]) {
+ last_terminated = 1;
+ max_lineno++;
+ goto again;
+ }
+ reached_eof:
+ last_terminated = terminated;
+ flines = xrealloc(flines, (max_fline+1) * sizeof(char *));
+ if (option_mask32 & FLAG_N) {
+ /* Width of 7 preserves tab spacing in the text */
+ flines[max_fline] = xasprintf(
+ (max_lineno <= 9999999) ? "%7u %s" : "%07u %s",
+ max_lineno % 10000000, current_line);
+ free(current_line);
+ if (terminated)
+ max_lineno++;
+ } else {
+ flines[max_fline] = xrealloc(current_line, strlen(current_line)+1);
+ }
+ if (max_fline >= MAXLINES)
+ break;
+ if (max_fline > cur_fline + max_displayed_line)
+ break;
+ if (eof_error <= 0) {
+ if (eof_error < 0 && errno == EAGAIN) {
+ /* not yet eof or error, reset flag (or else
+ * we will hog CPU - select() will return
+ * immediately */
+ eof_error = 1;
+ }
+ break;
+ }
+ max_fline++;
+ current_line = xmalloc(w);
+ p = current_line;
+ linepos = 0;
+ }
+ fill_match_lines(old_max_fline);
+#undef readbuf