2 * Copyright (C) 1984-2021 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 * Put the cursor at "home" (just after the prompt),
264 * and set cp to the corresponding char in cmdbuf.
269 while (cmd_col > prompt_col)
273 cmd_step_left(&cp, &width, &bswidth);
274 while (bswidth-- > 0)
279 cp = &cmdbuf[cmd_offset];
283 * Repaint the line from cp onwards.
284 * Then position the cursor just after the char old_cp (a pointer into cmdbuf).
288 constant char *old_cp;
291 * Repaint the line from the current position.
303 char *pr = cmd_step_right(&np, &width, NULL);
304 if (cmd_col + width >= sc_width)
314 char *pr = cmd_step_right(&np, &width, NULL);
322 * Back up the cursor to the correct position.
329 * Shift the cmdbuf display left a half-screen.
332 cmd_lshift(VOID_PARAM)
339 * Start at the first displayed char, count how far to the
340 * right we'd have to move to reach the center of the screen.
342 s = cmdbuf + cmd_offset;
344 while (cols < (sc_width - prompt_col) / 2 && *s != '\0')
347 cmd_step_right(&s, &width, NULL);
354 cmd_step_right(&ns, &width, NULL);
360 cmd_offset = (int) (s - cmdbuf);
363 cmd_repaint(save_cp);
367 * Shift the cmdbuf display right a half-screen.
370 cmd_rshift(VOID_PARAM)
377 * Start at the first displayed char, count how far to the
378 * left we'd have to move to traverse a half-screen width
379 * of displayed characters.
381 s = cmdbuf + cmd_offset;
383 while (cols < (sc_width - prompt_col) / 2 && s > cmdbuf)
386 cmd_step_left(&s, &width, NULL);
390 cmd_offset = (int) (s - cmdbuf);
393 cmd_repaint(save_cp);
397 * Move cursor right one character.
400 cmd_right(VOID_PARAM)
408 /* Already at the end of the line. */
412 pr = cmd_step_right(&ncp, &width, NULL);
413 if (cmd_col + width >= sc_width)
415 else if (cmd_col + width == sc_width - 1 && cp[1] != '\0')
422 pr = cmd_step_right(&ncp, &width, NULL);
432 * Move cursor left one character.
443 /* Already at the beginning of the line */
449 cmd_step_left(&ncp, &width, &bswidth);
453 if (cmd_col < prompt_col + width)
457 while (bswidth-- > 0)
463 * Insert a char into the command buffer, at the current position.
472 if (strlen(cmdbuf) + clen >= sizeof(cmdbuf)-1)
474 /* No room in the command buffer for another char. */
480 * Make room for the new character (shift the tail of the buffer right).
482 for (s = &cmdbuf[strlen(cmdbuf)]; s >= cp; s--)
485 * Insert the character into the buffer.
487 for (s = cp; s < cp + clen; s++)
490 * Reprint the tail of the line from the inserted char.
499 * Backspace in the command buffer.
500 * Delete the char to the left of the cursor.
503 cmd_erase(VOID_PARAM)
511 * Backspace past beginning of the buffer:
512 * this usually means abort the command.
517 * Move cursor left (to the char being erased).
521 clen = (int) (s - cp);
524 * Remove the char from the buffer (shift the buffer left).
534 * Repaint the buffer after the erased char.
540 * We say that erasing the entire command string causes us
541 * to abort the current command, if CF_QUIT_ON_ERASE is set.
543 if ((curr_cmdflags & CF_QUIT_ON_ERASE) && cp == cmdbuf && *cp == '\0')
549 * Delete the char under the cursor.
552 cmd_delete(VOID_PARAM)
556 /* At end of string; there is no char under the cursor. */
560 * Move right, then use cmd_erase.
568 * Delete the "word" to the left of the cursor.
571 cmd_werase(VOID_PARAM)
573 if (cp > cmdbuf && cp[-1] == ' ')
576 * If the char left of cursor is a space,
577 * erase all the spaces left of cursor (to the first non-space).
579 while (cp > cmdbuf && cp[-1] == ' ')
584 * If the char left of cursor is not a space,
585 * erase all the nonspaces left of cursor (the whole "word").
587 while (cp > cmdbuf && cp[-1] != ' ')
594 * Delete the "word" under the cursor.
597 cmd_wdelete(VOID_PARAM)
602 * If the char under the cursor is a space,
603 * delete it and all the spaces right of cursor.
610 * If the char under the cursor is not a space,
611 * delete it and all nonspaces right of cursor (the whole word).
613 while (*cp != ' ' && *cp != '\0')
620 * Delete all chars in the command buffer.
625 if (cmdbuf[0] == '\0')
627 /* Buffer is already empty; abort the current command. */
637 * We say that erasing the entire command string causes us
638 * to abort the current command, if CF_QUIT_ON_ERASE is set.
640 if (curr_cmdflags & CF_QUIT_ON_ERASE)
646 * Select an mlist structure to be the current command history.
649 set_mlist(mlist, cmdflags)
654 curr_mlist = (struct mlist *) mlist;
655 curr_cmdflags = cmdflags;
657 /* Make sure the next up-arrow moves to the last string in the mlist. */
658 if (curr_mlist != NULL)
659 curr_mlist->curr_mp = curr_mlist;
665 * Move up or down in the currently selected command history list.
666 * Only consider entries whose first updown_match chars are equal to
667 * cmdbuf's corresponding chars.
676 if (curr_mlist == NULL)
679 * The current command has no history list.
685 if (updown_match < 0)
687 updown_match = (int) (cp - cmdbuf);
691 * Find the next history entry which matches.
693 for (ml = curr_mlist->curr_mp;;)
695 ml = (action == EC_UP) ? ml->prev : ml->next;
696 if (ml == curr_mlist)
699 * We reached the end (or beginning) of the list.
703 if (strncmp(cmdbuf, ml->string, updown_match) == 0)
706 * This entry matches; stop here.
707 * Copy the entry into cmdbuf and echo it on the screen.
709 curr_mlist->curr_mp = ml;
717 for (cp = cmdbuf; *cp != '\0'; )
723 * We didn't find a history entry that matches.
739 ml->prev = mlist->prev;
740 mlist->prev->next = ml;
751 ml->prev->next = ml->next;
752 ml->next->prev = ml->prev;
756 * Add a string to an mlist.
759 cmd_addhist(mlist, cmd, modified)
768 * Don't save a trivial command.
770 if (strlen(cmd) == 0)
775 struct mlist *next = NULL;
776 for (ml = mlist->next; ml->string != NULL; ml = next)
779 if (strcmp(ml->string, cmd) == 0)
789 * Save the command unless it's a duplicate of the
790 * last command in the history.
793 if (ml == mlist || strcmp(ml->string, cmd) != 0)
796 * Did not find command in history.
797 * Save the command and put it at the end of the history list.
799 ml = (struct mlist *) ecalloc(1, sizeof(struct mlist));
800 ml->string = save(cmd);
801 ml->modified = modified;
805 * Point to the cmd just after the just-accepted command.
806 * Thus, an UPARROW will always retrieve the previous command.
808 mlist->curr_mp = ml->next;
813 * Accept the command in the command buffer.
814 * Add it to the currently selected history list.
817 cmd_accept(VOID_PARAM)
821 * Nothing to do if there is no currently selected history list.
823 if (curr_mlist == NULL || curr_mlist == ml_examine)
825 cmd_addhist(curr_mlist, cmdbuf, 1);
826 curr_mlist->modified = 1;
831 * Try to perform a line-edit function on the command buffer,
832 * using a specified char as a line-editing command.
834 * CC_PASS The char does not invoke a line edit function.
835 * CC_OK Line edit function done.
836 * CC_QUIT The char requests the current command to be aborted.
845 #if TAB_COMPLETE_FILENAME
846 #define not_in_completion() in_completion = 0
848 #define not_in_completion(VOID_PARAM)
852 * See if the char is indeed a line-editing command.
856 if (curr_mlist == NULL)
858 * No current history; don't accept history manipulation cmds.
860 flags |= ECF_NOHISTORY;
862 #if TAB_COMPLETE_FILENAME
863 if (curr_mlist == ml_search)
865 * In a search command; don't accept file-completion cmds.
867 flags |= ECF_NOCOMPLETE;
870 action = editchar(c, flags);
878 return (cmd_right());
884 while (*cp != '\0' && *cp != ' ')
891 while (cp > cmdbuf && cp[-1] == ' ')
893 while (cp > cmdbuf && cp[-1] != ' ')
912 return (cmd_erase());
922 return (cmd_werase());
925 return (cmd_delete());
928 return (cmd_wdelete());
936 return (cmd_updown(action));
938 #if TAB_COMPLETE_FILENAME
942 return (cmd_complete(action));
950 #if TAB_COMPLETE_FILENAME
952 * Insert a string into the command buffer, at the current position.
960 char *endline = str + strlen(str);
962 for (s = str; *s != '\0'; )
965 step_char(&s, +1, endline);
966 action = cmd_ichar(os, s - os);
974 * Find the beginning and end of the "current" word.
975 * This is the word which the cursor (cp) is inside or at the end of.
976 * Return pointer to the beginning of the word and put the
977 * cursor at the end of the word.
980 delimit_word(VOID_PARAM)
983 #if SPACES_IN_FILENAMES
985 int delim_quoted = 0;
987 constant char *esc = get_meta_escape();
988 int esclen = (int) strlen(esc);
992 * Move cursor to end of word.
994 if (*cp != ' ' && *cp != '\0')
997 * Cursor is on a nonspace.
998 * Move cursor right to the next space.
1000 while (*cp != ' ' && *cp != '\0')
1002 } else if (cp > cmdbuf && cp[-1] != ' ')
1005 * Cursor is on a space, and char to the left is a nonspace.
1006 * We're already at the end of the word.
1013 * Cursor is on a space and char to the left is a space.
1014 * Huh? There's no word here.
1020 * Find the beginning of the word which the cursor is in.
1024 #if SPACES_IN_FILENAMES
1026 * If we have an unbalanced quote (that is, an open quote
1027 * without a corresponding close quote), we return everything
1028 * from the open quote, including spaces.
1030 for (word = cmdbuf; word < cp; word++)
1035 for (p = cmdbuf; p < cp; p++)
1040 } else if (esclen > 0 && p + esclen < cp &&
1041 strncmp(p, esc, esclen) == 0)
1045 } else if (delim_quoted)
1047 if (*p == closequote)
1049 } else /* (!delim_quoted) */
1051 if (*p == openquote)
1062 * Set things up to enter completion mode.
1063 * Expand the word under the cursor into a list of filenames
1064 * which start with that word, and set tk_text to that list.
1067 init_compl(VOID_PARAM)
1073 * Get rid of any previous tk_text.
1075 if (tk_text != NULL)
1081 * Find the original (uncompleted) word in the command buffer.
1083 word = delimit_word();
1087 * Set the insertion point to the point in the command buffer
1088 * where the original (uncompleted) word now sits.
1092 * Save the original (uncompleted) word
1094 if (tk_original != NULL)
1096 tk_original = (char *) ecalloc(cp-word+1, sizeof(char));
1097 strncpy(tk_original, word, cp-word);
1099 * Get the expanded filename.
1100 * This may result in a single filename, or
1101 * a blank-separated list of filenames.
1105 if (*word != openquote)
1107 tk_text = fcomplete(word);
1113 char *qword = shell_quote(word+1);
1116 tk_text = fcomplete(word+1);
1119 tk_text = fcomplete(qword);
1127 * Return the next word in the current completion list.
1130 next_compl(action, prev)
1137 return (forw_textlist(&tk_tlist, prev));
1139 return (back_textlist(&tk_tlist, prev));
1146 * Complete the filename before (or under) the cursor.
1147 * cmd_complete may be called multiple times. The global in_completion
1148 * remembers whether this call is the first time (create the list),
1149 * or a subsequent time (step thru the list).
1152 cmd_complete(action)
1157 if (!in_completion || action == EC_EXPAND)
1160 * Expand the word under the cursor and
1161 * use the first word in the expansion
1162 * (or the entire expansion if we're doing EC_EXPAND).
1165 if (tk_text == NULL)
1170 if (action == EC_EXPAND)
1173 * Use the whole list.
1179 * Use the first filename in the list.
1182 init_textlist(&tk_tlist, tk_text);
1183 tk_trial = next_compl(action, (char*)NULL);
1188 * We already have a completion list.
1189 * Use the next/previous filename from the list.
1191 tk_trial = next_compl(action, tk_trial);
1195 * Remove the original word, or the previous trial completion.
1197 while (cp > tk_ipoint)
1200 if (tk_trial == NULL)
1203 * There are no more trial completions.
1204 * Insert the original (uncompleted) filename.
1207 if (cmd_istr(tk_original) != CC_OK)
1212 * Insert trial completion.
1214 if (cmd_istr(tk_trial) != CC_OK)
1217 * If it is a directory, append a slash.
1219 if (is_dir(tk_trial))
1221 if (cp > cmdbuf && cp[-1] == closequote)
1223 s = lgetenv("LESSSEPARATOR");
1226 if (cmd_istr(s) != CC_OK)
1239 #endif /* TAB_COMPLETE_FILENAME */
1242 * Process a single character of a multi-character command, such as
1243 * a number, or the pattern of a search command.
1245 * CC_OK The char was accepted.
1246 * CC_QUIT The char requests the command to be aborted.
1247 * CC_ERROR The char could not be accepted due to an error.
1262 /* Perform strict validation in all possible cases. */
1263 if (cmd_mbc_buf_len == 0)
1266 cmd_mbc_buf_index = 1;
1268 if (IS_ASCII_OCTET(c))
1269 cmd_mbc_buf_len = 1;
1270 #if MSDOS_COMPILER || OS2
1271 else if (c == (unsigned char) '\340' && IS_ASCII_OCTET(peekcc()))
1273 /* Assume a special key. */
1274 cmd_mbc_buf_len = 1;
1277 else if (IS_UTF8_LEAD(c))
1279 cmd_mbc_buf_len = utf_len(c);
1283 /* UTF8_INVALID or stray UTF8_TRAIL */
1287 } else if (IS_UTF8_TRAIL(c))
1289 cmd_mbc_buf[cmd_mbc_buf_index++] = c;
1290 if (cmd_mbc_buf_index < cmd_mbc_buf_len)
1292 if (!is_utf8_well_formed(cmd_mbc_buf, cmd_mbc_buf_index))
1294 /* complete, but not well formed (non-shortest form), sequence */
1295 cmd_mbc_buf_len = 0;
1301 /* Flush incomplete (truncated) sequence. */
1302 cmd_mbc_buf_len = 0;
1304 /* Handle new char. */
1308 len = cmd_mbc_buf_len;
1309 cmd_mbc_buf_len = 0;
1315 * Insert the char, even if it is a line-editing char.
1318 return (cmd_ichar(cmd_mbc_buf, len));
1322 * See if it is a line-editing character.
1324 if (in_mca() && len == 1)
1326 action = cmd_edit(c);
1338 * Insert the char into the command buffer.
1340 return (cmd_ichar(cmd_mbc_buf, len));
1344 * Return the number currently in the command buffer.
1354 for (p = cmdbuf; *p >= '0' && *p <= '9'; p++)
1355 n = (n * 10) + (*p - '0');
1359 *frac = getfraction(&p, NULL, &err);
1360 /* {{ do something if err is set? }} */
1366 * Return a pointer to the command buffer.
1369 get_cmdbuf(VOID_PARAM)
1376 * Return the last (most recent) string in the current command history.
1379 cmd_lastpattern(VOID_PARAM)
1381 if (curr_mlist == NULL)
1383 return (curr_mlist->curr_mp->prev->string);
1395 for (ml = ml->next; ml->string != NULL; ml = ml->next)
1401 * Get the name of the history file.
1404 histfile_name(VOID_PARAM)
1410 /* See if filename is explicitly specified by $LESSHISTFILE. */
1411 name = lgetenv("LESSHISTFILE");
1412 if (!isnullenv(name))
1414 if (strcmp(name, "-") == 0 || strcmp(name, "/dev/null") == 0)
1415 /* $LESSHISTFILE == "-" means don't use a history file. */
1417 return (save(name));
1420 /* See if history file is disabled in the build. */
1421 if (strcmp(LESSHISTFILE, "") == 0 || strcmp(LESSHISTFILE, "-") == 0)
1424 /* Otherwise, file is in $HOME. */
1425 home = lgetenv("HOME");
1426 if (isnullenv(home))
1429 home = lgetenv("INIT");
1430 if (isnullenv(home))
1434 len = (int) (strlen(home) + strlen(LESSHISTFILE) + 2);
1435 name = (char *) ecalloc(len, sizeof(char));
1436 SNPRINTF2(name, len, "%s/%s", home, LESSHISTFILE);
1441 * Read a .lesshst file and call a callback for each line in the file.
1444 read_cmdhist2(action, uparam, skip_search, skip_shell)
1445 void (*action)(void*,struct mlist*,char*);
1450 struct mlist *ml = NULL;
1451 char line[CMDBUF_SIZE];
1457 filename = histfile_name();
1458 if (filename == NULL)
1460 f = fopen(filename, "r");
1464 if (fgets(line, sizeof(line), f) == NULL ||
1465 strncmp(line, HISTFILE_FIRST_LINE, strlen(HISTFILE_FIRST_LINE)) != 0)
1470 while (fgets(line, sizeof(line), f) != NULL)
1472 for (p = line; *p != '\0'; p++)
1474 if (*p == '\n' || *p == '\r')
1480 if (strcmp(line, HISTFILE_SEARCH_SECTION) == 0)
1483 skip = &skip_search;
1484 } else if (strcmp(line, HISTFILE_SHELL_SECTION) == 0)
1486 #if SHELL_ESCAPE || PIPEC
1493 } else if (strcmp(line, HISTFILE_MARK_SECTION) == 0)
1496 } else if (*line == '"')
1500 if (skip != NULL && *skip > 0)
1503 (*action)(uparam, ml, line+1);
1505 } else if (*line == 'm')
1507 (*action)(uparam, NULL, line);
1514 read_cmdhist(action, uparam, skip_search, skip_shell)
1515 void (*action)(void*,struct mlist*,char*);
1520 read_cmdhist2(action, uparam, skip_search, skip_shell);
1521 (*action)(uparam, NULL, NULL); /* signal end of file */
1525 addhist_init(void *uparam, struct mlist *ml, char *string)
1528 cmd_addhist(ml, string, 0);
1529 else if (string != NULL)
1530 restore_mark(string);
1532 #endif /* CMD_HISTORY */
1535 * Initialize history from a .lesshist file.
1538 init_cmdhist(VOID_PARAM)
1541 read_cmdhist(&addhist_init, NULL, 0, 0);
1542 #endif /* CMD_HISTORY */
1546 * Write the header for a section of the history file.
1550 write_mlist_header(ml, f)
1554 if (ml == &mlist_search)
1555 fprintf(f, "%s\n", HISTFILE_SEARCH_SECTION);
1556 #if SHELL_ESCAPE || PIPEC
1557 else if (ml == &mlist_shell)
1558 fprintf(f, "%s\n", HISTFILE_SHELL_SECTION);
1563 * Write all modified entries in an mlist to the history file.
1570 for (ml = ml->next; ml->string != NULL; ml = ml->next)
1574 fprintf(f, "\"%s\n", ml->string);
1577 ml->modified = 0; /* entire mlist is now unmodified */
1581 * Make a temp name in the same directory as filename.
1584 make_tempname(filename)
1588 char *tempname = ecalloc(1, strlen(filename)+1);
1589 strcpy(tempname, filename);
1590 lastch = tempname[strlen(tempname)-1];
1591 tempname[strlen(tempname)-1] = (lastch == 'Q') ? 'Z' : 'Q';
1597 struct mlist *mlist;
1602 * Copy entries from the saved history file to a new file.
1603 * At the end of each mlist, append any new entries
1604 * created during this session.
1607 copy_hist(void *uparam, struct mlist *ml, char *string)
1609 struct save_ctx *ctx = (struct save_ctx *) uparam;
1611 if (ml != NULL && ml != ctx->mlist) {
1612 /* We're changing mlists. */
1614 /* Append any new entries to the end of the current mlist. */
1615 write_mlist(ctx->mlist, ctx->fout);
1616 /* Write the header for the new mlist. */
1618 write_mlist_header(ctx->mlist, ctx->fout);
1621 if (string == NULL) /* End of file */
1623 /* Write any sections that were not in the original file. */
1624 if (mlist_search.modified)
1626 write_mlist_header(&mlist_search, ctx->fout);
1627 write_mlist(&mlist_search, ctx->fout);
1629 #if SHELL_ESCAPE || PIPEC
1630 if (mlist_shell.modified)
1632 write_mlist_header(&mlist_shell, ctx->fout);
1633 write_mlist(&mlist_shell, ctx->fout);
1636 } else if (ml != NULL)
1638 /* Copy mlist entry. */
1639 fprintf(ctx->fout, "\"%s\n", string);
1643 #endif /* CMD_HISTORY */
1646 * Make a file readable only by its owner.
1649 make_file_private(f)
1655 struct stat statbuf;
1656 int r = fstat(fileno(f), &statbuf);
1657 if (r < 0 || !S_ISREG(statbuf.st_mode))
1658 /* Don't chmod if not a regular file. */
1662 fchmod(fileno(f), 0600);
1667 * Does the history file need to be updated?
1670 histfile_modified(VOID_PARAM)
1672 if (mlist_search.modified)
1674 #if SHELL_ESCAPE || PIPEC
1675 if (mlist_shell.modified)
1686 * Update the .lesshst file.
1689 save_cmdhist(VOID_PARAM)
1696 struct save_ctx ctx;
1701 if (!histfile_modified())
1703 histname = histfile_name();
1704 if (histname == NULL)
1706 tempname = make_tempname(histname);
1707 fout = fopen(tempname, "w");
1710 make_file_private(fout);
1711 s = lgetenv("LESSHISTSIZE");
1716 skip_search = mlist_size(&mlist_search) - histsize;
1717 #if SHELL_ESCAPE || PIPEC
1718 skip_shell = mlist_size(&mlist_shell) - histsize;
1720 fprintf(fout, "%s\n", HISTFILE_FIRST_LINE);
1723 read_cmdhist(©_hist, &ctx, skip_search, skip_shell);
1724 save_marks(fout, HISTFILE_MARK_SECTION);
1726 #if MSDOS_COMPILER==WIN32C
1728 * Windows rename doesn't remove an existing file,
1729 * making it useless for atomic operations. Sigh.
1733 rename(tempname, histname);
1737 #endif /* CMD_HISTORY */