2 * Copyright (C) 1984-2012 Mark Nudelman
3 * Modified for use with illumos by Garrett D'Amore.
4 * Copyright 2014 Garrett D'Amore <garrett@damore.org>
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.
13 * Functions which manipulate the command buffer.
14 * Used only by command() and related functions.
26 static char cmdbuf
[CMDBUF_SIZE
]; /* Buffer for holding a multi-char command */
27 static int cmd_col
; /* Current column of the cursor */
28 static int prompt_col
; /* Column of cursor just after prompt */
29 static char *cp
; /* Pointer into cmdbuf */
30 static int cmd_offset
; /* Index into cmdbuf of first displayed char */
31 static int literal
; /* Next input char should not be interpreted */
32 static int updown_match
= -1; /* Prefix length in up/down movement */
34 static int cmd_complete(int);
36 * These variables are statics used by cmd_complete.
38 static int in_completion
= 0;
40 static char *tk_original
;
41 static char *tk_ipoint
;
42 static char *tk_trial
;
43 static struct textlist tk_tlist
;
45 static int cmd_left(void);
46 static int cmd_right(void);
49 char closequote
= '"';
52 #define HISTFILE_FIRST_LINE ".less-history-file:"
53 #define HISTFILE_SEARCH_SECTION ".search"
54 #define HISTFILE_SHELL_SECTION ".shell"
57 * A mlist structure represents a command history.
62 struct mlist
*curr_mp
;
68 * These are the various command histories that exist.
70 struct mlist mlist_search
=
71 { &mlist_search
, &mlist_search
, &mlist_search
, NULL
, 0 };
72 void * const ml_search
= (void *) &mlist_search
;
74 struct mlist mlist_examine
=
75 { &mlist_examine
, &mlist_examine
, &mlist_examine
, NULL
, 0 };
76 void * const ml_examine
= (void *) &mlist_examine
;
78 struct mlist mlist_shell
=
79 { &mlist_shell
, &mlist_shell
, &mlist_shell
, NULL
, 0 };
80 void * const ml_shell
= (void *) &mlist_shell
;
83 * History for the current command.
85 static struct mlist
*curr_mlist
= NULL
;
86 static int curr_cmdflags
;
88 static char cmd_mbc_buf
[MAX_UTF_CHAR_LEN
];
89 static int cmd_mbc_buf_len
;
90 static int cmd_mbc_buf_index
;
94 * Reset command buffer (to empty).
109 * Clear command line.
114 cmd_col
= prompt_col
= 0;
120 * Display a string, usually as a prompt for input into the command buffer.
127 char *endline
= s
+ strlen(s
);
130 ch
= step_char(&ns
, +1, endline
);
136 } else if (!is_composing_char(ch
) &&
137 !is_combining_char(prev_ch
, ch
)) {
138 int width
= is_wide_char(ch
) ? 2 : 1;
147 * How many characters are in the command buffer?
153 char *endline
= s
+ strlen(s
);
157 step_char(&s
, +1, endline
);
164 * Common part of cmd_step_right() and cmd_step_left().
167 cmd_step_common(char *p
, LWCHAR ch
, int len
, int *pwidth
, int *bswidth
)
172 pr
= prchar((int)ch
);
173 if (pwidth
!= NULL
|| bswidth
!= NULL
) {
174 int prlen
= strlen(pr
);
182 if (pwidth
!= NULL
|| bswidth
!= NULL
) {
183 if (is_composing_char(ch
)) {
188 } else if (is_ubin_char(ch
)) {
189 int prlen
= strlen(pr
);
195 LWCHAR prev_ch
= step_char(&p
, -1, cmdbuf
);
196 if (is_combining_char(prev_ch
, ch
)) {
203 *pwidth
= is_wide_char(ch
)
216 * Step a pointer one character right in the command buffer.
219 cmd_step_right(char **pp
, int *pwidth
, int *bswidth
)
222 LWCHAR ch
= step_char(pp
, +1, p
+ strlen(p
));
224 return (cmd_step_common(p
, ch
, *pp
- p
, pwidth
, bswidth
));
228 * Step a pointer one character left in the command buffer.
231 cmd_step_left(char **pp
, int *pwidth
, int *bswidth
)
234 LWCHAR ch
= step_char(pp
, -1, cmdbuf
);
236 return (cmd_step_common(*pp
, ch
, p
- *pp
, pwidth
, bswidth
));
240 * Repaint the line from cp onwards.
241 * Then position the cursor just after the char old_cp (a pointer into cmdbuf).
244 cmd_repaint(char *old_cp
)
247 * Repaint the line from the current position.
250 while (*cp
!= '\0') {
253 char *pr
= cmd_step_right(&np
, &width
, NULL
);
254 if (cmd_col
+ width
>= sc_width
)
260 while (*cp
!= '\0') {
263 char *pr
= cmd_step_right(&np
, &width
, NULL
);
271 * Back up the cursor to the correct position.
278 * Put the cursor at "home" (just after the prompt),
279 * and set cp to the corresponding char in cmdbuf.
284 while (cmd_col
> prompt_col
) {
287 cmd_step_left(&cp
, &width
, &bswidth
);
288 while (bswidth
-- > 0)
293 cp
= &cmdbuf
[cmd_offset
];
297 * Shift the cmdbuf display left a half-screen.
307 * Start at the first displayed char, count how far to the
308 * right we'd have to move to reach the center of the screen.
310 s
= cmdbuf
+ cmd_offset
;
312 while (cols
< (sc_width
- prompt_col
) / 2 && *s
!= '\0') {
314 cmd_step_right(&s
, &width
, NULL
);
320 cmd_step_right(&ns
, &width
, NULL
);
326 cmd_offset
= s
- cmdbuf
;
329 cmd_repaint(save_cp
);
333 * Shift the cmdbuf display right a half-screen.
343 * Start at the first displayed char, count how far to the
344 * left we'd have to move to traverse a half-screen width
345 * of displayed characters.
347 s
= cmdbuf
+ cmd_offset
;
349 while (cols
< (sc_width
- prompt_col
) / 2 && s
> cmdbuf
) {
351 cmd_step_left(&s
, &width
, NULL
);
355 cmd_offset
= s
- cmdbuf
;
358 cmd_repaint(save_cp
);
362 * Move cursor right one character.
372 /* Already at the end of the line. */
376 pr
= cmd_step_right(&ncp
, &width
, NULL
);
377 if (cmd_col
+ width
>= sc_width
)
379 else if (cmd_col
+ width
== sc_width
- 1 && cp
[1] != '\0')
384 while (*cp
!= '\0') {
385 pr
= cmd_step_right(&ncp
, &width
, NULL
);
395 * Move cursor left one character.
404 /* Already at the beginning of the line */
408 while (ncp
> cmdbuf
) {
409 cmd_step_left(&ncp
, &width
, &bswidth
);
413 if (cmd_col
< prompt_col
+ width
)
417 while (bswidth
-- > 0)
423 * Insert a char into the command buffer, at the current position.
426 cmd_ichar(char *cs
, int clen
)
430 if (strlen(cmdbuf
) + clen
>= sizeof (cmdbuf
)-1) {
431 /* No room in the command buffer for another char. */
437 * Make room for the new character (shift the tail of the buffer right).
439 for (s
= &cmdbuf
[strlen(cmdbuf
)]; s
>= cp
; s
--)
442 * Insert the character into the buffer.
444 for (s
= cp
; s
< cp
+ clen
; s
++)
447 * Reprint the tail of the line from the inserted char.
456 * Backspace in the command buffer.
457 * Delete the char to the left of the cursor.
467 * Backspace past beginning of the buffer:
468 * this usually means abort the command.
473 * Move cursor left (to the char being erased).
480 * Remove the char from the buffer (shift the buffer left).
482 for (s
= cp
; ; s
++) {
489 * Repaint the buffer after the erased char.
495 * We say that erasing the entire command string causes us
496 * to abort the current command, if CF_QUIT_ON_ERASE is set.
498 if ((curr_cmdflags
& CF_QUIT_ON_ERASE
) && cp
== cmdbuf
&& *cp
== '\0')
504 * Delete the char under the cursor.
510 /* At end of string; there is no char under the cursor. */
514 * Move right, then use cmd_erase.
522 * Delete the "word" to the left of the cursor.
527 if (cp
> cmdbuf
&& cp
[-1] == ' ') {
529 * If the char left of cursor is a space,
530 * erase all the spaces left of cursor (to the first non-space).
532 while (cp
> cmdbuf
&& cp
[-1] == ' ')
536 * If the char left of cursor is not a space,
537 * erase all the nonspaces left of cursor (the whole "word").
539 while (cp
> cmdbuf
&& cp
[-1] != ' ')
546 * Delete the "word" under the cursor.
553 * If the char under the cursor is a space,
554 * delete it and all the spaces right of cursor.
560 * If the char under the cursor is not a space,
561 * delete it and all nonspaces right of cursor (the whole word).
563 while (*cp
!= ' ' && *cp
!= '\0')
570 * Delete all chars in the command buffer.
575 if (cmdbuf
[0] == '\0') {
576 /* Buffer is already empty; abort the current command. */
586 * We say that erasing the entire command string causes us
587 * to abort the current command, if CF_QUIT_ON_ERASE is set.
589 if (curr_cmdflags
& CF_QUIT_ON_ERASE
)
595 * Select an mlist structure to be the current command history.
598 set_mlist(void *mlist
, int cmdflags
)
600 curr_mlist
= (struct mlist
*)mlist
;
601 curr_cmdflags
= cmdflags
;
603 /* Make sure the next up-arrow moves to the last string in the mlist. */
604 if (curr_mlist
!= NULL
)
605 curr_mlist
->curr_mp
= curr_mlist
;
609 * Move up or down in the currently selected command history list.
610 * Only consider entries whose first updown_match chars are equal to
611 * cmdbuf's corresponding chars.
614 cmd_updown(int action
)
619 if (curr_mlist
== NULL
) {
621 * The current command has no history list.
627 if (updown_match
< 0) {
628 updown_match
= cp
- cmdbuf
;
632 * Find the next history entry which matches.
634 for (ml
= curr_mlist
->curr_mp
; ; ) {
635 ml
= (action
== EC_UP
) ? ml
->prev
: ml
->next
;
636 if (ml
== curr_mlist
) {
638 * We reached the end (or beginning) of the list.
642 if (strncmp(cmdbuf
, ml
->string
, updown_match
) == 0) {
644 * This entry matches; stop here.
645 * Copy the entry into cmdbuf and echo it on the screen.
647 curr_mlist
->curr_mp
= ml
;
653 strlcpy(cmdbuf
, s
, sizeof (cmdbuf
));
654 for (cp
= cmdbuf
; *cp
!= '\0'; )
660 * We didn't find a history entry that matches.
667 * Add a string to a history list.
670 cmd_addhist(struct mlist
*mlist
, const char *cmd
)
675 * Don't save a trivial command.
677 if (strlen(cmd
) == 0)
681 * Save the command unless it's a duplicate of the
682 * last command in the history.
685 if (ml
== mlist
|| strcmp(ml
->string
, cmd
) != 0) {
687 * Did not find command in history.
688 * Save the command and put it at the end of the history list.
690 ml
= ecalloc(1, sizeof (struct mlist
));
691 ml
->string
= estrdup(cmd
);
693 ml
->prev
= mlist
->prev
;
694 mlist
->prev
->next
= ml
;
698 * Point to the cmd just after the just-accepted command.
699 * Thus, an UPARROW will always retrieve the previous command.
701 mlist
->curr_mp
= ml
->next
;
705 * Accept the command in the command buffer.
706 * Add it to the currently selected history list.
712 * Nothing to do if there is no currently selected history list.
714 if (curr_mlist
== NULL
)
716 cmd_addhist(curr_mlist
, cmdbuf
);
717 curr_mlist
->modified
= 1;
721 * Try to perform a line-edit function on the command buffer,
722 * using a specified char as a line-editing command.
724 * CC_PASS The char does not invoke a line edit function.
725 * CC_OK Line edit function done.
726 * CC_QUIT The char requests the current command to be aborted.
734 #define not_in_completion() in_completion = 0
737 * See if the char is indeed a line-editing command.
740 if (curr_mlist
== NULL
)
742 * No current history; don't accept history manipulation cmds.
744 flags
|= EC_NOHISTORY
;
745 if (curr_mlist
== ml_search
)
747 * In a search command; don't accept file-completion cmds.
749 flags
|= EC_NOCOMPLETE
;
751 action
= editchar(c
, flags
);
756 return (cmd_right());
762 while (*cp
!= '\0' && *cp
!= ' ')
769 while (cp
> cmdbuf
&& cp
[-1] == ' ')
771 while (cp
> cmdbuf
&& cp
[-1] != ' ')
790 return (cmd_erase());
800 return (cmd_werase());
803 return (cmd_delete());
806 return (cmd_wdelete());
813 return (cmd_updown(action
));
817 return (cmd_complete(action
));
827 * Insert a string into the command buffer, at the current position.
834 char *endline
= str
+ strlen(str
);
836 for (s
= str
; *s
!= '\0'; ) {
838 step_char(&s
, +1, endline
);
839 action
= cmd_ichar(os
, s
- os
);
840 if (action
!= CC_OK
) {
849 * Find the beginning and end of the "current" word.
850 * This is the word which the cursor (cp) is inside or at the end of.
851 * Return pointer to the beginning of the word and put the
852 * cursor at the end of the word.
859 int delim_quoted
= 0;
861 char *esc
= get_meta_escape();
862 int esclen
= strlen(esc
);
865 * Move cursor to end of word.
867 if (*cp
!= ' ' && *cp
!= '\0') {
869 * Cursor is on a nonspace.
870 * Move cursor right to the next space.
872 while (*cp
!= ' ' && *cp
!= '\0')
877 * Find the beginning of the word which the cursor is in.
882 * If we have an unbalanced quote (that is, an open quote
883 * without a corresponding close quote), we return everything
884 * from the open quote, including spaces.
886 for (word
= cmdbuf
; word
< cp
; word
++)
891 for (p
= cmdbuf
; p
< cp
; p
++) {
894 } else if (esclen
> 0 && p
+ esclen
< cp
&&
895 strncmp(p
, esc
, esclen
) == 0) {
898 } else if (delim_quoted
) {
899 if (*p
== closequote
)
901 } else { /* (!delim_quoted) */
912 * Set things up to enter completion mode.
913 * Expand the word under the cursor into a list of filenames
914 * which start with that word, and set tk_text to that list.
925 * Find the original (uncompleted) word in the command buffer.
927 word
= delimit_word();
931 * Set the insertion point to the point in the command buffer
932 * where the original (uncompleted) word now sits.
936 * Save the original (uncompleted) word
939 tk_original
= ecalloc(cp
-word
+1, sizeof (char));
940 (void) strncpy(tk_original
, word
, cp
-word
);
942 * Get the expanded filename.
943 * This may result in a single filename, or
944 * a blank-separated list of filenames.
948 if (*word
!= openquote
) {
949 tk_text
= fcomplete(word
);
951 char *qword
= shell_quote(word
+1);
953 tk_text
= fcomplete(word
+1);
955 tk_text
= fcomplete(qword
);
962 * Return the next word in the current completion list.
965 next_compl(int action
, char *prev
)
969 return (forw_textlist(&tk_tlist
, prev
));
971 return (back_textlist(&tk_tlist
, prev
));
978 * Complete the filename before (or under) the cursor.
979 * cmd_complete may be called multiple times. The global in_completion
980 * remembers whether this call is the first time (create the list),
981 * or a subsequent time (step thru the list).
984 cmd_complete(int action
)
988 if (!in_completion
|| action
== EC_EXPAND
) {
990 * Expand the word under the cursor and
991 * use the first word in the expansion
992 * (or the entire expansion if we're doing EC_EXPAND).
995 if (tk_text
== NULL
) {
999 if (action
== EC_EXPAND
) {
1001 * Use the whole list.
1006 * Use the first filename in the list.
1009 init_textlist(&tk_tlist
, tk_text
);
1010 tk_trial
= next_compl(action
, NULL
);
1014 * We already have a completion list.
1015 * Use the next/previous filename from the list.
1017 tk_trial
= next_compl(action
, tk_trial
);
1021 * Remove the original word, or the previous trial completion.
1023 while (cp
> tk_ipoint
)
1026 if (tk_trial
== NULL
) {
1028 * There are no more trial completions.
1029 * Insert the original (uncompleted) filename.
1032 if (cmd_istr(tk_original
) != CC_OK
)
1036 * Insert trial completion.
1038 if (cmd_istr(tk_trial
) != CC_OK
)
1041 * If it is a directory, append a slash.
1043 if (is_dir(tk_trial
)) {
1044 if (cp
> cmdbuf
&& cp
[-1] == closequote
)
1046 s
= lgetenv("LESSSEPARATOR");
1049 if (cmd_istr(s
) != CC_OK
)
1063 * Process a single character of a multi-character command, such as
1064 * a number, or the pattern of a search command.
1066 * CC_OK The char was accepted.
1067 * CC_QUIT The char requests the command to be aborted.
1068 * CC_ERROR The char could not be accepted due to an error.
1077 cmd_mbc_buf
[0] = c
& 0xff;
1080 /* Perform strict validation in all possible cases. */
1081 if (cmd_mbc_buf_len
== 0) {
1083 cmd_mbc_buf_index
= 1;
1084 *cmd_mbc_buf
= c
& 0xff;
1085 if (IS_ASCII_OCTET(c
))
1086 cmd_mbc_buf_len
= 1;
1087 else if (IS_UTF8_LEAD(c
)) {
1088 cmd_mbc_buf_len
= utf_len(c
);
1091 /* UTF8_INVALID or stray UTF8_TRAIL */
1095 } else if (IS_UTF8_TRAIL(c
)) {
1096 cmd_mbc_buf
[cmd_mbc_buf_index
++] = c
& 0xff;
1097 if (cmd_mbc_buf_index
< cmd_mbc_buf_len
)
1099 if (!is_utf8_well_formed(cmd_mbc_buf
)) {
1101 * complete, but not well formed
1102 * (non-shortest form), sequence
1104 cmd_mbc_buf_len
= 0;
1109 /* Flush incomplete (truncated) sequence. */
1110 cmd_mbc_buf_len
= 0;
1112 /* Handle new char. */
1116 len
= cmd_mbc_buf_len
;
1117 cmd_mbc_buf_len
= 0;
1122 * Insert the char, even if it is a line-editing char.
1125 return (cmd_ichar(cmd_mbc_buf
, len
));
1129 * See if it is a line-editing character.
1131 if (in_mca() && len
== 1) {
1132 action
= cmd_edit(c
);
1143 * Insert the char into the command buffer.
1145 return (cmd_ichar(cmd_mbc_buf
, len
));
1149 * Return the number currently in the command buffer.
1158 for (p
= cmdbuf
; *p
>= '0' && *p
<= '9'; p
++)
1159 n
= (n
* 10) + (*p
- '0');
1162 *frac
= getfraction(&p
, NULL
, &err
);
1163 /* {{ do something if err is set? }} */
1169 * Return a pointer to the command buffer.
1178 * Return the last (most recent) string in the current command history.
1181 cmd_lastpattern(void)
1183 if (curr_mlist
== NULL
)
1185 return (curr_mlist
->curr_mp
->prev
->string
);
1189 * Get the name of the history file.
1197 /* See if filename is explicitly specified by $LESSHISTFILE. */
1198 name
= lgetenv("LESSHISTFILE");
1199 if (name
!= NULL
&& *name
!= '\0') {
1200 if (strcmp(name
, "-") == 0 || strcmp(name
, "/dev/null") == 0)
1201 /* $LESSHISTFILE == "-" means don't use history file */
1203 return (estrdup(name
));
1206 /* Otherwise, file is in $HOME if enabled. */
1207 if (strcmp(LESSHISTFILE
, "-") == 0)
1209 home
= lgetenv("HOME");
1210 if (home
== NULL
|| *home
== '\0') {
1213 return (easprintf("%s/%s", home
, LESSHISTFILE
));
1217 * Initialize history from a .lesshist file.
1222 struct mlist
*ml
= NULL
;
1223 char line
[CMDBUF_SIZE
];
1228 filename
= histfile_name();
1229 if (filename
== NULL
)
1231 f
= fopen(filename
, "r");
1235 if (fgets(line
, sizeof (line
), f
) == NULL
||
1236 strncmp(line
, HISTFILE_FIRST_LINE
,
1237 strlen(HISTFILE_FIRST_LINE
)) != 0) {
1241 while (fgets(line
, sizeof (line
), f
) != NULL
) {
1242 for (p
= line
; *p
!= '\0'; p
++) {
1243 if (*p
== '\n' || *p
== '\r') {
1248 if (strcmp(line
, HISTFILE_SEARCH_SECTION
) == 0)
1250 else if (strcmp(line
, HISTFILE_SHELL_SECTION
) == 0) {
1252 } else if (*line
== '"') {
1254 cmd_addhist(ml
, line
+1);
1264 save_mlist(struct mlist
*ml
, FILE *f
)
1270 s
= lgetenv("LESSHISTSIZE");
1277 for (n
= 0; n
< histsize
; n
++) {
1278 if (ml
->string
== NULL
)
1282 for (ml
= ml
->next
; ml
->string
!= NULL
; ml
= ml
->next
)
1283 (void) fprintf(f
, "\"%s\n", ml
->string
);
1296 struct stat statbuf
;
1299 if (mlist_search
.modified
)
1301 if (mlist_shell
.modified
)
1305 filename
= histfile_name();
1306 if (filename
== NULL
)
1308 f
= fopen(filename
, "w");
1313 /* Make history file readable only by owner. */
1314 r
= fstat(fileno(f
), &statbuf
);
1315 if (r
< 0 || !S_ISREG(statbuf
.st_mode
))
1316 /* Don't chmod if not a regular file. */
1319 (void) fchmod(fileno(f
), 0600);
1321 (void) fprintf(f
, "%s\n", HISTFILE_FIRST_LINE
);
1323 (void) fprintf(f
, "%s\n", HISTFILE_SEARCH_SECTION
);
1324 save_mlist(&mlist_search
, f
);
1326 (void) fprintf(f
, "%s\n", HISTFILE_SHELL_SECTION
);
1327 save_mlist(&mlist_shell
, f
);