1 /* vi: set sw=4 ts=4: */
3 * Mini less implementation for busybox
5 * Copyright (C) 2005 by Rob Sullivan <cogito.ergo.cogito@gmail.com>
7 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
12 * - Add more regular expression support - search modifiers, certain matches, etc.
13 * - Add more complex bracket searching - currently, nested brackets are
15 * - Add support for "F" as an input. This causes less to act in
16 * a similar way to tail -f.
17 * - Allow horizontal scrolling.
20 * - the inp file pointer is used so that keyboard input works after
21 * redirected input has been read from stdin
28 //config: 'less' is a pager, meaning that it displays text files. It possesses
29 //config: a wide array of features, and is an improvement over 'more'.
31 //config:config FEATURE_LESS_MAXLINES
32 //config: int "Max number of input lines less will try to eat"
33 //config: default 9999999
34 //config: depends on LESS
36 //config:config FEATURE_LESS_BRACKETS
37 //config: bool "Enable bracket searching"
39 //config: depends on LESS
41 //config: This option adds the capability to search for matching left and right
42 //config: brackets, facilitating programming.
44 //config:config FEATURE_LESS_FLAGS
45 //config: bool "Enable -m/-M"
47 //config: depends on LESS
49 //config: The -M/-m flag enables a more sophisticated status line.
51 //config:config FEATURE_LESS_MARKS
52 //config: bool "Enable marks"
54 //config: depends on LESS
56 //config: Marks enable positions in a file to be stored for easy reference.
58 //config:config FEATURE_LESS_REGEXP
59 //config: bool "Enable regular expressions"
61 //config: depends on LESS
63 //config: Enable regular expressions, allowing complex file searches.
65 //config:config FEATURE_LESS_WINCH
66 //config: bool "Enable automatic resizing on window size changes"
68 //config: depends on LESS
70 //config: Makes less track window size changes.
72 //config:config FEATURE_LESS_ASK_TERMINAL
73 //config: bool "Use 'tell me cursor position' ESC sequence to measure window"
75 //config: depends on FEATURE_LESS_WINCH
77 //config: Makes less track window size changes.
78 //config: If terminal size can't be retrieved and $LINES/$COLUMNS are not set,
79 //config: this option makes less perform a last-ditch effort to find it:
80 //config: position cursor to 999,999 and ask terminal to report real
81 //config: cursor position using "ESC [ 6 n" escape sequence, then read stdin.
83 //config: This is not clean but helps a lot on serial lines and such.
85 //config:config FEATURE_LESS_DASHCMD
86 //config: bool "Enable flag changes ('-' command)"
88 //config: depends on LESS
90 //config: This enables the ability to change command-line flags within
91 //config: less itself ('-' keyboard command).
93 //config:config FEATURE_LESS_LINENUMS
94 //config: bool "Enable dynamic switching of line numbers"
96 //config: depends on FEATURE_LESS_DASHCMD
98 //config: Enables "-N" command.
100 //usage:#define less_trivial_usage
101 //usage: "[-E" IF_FEATURE_LESS_REGEXP("I")IF_FEATURE_LESS_FLAGS("Mm") "Nh~] [FILE]..."
102 //usage:#define less_full_usage "\n\n"
103 //usage: "View FILE (or stdin) one screenful at a time\n"
104 //usage: "\n -E Quit once the end of a file is reached"
105 //usage: IF_FEATURE_LESS_REGEXP(
106 //usage: "\n -I Ignore case in all searches"
108 //usage: IF_FEATURE_LESS_FLAGS(
109 //usage: "\n -M,-m Display status line with line numbers"
110 //usage: "\n and percentage through the file"
112 //usage: "\n -N Prefix line number to each line"
113 //usage: "\n -~ Suppress ~s displayed past EOF"
115 #include <sched.h> /* sched_yield() */
118 #if ENABLE_FEATURE_LESS_REGEXP
124 /* The escape codes for highlighted and normal text */
125 #define HIGHLIGHT ESC"[7m"
126 #define NORMAL ESC"[0m"
127 /* The escape code to home and clear to the end of screen */
128 #define CLEAR ESC"[H\033[J"
129 /* The escape code to clear to the end of line */
130 #define CLEAR_2_EOL ESC"[K"
133 /* Absolute max of lines eaten */
134 MAXLINES
= CONFIG_FEATURE_LESS_MAXLINES
,
135 /* This many "after the end" lines we will show (at max) */
139 /* Command line options */
147 FLAG_S
= (1 << 6) * ENABLE_FEATURE_LESS_DASHCMD
,
148 /* hijack command line options variable for internal state vars */
149 LESS_STATE_MATCH_BACKWARDS
= 1 << 15,
152 #if !ENABLE_FEATURE_LESS_REGEXP
153 enum { pattern_valid
= 0 };
157 int cur_fline
; /* signed */
158 int kbd_fd
; /* fd to get input from */
160 /* last position in last line, taking into account tabs */
161 size_t last_line_pos
;
163 unsigned max_lineno
; /* this one tracks linewrap */
164 unsigned max_displayed_line
;
166 #if ENABLE_FEATURE_LESS_WINCH
167 unsigned winch_counter
;
169 ssize_t eof_error
; /* eof if 0, error if < 0 */
171 ssize_t readeof
; /* must be signed */
174 const char *empty_line_marker
;
176 unsigned current_file
;
179 #if ENABLE_FEATURE_LESS_MARKS
181 unsigned mark_lines
[15][2];
183 #if ENABLE_FEATURE_LESS_REGEXP
184 unsigned *match_lines
;
185 int match_pos
; /* signed! */
186 int wanted_match
; /* signed! */
189 smallint pattern_valid
;
191 #if ENABLE_FEATURE_LESS_ASK_TERMINAL
192 smallint winsize_err
;
195 struct termios term_orig
, term_less
;
196 char kbd_input
[KEYCODE_BUFFER_SIZE
];
198 #define G (*ptr_to_globals)
199 #define cur_fline (G.cur_fline )
200 #define kbd_fd (G.kbd_fd )
201 #define less_gets_pos (G.less_gets_pos )
202 #define last_line_pos (G.last_line_pos )
203 #define max_fline (G.max_fline )
204 #define max_lineno (G.max_lineno )
205 #define max_displayed_line (G.max_displayed_line)
206 #define width (G.width )
207 #define winch_counter (G.winch_counter )
208 /* This one is 100% not cached by compiler on read access */
209 #define WINCH_COUNTER (*(volatile unsigned *)&winch_counter)
210 #define eof_error (G.eof_error )
211 #define readpos (G.readpos )
212 #define readeof (G.readeof )
213 #define buffer (G.buffer )
214 #define flines (G.flines )
215 #define empty_line_marker (G.empty_line_marker )
216 #define num_files (G.num_files )
217 #define current_file (G.current_file )
218 #define filename (G.filename )
219 #define files (G.files )
220 #define num_marks (G.num_marks )
221 #define mark_lines (G.mark_lines )
222 #if ENABLE_FEATURE_LESS_REGEXP
223 #define match_lines (G.match_lines )
224 #define match_pos (G.match_pos )
225 #define num_matches (G.num_matches )
226 #define wanted_match (G.wanted_match )
227 #define pattern (G.pattern )
228 #define pattern_valid (G.pattern_valid )
230 #define terminated (G.terminated )
231 #define term_orig (G.term_orig )
232 #define term_less (G.term_less )
233 #define kbd_input (G.kbd_input )
234 #define INIT_G() do { \
235 SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \
236 less_gets_pos = -1; \
237 empty_line_marker = "~"; \
242 IF_FEATURE_LESS_REGEXP(wanted_match = -1;) \
245 /* flines[] are lines read from stdin, each in malloc'ed buffer.
246 * Line numbers are stored as uint32_t prepended to each line.
247 * Pointer is adjusted so that flines[i] points directly past
248 * line number. Accesor: */
249 #define MEMPTR(p) ((char*)(p) - 4)
250 #define LINENO(p) (*(uint32_t*)((p) - 4))
253 /* Reset terminal input to normal */
254 static void set_tty_cooked(void)
257 tcsetattr(kbd_fd
, TCSANOW
, &term_orig
);
260 /* Move the cursor to a position (x,y), where (0,0) is the
261 top-left corner of the console */
262 static void move_cursor(int line
, int row
)
264 printf(ESC
"[%u;%uH", line
, row
);
267 static void clear_line(void)
269 printf(ESC
"[%u;0H" CLEAR_2_EOL
, max_displayed_line
+ 2);
272 static void print_hilite(const char *str
)
274 printf(HIGHLIGHT
"%s"NORMAL
, str
);
277 static void print_statusline(const char *str
)
280 printf(HIGHLIGHT
"%.*s"NORMAL
, width
- 1, str
);
283 /* Exit the program gracefully */
284 static void less_exit(int code
)
289 kill_myself_with_sig(- code
); /* does not return */
293 #if (ENABLE_FEATURE_LESS_DASHCMD && ENABLE_FEATURE_LESS_LINENUMS) \
294 || ENABLE_FEATURE_LESS_WINCH
295 static void re_wrap(void)
301 int new_cur_fline
= 0;
304 const char **old_flines
= flines
;
306 char **new_flines
= NULL
;
309 if (option_mask32
& FLAG_N
)
322 if (*d
== '\t') /* tab */
326 if (new_line_pos
>= w
) {
328 /* new line is full, create next one */
331 sz
= (d
- linebuf
) + 1; /* + 1: NUL */
332 d
= ((char*)xmalloc(sz
+ 4)) + 4;
334 memcpy(d
, linebuf
, sz
);
335 new_flines
= xrealloc_vector(new_flines
, 8, dst_idx
);
336 new_flines
[dst_idx
] = d
;
338 if (new_line_pos
< w
) {
339 /* if we came here thru "goto next_new" */
340 if (src_idx
> max_fline
)
349 /* *d == NUL: old line ended, go to next old one */
350 free(MEMPTR(old_flines
[src_idx
]));
351 /* btw, convert cur_fline... */
352 if (cur_fline
== src_idx
)
353 new_cur_fline
= dst_idx
;
355 /* no more lines? finish last new line (and exit the loop) */
356 if (src_idx
> max_fline
)
358 s
= old_flines
[src_idx
];
359 if (lineno
!= LINENO(s
)) {
360 /* this is not a continuation line!
361 * create next _new_ line too */
367 flines
= (const char **)new_flines
;
369 max_fline
= dst_idx
- 1;
370 last_line_pos
= new_line_pos
;
371 cur_fline
= new_cur_fline
;
372 /* max_lineno is screen-size independent */
373 #if ENABLE_FEATURE_LESS_REGEXP
379 #if ENABLE_FEATURE_LESS_REGEXP
380 static void fill_match_lines(unsigned pos
);
382 #define fill_match_lines(pos) ((void)0)
385 /* Devilishly complex routine.
387 * Has to deal with EOF and EPIPE on input,
388 * with line wrapping, with last line not ending in '\n'
389 * (possibly not ending YET!), with backspace and tabs.
390 * It reads input again if last time we got an EOF (thus supporting
391 * growing files) or EPIPE (watching output of slow process like make).
394 * flines[] - array of lines already read. Linewrap may cause
395 * one source file line to occupy several flines[n].
396 * flines[max_fline] - last line, possibly incomplete.
397 * terminated - 1 if flines[max_fline] is 'terminated'
398 * (if there was '\n' [which isn't stored itself, we just remember
400 * max_lineno - last line's number, this one doesn't increment
401 * on line wrap, only on "real" new lines.
402 * readbuf[0..readeof-1] - small preliminary buffer.
403 * readbuf[readpos] - next character to add to current line.
404 * last_line_pos - screen line position of next char to be read
405 * (takes into account tabs and backspaces)
406 * eof_error - < 0 error, == 0 EOF, > 0 not EOF/error
408 * "git log -p | less -m" on the kernel git tree is a good test for EAGAINs,
409 * "/search on very long input" and "reaching max line count" corner cases.
411 static void read_lines(void)
413 #define readbuf bb_common_bufsiz1
414 char *current_line
, *p
;
416 char last_terminated
= terminated
;
417 time_t last_time
= 0;
418 int retry_EAGAIN
= 2;
419 #if ENABLE_FEATURE_LESS_REGEXP
420 unsigned old_max_fline
= max_fline
;
423 /* (careful: max_fline can be -1) */
424 if (max_fline
+ 1 > MAXLINES
)
427 if (option_mask32
& FLAG_N
)
430 p
= current_line
= ((char*)xmalloc(w
+ 4)) + 4;
431 if (!last_terminated
) {
432 const char *cp
= flines
[max_fline
];
435 /* last_line_pos is still valid from previous read_lines() */
441 while (1) { /* read lines until we reach cur_fline or wanted_match */
444 while (1) { /* read chars until we have a line */
446 /* if no unprocessed chars left, eat more */
447 if (readpos
>= readeof
) {
448 int flags
= ndelay_on(0);
454 eof_error
= safe_read(STDIN_FILENO
, readbuf
, sizeof(readbuf
));
458 if (t
!= last_time
) {
460 if (--retry_EAGAIN
< 0)
465 fcntl(0, F_SETFL
, flags
); /* ndelay_off(0) */
472 c
= readbuf
[readpos
];
473 /* backspace? [needed for manpages] */
474 /* <tab><bs> is (a) insane and */
475 /* (b) harder to do correctly, so we refuse to do it */
476 if (c
== '\x8' && last_line_pos
&& p
[-1] != '\t') {
477 readpos
++; /* eat it */
479 /* was buggy (p could end up <= current_line)... */
484 size_t new_last_line_pos
= last_line_pos
+ 1;
486 new_last_line_pos
+= 7;
487 new_last_line_pos
&= (~7);
489 if ((int)new_last_line_pos
>= w
)
491 last_line_pos
= new_last_line_pos
;
493 /* ok, we will eat this char */
500 /* NUL is substituted by '\n'! */
501 if (c
== '\0') c
= '\n';
504 } /* end of "read chars until we have a line" loop */
506 //BUG: also triggers on this:
507 // { printf "\nfoo\n"; sleep 1; printf "\nbar\n"; } | less
508 // (resulting in lost empty line between "foo" and "bar" lines)
509 // the "terminated" logic needs fixing (or explaining)
510 /* Corner case: linewrap with only "" wrapping to next line */
511 /* Looks ugly on screen, so we do not store this empty line */
512 if (!last_terminated
&& !current_line
[0]) {
519 last_terminated
= terminated
;
520 flines
= xrealloc_vector(flines
, 8, max_fline
);
522 flines
[max_fline
] = (char*)xrealloc(MEMPTR(current_line
), strlen(current_line
) + 1 + 4) + 4;
523 LINENO(flines
[max_fline
]) = max_lineno
;
527 if (max_fline
>= MAXLINES
) {
528 eof_error
= 0; /* Pretend we saw EOF */
531 if (!(option_mask32
& FLAG_S
)
532 ? (max_fline
> cur_fline
+ max_displayed_line
)
533 : (max_fline
>= cur_fline
534 && max_lineno
> LINENO(flines
[cur_fline
]) + max_displayed_line
)
536 #if !ENABLE_FEATURE_LESS_REGEXP
539 if (wanted_match
>= num_matches
) { /* goto_match called us */
540 fill_match_lines(old_max_fline
);
541 old_max_fline
= max_fline
;
543 if (wanted_match
< num_matches
)
547 if (eof_error
<= 0) {
551 current_line
= ((char*)xmalloc(w
+ 4)) + 4;
554 } /* end of "read lines until we reach cur_fline" loop */
557 if (errno
== EAGAIN
) {
560 print_statusline(bb_msg_read_error
);
564 fill_match_lines(old_max_fline
);
565 #if ENABLE_FEATURE_LESS_REGEXP
566 /* prevent us from being stuck in search for a match */
572 #if ENABLE_FEATURE_LESS_FLAGS
573 /* Interestingly, writing calc_percent as a function saves around 32 bytes
575 static int calc_percent(void)
577 unsigned p
= (100 * (cur_fline
+max_displayed_line
+1) + max_fline
/2) / (max_fline
+1);
578 return p
<= 100 ? p
: 100;
581 /* Print a status line if -M was specified */
582 static void m_status_print(void)
586 if (less_gets_pos
>= 0) /* don't touch statusline while input is done! */
590 printf(HIGHLIGHT
"%s", filename
);
592 printf(" (file %i of %i)", current_file
, num_files
);
593 printf(" lines %i-%i/%i ",
594 cur_fline
+ 1, cur_fline
+ max_displayed_line
+ 1,
596 if (cur_fline
>= (int)(max_fline
- max_displayed_line
)) {
597 printf("(END)"NORMAL
);
598 if (num_files
> 1 && current_file
!= num_files
)
599 printf(HIGHLIGHT
" - next: %s"NORMAL
, files
[current_file
]);
602 percentage
= calc_percent();
603 printf("%i%%"NORMAL
, percentage
);
607 /* Print the status line */
608 static void status_print(void)
612 if (less_gets_pos
>= 0) /* don't touch statusline while input is done! */
615 /* Change the status if flags have been set */
616 #if ENABLE_FEATURE_LESS_FLAGS
617 if (option_mask32
& (FLAG_M
|FLAG_m
)) {
625 if (cur_fline
&& cur_fline
< (int)(max_fline
- max_displayed_line
)) {
633 printf(HIGHLIGHT
"%s (file %i of %i)"NORMAL
,
634 p
, current_file
, num_files
);
640 static void cap_cur_fline(int nlines
)
645 if (cur_fline
+ max_displayed_line
> max_fline
+ TILDES
) {
649 diff
= max_fline
- (cur_fline
+ max_displayed_line
) + TILDES
;
650 /* As the number of lines requested was too large, we just move
651 * to the end of the file */
657 static const char controls
[] ALIGN1
=
658 /* NUL: never encountered; TAB: not converted */
659 /**/"\x01\x02\x03\x04\x05\x06\x07\x08" "\x0a\x0b\x0c\x0d\x0e\x0f"
660 "\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f"
661 "\x7f\x9b"; /* DEL and infamous Meta-ESC :( */
662 static const char ctrlconv
[] ALIGN1
=
663 /* why 40 instead of 4a below? - it is a replacement for '\n'.
664 * '\n' is a former NUL - we subst it with @, not J */
665 "\x40\x41\x42\x43\x44\x45\x46\x47\x48\x49\x40\x4b\x4c\x4d\x4e\x4f"
666 "\x50\x51\x52\x53\x54\x55\x56\x57\x58\x59\x5a\x5b\x5c\x5d\x5e\x5f";
668 static void lineno_str(char *nbuf9
, const char *line
)
671 if (option_mask32
& FLAG_N
) {
675 if (line
== empty_line_marker
) {
676 memset(nbuf9
, ' ', 8);
680 /* Width of 7 preserves tab spacing in the text */
682 n
= LINENO(line
) + 1;
687 sprintf(nbuf9
, fmt
, n
);
692 #if ENABLE_FEATURE_LESS_REGEXP
693 static void print_found(const char *line
)
698 regmatch_t match_structs
;
702 const char *str
= line
;
707 n
= strcspn(str
, controls
);
714 n
= strspn(str
, controls
);
721 /* buf[] holds quarantined version of str */
723 /* Each part of the line that matches has the HIGHLIGHT
724 * and NORMAL escape sequences placed around it.
725 * NB: we regex against line, but insert text
726 * from quarantined copy (buf[]) */
732 while (match_status
== 0) {
733 char *new = xasprintf("%s%.*s"HIGHLIGHT
"%.*s"NORMAL
,
734 growline
? growline
: "",
735 (int)match_structs
.rm_so
, str
,
736 (int)(match_structs
.rm_eo
- match_structs
.rm_so
),
737 str
+ match_structs
.rm_so
);
740 str
+= match_structs
.rm_eo
;
741 line
+= match_structs
.rm_eo
;
744 /* Most of the time doesn't find the regex, optimize for that */
745 match_status
= regexec(&pattern
, line
, 1, &match_structs
, eflags
);
746 /* if even "" matches, treat it as "not a match" */
747 if (match_structs
.rm_so
>= match_structs
.rm_eo
)
751 lineno_str(nbuf9
, line
);
753 printf(CLEAR_2_EOL
"%s%s\n", nbuf9
, str
);
756 printf(CLEAR_2_EOL
"%s%s%s\n", nbuf9
, growline
, str
);
760 void print_found(const char *line
);
763 static void print_ascii(const char *str
)
770 lineno_str(nbuf9
, str
);
771 printf(CLEAR_2_EOL
"%s", nbuf9
);
774 n
= strcspn(str
, controls
);
777 printf("%.*s", (int) n
, str
);
780 n
= strspn(str
, controls
);
785 else if (*str
== (char)0x9b)
786 /* VT100's CSI, aka Meta-ESC. Who's inventor? */
787 /* I want to know who committed this sin */
790 *p
++ = ctrlconv
[(unsigned char)*str
];
799 /* Print the buffer */
800 static void buffer_print(void)
805 for (i
= 0; i
<= max_displayed_line
; i
++) {
807 print_found(buffer
[i
]);
809 print_ascii(buffer
[i
]);
811 if ((option_mask32
& FLAG_E
)
813 && (max_fline
- cur_fline
) <= max_displayed_line
815 less_exit(EXIT_SUCCESS
);
820 static void buffer_fill_and_print(void)
823 #if ENABLE_FEATURE_LESS_DASHCMD
824 int fpos
= cur_fline
;
826 if (option_mask32
& FLAG_S
) {
827 /* Go back to the beginning of this line */
828 while (fpos
&& LINENO(flines
[fpos
]) == LINENO(flines
[fpos
-1]))
833 while (i
<= max_displayed_line
&& fpos
<= max_fline
) {
834 int lineno
= LINENO(flines
[fpos
]);
835 buffer
[i
] = flines
[fpos
];
839 } while ((fpos
<= max_fline
)
840 && (option_mask32
& FLAG_S
)
841 && lineno
== LINENO(flines
[fpos
])
845 for (i
= 0; i
<= max_displayed_line
&& cur_fline
+ i
<= max_fline
; i
++) {
846 buffer
[i
] = flines
[cur_fline
+ i
];
849 for (; i
<= max_displayed_line
; i
++) {
850 buffer
[i
] = empty_line_marker
;
855 /* Move the buffer up and down in the file in order to scroll */
856 static void buffer_down(int nlines
)
860 cap_cur_fline(nlines
);
861 buffer_fill_and_print();
864 static void buffer_up(int nlines
)
867 if (cur_fline
< 0) cur_fline
= 0;
869 buffer_fill_and_print();
872 static void buffer_line(int linenum
)
878 if (linenum
+ max_displayed_line
> max_fline
)
879 linenum
= max_fline
- max_displayed_line
+ TILDES
;
883 buffer_fill_and_print();
886 static void open_file_and_read_lines(void)
889 xmove_fd(xopen(filename
, O_RDONLY
), STDIN_FILENO
);
891 /* "less" with no arguments in argv[] */
892 /* For status line only */
893 filename
= xstrdup(bb_msg_standard_input
);
902 /* Reinitialize everything for a new file - free the memory and start over */
903 static void reinitialize(void)
908 for (i
= 0; i
<= max_fline
; i
++)
909 free(MEMPTR(flines
[i
]));
917 open_file_and_read_lines();
918 #if ENABLE_FEATURE_LESS_ASK_TERMINAL
920 printf("\033[999;999H" "\033[6n");
922 buffer_fill_and_print();
925 static int64_t getch_nowait(void)
929 struct pollfd pfd
[2];
931 pfd
[0].fd
= STDIN_FILENO
;
932 pfd
[0].events
= POLLIN
;
934 pfd
[1].events
= POLLIN
;
936 tcsetattr(kbd_fd
, TCSANOW
, &term_less
);
937 /* NB: select/poll returns whenever read will not block. Therefore:
938 * if eof is reached, select/poll will return immediately
939 * because read will immediately return 0 bytes.
940 * Even if select/poll says that input is available, read CAN block
941 * (switch fd into O_NONBLOCK'ed mode to avoid it)
944 /* Are we interested in stdin? */
945 //TODO: reuse code for determining this
946 if (!(option_mask32
& FLAG_S
)
947 ? !(max_fline
> cur_fline
+ max_displayed_line
)
948 : !(max_fline
>= cur_fline
949 && max_lineno
> LINENO(flines
[cur_fline
]) + max_displayed_line
)
951 if (eof_error
> 0) /* did NOT reach eof yet */
952 rd
= 0; /* yes, we are interested in stdin */
954 /* Position cursor if line input is done */
955 if (less_gets_pos
>= 0)
956 move_cursor(max_displayed_line
+ 2, less_gets_pos
+ 1);
959 if (kbd_input
[0] == 0) { /* if nothing is buffered */
960 #if ENABLE_FEATURE_LESS_WINCH
963 /* NB: SIGWINCH interrupts poll() */
964 r
= poll(pfd
+ rd
, 2 - rd
, -1);
965 if (/*r < 0 && errno == EINTR &&*/ winch_counter
)
966 return '\\'; /* anything which has no defined function */
970 safe_poll(pfd
+ rd
, 2 - rd
, -1);
974 /* We have kbd_fd in O_NONBLOCK mode, read inside read_key()
975 * would not block even if there is no input available */
976 key64
= read_key(kbd_fd
, kbd_input
, /*timeout off:*/ -2);
977 if ((int)key64
== -1) {
978 if (errno
== EAGAIN
) {
979 /* No keyboard input available. Since poll() did return,
980 * we should have input on stdin */
982 buffer_fill_and_print();
985 /* EOF/error (ssh session got killed etc) */
992 /* Grab a character from input without requiring the return key.
993 * May return KEYCODE_xxx values.
994 * Note that this function works best with raw input. */
995 static int64_t less_getch(int pos
)
1001 less_gets_pos
= pos
;
1002 key
= key64
= getch_nowait();
1005 /* Discard Ctrl-something chars.
1006 * (checking only lower 32 bits is a size optimization:
1007 * upper 32 bits are used only by KEYCODE_CURSOR_POS)
1009 if (key
>= 0 && key
< ' ' && key
!= 0x0d && key
!= 8)
1015 static char* less_gets(int sz
)
1019 char *result
= xzalloc(1);
1023 less_gets_pos
= sz
+ i
;
1036 if (c
< ' ') /* filters out KEYCODE_xxx too (<0) */
1038 if (i
>= width
- sz
- 1)
1039 continue; /* len limit */
1042 result
= xrealloc(result
, i
+1);
1046 static void examine_file(void)
1050 print_statusline("Examine: ");
1051 new_fname
= less_gets(sizeof("Examine: ") - 1);
1052 if (!new_fname
[0]) {
1058 if (access(new_fname
, R_OK
) != 0) {
1059 print_statusline("Cannot read this file");
1063 filename
= new_fname
;
1064 /* files start by = argv. why we assume that argv is infinitely long??
1065 files[num_files] = filename;
1066 current_file = num_files + 1;
1068 files
[0] = filename
;
1069 num_files
= current_file
= 1;
1073 /* This function changes the file currently being paged. direction can be one of the following:
1074 * -1: go back one file
1075 * 0: go to the first file
1076 * 1: go forward one file */
1077 static void change_file(int direction
)
1079 if (current_file
!= ((direction
> 0) ? num_files
: 1)) {
1080 current_file
= direction
? current_file
+ direction
: 1;
1082 filename
= xstrdup(files
[current_file
- 1]);
1085 print_statusline(direction
> 0 ? "No next file" : "No previous file");
1089 static void remove_current_file(void)
1096 if (current_file
!= 1) {
1098 for (i
= 3; i
<= num_files
; i
++)
1099 files
[i
- 2] = files
[i
- 1];
1103 for (i
= 2; i
<= num_files
; i
++)
1104 files
[i
- 2] = files
[i
- 1];
1110 static void colon_process(void)
1114 /* Clear the current line and print a prompt */
1115 print_statusline(" :");
1117 keypress
= less_getch(2);
1120 remove_current_file();
1125 #if ENABLE_FEATURE_LESS_FLAGS
1137 less_exit(EXIT_SUCCESS
);
1145 #if ENABLE_FEATURE_LESS_REGEXP
1146 static void normalize_match_pos(int match
)
1148 if (match
>= num_matches
)
1149 match
= num_matches
- 1;
1155 static void goto_match(int match
)
1161 /* Try to find next match if eof isn't reached yet */
1162 if (match
>= num_matches
&& eof_error
> 0) {
1163 wanted_match
= match
; /* "I want to read until I see N'th match" */
1167 normalize_match_pos(match
);
1168 buffer_line(match_lines
[match_pos
]);
1170 print_statusline("No matches found");
1174 static void fill_match_lines(unsigned pos
)
1178 /* Run the regex on each line of the current file */
1179 while (pos
<= max_fline
) {
1180 /* If this line matches */
1181 if (regexec(&pattern
, flines
[pos
], 0, NULL
, 0) == 0
1182 /* and we didn't match it last time */
1183 && !(num_matches
&& match_lines
[num_matches
-1] == pos
)
1185 match_lines
= xrealloc_vector(match_lines
, 4, num_matches
);
1186 match_lines
[num_matches
++] = pos
;
1192 static void regex_process(void)
1194 char *uncomp_regex
, *err
;
1196 /* Reset variables */
1201 if (pattern_valid
) {
1206 /* Get the uncompiled regular expression from the user */
1208 bb_putchar((option_mask32
& LESS_STATE_MATCH_BACKWARDS
) ? '?' : '/');
1209 uncomp_regex
= less_gets(1);
1210 if (!uncomp_regex
[0]) {
1216 /* Compile the regex and check for errors */
1217 err
= regcomp_or_errmsg(&pattern
, uncomp_regex
,
1218 (option_mask32
& FLAG_I
) ? REG_ICASE
: 0);
1221 print_statusline(err
);
1228 fill_match_lines(0);
1229 while (match_pos
< num_matches
) {
1230 if ((int)match_lines
[match_pos
] > cur_fline
)
1234 if (option_mask32
& LESS_STATE_MATCH_BACKWARDS
)
1237 /* It's possible that no matches are found yet.
1238 * goto_match() will read input looking for match,
1240 goto_match(match_pos
);
1244 static void number_process(int first_digit
)
1249 char num_input
[sizeof(int)*4]; /* more than enough */
1251 num_input
[0] = first_digit
;
1253 /* Clear the current line, print a prompt, and then print the digit */
1255 printf(":%c", first_digit
);
1257 /* Receive input until a letter is given */
1259 while (i
< sizeof(num_input
)-1) {
1260 keypress
= less_getch(i
+ 1);
1261 if ((unsigned)keypress
> 255 || !isdigit(num_input
[i
]))
1263 num_input
[i
] = keypress
;
1264 bb_putchar(keypress
);
1268 num_input
[i
] = '\0';
1269 num
= bb_strtou(num_input
, NULL
, 10);
1270 /* on format error, num == -1 */
1271 if (num
< 1 || num
> MAXLINES
) {
1276 /* We now know the number and the letter entered, so we process them */
1278 case KEYCODE_DOWN
: case 'z': case 'd': case 'e': case ' ': case '\015':
1281 case KEYCODE_UP
: case 'b': case 'w': case 'y': case 'u':
1284 case 'g': case '<': case 'G': case '>':
1285 cur_fline
= num
+ max_displayed_line
;
1287 buffer_line(num
- 1);
1290 num
= num
* (max_fline
/ 100); /* + max_fline / 2; */
1291 cur_fline
= num
+ max_displayed_line
;
1295 #if ENABLE_FEATURE_LESS_REGEXP
1297 goto_match(match_pos
+ num
);
1300 option_mask32
&= ~LESS_STATE_MATCH_BACKWARDS
;
1304 option_mask32
|= LESS_STATE_MATCH_BACKWARDS
;
1311 #if ENABLE_FEATURE_LESS_DASHCMD
1312 static void flag_change(void)
1318 keypress
= less_getch(1);
1322 option_mask32
^= FLAG_M
;
1325 option_mask32
^= FLAG_m
;
1328 option_mask32
^= FLAG_E
;
1331 option_mask32
^= FLAG_TILDE
;
1334 option_mask32
^= FLAG_S
;
1335 buffer_fill_and_print();
1337 #if ENABLE_FEATURE_LESS_LINENUMS
1339 option_mask32
^= FLAG_N
;
1341 buffer_fill_and_print();
1348 static void show_flag_status(void)
1355 keypress
= less_getch(1);
1359 flag_val
= option_mask32
& FLAG_M
;
1362 flag_val
= option_mask32
& FLAG_m
;
1365 flag_val
= option_mask32
& FLAG_TILDE
;
1368 flag_val
= option_mask32
& FLAG_N
;
1371 flag_val
= option_mask32
& FLAG_E
;
1379 printf(HIGHLIGHT
"The status of the flag is: %u"NORMAL
, flag_val
!= 0);
1383 #endif /* ENABLE_FEATURE_LESS_DASHCMD */
1385 static void save_input_to_file(void)
1387 const char *msg
= "";
1392 print_statusline("Log file: ");
1393 current_line
= less_gets(sizeof("Log file: ")-1);
1394 if (current_line
[0]) {
1395 fp
= fopen_for_write(current_line
);
1397 msg
= "Error opening log file";
1400 for (i
= 0; i
<= max_fline
; i
++)
1401 fprintf(fp
, "%s\n", flines
[i
]);
1406 print_statusline(msg
);
1410 #if ENABLE_FEATURE_LESS_MARKS
1411 static void add_mark(void)
1415 print_statusline("Mark: ");
1416 letter
= less_getch(sizeof("Mark: ") - 1);
1418 if (isalpha(letter
)) {
1419 /* If we exceed 15 marks, start overwriting previous ones */
1420 if (num_marks
== 14)
1423 mark_lines
[num_marks
][0] = letter
;
1424 mark_lines
[num_marks
][1] = cur_fline
;
1427 print_statusline("Invalid mark letter");
1431 static void goto_mark(void)
1436 print_statusline("Go to mark: ");
1437 letter
= less_getch(sizeof("Go to mark: ") - 1);
1440 if (isalpha(letter
)) {
1441 for (i
= 0; i
<= num_marks
; i
++)
1442 if (letter
== mark_lines
[i
][0]) {
1443 buffer_line(mark_lines
[i
][1]);
1446 if (num_marks
== 14 && letter
!= mark_lines
[14][0])
1447 print_statusline("Mark not set");
1449 print_statusline("Invalid mark letter");
1453 #if ENABLE_FEATURE_LESS_BRACKETS
1454 static char opp_bracket(char bracket
)
1457 case '{': case '[': /* '}' == '{' + 2. Same for '[' */
1459 case '(': /* ')' == '(' + 1 */
1471 static void match_right_bracket(char bracket
)
1475 if (strchr(flines
[cur_fline
], bracket
) == NULL
) {
1476 print_statusline("No bracket in top line");
1479 bracket
= opp_bracket(bracket
);
1480 for (i
= cur_fline
+ 1; i
< max_fline
; i
++) {
1481 if (strchr(flines
[i
], bracket
) != NULL
) {
1486 print_statusline("No matching bracket found");
1489 static void match_left_bracket(char bracket
)
1493 if (strchr(flines
[cur_fline
+ max_displayed_line
], bracket
) == NULL
) {
1494 print_statusline("No bracket in bottom line");
1498 bracket
= opp_bracket(bracket
);
1499 for (i
= cur_fline
+ max_displayed_line
; i
>= 0; i
--) {
1500 if (strchr(flines
[i
], bracket
) != NULL
) {
1505 print_statusline("No matching bracket found");
1507 #endif /* FEATURE_LESS_BRACKETS */
1509 static void keypress_process(int keypress
)
1512 case KEYCODE_DOWN
: case 'e': case 'j': case 0x0d:
1515 case KEYCODE_UP
: case 'y': case 'k':
1518 case KEYCODE_PAGEDOWN
: case ' ': case 'z': case 'f':
1519 buffer_down(max_displayed_line
+ 1);
1521 case KEYCODE_PAGEUP
: case 'w': case 'b':
1522 buffer_up(max_displayed_line
+ 1);
1525 buffer_down((max_displayed_line
+ 1) / 2);
1528 buffer_up((max_displayed_line
+ 1) / 2);
1530 case KEYCODE_HOME
: case 'g': case 'p': case '<': case '%':
1533 case KEYCODE_END
: case 'G': case '>':
1534 cur_fline
= MAXLINES
;
1536 buffer_line(cur_fline
);
1539 less_exit(EXIT_SUCCESS
);
1541 #if ENABLE_FEATURE_LESS_MARKS
1552 /* TODO: (1) also bind ^R, ^L to this?
1553 * (2) re-measure window size?
1561 save_input_to_file();
1566 #if ENABLE_FEATURE_LESS_FLAGS
1571 #if ENABLE_FEATURE_LESS_REGEXP
1573 option_mask32
&= ~LESS_STATE_MATCH_BACKWARDS
;
1577 goto_match(match_pos
+ 1);
1580 goto_match(match_pos
- 1);
1583 option_mask32
|= LESS_STATE_MATCH_BACKWARDS
;
1587 #if ENABLE_FEATURE_LESS_DASHCMD
1598 #if ENABLE_FEATURE_LESS_BRACKETS
1599 case '{': case '(': case '[':
1600 match_right_bracket(keypress
);
1602 case '}': case ')': case ']':
1603 match_left_bracket(keypress
);
1611 if (isdigit(keypress
))
1612 number_process(keypress
);
1615 static void sig_catcher(int sig
)
1620 #if ENABLE_FEATURE_LESS_WINCH
1621 static void sigwinch_handler(int sig UNUSED_PARAM
)
1627 int less_main(int argc
, char **argv
) MAIN_EXTERNALLY_VISIBLE
;
1628 int less_main(int argc
, char **argv
)
1635 /* TODO: -x: do not interpret backspace, -xx: tab also
1636 * -xxx: newline also
1637 * -w N: assume width N (-xxx -w 32: hex viewer of sorts)
1638 * -s: condense many empty lines to one
1639 * (used by some setups for manpage display)
1641 getopt32(argv
, "EMmN~I" IF_FEATURE_LESS_DASHCMD("S") /*ignored:*/"s");
1647 /* Another popular pager, most, detects when stdout
1648 * is not a tty and turns into cat. This makes sense. */
1649 if (!isatty(STDOUT_FILENO
))
1650 return bb_cat(argv
);
1653 if (isatty(STDIN_FILENO
)) {
1654 /* Just "less"? No args and no redirection? */
1655 bb_error_msg("missing filename");
1659 filename
= xstrdup(files
[0]);
1662 if (option_mask32
& FLAG_TILDE
)
1663 empty_line_marker
= "";
1665 /* Some versions of less can survive w/o controlling tty,
1666 * try to do the same. This also allows to specify an alternative
1667 * tty via "less 1<>TTY".
1668 * We don't try to use STDOUT_FILENO directly,
1669 * since we want to set this fd to non-blocking mode,
1670 * and not bother with restoring it on exit.
1672 tty_name
= xmalloc_ttyname(STDOUT_FILENO
);
1674 tty_fd
= open(tty_name
, O_RDONLY
);
1679 /* Try controlling tty */
1681 tty_fd
= open(CURRENT_TTY
, O_RDONLY
);
1683 return bb_cat(argv
);
1686 kbd_fd
= tty_fd
; /* save in a global */
1688 tcgetattr(kbd_fd
, &term_orig
);
1689 term_less
= term_orig
;
1690 term_less
.c_lflag
&= ~(ICANON
| ECHO
);
1691 term_less
.c_iflag
&= ~(IXON
| ICRNL
);
1692 /*term_less.c_oflag &= ~ONLCR;*/
1693 term_less
.c_cc
[VMIN
] = 1;
1694 term_less
.c_cc
[VTIME
] = 0;
1696 IF_FEATURE_LESS_ASK_TERMINAL(G
.winsize_err
=) get_terminal_width_height(kbd_fd
, &width
, &max_displayed_line
);
1697 /* 20: two tabstops + 4 */
1698 if (width
< 20 || max_displayed_line
< 3)
1699 return bb_cat(argv
);
1700 max_displayed_line
-= 2;
1702 /* We want to restore term_orig on exit */
1703 bb_signals(BB_FATAL_SIGS
, sig_catcher
);
1704 #if ENABLE_FEATURE_LESS_WINCH
1705 signal(SIGWINCH
, sigwinch_handler
);
1708 buffer
= xmalloc((max_displayed_line
+1) * sizeof(char *));
1713 #if ENABLE_FEATURE_LESS_WINCH
1714 while (WINCH_COUNTER
) {
1717 IF_FEATURE_LESS_ASK_TERMINAL(G
.winsize_err
=) get_terminal_width_height(kbd_fd
, &width
, &max_displayed_line
);
1718 IF_FEATURE_LESS_ASK_TERMINAL(got_size
:)
1719 /* 20: two tabstops + 4 */
1722 if (max_displayed_line
< 3)
1723 max_displayed_line
= 3;
1724 max_displayed_line
-= 2;
1726 buffer
= xmalloc((max_displayed_line
+1) * sizeof(char *));
1727 /* Avoid re-wrap and/or redraw if we already know
1728 * we need to do it again. These ops are expensive */
1734 buffer_fill_and_print();
1735 /* This took some time. Loop back and check,
1736 * were there another SIGWINCH? */
1738 keypress
= less_getch(-1); /* -1: do not position cursor */
1739 # if ENABLE_FEATURE_LESS_ASK_TERMINAL
1740 if ((int32_t)keypress
== KEYCODE_CURSOR_POS
) {
1741 uint32_t rc
= (keypress
>> 32);
1742 width
= (rc
& 0x7fff);
1743 max_displayed_line
= ((rc
>> 16) & 0x7fff);
1748 keypress
= less_getch(-1); /* -1: do not position cursor */
1750 keypress_process(keypress
);
1755 Help text of less version 418 is below.
1756 If you are implementing something, keeping
1757 key and/or command line switch compatibility is a good idea:
1760 SUMMARY OF LESS COMMANDS
1762 Commands marked with * may be preceded by a number, N.
1763 Notes in parentheses indicate the behavior if N is given.
1764 h H Display this help.
1766 ---------------------------------------------------------------------------
1768 e ^E j ^N CR * Forward one line (or N lines).
1769 y ^Y k ^K ^P * Backward one line (or N lines).
1770 f ^F ^V SPACE * Forward one window (or N lines).
1771 b ^B ESC-v * Backward one window (or N lines).
1772 z * Forward one window (and set window to N).
1773 w * Backward one window (and set window to N).
1774 ESC-SPACE * Forward one window, but don't stop at end-of-file.
1775 d ^D * Forward one half-window (and set half-window to N).
1776 u ^U * Backward one half-window (and set half-window to N).
1777 ESC-) RightArrow * Left one half screen width (or N positions).
1778 ESC-( LeftArrow * Right one half screen width (or N positions).
1779 F Forward forever; like "tail -f".
1780 r ^R ^L Repaint screen.
1781 R Repaint screen, discarding buffered input.
1782 ---------------------------------------------------
1783 Default "window" is the screen height.
1784 Default "half-window" is half of the screen height.
1785 ---------------------------------------------------------------------------
1787 /pattern * Search forward for (N-th) matching line.
1788 ?pattern * Search backward for (N-th) matching line.
1789 n * Repeat previous search (for N-th occurrence).
1790 N * Repeat previous search in reverse direction.
1791 ESC-n * Repeat previous search, spanning files.
1792 ESC-N * Repeat previous search, reverse dir. & spanning files.
1793 ESC-u Undo (toggle) search highlighting.
1794 ---------------------------------------------------
1795 Search patterns may be modified by one or more of:
1796 ^N or ! Search for NON-matching lines.
1797 ^E or * Search multiple files (pass thru END OF FILE).
1798 ^F or @ Start search at FIRST file (for /) or last file (for ?).
1799 ^K Highlight matches, but don't move (KEEP position).
1800 ^R Don't use REGULAR EXPRESSIONS.
1801 ---------------------------------------------------------------------------
1803 g < ESC-< * Go to first line in file (or line N).
1804 G > ESC-> * Go to last line in file (or line N).
1805 p % * Go to beginning of file (or N percent into file).
1806 t * Go to the (N-th) next tag.
1807 T * Go to the (N-th) previous tag.
1808 { ( [ * Find close bracket } ) ].
1809 } ) ] * Find open bracket { ( [.
1810 ESC-^F <c1> <c2> * Find close bracket <c2>.
1811 ESC-^B <c1> <c2> * Find open bracket <c1>
1812 ---------------------------------------------------
1813 Each "find close bracket" command goes forward to the close bracket
1814 matching the (N-th) open bracket in the top line.
1815 Each "find open bracket" command goes backward to the open bracket
1816 matching the (N-th) close bracket in the bottom line.
1817 m<letter> Mark the current position with <letter>.
1818 '<letter> Go to a previously marked position.
1819 '' Go to the previous position.
1821 ---------------------------------------------------
1822 A mark is any upper-case or lower-case letter.
1823 Certain marks are predefined:
1824 ^ means beginning of the file
1825 $ means end of the file
1826 ---------------------------------------------------------------------------
1828 :e [file] Examine a new file.
1830 :n * Examine the (N-th) next file from the command line.
1831 :p * Examine the (N-th) previous file from the command line.
1832 :x * Examine the first (or N-th) file from the command line.
1833 :d Delete the current file from the command line list.
1834 = ^G :f Print current file name.
1835 ---------------------------------------------------------------------------
1836 MISCELLANEOUS COMMANDS
1837 -<flag> Toggle a command line option [see OPTIONS below].
1838 --<name> Toggle a command line option, by name.
1839 _<flag> Display the setting of a command line option.
1840 __<name> Display the setting of an option, by name.
1841 +cmd Execute the less cmd each time a new file is examined.
1842 !command Execute the shell command with $SHELL.
1843 |Xcommand Pipe file between current pos & mark X to shell command.
1844 v Edit the current file with $VISUAL or $EDITOR.
1845 V Print version number of "less".
1846 ---------------------------------------------------------------------------
1848 Most options may be changed either on the command line,
1849 or from within less by using the - or -- command.
1850 Options may be given in one of two forms: either a single
1851 character preceded by a -, or a name preceeded by --.
1853 Display help (from command line).
1854 -a ........ --search-skip-screen
1855 Forward search skips current screen.
1856 -b [N] .... --buffers=[N]
1858 -B ........ --auto-buffers
1859 Don't automatically allocate buffers for pipes.
1860 -c ........ --clear-screen
1861 Repaint by clearing rather than scrolling.
1864 -D [xn.n] . --color=xn.n
1865 Set screen colors. (MS-DOS only)
1866 -e -E .... --quit-at-eof --QUIT-AT-EOF
1867 Quit at end of file.
1869 Force open non-regular files.
1870 -F ........ --quit-if-one-screen
1871 Quit if entire file fits on first screen.
1872 -g ........ --hilite-search
1873 Highlight only last match for searches.
1874 -G ........ --HILITE-SEARCH
1875 Don't highlight any matches for searches.
1876 -h [N] .... --max-back-scroll=[N]
1877 Backward scroll limit.
1878 -i ........ --ignore-case
1879 Ignore case in searches that do not contain uppercase.
1880 -I ........ --IGNORE-CASE
1881 Ignore case in all searches.
1882 -j [N] .... --jump-target=[N]
1883 Screen position of target lines.
1884 -J ........ --status-column
1885 Display a status column at left edge of screen.
1886 -k [file] . --lesskey-file=[file]
1888 -L ........ --no-lessopen
1889 Ignore the LESSOPEN environment variable.
1890 -m -M .... --long-prompt --LONG-PROMPT
1892 -n -N .... --line-numbers --LINE-NUMBERS
1893 Don't use line numbers.
1894 -o [file] . --log-file=[file]
1895 Copy to log file (standard input only).
1896 -O [file] . --LOG-FILE=[file]
1897 Copy to log file (unconditionally overwrite).
1898 -p [pattern] --pattern=[pattern]
1899 Start at pattern (from command line).
1900 -P [prompt] --prompt=[prompt]
1902 -q -Q .... --quiet --QUIET --silent --SILENT
1903 Quiet the terminal bell.
1904 -r -R .... --raw-control-chars --RAW-CONTROL-CHARS
1905 Output "raw" control characters.
1906 -s ........ --squeeze-blank-lines
1907 Squeeze multiple blank lines.
1908 -S ........ --chop-long-lines
1910 -t [tag] .. --tag=[tag]
1912 -T [tagsfile] --tag-file=[tagsfile]
1913 Use an alternate tags file.
1914 -u -U .... --underline-special --UNDERLINE-SPECIAL
1915 Change handling of backspaces.
1916 -V ........ --version
1917 Display the version number of "less".
1918 -w ........ --hilite-unread
1919 Highlight first new line after forward-screen.
1920 -W ........ --HILITE-UNREAD
1921 Highlight first new line after any forward movement.
1922 -x [N[,...]] --tabs=[N[,...]]
1924 -X ........ --no-init
1925 Don't use termcap init/deinit strings.
1927 Don't use termcap keypad init/deinit strings.
1928 -y [N] .... --max-forw-scroll=[N]
1929 Forward scroll limit.
1930 -z [N] .... --window=[N]
1932 -" [c[c]] . --quotes=[c[c]]
1933 Set shell quote characters.
1935 Don't display tildes after end of file.
1936 -# [N] .... --shift=[N]
1937 Horizontal scroll amount (0 = one half screen width)
1939 ---------------------------------------------------------------------------
1941 These keys can be used to edit text being entered
1942 on the "command line" at the bottom of the screen.
1943 RightArrow ESC-l Move cursor right one character.
1944 LeftArrow ESC-h Move cursor left one character.
1945 CNTL-RightArrow ESC-RightArrow ESC-w Move cursor right one word.
1946 CNTL-LeftArrow ESC-LeftArrow ESC-b Move cursor left one word.
1947 HOME ESC-0 Move cursor to start of line.
1948 END ESC-$ Move cursor to end of line.
1949 BACKSPACE Delete char to left of cursor.
1950 DELETE ESC-x Delete char under cursor.
1951 CNTL-BACKSPACE ESC-BACKSPACE Delete word to left of cursor.
1952 CNTL-DELETE ESC-DELETE ESC-X Delete word under cursor.
1953 CNTL-U ESC (MS-DOS only) Delete entire line.
1954 UpArrow ESC-k Retrieve previous command line.
1955 DownArrow ESC-j Retrieve next command line.
1956 TAB Complete filename & cycle.
1957 SHIFT-TAB ESC-TAB Complete filename & reverse cycle.
1958 CNTL-L Complete filename, list all.