2 * Copyright (C) 1984-2020 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;
28 static char cmdbuf[CMDBUF_SIZE]; /* Buffer for holding a multi-char command */
29 static int cmd_col; /* Current column of the cursor */
30 static int prompt_col; /* Column of cursor just after prompt */
31 static char *cp; /* Pointer into cmdbuf */
32 static int cmd_offset; /* Index into cmdbuf of first displayed char */
33 static int literal; /* Next input char should not be interpreted */
34 static int updown_match = -1; /* Prefix length in up/down movement */
36 #if TAB_COMPLETE_FILENAME
37 static int cmd_complete LESSPARAMS((int action));
39 * These variables are statics used by cmd_complete.
41 static int in_completion = 0;
43 static char *tk_original;
44 static char *tk_ipoint;
45 static char *tk_trial = NULL;
46 static struct textlist tk_tlist;
49 static int cmd_left();
50 static int cmd_right();
52 #if SPACES_IN_FILENAMES
53 public char openquote = '"';
54 public char closequote = '"';
60 #define HISTFILE_FIRST_LINE ".less-history-file:"
61 #define HISTFILE_SEARCH_SECTION ".search"
62 #define HISTFILE_SHELL_SECTION ".shell"
63 #define HISTFILE_MARK_SECTION ".mark"
66 * A mlist structure represents a command history.
72 struct mlist *curr_mp;
78 * These are the various command histories that exist.
80 struct mlist mlist_search =
81 { &mlist_search, &mlist_search, &mlist_search, NULL, 0 };
82 public void *ml_search = (void *) &mlist_search;
84 struct mlist mlist_examine =
85 { &mlist_examine, &mlist_examine, &mlist_examine, NULL, 0 };
86 public void *ml_examine = (void *) &mlist_examine;
88 #if SHELL_ESCAPE || PIPEC
89 struct mlist mlist_shell =
90 { &mlist_shell, &mlist_shell, &mlist_shell, NULL, 0 };
91 public void *ml_shell = (void *) &mlist_shell;
94 #else /* CMD_HISTORY */
96 /* If CMD_HISTORY is off, these are just flags. */
97 public void *ml_search = (void *)1;
98 public void *ml_examine = (void *)2;
99 #if SHELL_ESCAPE || PIPEC
100 public void *ml_shell = (void *)3;
103 #endif /* CMD_HISTORY */
106 * History for the current command.
108 static struct mlist *curr_mlist = NULL;
109 static int curr_cmdflags;
111 static char cmd_mbc_buf[MAX_UTF_CHAR_LEN];
112 static int cmd_mbc_buf_len;
113 static int cmd_mbc_buf_index;
117 * Reset command buffer (to empty).
120 cmd_reset(VOID_PARAM)
132 * Clear command line.
135 clear_cmd(VOID_PARAM)
137 cmd_col = prompt_col = 0;
143 * Display a string, usually as a prompt for input into the command buffer.
151 constant char *endline = s + strlen(s);
154 char *ns = (char *) s;
156 ch = step_char(&ns, +1, endline);
161 else if (is_composing_char(ch) || is_combining_char(prev_ch, ch))
164 width = is_wide_char(ch) ? 2 : 1;
172 * How many characters are in the command buffer?
175 len_cmdbuf(VOID_PARAM)
178 char *endline = s + strlen(s);
183 step_char(&s, +1, endline);
190 * Common part of cmd_step_right() and cmd_step_left().
191 * {{ Returning pwidth and bswidth separately is a historical artifact
192 * since they're always the same. Maybe clean this up someday. }}
195 cmd_step_common(p, ch, len, pwidth, bswidth)
207 pr = prchar((int) ch);
208 width = (int) strlen(pr);
212 if (is_composing_char(ch))
214 else if (is_ubin_char(ch))
215 width = (int) strlen(pr);
218 LWCHAR prev_ch = step_char(&p, -1, cmdbuf);
219 if (is_combining_char(prev_ch, ch))
222 width = is_wide_char(ch) ? 2 : 1;
233 * Step a pointer one character right in the command buffer.
236 cmd_step_right(pp, pwidth, bswidth)
242 LWCHAR ch = step_char(pp, +1, p + strlen(p));
244 return cmd_step_common(p, ch, *pp - p, pwidth, bswidth);
248 * Step a pointer one character left in the command buffer.
251 cmd_step_left(pp, pwidth, bswidth)
257 LWCHAR ch = step_char(pp, -1, cmdbuf);
259 return cmd_step_common(*pp, ch, p - *pp, pwidth, bswidth);
263 * Repaint the line from cp onwards.
264 * Then position the cursor just after the char old_cp (a pointer into cmdbuf).
268 constant char *old_cp;
271 * Repaint the line from the current position.
278 char *pr = cmd_step_right(&np, &width, NULL);
279 if (cmd_col + width >= sc_width)
289 char *pr = cmd_step_right(&np, &width, NULL);
297 * Back up the cursor to the correct position.
304 * Put the cursor at "home" (just after the prompt),
305 * and set cp to the corresponding char in cmdbuf.
310 while (cmd_col > prompt_col)
314 cmd_step_left(&cp, &width, &bswidth);
315 while (bswidth-- > 0)
320 cp = &cmdbuf[cmd_offset];
324 * Shift the cmdbuf display left a half-screen.
327 cmd_lshift(VOID_PARAM)
334 * Start at the first displayed char, count how far to the
335 * right we'd have to move to reach the center of the screen.
337 s = cmdbuf + cmd_offset;
339 while (cols < (sc_width - prompt_col) / 2 && *s != '\0')
342 cmd_step_right(&s, &width, NULL);
349 cmd_step_right(&ns, &width, NULL);
355 cmd_offset = (int) (s - cmdbuf);
358 cmd_repaint(save_cp);
362 * Shift the cmdbuf display right a half-screen.
365 cmd_rshift(VOID_PARAM)
372 * Start at the first displayed char, count how far to the
373 * left we'd have to move to traverse a half-screen width
374 * of displayed characters.
376 s = cmdbuf + cmd_offset;
378 while (cols < (sc_width - prompt_col) / 2 && s > cmdbuf)
381 cmd_step_left(&s, &width, NULL);
385 cmd_offset = (int) (s - cmdbuf);
388 cmd_repaint(save_cp);
392 * Move cursor right one character.
395 cmd_right(VOID_PARAM)
403 /* Already at the end of the line. */
407 pr = cmd_step_right(&ncp, &width, NULL);
408 if (cmd_col + width >= sc_width)
410 else if (cmd_col + width == sc_width - 1 && cp[1] != '\0')
417 pr = cmd_step_right(&ncp, &width, NULL);
427 * Move cursor left one character.
438 /* Already at the beginning of the line */
444 cmd_step_left(&ncp, &width, &bswidth);
448 if (cmd_col < prompt_col + width)
452 while (bswidth-- > 0)
458 * Insert a char into the command buffer, at the current position.
467 if (strlen(cmdbuf) + clen >= sizeof(cmdbuf)-1)
469 /* No room in the command buffer for another char. */
475 * Make room for the new character (shift the tail of the buffer right).
477 for (s = &cmdbuf[strlen(cmdbuf)]; s >= cp; s--)
480 * Insert the character into the buffer.
482 for (s = cp; s < cp + clen; s++)
485 * Reprint the tail of the line from the inserted char.
494 * Backspace in the command buffer.
495 * Delete the char to the left of the cursor.
498 cmd_erase(VOID_PARAM)
506 * Backspace past beginning of the buffer:
507 * this usually means abort the command.
512 * Move cursor left (to the char being erased).
516 clen = (int) (s - cp);
519 * Remove the char from the buffer (shift the buffer left).
529 * Repaint the buffer after the erased char.
535 * We say that erasing the entire command string causes us
536 * to abort the current command, if CF_QUIT_ON_ERASE is set.
538 if ((curr_cmdflags & CF_QUIT_ON_ERASE) && cp == cmdbuf && *cp == '\0')
544 * Delete the char under the cursor.
547 cmd_delete(VOID_PARAM)
551 /* At end of string; there is no char under the cursor. */
555 * Move right, then use cmd_erase.
563 * Delete the "word" to the left of the cursor.
566 cmd_werase(VOID_PARAM)
568 if (cp > cmdbuf && cp[-1] == ' ')
571 * If the char left of cursor is a space,
572 * erase all the spaces left of cursor (to the first non-space).
574 while (cp > cmdbuf && cp[-1] == ' ')
579 * If the char left of cursor is not a space,
580 * erase all the nonspaces left of cursor (the whole "word").
582 while (cp > cmdbuf && cp[-1] != ' ')
589 * Delete the "word" under the cursor.
592 cmd_wdelete(VOID_PARAM)
597 * If the char under the cursor is a space,
598 * delete it and all the spaces right of cursor.
605 * If the char under the cursor is not a space,
606 * delete it and all nonspaces right of cursor (the whole word).
608 while (*cp != ' ' && *cp != '\0')
615 * Delete all chars in the command buffer.
620 if (cmdbuf[0] == '\0')
622 /* Buffer is already empty; abort the current command. */
632 * We say that erasing the entire command string causes us
633 * to abort the current command, if CF_QUIT_ON_ERASE is set.
635 if (curr_cmdflags & CF_QUIT_ON_ERASE)
641 * Select an mlist structure to be the current command history.
644 set_mlist(mlist, cmdflags)
649 curr_mlist = (struct mlist *) mlist;
650 curr_cmdflags = cmdflags;
652 /* Make sure the next up-arrow moves to the last string in the mlist. */
653 if (curr_mlist != NULL)
654 curr_mlist->curr_mp = curr_mlist;
660 * Move up or down in the currently selected command history list.
661 * Only consider entries whose first updown_match chars are equal to
662 * cmdbuf's corresponding chars.
671 if (curr_mlist == NULL)
674 * The current command has no history list.
680 if (updown_match < 0)
682 updown_match = (int) (cp - cmdbuf);
686 * Find the next history entry which matches.
688 for (ml = curr_mlist->curr_mp;;)
690 ml = (action == EC_UP) ? ml->prev : ml->next;
691 if (ml == curr_mlist)
694 * We reached the end (or beginning) of the list.
698 if (strncmp(cmdbuf, ml->string, updown_match) == 0)
701 * This entry matches; stop here.
702 * Copy the entry into cmdbuf and echo it on the screen.
704 curr_mlist->curr_mp = ml;
712 for (cp = cmdbuf; *cp != '\0'; )
718 * We didn't find a history entry that matches.
734 ml->prev = mlist->prev;
735 mlist->prev->next = ml;
746 ml->prev->next = ml->next;
747 ml->next->prev = ml->prev;
751 * Add a string to an mlist.
754 cmd_addhist(mlist, cmd, modified)
763 * Don't save a trivial command.
765 if (strlen(cmd) == 0)
770 struct mlist *next = NULL;
771 for (ml = mlist->next; ml->string != NULL; ml = next)
774 if (strcmp(ml->string, cmd) == 0)
784 * Save the command unless it's a duplicate of the
785 * last command in the history.
788 if (ml == mlist || strcmp(ml->string, cmd) != 0)
791 * Did not find command in history.
792 * Save the command and put it at the end of the history list.
794 ml = (struct mlist *) ecalloc(1, sizeof(struct mlist));
795 ml->string = save(cmd);
796 ml->modified = modified;
800 * Point to the cmd just after the just-accepted command.
801 * Thus, an UPARROW will always retrieve the previous command.
803 mlist->curr_mp = ml->next;
808 * Accept the command in the command buffer.
809 * Add it to the currently selected history list.
812 cmd_accept(VOID_PARAM)
816 * Nothing to do if there is no currently selected history list.
818 if (curr_mlist == NULL || curr_mlist == ml_examine)
820 cmd_addhist(curr_mlist, cmdbuf, 1);
821 curr_mlist->modified = 1;
826 * Try to perform a line-edit function on the command buffer,
827 * using a specified char as a line-editing command.
829 * CC_PASS The char does not invoke a line edit function.
830 * CC_OK Line edit function done.
831 * CC_QUIT The char requests the current command to be aborted.
840 #if TAB_COMPLETE_FILENAME
841 #define not_in_completion() in_completion = 0
843 #define not_in_completion(VOID_PARAM)
847 * See if the char is indeed a line-editing command.
851 if (curr_mlist == NULL)
853 * No current history; don't accept history manipulation cmds.
855 flags |= EC_NOHISTORY;
857 #if TAB_COMPLETE_FILENAME
858 if (curr_mlist == ml_search)
860 * In a search command; don't accept file-completion cmds.
862 flags |= EC_NOCOMPLETE;
865 action = editchar(c, flags);
871 return (cmd_right());
877 while (*cp != '\0' && *cp != ' ')
884 while (cp > cmdbuf && cp[-1] == ' ')
886 while (cp > cmdbuf && cp[-1] != ' ')
905 return (cmd_erase());
915 return (cmd_werase());
918 return (cmd_delete());
921 return (cmd_wdelete());
929 return (cmd_updown(action));
931 #if TAB_COMPLETE_FILENAME
935 return (cmd_complete(action));
945 #if TAB_COMPLETE_FILENAME
947 * Insert a string into the command buffer, at the current position.
955 char *endline = str + strlen(str);
957 for (s = str; *s != '\0'; )
960 step_char(&s, +1, endline);
961 action = cmd_ichar(os, s - os);
969 * Find the beginning and end of the "current" word.
970 * This is the word which the cursor (cp) is inside or at the end of.
971 * Return pointer to the beginning of the word and put the
972 * cursor at the end of the word.
975 delimit_word(VOID_PARAM)
978 #if SPACES_IN_FILENAMES
980 int delim_quoted = 0;
982 constant char *esc = get_meta_escape();
983 int esclen = (int) strlen(esc);
987 * Move cursor to end of word.
989 if (*cp != ' ' && *cp != '\0')
992 * Cursor is on a nonspace.
993 * Move cursor right to the next space.
995 while (*cp != ' ' && *cp != '\0')
997 } else if (cp > cmdbuf && cp[-1] != ' ')
1000 * Cursor is on a space, and char to the left is a nonspace.
1001 * We're already at the end of the word.
1008 * Cursor is on a space and char to the left is a space.
1009 * Huh? There's no word here.
1015 * Find the beginning of the word which the cursor is in.
1019 #if SPACES_IN_FILENAMES
1021 * If we have an unbalanced quote (that is, an open quote
1022 * without a corresponding close quote), we return everything
1023 * from the open quote, including spaces.
1025 for (word = cmdbuf; word < cp; word++)
1030 for (p = cmdbuf; p < cp; p++)
1035 } else if (esclen > 0 && p + esclen < cp &&
1036 strncmp(p, esc, esclen) == 0)
1040 } else if (delim_quoted)
1042 if (*p == closequote)
1044 } else /* (!delim_quoted) */
1046 if (*p == openquote)
1057 * Set things up to enter completion mode.
1058 * Expand the word under the cursor into a list of filenames
1059 * which start with that word, and set tk_text to that list.
1062 init_compl(VOID_PARAM)
1068 * Get rid of any previous tk_text.
1070 if (tk_text != NULL)
1076 * Find the original (uncompleted) word in the command buffer.
1078 word = delimit_word();
1082 * Set the insertion point to the point in the command buffer
1083 * where the original (uncompleted) word now sits.
1087 * Save the original (uncompleted) word
1089 if (tk_original != NULL)
1091 tk_original = (char *) ecalloc(cp-word+1, sizeof(char));
1092 strncpy(tk_original, word, cp-word);
1094 * Get the expanded filename.
1095 * This may result in a single filename, or
1096 * a blank-separated list of filenames.
1100 if (*word != openquote)
1102 tk_text = fcomplete(word);
1108 char *qword = shell_quote(word+1);
1111 tk_text = fcomplete(word+1);
1114 tk_text = fcomplete(qword);
1122 * Return the next word in the current completion list.
1125 next_compl(action, prev)
1132 return (forw_textlist(&tk_tlist, prev));
1134 return (back_textlist(&tk_tlist, prev));
1141 * Complete the filename before (or under) the cursor.
1142 * cmd_complete may be called multiple times. The global in_completion
1143 * remembers whether this call is the first time (create the list),
1144 * or a subsequent time (step thru the list).
1147 cmd_complete(action)
1152 if (!in_completion || action == EC_EXPAND)
1155 * Expand the word under the cursor and
1156 * use the first word in the expansion
1157 * (or the entire expansion if we're doing EC_EXPAND).
1160 if (tk_text == NULL)
1165 if (action == EC_EXPAND)
1168 * Use the whole list.
1174 * Use the first filename in the list.
1177 init_textlist(&tk_tlist, tk_text);
1178 tk_trial = next_compl(action, (char*)NULL);
1183 * We already have a completion list.
1184 * Use the next/previous filename from the list.
1186 tk_trial = next_compl(action, tk_trial);
1190 * Remove the original word, or the previous trial completion.
1192 while (cp > tk_ipoint)
1195 if (tk_trial == NULL)
1198 * There are no more trial completions.
1199 * Insert the original (uncompleted) filename.
1202 if (cmd_istr(tk_original) != CC_OK)
1207 * Insert trial completion.
1209 if (cmd_istr(tk_trial) != CC_OK)
1212 * If it is a directory, append a slash.
1214 if (is_dir(tk_trial))
1216 if (cp > cmdbuf && cp[-1] == closequote)
1218 s = lgetenv("LESSSEPARATOR");
1221 if (cmd_istr(s) != CC_OK)
1234 #endif /* TAB_COMPLETE_FILENAME */
1237 * Process a single character of a multi-character command, such as
1238 * a number, or the pattern of a search command.
1240 * CC_OK The char was accepted.
1241 * CC_QUIT The char requests the command to be aborted.
1242 * CC_ERROR The char could not be accepted due to an error.
1257 /* Perform strict validation in all possible cases. */
1258 if (cmd_mbc_buf_len == 0)
1261 cmd_mbc_buf_index = 1;
1263 if (IS_ASCII_OCTET(c))
1264 cmd_mbc_buf_len = 1;
1265 #if MSDOS_COMPILER || OS2
1266 else if (c == (unsigned char) '\340' && IS_ASCII_OCTET(peekcc()))
1268 /* Assume a special key. */
1269 cmd_mbc_buf_len = 1;
1272 else if (IS_UTF8_LEAD(c))
1274 cmd_mbc_buf_len = utf_len(c);
1278 /* UTF8_INVALID or stray UTF8_TRAIL */
1282 } else if (IS_UTF8_TRAIL(c))
1284 cmd_mbc_buf[cmd_mbc_buf_index++] = c;
1285 if (cmd_mbc_buf_index < cmd_mbc_buf_len)
1287 if (!is_utf8_well_formed(cmd_mbc_buf, cmd_mbc_buf_index))
1289 /* complete, but not well formed (non-shortest form), sequence */
1290 cmd_mbc_buf_len = 0;
1296 /* Flush incomplete (truncated) sequence. */
1297 cmd_mbc_buf_len = 0;
1299 /* Handle new char. */
1303 len = cmd_mbc_buf_len;
1304 cmd_mbc_buf_len = 0;
1310 * Insert the char, even if it is a line-editing char.
1313 return (cmd_ichar(cmd_mbc_buf, len));
1317 * See if it is a line-editing character.
1319 if (in_mca() && len == 1)
1321 action = cmd_edit(c);
1333 * Insert the char into the command buffer.
1335 return (cmd_ichar(cmd_mbc_buf, len));
1339 * Return the number currently in the command buffer.
1349 for (p = cmdbuf; *p >= '0' && *p <= '9'; p++)
1350 n = (n * 10) + (*p - '0');
1354 *frac = getfraction(&p, NULL, &err);
1355 /* {{ do something if err is set? }} */
1361 * Return a pointer to the command buffer.
1364 get_cmdbuf(VOID_PARAM)
1371 * Return the last (most recent) string in the current command history.
1374 cmd_lastpattern(VOID_PARAM)
1376 if (curr_mlist == NULL)
1378 return (curr_mlist->curr_mp->prev->string);
1390 for (ml = ml->next; ml->string != NULL; ml = ml->next)
1396 * Get the name of the history file.
1399 histfile_name(VOID_PARAM)
1405 /* See if filename is explicitly specified by $LESSHISTFILE. */
1406 name = lgetenv("LESSHISTFILE");
1407 if (!isnullenv(name))
1409 if (strcmp(name, "-") == 0 || strcmp(name, "/dev/null") == 0)
1410 /* $LESSHISTFILE == "-" means don't use a history file. */
1412 return (save(name));
1415 /* See if history file is disabled in the build. */
1416 if (strcmp(LESSHISTFILE, "") == 0 || strcmp(LESSHISTFILE, "-") == 0)
1419 /* Otherwise, file is in $HOME. */
1420 home = lgetenv("HOME");
1421 if (isnullenv(home))
1424 home = lgetenv("INIT");
1425 if (isnullenv(home))
1429 len = (int) (strlen(home) + strlen(LESSHISTFILE) + 2);
1430 name = (char *) ecalloc(len, sizeof(char));
1431 SNPRINTF2(name, len, "%s/%s", home, LESSHISTFILE);
1436 * Read a .lesshst file and call a callback for each line in the file.
1439 read_cmdhist2(action, uparam, skip_search, skip_shell)
1440 void (*action)(void*,struct mlist*,char*);
1445 struct mlist *ml = NULL;
1446 char line[CMDBUF_SIZE];
1452 filename = histfile_name();
1453 if (filename == NULL)
1455 f = fopen(filename, "r");
1459 if (fgets(line, sizeof(line), f) == NULL ||
1460 strncmp(line, HISTFILE_FIRST_LINE, strlen(HISTFILE_FIRST_LINE)) != 0)
1465 while (fgets(line, sizeof(line), f) != NULL)
1467 for (p = line; *p != '\0'; p++)
1469 if (*p == '\n' || *p == '\r')
1475 if (strcmp(line, HISTFILE_SEARCH_SECTION) == 0)
1478 skip = &skip_search;
1479 } else if (strcmp(line, HISTFILE_SHELL_SECTION) == 0)
1481 #if SHELL_ESCAPE || PIPEC
1488 } else if (strcmp(line, HISTFILE_MARK_SECTION) == 0)
1491 } else if (*line == '"')
1495 if (skip != NULL && *skip > 0)
1498 (*action)(uparam, ml, line+1);
1500 } else if (*line == 'm')
1502 (*action)(uparam, NULL, line);
1509 read_cmdhist(action, uparam, skip_search, skip_shell)
1510 void (*action)(void*,struct mlist*,char*);
1515 read_cmdhist2(action, uparam, skip_search, skip_shell);
1516 (*action)(uparam, NULL, NULL); /* signal end of file */
1520 addhist_init(void *uparam, struct mlist *ml, char *string)
1523 cmd_addhist(ml, string, 0);
1524 else if (string != NULL)
1525 restore_mark(string);
1527 #endif /* CMD_HISTORY */
1530 * Initialize history from a .lesshist file.
1533 init_cmdhist(VOID_PARAM)
1536 read_cmdhist(&addhist_init, NULL, 0, 0);
1537 #endif /* CMD_HISTORY */
1541 * Write the header for a section of the history file.
1545 write_mlist_header(ml, f)
1549 if (ml == &mlist_search)
1550 fprintf(f, "%s\n", HISTFILE_SEARCH_SECTION);
1551 #if SHELL_ESCAPE || PIPEC
1552 else if (ml == &mlist_shell)
1553 fprintf(f, "%s\n", HISTFILE_SHELL_SECTION);
1558 * Write all modified entries in an mlist to the history file.
1565 for (ml = ml->next; ml->string != NULL; ml = ml->next)
1569 fprintf(f, "\"%s\n", ml->string);
1572 ml->modified = 0; /* entire mlist is now unmodified */
1576 * Make a temp name in the same directory as filename.
1579 make_tempname(filename)
1583 char *tempname = ecalloc(1, strlen(filename)+1);
1584 strcpy(tempname, filename);
1585 lastch = tempname[strlen(tempname)-1];
1586 tempname[strlen(tempname)-1] = (lastch == 'Q') ? 'Z' : 'Q';
1592 struct mlist *mlist;
1597 * Copy entries from the saved history file to a new file.
1598 * At the end of each mlist, append any new entries
1599 * created during this session.
1602 copy_hist(void *uparam, struct mlist *ml, char *string)
1604 struct save_ctx *ctx = (struct save_ctx *) uparam;
1606 if (ml != NULL && ml != ctx->mlist) {
1607 /* We're changing mlists. */
1609 /* Append any new entries to the end of the current mlist. */
1610 write_mlist(ctx->mlist, ctx->fout);
1611 /* Write the header for the new mlist. */
1613 write_mlist_header(ctx->mlist, ctx->fout);
1616 if (string == NULL) /* End of file */
1618 /* Write any sections that were not in the original file. */
1619 if (mlist_search.modified)
1621 write_mlist_header(&mlist_search, ctx->fout);
1622 write_mlist(&mlist_search, ctx->fout);
1624 #if SHELL_ESCAPE || PIPEC
1625 if (mlist_shell.modified)
1627 write_mlist_header(&mlist_shell, ctx->fout);
1628 write_mlist(&mlist_shell, ctx->fout);
1631 } else if (ml != NULL)
1633 /* Copy mlist entry. */
1634 fprintf(ctx->fout, "\"%s\n", string);
1638 #endif /* CMD_HISTORY */
1641 * Make a file readable only by its owner.
1644 make_file_private(f)
1650 struct stat statbuf;
1651 int r = fstat(fileno(f), &statbuf);
1652 if (r < 0 || !S_ISREG(statbuf.st_mode))
1653 /* Don't chmod if not a regular file. */
1657 fchmod(fileno(f), 0600);
1662 * Does the history file need to be updated?
1665 histfile_modified(VOID_PARAM)
1667 if (mlist_search.modified)
1669 #if SHELL_ESCAPE || PIPEC
1670 if (mlist_shell.modified)
1681 * Update the .lesshst file.
1684 save_cmdhist(VOID_PARAM)
1691 struct save_ctx ctx;
1696 if (!histfile_modified())
1698 histname = histfile_name();
1699 if (histname == NULL)
1701 tempname = make_tempname(histname);
1702 fout = fopen(tempname, "w");
1705 make_file_private(fout);
1706 s = lgetenv("LESSHISTSIZE");
1711 skip_search = mlist_size(&mlist_search) - histsize;
1712 #if SHELL_ESCAPE || PIPEC
1713 skip_shell = mlist_size(&mlist_shell) - histsize;
1715 fprintf(fout, "%s\n", HISTFILE_FIRST_LINE);
1718 read_cmdhist(©_hist, &ctx, skip_search, skip_shell);
1719 save_marks(fout, HISTFILE_MARK_SECTION);
1721 #if MSDOS_COMPILER==WIN32C
1723 * Windows rename doesn't remove an existing file,
1724 * making it useless for atomic operations. Sigh.
1728 rename(tempname, histname);
1732 #endif /* CMD_HISTORY */