2 * Copyright (C) 1984-2023 Mark Nudelman
4 * You may distribute under the terms of either the GNU General Public
5 * License or the Less License, as specified in the README file.
7 * For more information, see the README file.
12 * Functions which manipulate the command buffer.
13 * Used only by command() and related functions.
25 extern int no_hist_dups;
26 extern int marks_modified;
29 static char cmdbuf[CMDBUF_SIZE]; /* Buffer for holding a multi-char command */
30 static int cmd_col; /* Current column of the cursor */
31 static int prompt_col; /* Column of cursor just after prompt */
32 static char *cp; /* Pointer into cmdbuf */
33 static int cmd_offset; /* Index into cmdbuf of first displayed char */
34 static int literal; /* Next input char should not be interpreted */
35 public int updown_match = -1; /* Prefix length in up/down movement */
37 #if TAB_COMPLETE_FILENAME
38 static int cmd_complete(int action);
40 * These variables are statics used by cmd_complete.
42 static int in_completion = 0;
44 static char *tk_original;
45 static char *tk_ipoint;
46 static char *tk_trial = NULL;
47 static struct textlist tk_tlist;
50 static int cmd_left();
51 static int cmd_right();
53 #if SPACES_IN_FILENAMES
54 public char openquote = '"';
55 public char closequote = '"';
61 #define HISTFILE_FIRST_LINE ".less-history-file:"
62 #define HISTFILE_SEARCH_SECTION ".search"
63 #define HISTFILE_SHELL_SECTION ".shell"
64 #define HISTFILE_MARK_SECTION ".mark"
67 * A mlist structure represents a command history.
73 struct mlist *curr_mp;
79 * These are the various command histories that exist.
81 struct mlist mlist_search =
82 { &mlist_search, &mlist_search, &mlist_search, NULL, 0 };
83 public void *ml_search = (void *) &mlist_search;
85 struct mlist mlist_examine =
86 { &mlist_examine, &mlist_examine, &mlist_examine, NULL, 0 };
87 public void *ml_examine = (void *) &mlist_examine;
89 #if SHELL_ESCAPE || PIPEC
90 struct mlist mlist_shell =
91 { &mlist_shell, &mlist_shell, &mlist_shell, NULL, 0 };
92 public void *ml_shell = (void *) &mlist_shell;
95 #else /* CMD_HISTORY */
97 /* If CMD_HISTORY is off, these are just flags. */
98 public void *ml_search = (void *)1;
99 public void *ml_examine = (void *)2;
100 #if SHELL_ESCAPE || PIPEC
101 public void *ml_shell = (void *)3;
104 #endif /* CMD_HISTORY */
107 * History for the current command.
109 static struct mlist *curr_mlist = NULL;
110 static int curr_cmdflags;
112 static char cmd_mbc_buf[MAX_UTF_CHAR_LEN];
113 static int cmd_mbc_buf_len;
114 static int cmd_mbc_buf_index;
118 * Reset command buffer (to empty).
120 public void cmd_reset(void)
132 * Clear command line.
134 public void clear_cmd(void)
136 cmd_col = prompt_col = 0;
142 * Display a string, usually as a prompt for input into the command buffer.
144 public void cmd_putstr(constant char *s)
148 constant char *endline = s + strlen(s);
151 char *ns = (char *) s;
153 ch = step_char(&ns, +1, endline);
158 else if (is_composing_char(ch) || is_combining_char(prev_ch, ch))
161 width = is_wide_char(ch) ? 2 : 1;
169 * How many characters are in the command buffer?
171 public int len_cmdbuf(void)
174 char *endline = s + strlen(s);
179 step_char(&s, +1, endline);
186 * Common part of cmd_step_right() and cmd_step_left().
187 * {{ Returning pwidth and bswidth separately is a historical artifact
188 * since they're always the same. Maybe clean this up someday. }}
190 static char * cmd_step_common(char *p, LWCHAR ch, int len, int *pwidth, int *bswidth)
197 pr = prchar((int) ch);
198 width = (int) strlen(pr);
202 if (is_composing_char(ch))
204 else if (is_ubin_char(ch))
205 width = (int) strlen(pr);
208 LWCHAR prev_ch = step_char(&p, -1, cmdbuf);
209 if (is_combining_char(prev_ch, ch))
212 width = is_wide_char(ch) ? 2 : 1;
223 * Step a pointer one character right in the command buffer.
225 static char * cmd_step_right(char **pp, int *pwidth, int *bswidth)
228 LWCHAR ch = step_char(pp, +1, p + strlen(p));
230 return cmd_step_common(p, ch, *pp - p, pwidth, bswidth);
234 * Step a pointer one character left in the command buffer.
236 static char * cmd_step_left(char **pp, int *pwidth, int *bswidth)
239 LWCHAR ch = step_char(pp, -1, cmdbuf);
241 return cmd_step_common(*pp, ch, p - *pp, pwidth, bswidth);
245 * Put the cursor at "home" (just after the prompt),
246 * and set cp to the corresponding char in cmdbuf.
248 static void cmd_home(void)
250 while (cmd_col > prompt_col)
254 cmd_step_left(&cp, &width, &bswidth);
255 while (bswidth-- > 0)
260 cp = &cmdbuf[cmd_offset];
264 * Repaint the line from cp onwards.
265 * Then position the cursor just after the char old_cp (a pointer into cmdbuf).
267 public void cmd_repaint(constant char *old_cp)
270 * Repaint the line from the current position.
282 char *pr = cmd_step_right(&np, &width, NULL);
283 if (cmd_col + width >= sc_width)
293 char *pr = cmd_step_right(&np, &width, NULL);
301 * Back up the cursor to the correct position.
308 * Shift the cmdbuf display left a half-screen.
310 static void cmd_lshift(void)
317 * Start at the first displayed char, count how far to the
318 * right we'd have to move to reach the center of the screen.
320 s = cmdbuf + cmd_offset;
322 while (cols < (sc_width - prompt_col) / 2 && *s != '\0')
325 cmd_step_right(&s, &width, NULL);
332 cmd_step_right(&ns, &width, NULL);
338 cmd_offset = (int) (s - cmdbuf);
341 cmd_repaint(save_cp);
345 * Shift the cmdbuf display right a half-screen.
347 static void cmd_rshift(void)
354 * Start at the first displayed char, count how far to the
355 * left we'd have to move to traverse a half-screen width
356 * of displayed characters.
358 s = cmdbuf + cmd_offset;
360 while (cols < (sc_width - prompt_col) / 2 && s > cmdbuf)
363 cmd_step_left(&s, &width, NULL);
367 cmd_offset = (int) (s - cmdbuf);
370 cmd_repaint(save_cp);
374 * Move cursor right one character.
376 static int cmd_right(void)
384 /* Already at the end of the line. */
388 pr = cmd_step_right(&ncp, &width, NULL);
389 if (cmd_col + width >= sc_width)
391 else if (cmd_col + width == sc_width - 1 && cp[1] != '\0')
398 pr = cmd_step_right(&ncp, &width, NULL);
408 * Move cursor left one character.
410 static int cmd_left(void)
418 /* Already at the beginning of the line */
424 cmd_step_left(&ncp, &width, &bswidth);
428 if (cmd_col < prompt_col + width)
432 while (bswidth-- > 0)
438 * Insert a char into the command buffer, at the current position.
440 static int cmd_ichar(char *cs, int clen)
444 if (strlen(cmdbuf) + clen >= sizeof(cmdbuf)-1)
446 /* No room in the command buffer for another char. */
452 * Make room for the new character (shift the tail of the buffer right).
454 for (s = &cmdbuf[strlen(cmdbuf)]; s >= cp; s--)
457 * Insert the character into the buffer.
459 for (s = cp; s < cp + clen; s++)
462 * Reprint the tail of the line from the inserted char.
471 * Backspace in the command buffer.
472 * Delete the char to the left of the cursor.
474 static int cmd_erase(void)
482 * Backspace past beginning of the buffer:
483 * this usually means abort the command.
488 * Move cursor left (to the char being erased).
492 clen = (int) (s - cp);
495 * Remove the char from the buffer (shift the buffer left).
505 * Repaint the buffer after the erased char.
511 * We say that erasing the entire command string causes us
512 * to abort the current command, if CF_QUIT_ON_ERASE is set.
514 if ((curr_cmdflags & CF_QUIT_ON_ERASE) && cp == cmdbuf && *cp == '\0')
520 * Delete the char under the cursor.
522 static int cmd_delete(void)
526 /* At end of string; there is no char under the cursor. */
530 * Move right, then use cmd_erase.
538 * Delete the "word" to the left of the cursor.
540 static int cmd_werase(void)
542 if (cp > cmdbuf && cp[-1] == ' ')
545 * If the char left of cursor is a space,
546 * erase all the spaces left of cursor (to the first non-space).
548 while (cp > cmdbuf && cp[-1] == ' ')
553 * If the char left of cursor is not a space,
554 * erase all the nonspaces left of cursor (the whole "word").
556 while (cp > cmdbuf && cp[-1] != ' ')
563 * Delete the "word" under the cursor.
565 static int cmd_wdelete(void)
570 * If the char under the cursor is a space,
571 * delete it and all the spaces right of cursor.
578 * If the char under the cursor is not a space,
579 * delete it and all nonspaces right of cursor (the whole word).
581 while (*cp != ' ' && *cp != '\0')
588 * Delete all chars in the command buffer.
590 static int cmd_kill(void)
592 if (cmdbuf[0] == '\0')
594 /* Buffer is already empty; abort the current command. */
604 * We say that erasing the entire command string causes us
605 * to abort the current command, if CF_QUIT_ON_ERASE is set.
607 if (curr_cmdflags & CF_QUIT_ON_ERASE)
613 * Select an mlist structure to be the current command history.
615 public void set_mlist(void *mlist, int cmdflags)
618 curr_mlist = (struct mlist *) mlist;
619 curr_cmdflags = cmdflags;
621 /* Make sure the next up-arrow moves to the last string in the mlist. */
622 if (curr_mlist != NULL)
623 curr_mlist->curr_mp = curr_mlist;
629 * Move up or down in the currently selected command history list.
630 * Only consider entries whose first updown_match chars are equal to
631 * cmdbuf's corresponding chars.
633 static int cmd_updown(int action)
638 if (curr_mlist == NULL)
641 * The current command has no history list.
647 if (updown_match < 0)
649 updown_match = (int) (cp - cmdbuf);
653 * Find the next history entry which matches.
655 for (ml = curr_mlist->curr_mp;;)
657 ml = (action == EC_UP) ? ml->prev : ml->next;
658 if (ml == curr_mlist)
661 * We reached the end (or beginning) of the list.
665 if (strncmp(cmdbuf, ml->string, updown_match) == 0)
668 * This entry matches; stop here.
669 * Copy the entry into cmdbuf and echo it on the screen.
671 curr_mlist->curr_mp = ml;
679 for (cp = cmdbuf; *cp != '\0'; )
685 * We didn't find a history entry that matches.
695 static void ml_link(struct mlist *mlist, struct mlist *ml)
698 ml->prev = mlist->prev;
699 mlist->prev->next = ml;
706 static void ml_unlink(struct mlist *ml)
708 ml->prev->next = ml->next;
709 ml->next->prev = ml->prev;
713 * Add a string to an mlist.
715 public void cmd_addhist(struct mlist *mlist, constant char *cmd, int modified)
721 * Don't save a trivial command.
723 if (strlen(cmd) == 0)
728 struct mlist *next = NULL;
729 for (ml = mlist->next; ml->string != NULL; ml = next)
732 if (strcmp(ml->string, cmd) == 0)
742 * Save the command unless it's a duplicate of the
743 * last command in the history.
746 if (ml == mlist || strcmp(ml->string, cmd) != 0)
749 * Did not find command in history.
750 * Save the command and put it at the end of the history list.
752 ml = (struct mlist *) ecalloc(1, sizeof(struct mlist));
753 ml->string = save(cmd);
754 ml->modified = modified;
758 * Point to the cmd just after the just-accepted command.
759 * Thus, an UPARROW will always retrieve the previous command.
761 mlist->curr_mp = ml->next;
766 * Accept the command in the command buffer.
767 * Add it to the currently selected history list.
769 public void cmd_accept(void)
773 * Nothing to do if there is no currently selected history list.
775 if (curr_mlist == NULL || curr_mlist == ml_examine)
777 cmd_addhist(curr_mlist, cmdbuf, 1);
778 curr_mlist->modified = 1;
783 * Try to perform a line-edit function on the command buffer,
784 * using a specified char as a line-editing command.
786 * CC_PASS The char does not invoke a line edit function.
787 * CC_OK Line edit function done.
788 * CC_QUIT The char requests the current command to be aborted.
790 static int cmd_edit(int c)
795 #if TAB_COMPLETE_FILENAME
796 #define not_in_completion() in_completion = 0
798 #define not_in_completion(void)
802 * See if the char is indeed a line-editing command.
806 if (curr_mlist == NULL)
808 * No current history; don't accept history manipulation cmds.
810 flags |= ECF_NOHISTORY;
812 #if TAB_COMPLETE_FILENAME
813 if (curr_mlist == ml_search || curr_mlist == NULL)
815 * Don't accept file-completion cmds in contexts
816 * such as search pattern, digits, long option name, etc.
818 flags |= ECF_NOCOMPLETE;
821 action = editchar(c, flags);
829 return (cmd_right());
835 while (*cp != '\0' && *cp != ' ')
842 while (cp > cmdbuf && cp[-1] == ' ')
844 while (cp > cmdbuf && cp[-1] != ' ')
863 return (cmd_erase());
873 return (cmd_werase());
876 return (cmd_delete());
879 return (cmd_wdelete());
887 return (cmd_updown(action));
889 #if TAB_COMPLETE_FILENAME
893 return (cmd_complete(action));
901 #if TAB_COMPLETE_FILENAME
903 * Insert a string into the command buffer, at the current position.
905 static int cmd_istr(char *str)
909 char *endline = str + strlen(str);
911 for (s = str; *s != '\0'; )
914 step_char(&s, +1, endline);
915 action = cmd_ichar(os, s - os);
923 * Find the beginning and end of the "current" word.
924 * This is the word which the cursor (cp) is inside or at the end of.
925 * Return pointer to the beginning of the word and put the
926 * cursor at the end of the word.
928 static char * delimit_word(void)
931 #if SPACES_IN_FILENAMES
933 int delim_quoted = 0;
935 constant char *esc = get_meta_escape();
936 int esclen = (int) strlen(esc);
940 * Move cursor to end of word.
942 if (*cp != ' ' && *cp != '\0')
945 * Cursor is on a nonspace.
946 * Move cursor right to the next space.
948 while (*cp != ' ' && *cp != '\0')
950 } else if (cp > cmdbuf && cp[-1] != ' ')
953 * Cursor is on a space, and char to the left is a nonspace.
954 * We're already at the end of the word.
961 * Cursor is on a space and char to the left is a space.
962 * Huh? There's no word here.
968 * Find the beginning of the word which the cursor is in.
972 #if SPACES_IN_FILENAMES
974 * If we have an unbalanced quote (that is, an open quote
975 * without a corresponding close quote), we return everything
976 * from the open quote, including spaces.
978 for (word = cmdbuf; word < cp; word++)
983 for (p = cmdbuf; p < cp; p++)
988 } else if (esclen > 0 && p + esclen < cp &&
989 strncmp(p, esc, esclen) == 0)
993 } else if (delim_quoted)
995 if (*p == closequote)
997 } else /* (!delim_quoted) */
1010 * Set things up to enter completion mode.
1011 * Expand the word under the cursor into a list of filenames
1012 * which start with that word, and set tk_text to that list.
1014 static void init_compl(void)
1020 * Get rid of any previous tk_text.
1022 if (tk_text != NULL)
1028 * Find the original (uncompleted) word in the command buffer.
1030 word = delimit_word();
1034 * Set the insertion point to the point in the command buffer
1035 * where the original (uncompleted) word now sits.
1039 * Save the original (uncompleted) word
1041 if (tk_original != NULL)
1043 tk_original = (char *) ecalloc(cp-word+1, sizeof(char));
1044 strncpy(tk_original, word, cp-word);
1046 * Get the expanded filename.
1047 * This may result in a single filename, or
1048 * a blank-separated list of filenames.
1052 if (*word != openquote)
1054 tk_text = fcomplete(word);
1060 char *qword = shell_quote(word+1);
1063 tk_text = fcomplete(word+1);
1066 tk_text = fcomplete(qword);
1074 * Return the next word in the current completion list.
1076 static char * next_compl(int action, char *prev)
1081 return (forw_textlist(&tk_tlist, prev));
1083 return (back_textlist(&tk_tlist, prev));
1090 * Complete the filename before (or under) the cursor.
1091 * cmd_complete may be called multiple times. The global in_completion
1092 * remembers whether this call is the first time (create the list),
1093 * or a subsequent time (step thru the list).
1095 static int cmd_complete(int action)
1099 if (!in_completion || action == EC_EXPAND)
1102 * Expand the word under the cursor and
1103 * use the first word in the expansion
1104 * (or the entire expansion if we're doing EC_EXPAND).
1107 if (tk_text == NULL)
1112 if (action == EC_EXPAND)
1115 * Use the whole list.
1121 * Use the first filename in the list.
1124 init_textlist(&tk_tlist, tk_text);
1125 tk_trial = next_compl(action, (char*)NULL);
1130 * We already have a completion list.
1131 * Use the next/previous filename from the list.
1133 tk_trial = next_compl(action, tk_trial);
1137 * Remove the original word, or the previous trial completion.
1139 while (cp > tk_ipoint)
1142 if (tk_trial == NULL)
1145 * There are no more trial completions.
1146 * Insert the original (uncompleted) filename.
1149 if (cmd_istr(tk_original) != CC_OK)
1154 * Insert trial completion.
1156 if (cmd_istr(tk_trial) != CC_OK)
1159 * If it is a directory, append a slash.
1161 if (is_dir(tk_trial))
1163 if (cp > cmdbuf && cp[-1] == closequote)
1165 s = lgetenv("LESSSEPARATOR");
1168 if (cmd_istr(s) != CC_OK)
1181 #endif /* TAB_COMPLETE_FILENAME */
1184 * Process a single character of a multi-character command, such as
1185 * a number, or the pattern of a search command.
1187 * CC_OK The char was accepted.
1188 * CC_QUIT The char requests the command to be aborted.
1189 * CC_ERROR The char could not be accepted due to an error.
1191 public int cmd_char(int c)
1202 /* Perform strict validation in all possible cases. */
1203 if (cmd_mbc_buf_len == 0)
1206 cmd_mbc_buf_index = 1;
1208 if (IS_ASCII_OCTET(c))
1209 cmd_mbc_buf_len = 1;
1210 #if MSDOS_COMPILER || OS2
1211 else if (c == (unsigned char) '\340' && IS_ASCII_OCTET(peekcc()))
1213 /* Assume a special key. */
1214 cmd_mbc_buf_len = 1;
1217 else if (IS_UTF8_LEAD(c))
1219 cmd_mbc_buf_len = utf_len(c);
1223 /* UTF8_INVALID or stray UTF8_TRAIL */
1227 } else if (IS_UTF8_TRAIL(c))
1229 cmd_mbc_buf[cmd_mbc_buf_index++] = c;
1230 if (cmd_mbc_buf_index < cmd_mbc_buf_len)
1232 if (!is_utf8_well_formed(cmd_mbc_buf, cmd_mbc_buf_index))
1234 /* complete, but not well formed (non-shortest form), sequence */
1235 cmd_mbc_buf_len = 0;
1241 /* Flush incomplete (truncated) sequence. */
1242 cmd_mbc_buf_len = 0;
1244 /* Handle new char. */
1248 len = cmd_mbc_buf_len;
1249 cmd_mbc_buf_len = 0;
1255 * Insert the char, even if it is a line-editing char.
1258 return (cmd_ichar(cmd_mbc_buf, len));
1262 * See if it is a line-editing character.
1264 if (in_mca() && len == 1)
1266 action = cmd_edit(c);
1278 * Insert the char into the command buffer.
1280 return (cmd_ichar(cmd_mbc_buf, len));
1284 * Return the number currently in the command buffer.
1286 public LINENUM cmd_int(long *frac)
1292 for (p = cmdbuf; *p >= '0' && *p <= '9'; p++)
1294 if (ckd_mul(&n, n, 10) || ckd_add(&n, n, *p - '0'))
1296 error("Integer is too big", NULL_PARG);
1303 *frac = getfraction(&p, NULL, &err);
1304 /* {{ do something if err is set? }} */
1310 * Return a pointer to the command buffer.
1312 public char * get_cmdbuf(void)
1314 if (cmd_mbc_buf_index < cmd_mbc_buf_len)
1315 /* Don't return buffer containing an incomplete multibyte char. */
1322 * Return the last (most recent) string in the current command history.
1324 public char * cmd_lastpattern(void)
1326 if (curr_mlist == NULL)
1328 return (curr_mlist->curr_mp->prev->string);
1335 static int mlist_size(struct mlist *ml)
1338 for (ml = ml->next; ml->string != NULL; ml = ml->next)
1344 * Get the name of the history file.
1346 static char * histfile_find(int must_exist)
1348 char *home = lgetenv("HOME");
1351 /* Try in $XDG_STATE_HOME, then in $HOME/.local/state, then in $XDG_DATA_HOME, then in $HOME. */
1353 if (isnullenv(home))
1354 home = lgetenv("INIT");
1356 name = dirfile(lgetenv("XDG_STATE_HOME"), &LESSHISTFILE[1], must_exist);
1359 char *dir = dirfile(home, ".local/state", 1);
1362 name = dirfile(dir, &LESSHISTFILE[1], must_exist);
1367 name = dirfile(lgetenv("XDG_DATA_HOME"), &LESSHISTFILE[1], must_exist);
1369 name = dirfile(home, LESSHISTFILE, must_exist);
1373 static char * histfile_name(int must_exist)
1377 /* See if filename is explicitly specified by $LESSHISTFILE. */
1378 name = lgetenv("LESSHISTFILE");
1379 if (!isnullenv(name))
1381 if (strcmp(name, "-") == 0 || strcmp(name, "/dev/null") == 0)
1382 /* $LESSHISTFILE == "-" means don't use a history file. */
1384 return (save(name));
1387 /* See if history file is disabled in the build. */
1388 if (strcmp(LESSHISTFILE, "") == 0 || strcmp(LESSHISTFILE, "-") == 0)
1394 /* If we're writing the file and the file already exists, use it. */
1395 name = histfile_find(1);
1398 name = histfile_find(must_exist);
1403 * Read a .lesshst file and call a callback for each line in the file.
1405 static void read_cmdhist2(void (*action)(void*,struct mlist*,char*), void *uparam, int skip_search, int skip_shell)
1407 struct mlist *ml = NULL;
1408 char line[CMDBUF_SIZE];
1414 filename = histfile_name(1);
1415 if (filename == NULL)
1417 f = fopen(filename, "r");
1421 if (fgets(line, sizeof(line), f) == NULL ||
1422 strncmp(line, HISTFILE_FIRST_LINE, strlen(HISTFILE_FIRST_LINE)) != 0)
1427 while (fgets(line, sizeof(line), f) != NULL)
1429 for (p = line; *p != '\0'; p++)
1431 if (*p == '\n' || *p == '\r')
1437 if (strcmp(line, HISTFILE_SEARCH_SECTION) == 0)
1440 skip = &skip_search;
1441 } else if (strcmp(line, HISTFILE_SHELL_SECTION) == 0)
1443 #if SHELL_ESCAPE || PIPEC
1450 } else if (strcmp(line, HISTFILE_MARK_SECTION) == 0)
1453 } else if (*line == '"')
1457 if (skip != NULL && *skip > 0)
1460 (*action)(uparam, ml, line+1);
1462 } else if (*line == 'm')
1464 (*action)(uparam, NULL, line);
1470 static void read_cmdhist(void (*action)(void*,struct mlist*,char*), void *uparam, int skip_search, int skip_shell)
1474 read_cmdhist2(action, uparam, skip_search, skip_shell);
1475 (*action)(uparam, NULL, NULL); /* signal end of file */
1478 static void addhist_init(void *uparam, struct mlist *ml, char *string)
1481 cmd_addhist(ml, string, 0);
1482 else if (string != NULL)
1483 restore_mark((char*)string); /* stupid const cast */
1485 #endif /* CMD_HISTORY */
1488 * Initialize history from a .lesshist file.
1490 public void init_cmdhist(void)
1493 read_cmdhist(&addhist_init, NULL, 0, 0);
1494 #endif /* CMD_HISTORY */
1498 * Write the header for a section of the history file.
1501 static void write_mlist_header(struct mlist *ml, FILE *f)
1503 if (ml == &mlist_search)
1504 fprintf(f, "%s\n", HISTFILE_SEARCH_SECTION);
1505 #if SHELL_ESCAPE || PIPEC
1506 else if (ml == &mlist_shell)
1507 fprintf(f, "%s\n", HISTFILE_SHELL_SECTION);
1512 * Write all modified entries in an mlist to the history file.
1514 static void write_mlist(struct mlist *ml, FILE *f)
1516 for (ml = ml->next; ml->string != NULL; ml = ml->next)
1520 fprintf(f, "\"%s\n", ml->string);
1523 ml->modified = 0; /* entire mlist is now unmodified */
1527 * Make a temp name in the same directory as filename.
1529 static char * make_tempname(char *filename)
1532 char *tempname = ecalloc(1, strlen(filename)+1);
1533 strcpy(tempname, filename);
1534 lastch = tempname[strlen(tempname)-1];
1535 tempname[strlen(tempname)-1] = (lastch == 'Q') ? 'Z' : 'Q';
1541 struct mlist *mlist;
1546 * Copy entries from the saved history file to a new file.
1547 * At the end of each mlist, append any new entries
1548 * created during this session.
1550 static void copy_hist(void *uparam, struct mlist *ml, char *string)
1552 struct save_ctx *ctx = (struct save_ctx *) uparam;
1554 if (ml != NULL && ml != ctx->mlist) {
1555 /* We're changing mlists. */
1557 /* Append any new entries to the end of the current mlist. */
1558 write_mlist(ctx->mlist, ctx->fout);
1559 /* Write the header for the new mlist. */
1561 write_mlist_header(ctx->mlist, ctx->fout);
1564 if (string == NULL) /* End of file */
1566 /* Write any sections that were not in the original file. */
1567 if (mlist_search.modified)
1569 write_mlist_header(&mlist_search, ctx->fout);
1570 write_mlist(&mlist_search, ctx->fout);
1572 #if SHELL_ESCAPE || PIPEC
1573 if (mlist_shell.modified)
1575 write_mlist_header(&mlist_shell, ctx->fout);
1576 write_mlist(&mlist_shell, ctx->fout);
1579 } else if (ml != NULL)
1581 /* Copy mlist entry. */
1582 fprintf(ctx->fout, "\"%s\n", string);
1586 #endif /* CMD_HISTORY */
1589 * Make a file readable only by its owner.
1591 static void make_file_private(FILE *f)
1596 struct stat statbuf;
1597 int r = fstat(fileno(f), &statbuf);
1598 if (r < 0 || !S_ISREG(statbuf.st_mode))
1599 /* Don't chmod if not a regular file. */
1603 fchmod(fileno(f), 0600);
1608 * Does the history file need to be updated?
1611 static int histfile_modified(void)
1613 if (mlist_search.modified)
1615 #if SHELL_ESCAPE || PIPEC
1616 if (mlist_shell.modified)
1626 * Update the .lesshst file.
1628 public void save_cmdhist(void)
1635 struct save_ctx ctx;
1640 if (secure || !histfile_modified())
1642 histname = histfile_name(0);
1643 if (histname == NULL)
1645 tempname = make_tempname(histname);
1646 fout = fopen(tempname, "w");
1649 make_file_private(fout);
1650 s = lgetenv("LESSHISTSIZE");
1655 skip_search = mlist_size(&mlist_search) - histsize;
1656 #if SHELL_ESCAPE || PIPEC
1657 skip_shell = mlist_size(&mlist_shell) - histsize;
1659 fprintf(fout, "%s\n", HISTFILE_FIRST_LINE);
1662 read_cmdhist(©_hist, &ctx, skip_search, skip_shell);
1663 save_marks(fout, HISTFILE_MARK_SECTION);
1665 #if MSDOS_COMPILER==WIN32C
1667 * Windows rename doesn't remove an existing file,
1668 * making it useless for atomic operations. Sigh.
1672 rename(tempname, histname);
1676 #endif /* CMD_HISTORY */