2 * Copyright (C) 1984-2014 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.
26 static char cmdbuf
[CMDBUF_SIZE
]; /* Buffer for holding a multi-char command */
27 static int cmd_col
; /* Current column of the cursor */
28 static int prompt_col
; /* Column of cursor just after prompt */
29 static char *cp
; /* Pointer into cmdbuf */
30 static int cmd_offset
; /* Index into cmdbuf of first displayed char */
31 static int literal
; /* Next input char should not be interpreted */
32 static int updown_match
= -1; /* Prefix length in up/down movement */
34 #if TAB_COMPLETE_FILENAME
35 static int cmd_complete();
37 * These variables are statics used by cmd_complete.
39 static int in_completion
= 0;
41 static char *tk_original
;
42 static char *tk_ipoint
;
43 static char *tk_trial
;
44 static struct textlist tk_tlist
;
47 static int cmd_left();
48 static int cmd_right();
50 #if SPACES_IN_FILENAMES
51 public char openquote
= '"';
52 public char closequote
= '"';
58 #define HISTFILE_FIRST_LINE ".less-history-file:"
59 #define HISTFILE_SEARCH_SECTION ".search"
60 #define HISTFILE_SHELL_SECTION ".shell"
63 * A mlist structure represents a command history.
69 struct mlist
*curr_mp
;
75 * These are the various command histories that exist.
77 struct mlist mlist_search
=
78 { &mlist_search
, &mlist_search
, &mlist_search
, NULL
, 0 };
79 public void * constant ml_search
= (void *) &mlist_search
;
81 struct mlist mlist_examine
=
82 { &mlist_examine
, &mlist_examine
, &mlist_examine
, NULL
, 0 };
83 public void * constant ml_examine
= (void *) &mlist_examine
;
85 #if SHELL_ESCAPE || PIPEC
86 struct mlist mlist_shell
=
87 { &mlist_shell
, &mlist_shell
, &mlist_shell
, NULL
, 0 };
88 public void * constant ml_shell
= (void *) &mlist_shell
;
91 #else /* CMD_HISTORY */
93 /* If CMD_HISTORY is off, these are just flags. */
94 public void * constant ml_search
= (void *)1;
95 public void * constant ml_examine
= (void *)2;
96 #if SHELL_ESCAPE || PIPEC
97 public void * constant ml_shell
= (void *)3;
100 #endif /* CMD_HISTORY */
103 * History for the current command.
105 static struct mlist
*curr_mlist
= NULL
;
106 static int curr_cmdflags
;
108 static char cmd_mbc_buf
[MAX_UTF_CHAR_LEN
];
109 static int cmd_mbc_buf_len
;
110 static int cmd_mbc_buf_index
;
114 * Reset command buffer (to empty).
129 * Clear command line.
134 cmd_col
= prompt_col
= 0;
140 * Display a string, usually as a prompt for input into the command buffer.
148 char *endline
= s
+ strlen(s
);
152 ch
= step_char(&ns
, +1, endline
);
159 } else if (!is_composing_char(ch
) &&
160 !is_combining_char(prev_ch
, ch
))
162 int width
= is_wide_char(ch
) ? 2 : 1;
171 * How many characters are in the command buffer?
177 char *endline
= s
+ strlen(s
);
182 step_char(&s
, +1, endline
);
189 * Common part of cmd_step_right() and cmd_step_left().
192 cmd_step_common(p
, ch
, len
, pwidth
, bswidth
)
203 pr
= prchar((int) ch
);
204 if (pwidth
!= NULL
|| bswidth
!= NULL
)
206 int len
= (int) strlen(pr
);
215 if (pwidth
!= NULL
|| bswidth
!= NULL
)
217 if (is_composing_char(ch
))
223 } else if (is_ubin_char(ch
))
225 int len
= (int) strlen(pr
);
232 LWCHAR prev_ch
= step_char(&p
, -1, cmdbuf
);
233 if (is_combining_char(prev_ch
, ch
))
242 *pwidth
= is_wide_char(ch
)
256 * Step a pointer one character right in the command buffer.
259 cmd_step_right(pp
, pwidth
, bswidth
)
265 LWCHAR ch
= step_char(pp
, +1, p
+ strlen(p
));
267 return cmd_step_common(p
, ch
, *pp
- p
, pwidth
, bswidth
);
271 * Step a pointer one character left in the command buffer.
274 cmd_step_left(pp
, pwidth
, bswidth
)
280 LWCHAR ch
= step_char(pp
, -1, cmdbuf
);
282 return cmd_step_common(*pp
, ch
, p
- *pp
, pwidth
, bswidth
);
286 * Repaint the line from cp onwards.
287 * Then position the cursor just after the char old_cp (a pointer into cmdbuf).
294 * Repaint the line from the current position.
301 char *pr
= cmd_step_right(&np
, &width
, NULL
);
302 if (cmd_col
+ width
>= sc_width
)
312 char *pr
= cmd_step_right(&np
, &width
, NULL
);
320 * Back up the cursor to the correct position.
327 * Put the cursor at "home" (just after the prompt),
328 * and set cp to the corresponding char in cmdbuf.
333 while (cmd_col
> prompt_col
)
337 cmd_step_left(&cp
, &width
, &bswidth
);
338 while (bswidth
-- > 0)
343 cp
= &cmdbuf
[cmd_offset
];
347 * Shift the cmdbuf display left a half-screen.
357 * Start at the first displayed char, count how far to the
358 * right we'd have to move to reach the center of the screen.
360 s
= cmdbuf
+ cmd_offset
;
362 while (cols
< (sc_width
- prompt_col
) / 2 && *s
!= '\0')
365 cmd_step_right(&s
, &width
, NULL
);
372 cmd_step_right(&ns
, &width
, NULL
);
378 cmd_offset
= (int) (s
- cmdbuf
);
381 cmd_repaint(save_cp
);
385 * Shift the cmdbuf display right a half-screen.
395 * Start at the first displayed char, count how far to the
396 * left we'd have to move to traverse a half-screen width
397 * of displayed characters.
399 s
= cmdbuf
+ cmd_offset
;
401 while (cols
< (sc_width
- prompt_col
) / 2 && s
> cmdbuf
)
404 cmd_step_left(&s
, &width
, NULL
);
408 cmd_offset
= (int) (s
- cmdbuf
);
411 cmd_repaint(save_cp
);
415 * Move cursor right one character.
426 /* Already at the end of the line. */
430 pr
= cmd_step_right(&ncp
, &width
, NULL
);
431 if (cmd_col
+ width
>= sc_width
)
433 else if (cmd_col
+ width
== sc_width
- 1 && cp
[1] != '\0')
440 pr
= cmd_step_right(&ncp
, &width
, NULL
);
450 * Move cursor left one character.
460 /* Already at the beginning of the line */
466 cmd_step_left(&ncp
, &width
, &bswidth
);
470 if (cmd_col
< prompt_col
+ width
)
474 while (bswidth
-- > 0)
480 * Insert a char into the command buffer, at the current position.
489 if (strlen(cmdbuf
) + clen
>= sizeof(cmdbuf
)-1)
491 /* No room in the command buffer for another char. */
497 * Make room for the new character (shift the tail of the buffer right).
499 for (s
= &cmdbuf
[strlen(cmdbuf
)]; s
>= cp
; s
--)
502 * Insert the character into the buffer.
504 for (s
= cp
; s
< cp
+ clen
; s
++)
507 * Reprint the tail of the line from the inserted char.
516 * Backspace in the command buffer.
517 * Delete the char to the left of the cursor.
528 * Backspace past beginning of the buffer:
529 * this usually means abort the command.
534 * Move cursor left (to the char being erased).
538 clen
= (int) (s
- cp
);
541 * Remove the char from the buffer (shift the buffer left).
551 * Repaint the buffer after the erased char.
557 * We say that erasing the entire command string causes us
558 * to abort the current command, if CF_QUIT_ON_ERASE is set.
560 if ((curr_cmdflags
& CF_QUIT_ON_ERASE
) && cp
== cmdbuf
&& *cp
== '\0')
566 * Delete the char under the cursor.
573 /* At end of string; there is no char under the cursor. */
577 * Move right, then use cmd_erase.
585 * Delete the "word" to the left of the cursor.
590 if (cp
> cmdbuf
&& cp
[-1] == ' ')
593 * If the char left of cursor is a space,
594 * erase all the spaces left of cursor (to the first non-space).
596 while (cp
> cmdbuf
&& cp
[-1] == ' ')
601 * If the char left of cursor is not a space,
602 * erase all the nonspaces left of cursor (the whole "word").
604 while (cp
> cmdbuf
&& cp
[-1] != ' ')
611 * Delete the "word" under the cursor.
619 * If the char under the cursor is a space,
620 * delete it and all the spaces right of cursor.
627 * If the char under the cursor is not a space,
628 * delete it and all nonspaces right of cursor (the whole word).
630 while (*cp
!= ' ' && *cp
!= '\0')
637 * Delete all chars in the command buffer.
642 if (cmdbuf
[0] == '\0')
644 /* Buffer is already empty; abort the current command. */
654 * We say that erasing the entire command string causes us
655 * to abort the current command, if CF_QUIT_ON_ERASE is set.
657 if (curr_cmdflags
& CF_QUIT_ON_ERASE
)
663 * Select an mlist structure to be the current command history.
666 set_mlist(mlist
, cmdflags
)
671 curr_mlist
= (struct mlist
*) mlist
;
672 curr_cmdflags
= cmdflags
;
674 /* Make sure the next up-arrow moves to the last string in the mlist. */
675 if (curr_mlist
!= NULL
)
676 curr_mlist
->curr_mp
= curr_mlist
;
682 * Move up or down in the currently selected command history list.
683 * Only consider entries whose first updown_match chars are equal to
684 * cmdbuf's corresponding chars.
693 if (curr_mlist
== NULL
)
696 * The current command has no history list.
702 if (updown_match
< 0)
704 updown_match
= (int) (cp
- cmdbuf
);
708 * Find the next history entry which matches.
710 for (ml
= curr_mlist
->curr_mp
;;)
712 ml
= (action
== EC_UP
) ? ml
->prev
: ml
->next
;
713 if (ml
== curr_mlist
)
716 * We reached the end (or beginning) of the list.
720 if (strncmp(cmdbuf
, ml
->string
, updown_match
) == 0)
723 * This entry matches; stop here.
724 * Copy the entry into cmdbuf and echo it on the screen.
726 curr_mlist
->curr_mp
= ml
;
733 for (cp
= cmdbuf
; *cp
!= '\0'; )
739 * We didn't find a history entry that matches.
747 * Add a string to an mlist.
750 cmd_addhist(mlist
, cmd
, modified
)
759 * Don't save a trivial command.
761 if (strlen(cmd
) == 0)
765 * Save the command unless it's a duplicate of the
766 * last command in the history.
769 if (ml
== mlist
|| strcmp(ml
->string
, cmd
) != 0)
772 * Did not find command in history.
773 * Save the command and put it at the end of the history list.
775 ml
= (struct mlist
*) ecalloc(1, sizeof(struct mlist
));
776 ml
->string
= save(cmd
);
777 ml
->modified
= modified
;
779 ml
->prev
= mlist
->prev
;
780 mlist
->prev
->next
= ml
;
784 * Point to the cmd just after the just-accepted command.
785 * Thus, an UPARROW will always retrieve the previous command.
787 mlist
->curr_mp
= ml
->next
;
792 * Accept the command in the command buffer.
793 * Add it to the currently selected history list.
800 * Nothing to do if there is no currently selected history list.
802 if (curr_mlist
== NULL
)
804 cmd_addhist(curr_mlist
, cmdbuf
, 1);
805 curr_mlist
->modified
= 1;
810 * Try to perform a line-edit function on the command buffer,
811 * using a specified char as a line-editing command.
813 * CC_PASS The char does not invoke a line edit function.
814 * CC_OK Line edit function done.
815 * CC_QUIT The char requests the current command to be aborted.
824 #if TAB_COMPLETE_FILENAME
825 #define not_in_completion() in_completion = 0
827 #define not_in_completion()
831 * See if the char is indeed a line-editing command.
835 if (curr_mlist
== NULL
)
837 * No current history; don't accept history manipulation cmds.
839 flags
|= EC_NOHISTORY
;
841 #if TAB_COMPLETE_FILENAME
842 if (curr_mlist
== ml_search
)
844 * In a search command; don't accept file-completion cmds.
846 flags
|= EC_NOCOMPLETE
;
849 action
= editchar(c
, flags
);
855 return (cmd_right());
861 while (*cp
!= '\0' && *cp
!= ' ')
868 while (cp
> cmdbuf
&& cp
[-1] == ' ')
870 while (cp
> cmdbuf
&& cp
[-1] != ' ')
889 return (cmd_erase());
899 return (cmd_werase());
902 return (cmd_delete());
905 return (cmd_wdelete());
913 return (cmd_updown(action
));
915 #if TAB_COMPLETE_FILENAME
919 return (cmd_complete(action
));
929 #if TAB_COMPLETE_FILENAME
931 * Insert a string into the command buffer, at the current position.
939 char *endline
= str
+ strlen(str
);
941 for (s
= str
; *s
!= '\0'; )
944 step_char(&s
, +1, endline
);
945 action
= cmd_ichar(os
, s
- os
);
956 * Find the beginning and end of the "current" word.
957 * This is the word which the cursor (cp) is inside or at the end of.
958 * Return pointer to the beginning of the word and put the
959 * cursor at the end of the word.
965 #if SPACES_IN_FILENAMES
967 int delim_quoted
= 0;
969 char *esc
= get_meta_escape();
970 int esclen
= (int) strlen(esc
);
974 * Move cursor to end of word.
976 if (*cp
!= ' ' && *cp
!= '\0')
979 * Cursor is on a nonspace.
980 * Move cursor right to the next space.
982 while (*cp
!= ' ' && *cp
!= '\0')
984 } else if (cp
> cmdbuf
&& cp
[-1] != ' ')
987 * Cursor is on a space, and char to the left is a nonspace.
988 * We're already at the end of the word.
995 * Cursor is on a space and char to the left is a space.
996 * Huh? There's no word here.
1002 * Find the beginning of the word which the cursor is in.
1006 #if SPACES_IN_FILENAMES
1008 * If we have an unbalanced quote (that is, an open quote
1009 * without a corresponding close quote), we return everything
1010 * from the open quote, including spaces.
1012 for (word
= cmdbuf
; word
< cp
; word
++)
1017 for (p
= cmdbuf
; p
< cp
; p
++)
1022 } else if (esclen
> 0 && p
+ esclen
< cp
&&
1023 strncmp(p
, esc
, esclen
) == 0)
1027 } else if (delim_quoted
)
1029 if (*p
== closequote
)
1031 } else /* (!delim_quoted) */
1033 if (*p
== openquote
)
1044 * Set things up to enter completion mode.
1045 * Expand the word under the cursor into a list of filenames
1046 * which start with that word, and set tk_text to that list.
1055 * Get rid of any previous tk_text.
1057 if (tk_text
!= NULL
)
1063 * Find the original (uncompleted) word in the command buffer.
1065 word
= delimit_word();
1069 * Set the insertion point to the point in the command buffer
1070 * where the original (uncompleted) word now sits.
1074 * Save the original (uncompleted) word
1076 if (tk_original
!= NULL
)
1078 tk_original
= (char *) ecalloc(cp
-word
+1, sizeof(char));
1079 strncpy(tk_original
, word
, cp
-word
);
1081 * Get the expanded filename.
1082 * This may result in a single filename, or
1083 * a blank-separated list of filenames.
1087 if (*word
!= openquote
)
1089 tk_text
= fcomplete(word
);
1095 char *qword
= shell_quote(word
+1);
1098 tk_text
= fcomplete(word
+1);
1101 tk_text
= fcomplete(qword
);
1109 * Return the next word in the current completion list.
1112 next_compl(action
, prev
)
1119 return (forw_textlist(&tk_tlist
, prev
));
1121 return (back_textlist(&tk_tlist
, prev
));
1128 * Complete the filename before (or under) the cursor.
1129 * cmd_complete may be called multiple times. The global in_completion
1130 * remembers whether this call is the first time (create the list),
1131 * or a subsequent time (step thru the list).
1134 cmd_complete(action
)
1139 if (!in_completion
|| action
== EC_EXPAND
)
1142 * Expand the word under the cursor and
1143 * use the first word in the expansion
1144 * (or the entire expansion if we're doing EC_EXPAND).
1147 if (tk_text
== NULL
)
1152 if (action
== EC_EXPAND
)
1155 * Use the whole list.
1161 * Use the first filename in the list.
1164 init_textlist(&tk_tlist
, tk_text
);
1165 tk_trial
= next_compl(action
, (char*)NULL
);
1170 * We already have a completion list.
1171 * Use the next/previous filename from the list.
1173 tk_trial
= next_compl(action
, tk_trial
);
1177 * Remove the original word, or the previous trial completion.
1179 while (cp
> tk_ipoint
)
1182 if (tk_trial
== NULL
)
1185 * There are no more trial completions.
1186 * Insert the original (uncompleted) filename.
1189 if (cmd_istr(tk_original
) != CC_OK
)
1194 * Insert trial completion.
1196 if (cmd_istr(tk_trial
) != CC_OK
)
1199 * If it is a directory, append a slash.
1201 if (is_dir(tk_trial
))
1203 if (cp
> cmdbuf
&& cp
[-1] == closequote
)
1205 s
= lgetenv("LESSSEPARATOR");
1208 if (cmd_istr(s
) != CC_OK
)
1221 #endif /* TAB_COMPLETE_FILENAME */
1224 * Process a single character of a multi-character command, such as
1225 * a number, or the pattern of a search command.
1227 * CC_OK The char was accepted.
1228 * CC_QUIT The char requests the command to be aborted.
1229 * CC_ERROR The char could not be accepted due to an error.
1244 /* Perform strict validation in all possible cases. */
1245 if (cmd_mbc_buf_len
== 0)
1248 cmd_mbc_buf_index
= 1;
1250 if (IS_ASCII_OCTET(c
))
1251 cmd_mbc_buf_len
= 1;
1252 else if (IS_UTF8_LEAD(c
))
1254 cmd_mbc_buf_len
= utf_len(c
);
1258 /* UTF8_INVALID or stray UTF8_TRAIL */
1262 } else if (IS_UTF8_TRAIL(c
))
1264 cmd_mbc_buf
[cmd_mbc_buf_index
++] = c
;
1265 if (cmd_mbc_buf_index
< cmd_mbc_buf_len
)
1267 if (!is_utf8_well_formed(cmd_mbc_buf
))
1269 /* complete, but not well formed (non-shortest form), sequence */
1270 cmd_mbc_buf_len
= 0;
1276 /* Flush incomplete (truncated) sequence. */
1277 cmd_mbc_buf_len
= 0;
1279 /* Handle new char. */
1283 len
= cmd_mbc_buf_len
;
1284 cmd_mbc_buf_len
= 0;
1290 * Insert the char, even if it is a line-editing char.
1293 return (cmd_ichar(cmd_mbc_buf
, len
));
1297 * See if it is a line-editing character.
1299 if (in_mca() && len
== 1)
1301 action
= cmd_edit(c
);
1313 * Insert the char into the command buffer.
1315 return (cmd_ichar(cmd_mbc_buf
, len
));
1319 * Return the number currently in the command buffer.
1329 for (p
= cmdbuf
; *p
>= '0' && *p
<= '9'; p
++)
1330 n
= (n
* 10) + (*p
- '0');
1334 *frac
= getfraction(&p
, NULL
, &err
);
1335 /* {{ do something if err is set? }} */
1341 * Return a pointer to the command buffer.
1351 * Return the last (most recent) string in the current command history.
1356 if (curr_mlist
== NULL
)
1358 return (curr_mlist
->curr_mp
->prev
->string
);
1370 for (ml
= ml
->next
; ml
->string
!= NULL
; ml
= ml
->next
)
1376 * Get the name of the history file.
1385 /* See if filename is explicitly specified by $LESSHISTFILE. */
1386 name
= lgetenv("LESSHISTFILE");
1387 if (name
!= NULL
&& *name
!= '\0')
1389 if (strcmp(name
, "-") == 0 || strcmp(name
, "/dev/null") == 0)
1390 /* $LESSHISTFILE == "-" means don't use a history file. */
1392 return (save(name
));
1395 /* See if history file is disabled in the build. */
1396 if (strcmp(LESSHISTFILE
, "") == 0 || strcmp(LESSHISTFILE
, "-") == 0)
1399 /* Otherwise, file is in $HOME. */
1400 home
= lgetenv("HOME");
1401 if (home
== NULL
|| *home
== '\0')
1404 home
= lgetenv("INIT");
1405 if (home
== NULL
|| *home
== '\0')
1409 len
= (int) (strlen(home
) + strlen(LESSHISTFILE
) + 2);
1410 name
= (char *) ecalloc(len
, sizeof(char));
1411 SNPRINTF2(name
, len
, "%s/%s", home
, LESSHISTFILE
);
1416 * Read a .lesshst file and call a callback for each line in the file.
1419 read_cmdhist2(action
, uparam
, skip_search
, skip_shell
)
1420 void (*action
)(void*,struct mlist
*,char*);
1425 struct mlist
*ml
= NULL
;
1426 char line
[CMDBUF_SIZE
];
1432 filename
= histfile_name();
1433 if (filename
== NULL
)
1435 f
= fopen(filename
, "r");
1439 if (fgets(line
, sizeof(line
), f
) == NULL
||
1440 strncmp(line
, HISTFILE_FIRST_LINE
, strlen(HISTFILE_FIRST_LINE
)) != 0)
1445 while (fgets(line
, sizeof(line
), f
) != NULL
)
1447 for (p
= line
; *p
!= '\0'; p
++)
1449 if (*p
== '\n' || *p
== '\r')
1455 if (strcmp(line
, HISTFILE_SEARCH_SECTION
) == 0)
1458 skip
= &skip_search
;
1459 } else if (strcmp(line
, HISTFILE_SHELL_SECTION
) == 0)
1461 #if SHELL_ESCAPE || PIPEC
1468 } else if (*line
== '"')
1472 if (skip
!= NULL
&& *skip
> 0)
1475 (*action
)(uparam
, ml
, line
+1);
1483 read_cmdhist(action
, uparam
, skip_search
, skip_shell
)
1484 void (*action
)(void*,struct mlist
*,char*);
1489 read_cmdhist2(action
, uparam
, skip_search
, skip_shell
);
1490 (*action
)(uparam
, NULL
, NULL
); /* signal end of file */
1494 addhist_init(void *uparam
, struct mlist
*ml
, char *string
)
1496 if (ml
== NULL
|| string
== NULL
)
1498 cmd_addhist(ml
, string
, 0);
1500 #endif /* CMD_HISTORY */
1503 * Initialize history from a .lesshist file.
1509 read_cmdhist(&addhist_init
, NULL
, 0, 0);
1510 #endif /* CMD_HISTORY */
1514 * Write the header for a section of the history file.
1518 write_mlist_header(ml
, f
)
1522 if (ml
== &mlist_search
)
1523 fprintf(f
, "%s\n", HISTFILE_SEARCH_SECTION
);
1524 #if SHELL_ESCAPE || PIPEC
1525 else if (ml
== &mlist_shell
)
1526 fprintf(f
, "%s\n", HISTFILE_SHELL_SECTION
);
1531 * Write all modified entries in an mlist to the history file.
1538 for (ml
= ml
->next
; ml
->string
!= NULL
; ml
= ml
->next
)
1542 fprintf(f
, "\"%s\n", ml
->string
);
1545 ml
->modified
= 0; /* entire mlist is now unmodified */
1549 * Make a temp name in the same directory as filename.
1552 make_tempname(filename
)
1556 char *tempname
= ecalloc(1, strlen(filename
)+1);
1557 strcpy(tempname
, filename
);
1558 lastch
= tempname
[strlen(tempname
)-1];
1559 tempname
[strlen(tempname
)-1] = (lastch
== 'Q') ? 'Z' : 'Q';
1565 struct mlist
*mlist
;
1570 * Copy entries from the saved history file to a new file.
1571 * At the end of each mlist, append any new entries
1572 * created during this session.
1575 copy_hist(void *uparam
, struct mlist
*ml
, char *string
)
1577 struct save_ctx
*ctx
= (struct save_ctx
*) uparam
;
1579 if (ml
!= ctx
->mlist
) {
1580 /* We're changing mlists. */
1582 /* Append any new entries to the end of the current mlist. */
1583 write_mlist(ctx
->mlist
, ctx
->fout
);
1584 /* Write the header for the new mlist. */
1586 write_mlist_header(ctx
->mlist
, ctx
->fout
);
1590 /* Copy the entry. */
1591 fprintf(ctx
->fout
, "\"%s\n", string
);
1593 if (ml
== NULL
) /* End of file */
1595 /* Write any sections that were not in the original file. */
1596 if (mlist_search
.modified
)
1598 write_mlist_header(&mlist_search
, ctx
->fout
);
1599 write_mlist(&mlist_search
, ctx
->fout
);
1601 #if SHELL_ESCAPE || PIPEC
1602 if (mlist_shell
.modified
)
1604 write_mlist_header(&mlist_shell
, ctx
->fout
);
1605 write_mlist(&mlist_shell
, ctx
->fout
);
1610 #endif /* CMD_HISTORY */
1613 * Make a file readable only by its owner.
1616 make_file_private(f
)
1622 struct stat statbuf
;
1623 int r
= fstat(fileno(f
), &statbuf
);
1624 if (r
< 0 || !S_ISREG(statbuf
.st_mode
))
1625 /* Don't chmod if not a regular file. */
1629 fchmod(fileno(f
), 0600);
1634 * Does the history file need to be updated?
1639 if (mlist_search
.modified
)
1641 #if SHELL_ESCAPE || PIPEC
1642 if (mlist_shell
.modified
)
1649 * Update the .lesshst file.
1659 struct save_ctx ctx
;
1664 if (!histfile_modified())
1666 histname
= histfile_name();
1667 if (histname
== NULL
)
1669 tempname
= make_tempname(histname
);
1670 fout
= fopen(tempname
, "w");
1673 make_file_private(fout
);
1674 s
= lgetenv("LESSHISTSIZE");
1679 skip_search
= mlist_size(&mlist_search
) - histsize
;
1680 #if SHELL_ESCAPE || PIPEC
1681 skip_shell
= mlist_size(&mlist_shell
) - histsize
;
1683 fprintf(fout
, "%s\n", HISTFILE_FIRST_LINE
);
1686 read_cmdhist(copy_hist
, &ctx
, skip_search
, skip_shell
);
1688 #if MSDOS_COMPILER==WIN32C
1690 * Windows rename doesn't remove an existing file,
1691 * making it useless for atomic operations. Sigh.
1695 rename(tempname
, histname
);
1699 #endif /* CMD_HISTORY */