int kbd_fd; /* fd to get input from */
int less_gets_pos;
/* last position in last line, taking into account tabs */
- size_t linepos;
+ size_t last_line_pos;
unsigned max_fline;
unsigned max_lineno; /* this one tracks linewrap */
unsigned max_displayed_line;
#endif
smallint terminated;
struct termios term_orig, term_less;
+ char kbd_input[KEYCODE_BUFFER_SIZE];
};
#define G (*ptr_to_globals)
#define cur_fline (G.cur_fline )
#define kbd_fd (G.kbd_fd )
#define less_gets_pos (G.less_gets_pos )
-#define linepos (G.linepos )
+#define last_line_pos (G.last_line_pos )
#define max_fline (G.max_fline )
#define max_lineno (G.max_lineno )
#define max_displayed_line (G.max_displayed_line)
#define terminated (G.terminated )
#define term_orig (G.term_orig )
#define term_less (G.term_less )
+#define kbd_input (G.kbd_input )
#define INIT_G() do { \
SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \
less_gets_pos = -1; \
current_file = 1; \
eof_error = 1; \
terminated = 1; \
- USE_FEATURE_LESS_REGEXP(wanted_match = -1;) \
+ IF_FEATURE_LESS_REGEXP(wanted_match = -1;) \
} while (0)
/* flines[] are lines read from stdin, each in malloc'ed buffer.
exit(code);
}
-#if ENABLE_FEATURE_LESS_LINENUMS || ENABLE_FEATURE_LESS_WINCH
+#if (ENABLE_FEATURE_LESS_DASHCMD && ENABLE_FEATURE_LESS_LINENUMS) \
+ || ENABLE_FEATURE_LESS_WINCH
static void re_wrap(void)
{
int w = width;
- int rem;
+ int new_line_pos;
int src_idx;
int dst_idx;
int new_cur_fline = 0;
s = old_flines[0];
lineno = LINENO(s);
d = linebuf;
- rem = w;
+ new_line_pos = 0;
while (1) {
*d = *s;
if (*d != '\0') {
+ new_line_pos++;
+ if (*d == '\t') /* tab */
+ new_line_pos += 7;
s++;
d++;
- rem--;
- if (rem == 0) {
+ if (new_line_pos >= w) {
int sz;
/* new line is full, create next one */
*d = '\0';
new_flines = xrealloc_vector(new_flines, 8, dst_idx);
new_flines[dst_idx] = d;
dst_idx++;
- if (rem) {
- /* did we come here thru "goto next_new"? */
+ if (new_line_pos < w) {
+ /* if we came here thru "goto next_new" */
if (src_idx > max_fline)
break;
lineno = LINENO(s);
}
d = linebuf;
- rem = w;
+ new_line_pos = 0;
}
continue;
}
/* *d == NUL: old line ended, go to next old one */
free(MEMPTR(old_flines[src_idx]));
/* btw, convert cur_fline... */
- if (cur_fline == src_idx) {
+ if (cur_fline == src_idx)
new_cur_fline = dst_idx;
- }
src_idx++;
/* no more lines? finish last new line (and exit the loop) */
- if (src_idx > max_fline) {
+ if (src_idx > max_fline)
goto next_new;
- }
s = old_flines[src_idx];
if (lineno != LINENO(s)) {
/* this is not a continuation line!
flines = (const char **)new_flines;
max_fline = dst_idx - 1;
- linepos = 0; // XXX
+ last_line_pos = new_line_pos;
cur_fline = new_cur_fline;
/* max_lineno is screen-size independent */
+#if ENABLE_FEATURE_LESS_REGEXP
pattern_valid = 0;
+#endif
}
#endif
* on line wrap, only on "real" new lines.
* readbuf[0..readeof-1] - small preliminary buffer.
* readbuf[readpos] - next character to add to current line.
- * linepos - screen line position of next char to be read
+ * last_line_pos - screen line position of next char to be read
* (takes into account tabs and backspaces)
* eof_error - < 0 error, == 0 EOF, > 0 not EOF/error
*/
if (option_mask32 & FLAG_N)
w -= 8;
- USE_FEATURE_LESS_REGEXP(again0:)
+ IF_FEATURE_LESS_REGEXP(again0:)
p = current_line = ((char*)xmalloc(w + 4)) + 4;
max_fline += last_terminated;
strcpy(p, cp);
p += strlen(current_line);
free(MEMPTR(flines[max_fline]));
- /* linepos is still valid from previous read_lines() */
+ /* last_line_pos is still valid from previous read_lines() */
} else {
- linepos = 0;
+ last_line_pos = 0;
}
while (1) { /* read lines until we reach cur_fline or wanted_match */
/* 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') {
+ if (c == '\x8' && last_line_pos && p[-1] != '\t') {
readpos++; /* eat it */
- linepos--;
+ last_line_pos--;
/* was buggy (p could end up <= current_line)... */
*--p = '\0';
continue;
}
{
- size_t new_linepos = linepos + 1;
+ size_t new_last_line_pos = last_line_pos + 1;
if (c == '\t') {
- new_linepos += 7;
- new_linepos &= (~7);
+ new_last_line_pos += 7;
+ new_last_line_pos &= (~7);
}
- if ((int)new_linepos >= w)
+ if ((int)new_last_line_pos >= w)
break;
- linepos = new_linepos;
+ last_line_pos = new_last_line_pos;
}
/* ok, we will eat this char */
readpos++;
if (c == '\n') {
terminated = 1;
- linepos = 0;
+ last_line_pos = 0;
break;
}
/* NUL is substituted by '\n'! */
max_fline++;
current_line = ((char*)xmalloc(w + 4)) + 4;
p = current_line;
- linepos = 0;
+ last_line_pos = 0;
} /* end of "read lines until we reach cur_fline" loop */
fill_match_lines(old_max_fline);
#if ENABLE_FEATURE_LESS_REGEXP
while (match_status == 0) {
char *new = xasprintf("%s%.*s"HIGHLIGHT"%.*s"NORMAL,
- growline ? : "",
+ growline ? growline : "",
match_structs.rm_so, str,
match_structs.rm_eo - match_structs.rm_so,
str + match_structs.rm_so);
static void open_file_and_read_lines(void)
{
if (filename) {
- int fd = xopen(filename, O_RDONLY);
- dup2(fd, 0);
- if (fd) close(fd);
+ xmove_fd(xopen(filename, O_RDONLY), STDIN_FILENO);
} else {
/* "less" with no arguments in argv[] */
/* For status line only */
}
readpos = 0;
readeof = 0;
- linepos = 0;
+ last_line_pos = 0;
terminated = 1;
read_lines();
}
buffer_fill_and_print();
}
-static ssize_t getch_nowait(void)
+static int getch_nowait(void)
{
- char input[KEYCODE_BUFFER_SIZE];
int rd;
struct pollfd pfd[2];
if (less_gets_pos >= 0)
move_cursor(max_displayed_line + 2, less_gets_pos + 1);
fflush(stdout);
+
+ if (kbd_input[0] == 0) { /* if nothing is buffered */
#if ENABLE_FEATURE_LESS_WINCH
- while (1) {
- int r;
- /* NB: SIGWINCH interrupts poll() */
- r = poll(pfd + rd, 2 - rd, -1);
- if (/*r < 0 && errno == EINTR &&*/ winch_counter)
- return '\\'; /* anything which has no defined function */
- if (r) break;
- }
+ while (1) {
+ int r;
+ /* NB: SIGWINCH interrupts poll() */
+ r = poll(pfd + rd, 2 - rd, -1);
+ if (/*r < 0 && errno == EINTR &&*/ winch_counter)
+ return '\\'; /* anything which has no defined function */
+ if (r) break;
+ }
#else
- safe_poll(pfd + rd, 2 - rd, -1);
+ safe_poll(pfd + rd, 2 - rd, -1);
#endif
+ }
/* We have kbd_fd in O_NONBLOCK mode, read inside read_key()
* would not block even if there is no input available */
- rd = read_key(kbd_fd, NULL, input);
- if (rd == -1 && errno == EAGAIN) {
- /* No keyboard input available. Since poll() did return,
- * we should have input on stdin */
- read_lines();
- buffer_fill_and_print();
- goto again;
+ rd = read_key(kbd_fd, kbd_input);
+ if (rd == -1) {
+ if (errno == EAGAIN) {
+ /* No keyboard input available. Since poll() did return,
+ * we should have input on stdin */
+ read_lines();
+ buffer_fill_and_print();
+ goto again;
+ }
+ /* EOF/error (ssh session got killed etc) */
+ less_exit(0);
}
set_tty_cooked();
return rd;
}
-/* Grab a character from input without requiring the return key. If the
- * character is ASCII \033, get more characters and assign certain sequences
- * special return codes. Note that this function works best with raw input. */
+/* Grab a character from input without requiring the return key.
+ * May return KEYCODE_xxx values.
+ * Note that this function works best with raw input. */
static int less_getch(int pos)
{
int i;
}
}
+#ifdef BLOAT
static void show_flag_status(void)
{
int keypress;
}
#endif
+#endif /* ENABLE_FEATURE_LESS_DASHCMD */
+
static void save_input_to_file(void)
{
const char *msg = "";
flag_change();
buffer_print();
break;
+#ifdef BLOAT
case '_':
show_flag_status();
break;
#endif
+#endif
#if ENABLE_FEATURE_LESS_BRACKETS
case '{': case '(': case '[':
match_right_bracket(keypress);
/* TODO: -x: do not interpret backspace, -xx: tab also */
/* -xxx: newline also */
/* -w N: assume width N (-xxx -w 32: hex viewer of sorts) */
- getopt32(argv, "EMmN~I" USE_FEATURE_LESS_DASHCMD("S"));
+ getopt32(argv, "EMmN~I" IF_FEATURE_LESS_DASHCMD("S"));
argc -= optind;
argv += optind;
num_files = argc;