1 /* $NetBSD: cmdbuf.c,v 1.4 2013/09/04 19:44:21 tron Exp $ */
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 * Functions which manipulate the command buffer.
15 * Used only by command() and related functions.
31 static char cmdbuf
[CMDBUF_SIZE
]; /* Buffer for holding a multi-char command */
32 static int cmd_col
; /* Current column of the cursor */
33 static int prompt_col
; /* Column of cursor just after prompt */
34 static char *cp
; /* Pointer into cmdbuf */
35 static int cmd_offset
; /* Index into cmdbuf of first displayed char */
36 static int literal
; /* Next input char should not be interpreted */
37 static int updown_match
= -1; /* Prefix length in up/down movement */
39 #if TAB_COMPLETE_FILENAME
40 static int cmd_complete
__P((int));
42 * These variables are statics used by cmd_complete.
44 static int in_completion
= 0;
46 static char *tk_original
;
47 static char *tk_ipoint
;
48 static char *tk_trial
;
49 static struct textlist tk_tlist
;
52 static void cmd_repaint
__P((char *));
53 static void cmd_home
__P((void));
54 static void cmd_lshift
__P((void));
55 static void cmd_rshift
__P((void));
56 static int cmd_right
__P((void));
57 static int cmd_left
__P((void));
58 static int cmd_ichar
__P((char *, int));
59 static int cmd_erase
__P((void));
60 static int cmd_delete
__P((void));
61 static int cmd_werase
__P((void));
62 static int cmd_wdelete
__P((void));
63 static int cmd_kill
__P((void));
64 static int cmd_updown
__P((int));
65 static int cmd_edit
__P((int));
66 static int cmd_istr
__P((char *));
67 static char *delimit_word
__P((void));
68 static void init_compl
__P((void));
69 static char *next_compl
__P((int, char *));
71 #if SPACES_IN_FILENAMES
72 public char openquote
= '"';
73 public char closequote
= '"';
79 #define HISTFILE_FIRST_LINE ".less-history-file:"
80 #define HISTFILE_SEARCH_SECTION ".search"
81 #define HISTFILE_SHELL_SECTION ".shell"
84 * A mlist structure represents a command history.
90 struct mlist
*curr_mp
;
96 * These are the various command histories that exist.
98 struct mlist mlist_search
=
99 { &mlist_search
, &mlist_search
, &mlist_search
, NULL
, 0 };
100 public void * constant ml_search
= (void *) &mlist_search
;
102 struct mlist mlist_examine
=
103 { &mlist_examine
, &mlist_examine
, &mlist_examine
, NULL
, 0 };
104 public void * constant ml_examine
= (void *) &mlist_examine
;
106 #if SHELL_ESCAPE || PIPEC
107 struct mlist mlist_shell
=
108 { &mlist_shell
, &mlist_shell
, &mlist_shell
, NULL
, 0 };
109 public void * constant ml_shell
= (void *) &mlist_shell
;
112 #else /* CMD_HISTORY */
114 /* If CMD_HISTORY is off, these are just flags. */
115 public void * constant ml_search
= (void *)1;
116 public void * constant ml_examine
= (void *)2;
117 #if SHELL_ESCAPE || PIPEC
118 public void * constant ml_shell
= (void *)3;
121 #endif /* CMD_HISTORY */
124 * History for the current command.
126 static struct mlist
*curr_mlist
= NULL
;
127 static int curr_cmdflags
;
129 static char cmd_mbc_buf
[MAX_UTF_CHAR_LEN
];
130 static int cmd_mbc_buf_len
;
131 static int cmd_mbc_buf_index
;
135 * Reset command buffer (to empty).
150 * Clear command line.
155 cmd_col
= prompt_col
= 0;
161 * Display a string, usually as a prompt for input into the command buffer.
169 char *endline
= s
+ strlen(s
);
173 ch
= step_char(&ns
, +1, endline
);
180 } else if (!is_composing_char(ch
) &&
181 !is_combining_char(prev_ch
, ch
))
183 int width
= is_wide_char(ch
) ? 2 : 1;
192 * How many characters are in the command buffer?
198 char *endline
= s
+ strlen(s
);
203 step_char(&s
, +1, endline
);
210 * Common part of cmd_step_right() and cmd_step_left().
213 cmd_step_common(p
, ch
, len
, pwidth
, bswidth
)
224 pr
= prchar((int) ch
);
225 if (pwidth
!= NULL
|| bswidth
!= NULL
)
227 int len
= strlen(pr
);
236 if (pwidth
!= NULL
|| bswidth
!= NULL
)
238 if (is_composing_char(ch
))
244 } else if (is_ubin_char(ch
))
246 int len
= strlen(pr
);
253 LWCHAR prev_ch
= step_char(&p
, -1, cmdbuf
);
254 if (is_combining_char(prev_ch
, ch
))
263 *pwidth
= is_wide_char(ch
)
277 * Step a pointer one character right in the command buffer.
280 cmd_step_right(pp
, pwidth
, bswidth
)
286 LWCHAR ch
= step_char(pp
, +1, p
+ strlen(p
));
288 return cmd_step_common(p
, ch
, *pp
- p
, pwidth
, bswidth
);
292 * Step a pointer one character left in the command buffer.
295 cmd_step_left(pp
, pwidth
, bswidth
)
301 LWCHAR ch
= step_char(pp
, -1, cmdbuf
);
303 return cmd_step_common(*pp
, ch
, p
- *pp
, pwidth
, bswidth
);
307 * Repaint the line from cp onwards.
308 * Then position the cursor just after the char old_cp (a pointer into cmdbuf).
315 * Repaint the line from the current position.
322 char *pr
= cmd_step_right(&np
, &width
, NULL
);
323 if (cmd_col
+ width
>= sc_width
)
333 char *pr
= cmd_step_right(&np
, &width
, NULL
);
341 * Back up the cursor to the correct position.
348 * Put the cursor at "home" (just after the prompt),
349 * and set cp to the corresponding char in cmdbuf.
354 while (cmd_col
> prompt_col
)
358 cmd_step_left(&cp
, &width
, &bswidth
);
359 while (bswidth
-- > 0)
364 cp
= &cmdbuf
[cmd_offset
];
368 * Shift the cmdbuf display left a half-screen.
378 * Start at the first displayed char, count how far to the
379 * right we'd have to move to reach the center of the screen.
381 s
= cmdbuf
+ cmd_offset
;
383 while (cols
< (sc_width
- prompt_col
) / 2 && *s
!= '\0')
386 cmd_step_right(&s
, &width
, NULL
);
393 cmd_step_right(&ns
, &width
, NULL
);
399 cmd_offset
= s
- cmdbuf
;
402 cmd_repaint(save_cp
);
406 * Shift the cmdbuf display right a half-screen.
416 * Start at the first displayed char, count how far to the
417 * left we'd have to move to traverse a half-screen width
418 * of displayed characters.
420 s
= cmdbuf
+ cmd_offset
;
422 while (cols
< (sc_width
- prompt_col
) / 2 && s
> cmdbuf
)
425 cmd_step_left(&s
, &width
, NULL
);
429 cmd_offset
= s
- cmdbuf
;
432 cmd_repaint(save_cp
);
436 * Move cursor right one character.
447 /* Already at the end of the line. */
451 pr
= cmd_step_right(&ncp
, &width
, NULL
);
452 if (cmd_col
+ width
>= sc_width
)
454 else if (cmd_col
+ width
== sc_width
- 1 && cp
[1] != '\0')
461 pr
= cmd_step_right(&ncp
, &width
, NULL
);
471 * Move cursor left one character.
481 /* Already at the beginning of the line */
487 cmd_step_left(&ncp
, &width
, &bswidth
);
491 if (cmd_col
< prompt_col
+ width
)
495 while (bswidth
-- > 0)
501 * Insert a char into the command buffer, at the current position.
510 if (strlen(cmdbuf
) + clen
>= sizeof(cmdbuf
)-1)
512 /* No room in the command buffer for another char. */
518 * Make room for the new character (shift the tail of the buffer right).
520 for (s
= &cmdbuf
[strlen(cmdbuf
)]; s
>= cp
; s
--)
523 * Insert the character into the buffer.
525 for (s
= cp
; s
< cp
+ clen
; s
++)
528 * Reprint the tail of the line from the inserted char.
537 * Backspace in the command buffer.
538 * Delete the char to the left of the cursor.
549 * Backspace past beginning of the buffer:
550 * this usually means abort the command.
555 * Move cursor left (to the char being erased).
562 * Remove the char from the buffer (shift the buffer left).
572 * Repaint the buffer after the erased char.
578 * We say that erasing the entire command string causes us
579 * to abort the current command, if CF_QUIT_ON_ERASE is set.
581 if ((curr_cmdflags
& CF_QUIT_ON_ERASE
) && cp
== cmdbuf
&& *cp
== '\0')
587 * Delete the char under the cursor.
594 /* At end of string; there is no char under the cursor. */
598 * Move right, then use cmd_erase.
606 * Delete the "word" to the left of the cursor.
611 if (cp
> cmdbuf
&& cp
[-1] == ' ')
614 * If the char left of cursor is a space,
615 * erase all the spaces left of cursor (to the first non-space).
617 while (cp
> cmdbuf
&& cp
[-1] == ' ')
622 * If the char left of cursor is not a space,
623 * erase all the nonspaces left of cursor (the whole "word").
625 while (cp
> cmdbuf
&& cp
[-1] != ' ')
632 * Delete the "word" under the cursor.
640 * If the char under the cursor is a space,
641 * delete it and all the spaces right of cursor.
648 * If the char under the cursor is not a space,
649 * delete it and all nonspaces right of cursor (the whole word).
651 while (*cp
!= ' ' && *cp
!= '\0')
658 * Delete all chars in the command buffer.
663 if (cmdbuf
[0] == '\0')
665 /* Buffer is already empty; abort the current command. */
675 * We say that erasing the entire command string causes us
676 * to abort the current command, if CF_QUIT_ON_ERASE is set.
678 if (curr_cmdflags
& CF_QUIT_ON_ERASE
)
684 * Select an mlist structure to be the current command history.
687 set_mlist(mlist
, cmdflags
)
692 curr_mlist
= (struct mlist
*) mlist
;
693 curr_cmdflags
= cmdflags
;
695 /* Make sure the next up-arrow moves to the last string in the mlist. */
696 if (curr_mlist
!= NULL
)
697 curr_mlist
->curr_mp
= curr_mlist
;
703 * Move up or down in the currently selected command history list.
704 * Only consider entries whose first updown_match chars are equal to
705 * cmdbuf's corresponding chars.
714 if (curr_mlist
== NULL
)
717 * The current command has no history list.
723 if (updown_match
< 0)
725 updown_match
= cp
- cmdbuf
;
729 * Find the next history entry which matches.
731 for (ml
= curr_mlist
->curr_mp
;;)
733 ml
= (action
== EC_UP
) ? ml
->prev
: ml
->next
;
734 if (ml
== curr_mlist
)
737 * We reached the end (or beginning) of the list.
741 if (strncmp(cmdbuf
, ml
->string
, updown_match
) == 0)
744 * This entry matches; stop here.
745 * Copy the entry into cmdbuf and echo it on the screen.
747 curr_mlist
->curr_mp
= ml
;
754 for (cp
= cmdbuf
; *cp
!= '\0'; )
760 * We didn't find a history entry that matches.
768 * Add a string to a history list.
771 cmd_addhist(mlist
, cmd
)
779 * Don't save a trivial command.
781 if (strlen(cmd
) == 0)
785 * Save the command unless it's a duplicate of the
786 * last command in the history.
789 if (ml
== mlist
|| strcmp(ml
->string
, cmd
) != 0)
792 * Did not find command in history.
793 * Save the command and put it at the end of the history list.
795 ml
= (struct mlist
*) ecalloc(1, sizeof(struct mlist
));
796 ml
->string
= save(cmd
);
798 ml
->prev
= mlist
->prev
;
799 mlist
->prev
->next
= ml
;
803 * Point to the cmd just after the just-accepted command.
804 * Thus, an UPARROW will always retrieve the previous command.
806 mlist
->curr_mp
= ml
->next
;
811 * Accept the command in the command buffer.
812 * Add it to the currently selected history list.
819 * Nothing to do if there is no currently selected history list.
821 if (curr_mlist
== NULL
)
823 cmd_addhist(curr_mlist
, cmdbuf
);
824 curr_mlist
->modified
= 1;
829 * Try to perform a line-edit function on the command buffer,
830 * using a specified char as a line-editing command.
832 * CC_PASS The char does not invoke a line edit function.
833 * CC_OK Line edit function done.
834 * CC_QUIT The char requests the current command to be aborted.
843 #if TAB_COMPLETE_FILENAME
844 #define not_in_completion() in_completion = 0
846 #define not_in_completion()
850 * See if the char is indeed a line-editing command.
854 if (curr_mlist
== NULL
)
856 * No current history; don't accept history manipulation cmds.
858 flags
|= EC_NOHISTORY
;
860 #if TAB_COMPLETE_FILENAME
861 if (curr_mlist
== ml_search
)
863 * In a search command; don't accept file-completion cmds.
865 flags
|= EC_NOCOMPLETE
;
868 action
= editchar(c
, flags
);
874 return (cmd_right());
880 while (*cp
!= '\0' && *cp
!= ' ')
887 while (cp
> cmdbuf
&& cp
[-1] == ' ')
889 while (cp
> cmdbuf
&& cp
[-1] != ' ')
908 return (cmd_erase());
918 return (cmd_werase());
921 return (cmd_delete());
924 return (cmd_wdelete());
932 return (cmd_updown(action
));
934 #if TAB_COMPLETE_FILENAME
938 return (cmd_complete(action
));
948 #if TAB_COMPLETE_FILENAME
950 * Insert a string into the command buffer, at the current position.
958 char *endline
= str
+ strlen(str
);
960 for (s
= str
; *s
!= '\0'; )
963 step_char(&s
, +1, endline
);
964 action
= cmd_ichar(os
, s
- os
);
975 * Find the beginning and end of the "current" word.
976 * This is the word which the cursor (cp) is inside or at the end of.
977 * Return pointer to the beginning of the word and put the
978 * cursor at the end of the word.
984 #if SPACES_IN_FILENAMES
986 int delim_quoted
= 0;
988 char *esc
= get_meta_escape();
989 int esclen
= strlen(esc
);
993 * Move cursor to end of word.
995 if (*cp
!= ' ' && *cp
!= '\0')
998 * Cursor is on a nonspace.
999 * Move cursor right to the next space.
1001 while (*cp
!= ' ' && *cp
!= '\0')
1003 } else if (cp
> cmdbuf
&& cp
[-1] != ' ')
1006 * Cursor is on a space, and char to the left is a nonspace.
1007 * We're already at the end of the word.
1014 * Cursor is on a space and char to the left is a space.
1015 * Huh? There's no word here.
1021 * Find the beginning of the word which the cursor is in.
1025 #if SPACES_IN_FILENAMES
1027 * If we have an unbalanced quote (that is, an open quote
1028 * without a corresponding close quote), we return everything
1029 * from the open quote, including spaces.
1031 for (word
= cmdbuf
; word
< cp
; word
++)
1036 for (p
= cmdbuf
; p
< cp
; p
++)
1041 } else if (esclen
> 0 && p
+ esclen
< cp
&&
1042 strncmp(p
, esc
, esclen
) == 0)
1046 } else if (delim_quoted
)
1048 if (*p
== closequote
)
1050 } else /* (!delim_quoted) */
1052 if (*p
== openquote
)
1063 * Set things up to enter completion mode.
1064 * Expand the word under the cursor into a list of filenames
1065 * which start with that word, and set tk_text to that list.
1074 * Get rid of any previous tk_text.
1076 if (tk_text
!= NULL
)
1082 * Find the original (uncompleted) word in the command buffer.
1084 word
= delimit_word();
1088 * Set the insertion point to the point in the command buffer
1089 * where the original (uncompleted) word now sits.
1093 * Save the original (uncompleted) word
1095 if (tk_original
!= NULL
)
1097 tk_original
= (char *) ecalloc(cp
-word
+1, sizeof(char));
1098 strncpy(tk_original
, word
, cp
-word
);
1100 * Get the expanded filename.
1101 * This may result in a single filename, or
1102 * a blank-separated list of filenames.
1106 if (*word
!= openquote
)
1108 tk_text
= fcomplete(word
);
1114 char *qword
= shell_quote(word
+1);
1117 tk_text
= fcomplete(word
+1);
1120 tk_text
= fcomplete(qword
);
1128 * Return the next word in the current completion list.
1131 next_compl(action
, prev
)
1138 return (forw_textlist(&tk_tlist
, prev
));
1140 return (back_textlist(&tk_tlist
, prev
));
1147 * Complete the filename before (or under) the cursor.
1148 * cmd_complete may be called multiple times. The global in_completion
1149 * remembers whether this call is the first time (create the list),
1150 * or a subsequent time (step thru the list).
1153 cmd_complete(action
)
1158 if (!in_completion
|| action
== EC_EXPAND
)
1161 * Expand the word under the cursor and
1162 * use the first word in the expansion
1163 * (or the entire expansion if we're doing EC_EXPAND).
1166 if (tk_text
== NULL
)
1171 if (action
== EC_EXPAND
)
1174 * Use the whole list.
1180 * Use the first filename in the list.
1183 init_textlist(&tk_tlist
, tk_text
);
1184 tk_trial
= next_compl(action
, (char*)NULL
);
1189 * We already have a completion list.
1190 * Use the next/previous filename from the list.
1192 tk_trial
= next_compl(action
, tk_trial
);
1196 * Remove the original word, or the previous trial completion.
1198 while (cp
> tk_ipoint
)
1201 if (tk_trial
== NULL
)
1204 * There are no more trial completions.
1205 * Insert the original (uncompleted) filename.
1208 if (cmd_istr(tk_original
) != CC_OK
)
1213 * Insert trial completion.
1215 if (cmd_istr(tk_trial
) != CC_OK
)
1218 * If it is a directory, append a slash.
1220 if (is_dir(tk_trial
))
1222 if (cp
> cmdbuf
&& cp
[-1] == closequote
)
1224 s
= lgetenv("LESSSEPARATOR");
1227 if (cmd_istr(s
) != CC_OK
)
1240 #endif /* TAB_COMPLETE_FILENAME */
1243 * Process a single character of a multi-character command, such as
1244 * a number, or the pattern of a search command.
1246 * CC_OK The char was accepted.
1247 * CC_QUIT The char requests the command to be aborted.
1248 * CC_ERROR The char could not be accepted due to an error.
1263 /* Perform strict validation in all possible cases. */
1264 if (cmd_mbc_buf_len
== 0)
1267 cmd_mbc_buf_index
= 1;
1269 if (IS_ASCII_OCTET(c
))
1270 cmd_mbc_buf_len
= 1;
1271 else if (IS_UTF8_LEAD(c
))
1273 cmd_mbc_buf_len
= utf_len(c
);
1277 /* UTF8_INVALID or stray UTF8_TRAIL */
1281 } else if (IS_UTF8_TRAIL(c
))
1283 cmd_mbc_buf
[cmd_mbc_buf_index
++] = c
;
1284 if (cmd_mbc_buf_index
< cmd_mbc_buf_len
)
1286 if (!is_utf8_well_formed(cmd_mbc_buf
))
1288 /* complete, but not well formed (non-shortest form), sequence */
1289 cmd_mbc_buf_len
= 0;
1295 /* Flush incomplete (truncated) sequence. */
1296 cmd_mbc_buf_len
= 0;
1298 /* Handle new char. */
1302 len
= cmd_mbc_buf_len
;
1303 cmd_mbc_buf_len
= 0;
1309 * Insert the char, even if it is a line-editing char.
1312 return (cmd_ichar(cmd_mbc_buf
, len
));
1316 * See if it is a line-editing character.
1318 if (in_mca() && len
== 1)
1320 action
= cmd_edit(c
);
1332 * Insert the char into the command buffer.
1334 return (cmd_ichar(cmd_mbc_buf
, len
));
1338 * Return the number currently in the command buffer.
1348 for (p
= cmdbuf
; *p
>= '0' && *p
<= '9'; p
++)
1349 n
= (n
* 10) + (*p
- '0');
1353 *frac
= getfraction(&p
, NULL
, &err
);
1354 /* {{ do something if err is set? }} */
1360 * Return a pointer to the command buffer.
1370 * Return the last (most recent) string in the current command history.
1375 if (curr_mlist
== NULL
)
1377 return (curr_mlist
->curr_mp
->prev
->string
);
1383 * Get the name of the history file.
1392 /* See if filename is explicitly specified by $LESSHISTFILE. */
1393 name
= lgetenv("LESSHISTFILE");
1394 if (name
!= NULL
&& *name
!= '\0')
1396 if (strcmp(name
, "-") == 0 || strcmp(name
, "/dev/null") == 0)
1397 /* $LESSHISTFILE == "-" means don't use a history file. */
1399 return (save(name
));
1402 /* Otherwise, file is in $HOME. */
1403 home
= lgetenv("HOME");
1404 if (home
== NULL
|| *home
== '\0')
1407 home
= lgetenv("INIT");
1408 if (home
== NULL
|| *home
== '\0')
1412 len
= strlen(home
) + strlen(LESSHISTFILE
) + 2;
1413 name
= (char *) ecalloc(len
, sizeof(char));
1414 SNPRINTF2(name
, len
, "%s/%s", home
, LESSHISTFILE
);
1417 #endif /* CMD_HISTORY */
1420 * Initialize history from a .lesshist file.
1426 struct mlist
*ml
= NULL
;
1427 char line
[CMDBUF_SIZE
];
1435 filename
= histfile_name();
1436 if (filename
== NULL
)
1439 /* ignore devices/fifos; allow symlinks */
1440 if (stat(filename
, &st
) < 0)
1442 if (!S_ISREG(st
.st_mode
))
1445 f
= fopen(filename
, "r");
1449 if (fgets(line
, sizeof(line
), f
) == NULL
||
1450 strncmp(line
, HISTFILE_FIRST_LINE
, strlen(HISTFILE_FIRST_LINE
)) != 0)
1455 while (fgets(line
, sizeof(line
), f
) != NULL
)
1457 for (p
= line
; *p
!= '\0'; p
++)
1459 if (*p
== '\n' || *p
== '\r')
1465 if (strcmp(line
, HISTFILE_SEARCH_SECTION
) == 0)
1467 else if (strcmp(line
, HISTFILE_SHELL_SECTION
) == 0)
1469 #if SHELL_ESCAPE || PIPEC
1474 } else if (*line
== '"')
1477 cmd_addhist(ml
, line
+1);
1481 #endif /* CMD_HISTORY */
1497 s
= lgetenv("LESSHISTSIZE");
1504 for (n
= 0; n
< histsize
; n
++)
1506 if (ml
->string
== NULL
)
1510 for (ml
= ml
->next
; ml
->string
!= NULL
; ml
= ml
->next
)
1511 fprintf(f
, "\"%s\n", ml
->string
);
1513 #endif /* CMD_HISTORY */
1526 if (mlist_search
.modified
)
1528 #if SHELL_ESCAPE || PIPEC
1529 if (mlist_shell
.modified
)
1534 filename
= histfile_name();
1535 if (filename
== NULL
)
1537 f
= fopen(filename
, "w");
1543 /* Make history file readable only by owner. */
1546 struct stat statbuf
;
1547 int r
= fstat(fileno(f
), &statbuf
);
1548 if (r
< 0 || !S_ISREG(statbuf
.st_mode
))
1549 /* Don't chmod if not a regular file. */
1553 fchmod(fileno(f
), 0600);
1557 fprintf(f
, "%s\n", HISTFILE_FIRST_LINE
);
1559 fprintf(f
, "%s\n", HISTFILE_SEARCH_SECTION
);
1560 save_mlist(&mlist_search
, f
);
1562 #if SHELL_ESCAPE || PIPEC
1563 fprintf(f
, "%s\n", HISTFILE_SHELL_SECTION
);
1564 save_mlist(&mlist_shell
, f
);
1568 #endif /* CMD_HISTORY */