Absolute File Name: | /home/opencoverage/opencoverage/guest-scripts/bash/src/builtins/history.def |
Source code | Switch to Preprocessed file |
Line | Source | Count | ||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
1 | This file is history.def, from which is created history.c. | - | ||||||||||||||||||||||||||||||
2 | It implements the builtin "history" in Bash. | - | ||||||||||||||||||||||||||||||
3 | - | |||||||||||||||||||||||||||||||
4 | Copyright (C) 1987-2018 Free Software Foundation, Inc. | - | ||||||||||||||||||||||||||||||
5 | - | |||||||||||||||||||||||||||||||
6 | This file is part of GNU Bash, the Bourne Again SHell. | - | ||||||||||||||||||||||||||||||
7 | - | |||||||||||||||||||||||||||||||
8 | Bash is free software: you can redistribute it and/or modify | - | ||||||||||||||||||||||||||||||
9 | it under the terms of the GNU General Public License as published by | - | ||||||||||||||||||||||||||||||
10 | the Free Software Foundation, either version 3 of the License, or | - | ||||||||||||||||||||||||||||||
11 | (at your option) any later version. | - | ||||||||||||||||||||||||||||||
12 | - | |||||||||||||||||||||||||||||||
13 | Bash is distributed in the hope that it will be useful, | - | ||||||||||||||||||||||||||||||
14 | but WITHOUT ANY WARRANTY; without even the implied warranty of | - | ||||||||||||||||||||||||||||||
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | - | ||||||||||||||||||||||||||||||
16 | GNU General Public License for more details. | - | ||||||||||||||||||||||||||||||
17 | - | |||||||||||||||||||||||||||||||
18 | You should have received a copy of the GNU General Public License | - | ||||||||||||||||||||||||||||||
19 | along with Bash. If not, see <http://www.gnu.org/licenses/>. | - | ||||||||||||||||||||||||||||||
20 | - | |||||||||||||||||||||||||||||||
21 | $PRODUCES history.c | - | ||||||||||||||||||||||||||||||
22 | - | |||||||||||||||||||||||||||||||
23 | $BUILTIN history | - | ||||||||||||||||||||||||||||||
24 | $FUNCTION history_builtin | - | ||||||||||||||||||||||||||||||
25 | $DEPENDS_ON HISTORY | - | ||||||||||||||||||||||||||||||
26 | $SHORT_DOC history [-c] [-d offset] [n] or history -anrw [filename] or history -ps arg [arg...] | - | ||||||||||||||||||||||||||||||
27 | Display or manipulate the history list. | - | ||||||||||||||||||||||||||||||
28 | - | |||||||||||||||||||||||||||||||
29 | Display the history list with line numbers, prefixing each modified | - | ||||||||||||||||||||||||||||||
30 | entry with a `*'. An argument of N lists only the last N entries. | - | ||||||||||||||||||||||||||||||
31 | - | |||||||||||||||||||||||||||||||
32 | Options: | - | ||||||||||||||||||||||||||||||
33 | -c clear the history list by deleting all of the entries | - | ||||||||||||||||||||||||||||||
34 | -d offset delete the history entry at position OFFSET. Negative | - | ||||||||||||||||||||||||||||||
35 | offsets count back from the end of the history list | - | ||||||||||||||||||||||||||||||
36 | - | |||||||||||||||||||||||||||||||
37 | -a append history lines from this session to the history file | - | ||||||||||||||||||||||||||||||
38 | -n read all history lines not already read from the history file | - | ||||||||||||||||||||||||||||||
39 | and append them to the history list | - | ||||||||||||||||||||||||||||||
40 | -r read the history file and append the contents to the history | - | ||||||||||||||||||||||||||||||
41 | list | - | ||||||||||||||||||||||||||||||
42 | -w write the current history to the history file | - | ||||||||||||||||||||||||||||||
43 | - | |||||||||||||||||||||||||||||||
44 | -p perform history expansion on each ARG and display the result | - | ||||||||||||||||||||||||||||||
45 | without storing it in the history list | - | ||||||||||||||||||||||||||||||
46 | -s append the ARGs to the history list as a single entry | - | ||||||||||||||||||||||||||||||
47 | - | |||||||||||||||||||||||||||||||
48 | If FILENAME is given, it is used as the history file. Otherwise, | - | ||||||||||||||||||||||||||||||
49 | if HISTFILE has a value, that is used, else ~/.bash_history. | - | ||||||||||||||||||||||||||||||
50 | - | |||||||||||||||||||||||||||||||
51 | If the HISTTIMEFORMAT variable is set and not null, its value is used | - | ||||||||||||||||||||||||||||||
52 | as a format string for strftime(3) to print the time stamp associated | - | ||||||||||||||||||||||||||||||
53 | with each displayed history entry. No time stamps are printed otherwise. | - | ||||||||||||||||||||||||||||||
54 | - | |||||||||||||||||||||||||||||||
55 | Exit Status: | - | ||||||||||||||||||||||||||||||
56 | Returns success unless an invalid option is given or an error occurs. | - | ||||||||||||||||||||||||||||||
57 | $END | - | ||||||||||||||||||||||||||||||
58 | - | |||||||||||||||||||||||||||||||
59 | #include <config.h> | - | ||||||||||||||||||||||||||||||
60 | - | |||||||||||||||||||||||||||||||
61 | #if defined (HISTORY) | - | ||||||||||||||||||||||||||||||
62 | #include "../bashtypes.h" | - | ||||||||||||||||||||||||||||||
63 | #if ! defined(_MINIX) && defined (HAVE_SYS_FILE_H) | - | ||||||||||||||||||||||||||||||
64 | # include <sys/file.h> | - | ||||||||||||||||||||||||||||||
65 | #endif | - | ||||||||||||||||||||||||||||||
66 | #include "posixstat.h" | - | ||||||||||||||||||||||||||||||
67 | #include "filecntl.h" | - | ||||||||||||||||||||||||||||||
68 | #include <errno.h> | - | ||||||||||||||||||||||||||||||
69 | #include <stdio.h> | - | ||||||||||||||||||||||||||||||
70 | #if defined (HAVE_UNISTD_H) | - | ||||||||||||||||||||||||||||||
71 | # include <unistd.h> | - | ||||||||||||||||||||||||||||||
72 | #endif | - | ||||||||||||||||||||||||||||||
73 | - | |||||||||||||||||||||||||||||||
74 | #include "../bashansi.h" | - | ||||||||||||||||||||||||||||||
75 | #include "../bashintl.h" | - | ||||||||||||||||||||||||||||||
76 | - | |||||||||||||||||||||||||||||||
77 | #include "../shell.h" | - | ||||||||||||||||||||||||||||||
78 | #include "../parser.h" | - | ||||||||||||||||||||||||||||||
79 | #include "../bashhist.h" | - | ||||||||||||||||||||||||||||||
80 | #include <readline/history.h> | - | ||||||||||||||||||||||||||||||
81 | #include "bashgetopt.h" | - | ||||||||||||||||||||||||||||||
82 | #include "common.h" | - | ||||||||||||||||||||||||||||||
83 | - | |||||||||||||||||||||||||||||||
84 | #if !defined (errno) | - | ||||||||||||||||||||||||||||||
85 | extern int errno; | - | ||||||||||||||||||||||||||||||
86 | #endif | - | ||||||||||||||||||||||||||||||
87 | - | |||||||||||||||||||||||||||||||
88 | static char *histtime __P((HIST_ENTRY *, const char *)); | - | ||||||||||||||||||||||||||||||
89 | static int display_history __P((WORD_LIST *)); | - | ||||||||||||||||||||||||||||||
90 | static void push_history __P((WORD_LIST *)); | - | ||||||||||||||||||||||||||||||
91 | static int expand_and_print_history __P((WORD_LIST *)); | - | ||||||||||||||||||||||||||||||
92 | - | |||||||||||||||||||||||||||||||
93 | #define AFLAG 0x01 | - | ||||||||||||||||||||||||||||||
94 | #define RFLAG 0x02 | - | ||||||||||||||||||||||||||||||
95 | #define WFLAG 0x04 | - | ||||||||||||||||||||||||||||||
96 | #define NFLAG 0x08 | - | ||||||||||||||||||||||||||||||
97 | #define SFLAG 0x10 | - | ||||||||||||||||||||||||||||||
98 | #define PFLAG 0x20 | - | ||||||||||||||||||||||||||||||
99 | #define CFLAG 0x40 | - | ||||||||||||||||||||||||||||||
100 | #define DFLAG 0x80 | - | ||||||||||||||||||||||||||||||
101 | - | |||||||||||||||||||||||||||||||
102 | int | - | ||||||||||||||||||||||||||||||
103 | history_builtin (list) | - | ||||||||||||||||||||||||||||||
104 | WORD_LIST *list; | - | ||||||||||||||||||||||||||||||
105 | { | - | ||||||||||||||||||||||||||||||
106 | int flags, opt, result, old_history_lines, obase, ind; | - | ||||||||||||||||||||||||||||||
107 | char *filename, *delete_arg, *range; | - | ||||||||||||||||||||||||||||||
108 | intmax_t delete_offset; | - | ||||||||||||||||||||||||||||||
109 | - | |||||||||||||||||||||||||||||||
110 | flags = 0; | - | ||||||||||||||||||||||||||||||
111 | reset_internal_getopt (); | - | ||||||||||||||||||||||||||||||
112 | while ((opt = internal_getopt (list, "acd:npsrw")) != -1)
| 32-45 | ||||||||||||||||||||||||||||||
113 | { | - | ||||||||||||||||||||||||||||||
114 | switch (opt) | - | ||||||||||||||||||||||||||||||
115 | { | - | ||||||||||||||||||||||||||||||
116 | case 'a': executed 2 times by 1 test: case 'a': Executed by:
| 2 | ||||||||||||||||||||||||||||||
117 | flags |= AFLAG; | - | ||||||||||||||||||||||||||||||
118 | break; executed 2 times by 1 test: break; Executed by:
| 2 | ||||||||||||||||||||||||||||||
119 | case 'c': executed 6 times by 1 test: case 'c': Executed by:
| 6 | ||||||||||||||||||||||||||||||
120 | flags |= CFLAG; | - | ||||||||||||||||||||||||||||||
121 | break; executed 6 times by 1 test: break; Executed by:
| 6 | ||||||||||||||||||||||||||||||
122 | case 'n': executed 1 time by 1 test: case 'n': Executed by:
| 1 | ||||||||||||||||||||||||||||||
123 | flags |= NFLAG; | - | ||||||||||||||||||||||||||||||
124 | break; executed 1 time by 1 test: break; Executed by:
| 1 | ||||||||||||||||||||||||||||||
125 | case 'r': executed 2 times by 1 test: case 'r': Executed by:
| 2 | ||||||||||||||||||||||||||||||
126 | flags |= RFLAG; | - | ||||||||||||||||||||||||||||||
127 | break; executed 2 times by 1 test: break; Executed by:
| 2 | ||||||||||||||||||||||||||||||
128 | case 'w': executed 3 times by 1 test: case 'w': Executed by:
| 3 | ||||||||||||||||||||||||||||||
129 | flags |= WFLAG; | - | ||||||||||||||||||||||||||||||
130 | break; executed 3 times by 1 test: break; Executed by:
| 3 | ||||||||||||||||||||||||||||||
131 | case 's': executed 3 times by 1 test: case 's': Executed by:
| 3 | ||||||||||||||||||||||||||||||
132 | flags |= SFLAG; | - | ||||||||||||||||||||||||||||||
133 | break; executed 3 times by 1 test: break; Executed by:
| 3 | ||||||||||||||||||||||||||||||
134 | case 'd': executed 7 times by 1 test: case 'd': Executed by:
| 7 | ||||||||||||||||||||||||||||||
135 | flags |= DFLAG; | - | ||||||||||||||||||||||||||||||
136 | delete_arg = list_optarg; | - | ||||||||||||||||||||||||||||||
137 | break; executed 7 times by 1 test: break; Executed by:
| 7 | ||||||||||||||||||||||||||||||
138 | case 'p': executed 7 times by 1 test: case 'p': Executed by:
| 7 | ||||||||||||||||||||||||||||||
139 | #if defined (BANG_HISTORY) | - | ||||||||||||||||||||||||||||||
140 | flags |= PFLAG; | - | ||||||||||||||||||||||||||||||
141 | #endif | - | ||||||||||||||||||||||||||||||
142 | break; executed 7 times by 1 test: break; Executed by:
| 7 | ||||||||||||||||||||||||||||||
143 | CASE_HELPOPT; never executed: return (258); never executed: case -99: | 0 | ||||||||||||||||||||||||||||||
144 | default: executed 1 time by 1 test: default: Executed by:
| 1 | ||||||||||||||||||||||||||||||
145 | builtin_usage (); | - | ||||||||||||||||||||||||||||||
146 | return (EX_USAGE); executed 1 time by 1 test: return (258); Executed by:
| 1 | ||||||||||||||||||||||||||||||
147 | } | - | ||||||||||||||||||||||||||||||
148 | } | - | ||||||||||||||||||||||||||||||
149 | list = loptend; | - | ||||||||||||||||||||||||||||||
150 | - | |||||||||||||||||||||||||||||||
151 | opt = flags & (AFLAG|RFLAG|WFLAG|NFLAG); | - | ||||||||||||||||||||||||||||||
152 | if (opt && opt != AFLAG && opt != RFLAG && opt != WFLAG && opt != NFLAG)
| 1-38 | ||||||||||||||||||||||||||||||
153 | { | - | ||||||||||||||||||||||||||||||
154 | builtin_error (_("cannot use more than one of -anrw")); | - | ||||||||||||||||||||||||||||||
155 | return (EXECUTION_FAILURE); executed 1 time by 1 test: return (1); Executed by:
| 1 | ||||||||||||||||||||||||||||||
156 | } | - | ||||||||||||||||||||||||||||||
157 | - | |||||||||||||||||||||||||||||||
158 | /* clear the history, but allow other arguments to add to it again. */ | - | ||||||||||||||||||||||||||||||
159 | if (flags & CFLAG)
| 6-38 | ||||||||||||||||||||||||||||||
160 | { | - | ||||||||||||||||||||||||||||||
161 | bash_clear_history (); | - | ||||||||||||||||||||||||||||||
162 | if (list == 0)
| 0-6 | ||||||||||||||||||||||||||||||
163 | return (EXECUTION_SUCCESS); executed 6 times by 1 test: return (0); Executed by:
| 6 | ||||||||||||||||||||||||||||||
164 | } never executed: end of block | 0 | ||||||||||||||||||||||||||||||
165 | - | |||||||||||||||||||||||||||||||
166 | if (flags & SFLAG)
| 3-35 | ||||||||||||||||||||||||||||||
167 | { | - | ||||||||||||||||||||||||||||||
168 | if (list)
| 0-3 | ||||||||||||||||||||||||||||||
169 | push_history (list); executed 3 times by 1 test: push_history (list); Executed by:
| 3 | ||||||||||||||||||||||||||||||
170 | return (EXECUTION_SUCCESS); executed 3 times by 1 test: return (0); Executed by:
| 3 | ||||||||||||||||||||||||||||||
171 | } | - | ||||||||||||||||||||||||||||||
172 | #if defined (BANG_HISTORY) | - | ||||||||||||||||||||||||||||||
173 | else if (flags & PFLAG)
| 7-28 | ||||||||||||||||||||||||||||||
174 | { | - | ||||||||||||||||||||||||||||||
175 | if (list)
| 0-7 | ||||||||||||||||||||||||||||||
176 | return (expand_and_print_history (list)); executed 7 times by 1 test: return (expand_and_print_history (list)); Executed by:
| 7 | ||||||||||||||||||||||||||||||
177 | return (sh_chkwrite (EXECUTION_SUCCESS)); never executed: return (sh_chkwrite (0)); | 0 | ||||||||||||||||||||||||||||||
178 | } | - | ||||||||||||||||||||||||||||||
179 | #endif | - | ||||||||||||||||||||||||||||||
180 | else if ((flags & DFLAG) && (range = strchr ((delete_arg[0] == '-') ? delete_arg + 1 : delete_arg, '-')))
| 0-21 | ||||||||||||||||||||||||||||||
181 | { | - | ||||||||||||||||||||||||||||||
182 | intmax_t delete_start, delete_end; | - | ||||||||||||||||||||||||||||||
183 | *range++ = '\0'; | - | ||||||||||||||||||||||||||||||
184 | if (legal_number (delete_arg, &delete_start) == 0 || legal_number (range, &delete_end) == 0)
| 0-7 | ||||||||||||||||||||||||||||||
185 | { | - | ||||||||||||||||||||||||||||||
186 | range[-1] = '-'; | - | ||||||||||||||||||||||||||||||
187 | sh_erange (delete_arg, _("history position")); | - | ||||||||||||||||||||||||||||||
188 | return (EXECUTION_FAILURE); executed 1 time by 1 test: return (1); Executed by:
| 1 | ||||||||||||||||||||||||||||||
189 | } | - | ||||||||||||||||||||||||||||||
190 | if (delete_arg[0] == '-' && delete_start < 0)
| 0-5 | ||||||||||||||||||||||||||||||
191 | { | - | ||||||||||||||||||||||||||||||
192 | /* the_history[history_length] == 0x0, so this is correct */ | - | ||||||||||||||||||||||||||||||
193 | delete_start += history_length; | - | ||||||||||||||||||||||||||||||
194 | if (delete_start < history_base)
| 0-1 | ||||||||||||||||||||||||||||||
195 | { | - | ||||||||||||||||||||||||||||||
196 | start_error: | - | ||||||||||||||||||||||||||||||
197 | sh_erange (delete_arg, _("history position")); | - | ||||||||||||||||||||||||||||||
198 | return (EXECUTION_FAILURE); executed 2 times by 1 test: return (1); Executed by:
| 2 | ||||||||||||||||||||||||||||||
199 | } | - | ||||||||||||||||||||||||||||||
200 | } never executed: end of block | 0 | ||||||||||||||||||||||||||||||
201 | /* numbers as displayed by display_history are offset by history_base */ | - | ||||||||||||||||||||||||||||||
202 | else if (delete_start > 0)
| 0-5 | ||||||||||||||||||||||||||||||
203 | delete_start -= history_base; executed 5 times by 1 test: delete_start -= history_base; Executed by:
| 5 | ||||||||||||||||||||||||||||||
204 | if (delete_start < 0 || delete_start >= history_length)
| 0-5 | ||||||||||||||||||||||||||||||
205 | goto start_error; executed 1 time by 1 test: goto start_error; Executed by:
| 1 | ||||||||||||||||||||||||||||||
206 | if (range[0] == '-' && delete_end < 0)
| 0-2 | ||||||||||||||||||||||||||||||
207 | { | - | ||||||||||||||||||||||||||||||
208 | delete_end += history_length; | - | ||||||||||||||||||||||||||||||
209 | if (delete_end < history_base)
| 1 | ||||||||||||||||||||||||||||||
210 | { | - | ||||||||||||||||||||||||||||||
211 | range_error: | - | ||||||||||||||||||||||||||||||
212 | sh_erange (range, _("history position")); | - | ||||||||||||||||||||||||||||||
213 | return (EXECUTION_FAILURE); executed 2 times by 1 test: return (1); Executed by:
| 2 | ||||||||||||||||||||||||||||||
214 | } | - | ||||||||||||||||||||||||||||||
215 | } executed 1 time by 1 test: end of block Executed by:
| 1 | ||||||||||||||||||||||||||||||
216 | else if (delete_end > 0)
| 0-2 | ||||||||||||||||||||||||||||||
217 | delete_end -= history_base; executed 2 times by 1 test: delete_end -= history_base; Executed by:
| 2 | ||||||||||||||||||||||||||||||
218 | if (delete_end < 0 || delete_end >= history_length)
| 0-3 | ||||||||||||||||||||||||||||||
219 | goto range_error; executed 1 time by 1 test: goto range_error; Executed by:
| 1 | ||||||||||||||||||||||||||||||
220 | result = bash_delete_history_range (delete_start, delete_end); | - | ||||||||||||||||||||||||||||||
221 | if (where_history () > history_length)
| 0-2 | ||||||||||||||||||||||||||||||
222 | history_set_pos (history_length); executed 2 times by 1 test: history_set_pos (history_length); Executed by:
| 2 | ||||||||||||||||||||||||||||||
223 | return (result ? EXECUTION_SUCCESS : EXECUTION_FAILURE); executed 2 times by 1 test: return (result ? 0 : 1); Executed by:
| 2 | ||||||||||||||||||||||||||||||
224 | } | - | ||||||||||||||||||||||||||||||
225 | else if (flags & DFLAG)
| 0-21 | ||||||||||||||||||||||||||||||
226 | { | - | ||||||||||||||||||||||||||||||
227 | if (legal_number (delete_arg, &delete_offset) == 0)
| 0 | ||||||||||||||||||||||||||||||
228 | { | - | ||||||||||||||||||||||||||||||
229 | sh_erange (delete_arg, _("history position")); | - | ||||||||||||||||||||||||||||||
230 | return (EXECUTION_FAILURE); never executed: return (1); | 0 | ||||||||||||||||||||||||||||||
231 | } | - | ||||||||||||||||||||||||||||||
232 | /* check for negative offsets, count back from end of list */ | - | ||||||||||||||||||||||||||||||
233 | if (delete_arg[0] == '-' && delete_offset < 0)
| 0 | ||||||||||||||||||||||||||||||
234 | { | - | ||||||||||||||||||||||||||||||
235 | /* since the_history[history_length] == 0x0, this calculation means | - | ||||||||||||||||||||||||||||||
236 | that history -d -1 will delete the last history entry, which at | - | ||||||||||||||||||||||||||||||
237 | this point is the history -d -1 we just added. */ | - | ||||||||||||||||||||||||||||||
238 | ind = history_length + delete_offset; | - | ||||||||||||||||||||||||||||||
239 | if (ind < history_base)
| 0 | ||||||||||||||||||||||||||||||
240 | { | - | ||||||||||||||||||||||||||||||
241 | sh_erange (delete_arg, _("history position")); | - | ||||||||||||||||||||||||||||||
242 | return (EXECUTION_FAILURE); never executed: return (1); | 0 | ||||||||||||||||||||||||||||||
243 | } | - | ||||||||||||||||||||||||||||||
244 | opt = ind + history_base; /* compensate for opt - history_base below */ | - | ||||||||||||||||||||||||||||||
245 | } never executed: end of block | 0 | ||||||||||||||||||||||||||||||
246 | else if ((delete_offset < history_base) || (delete_offset > (history_base + history_length)))
| 0 | ||||||||||||||||||||||||||||||
247 | { | - | ||||||||||||||||||||||||||||||
248 | sh_erange (delete_arg, _("history position")); | - | ||||||||||||||||||||||||||||||
249 | return (EXECUTION_FAILURE); never executed: return (1); | 0 | ||||||||||||||||||||||||||||||
250 | } | - | ||||||||||||||||||||||||||||||
251 | else | - | ||||||||||||||||||||||||||||||
252 | opt = delete_offset; never executed: opt = delete_offset; | 0 | ||||||||||||||||||||||||||||||
253 | - | |||||||||||||||||||||||||||||||
254 | /* Positive arguments from numbers as displayed by display_history need | - | ||||||||||||||||||||||||||||||
255 | to be offset by history_base */ | - | ||||||||||||||||||||||||||||||
256 | result = bash_delete_histent (opt - history_base); | - | ||||||||||||||||||||||||||||||
257 | /* Since remove_history changes history_length, this can happen if | - | ||||||||||||||||||||||||||||||
258 | we delete the last history entry. */ | - | ||||||||||||||||||||||||||||||
259 | if (where_history () > history_length)
| 0 | ||||||||||||||||||||||||||||||
260 | history_set_pos (history_length); never executed: history_set_pos (history_length); | 0 | ||||||||||||||||||||||||||||||
261 | return (result ? EXECUTION_SUCCESS : EXECUTION_FAILURE); never executed: return (result ? 0 : 1); | 0 | ||||||||||||||||||||||||||||||
262 | } | - | ||||||||||||||||||||||||||||||
263 | else if ((flags & (AFLAG|RFLAG|NFLAG|WFLAG|CFLAG)) == 0)
| 6-15 | ||||||||||||||||||||||||||||||
264 | { | - | ||||||||||||||||||||||||||||||
265 | result = display_history (list); | - | ||||||||||||||||||||||||||||||
266 | return (sh_chkwrite (result)); executed 15 times by 1 test: return (sh_chkwrite (result)); Executed by:
| 15 | ||||||||||||||||||||||||||||||
267 | } | - | ||||||||||||||||||||||||||||||
268 | - | |||||||||||||||||||||||||||||||
269 | filename = list ? list->word->word : get_string_value ("HISTFILE");
| 2-4 | ||||||||||||||||||||||||||||||
270 | result = EXECUTION_SUCCESS; | - | ||||||||||||||||||||||||||||||
271 | - | |||||||||||||||||||||||||||||||
272 | if (flags & AFLAG) /* Append session's history to file. */
| 2-4 | ||||||||||||||||||||||||||||||
273 | result = maybe_append_history (filename); executed 2 times by 1 test: result = maybe_append_history (filename); Executed by:
| 2 | ||||||||||||||||||||||||||||||
274 | else if (flags & WFLAG) /* Write entire history. */
| 2 | ||||||||||||||||||||||||||||||
275 | result = write_history (filename); executed 2 times by 1 test: result = write_history (filename); Executed by:
| 2 | ||||||||||||||||||||||||||||||
276 | else if (flags & RFLAG) /* Read entire file. */
| 1 | ||||||||||||||||||||||||||||||
277 | { | - | ||||||||||||||||||||||||||||||
278 | result = read_history (filename); | - | ||||||||||||||||||||||||||||||
279 | history_lines_in_file = history_lines_read_from_file; | - | ||||||||||||||||||||||||||||||
280 | /* history_lines_in_file = where_history () + history_base - 1; */ | - | ||||||||||||||||||||||||||||||
281 | } executed 1 time by 1 test: end of block Executed by:
| 1 | ||||||||||||||||||||||||||||||
282 | else if (flags & NFLAG) /* Read `new' history from file. */
| 0-1 | ||||||||||||||||||||||||||||||
283 | { | - | ||||||||||||||||||||||||||||||
284 | /* Read all of the lines in the file that we haven't already read. */ | - | ||||||||||||||||||||||||||||||
285 | old_history_lines = history_lines_in_file; | - | ||||||||||||||||||||||||||||||
286 | obase = history_base; | - | ||||||||||||||||||||||||||||||
287 | - | |||||||||||||||||||||||||||||||
288 | using_history (); | - | ||||||||||||||||||||||||||||||
289 | result = read_history_range (filename, history_lines_in_file, -1); | - | ||||||||||||||||||||||||||||||
290 | using_history (); | - | ||||||||||||||||||||||||||||||
291 | - | |||||||||||||||||||||||||||||||
292 | history_lines_in_file = history_lines_read_from_file; | - | ||||||||||||||||||||||||||||||
293 | /* history_lines_in_file = where_history () + history_base - 1; */ | - | ||||||||||||||||||||||||||||||
294 | - | |||||||||||||||||||||||||||||||
295 | /* If we're rewriting the history file at shell exit rather than just | - | ||||||||||||||||||||||||||||||
296 | appending the lines from this session to it, the question is whether | - | ||||||||||||||||||||||||||||||
297 | we reset history_lines_this_session to 0, losing any history entries | - | ||||||||||||||||||||||||||||||
298 | we had before we read the new entries from the history file, or | - | ||||||||||||||||||||||||||||||
299 | whether we count the new entries we just read from the file as | - | ||||||||||||||||||||||||||||||
300 | history lines added during this session. | - | ||||||||||||||||||||||||||||||
301 | Right now, we do the latter. This will cause these history entries | - | ||||||||||||||||||||||||||||||
302 | to be written to the history file along with any intermediate entries | - | ||||||||||||||||||||||||||||||
303 | we add when we do a `history -a', but the alternative is losing | - | ||||||||||||||||||||||||||||||
304 | them altogether. */ | - | ||||||||||||||||||||||||||||||
305 | if (force_append_history == 0)
| 0-1 | ||||||||||||||||||||||||||||||
306 | history_lines_this_session += history_lines_in_file - old_history_lines + executed 1 time by 1 test: history_lines_this_session += history_lines_in_file - old_history_lines + history_base - obase; Executed by:
| 1 | ||||||||||||||||||||||||||||||
307 | history_base - obase; executed 1 time by 1 test: history_lines_this_session += history_lines_in_file - old_history_lines + history_base - obase; Executed by:
| 1 | ||||||||||||||||||||||||||||||
308 | } executed 1 time by 1 test: end of block Executed by:
| 1 | ||||||||||||||||||||||||||||||
309 | - | |||||||||||||||||||||||||||||||
310 | return (result ? EXECUTION_FAILURE : EXECUTION_SUCCESS); executed 6 times by 1 test: return (result ? 1 : 0); Executed by:
| 6 | ||||||||||||||||||||||||||||||
311 | } | - | ||||||||||||||||||||||||||||||
312 | - | |||||||||||||||||||||||||||||||
313 | /* Accessors for HIST_ENTRY lists that are called HLIST. */ | - | ||||||||||||||||||||||||||||||
314 | #define histline(i) (hlist[(i)]->line) | - | ||||||||||||||||||||||||||||||
315 | #define histdata(i) (hlist[(i)]->data) | - | ||||||||||||||||||||||||||||||
316 | - | |||||||||||||||||||||||||||||||
317 | static char * | - | ||||||||||||||||||||||||||||||
318 | histtime (hlist, histtimefmt) | - | ||||||||||||||||||||||||||||||
319 | HIST_ENTRY *hlist; | - | ||||||||||||||||||||||||||||||
320 | const char *histtimefmt; | - | ||||||||||||||||||||||||||||||
321 | { | - | ||||||||||||||||||||||||||||||
322 | static char timestr[128]; | - | ||||||||||||||||||||||||||||||
323 | time_t t; | - | ||||||||||||||||||||||||||||||
324 | struct tm *tm; | - | ||||||||||||||||||||||||||||||
325 | - | |||||||||||||||||||||||||||||||
326 | t = history_get_time (hlist); | - | ||||||||||||||||||||||||||||||
327 | tm = t ? localtime (&t) : 0;
| 0 | ||||||||||||||||||||||||||||||
328 | if (t && tm)
| 0 | ||||||||||||||||||||||||||||||
329 | strftime (timestr, sizeof (timestr), histtimefmt, tm); never executed: strftime (timestr, sizeof (timestr), histtimefmt, tm); | 0 | ||||||||||||||||||||||||||||||
330 | else if (hlist->timestamp && hlist->timestamp[0])
| 0 | ||||||||||||||||||||||||||||||
331 | snprintf (timestr, sizeof (timestr), _("%s: invalid timestamp"), never executed: snprintf (timestr, sizeof (timestr), dcgettext (((void *)0), "%s: invalid timestamp" , 5) , (hlist->timestamp[0] == '#') ? hlist->timestamp + 1: hlist->timestamp); | 0 | ||||||||||||||||||||||||||||||
332 | (hlist->timestamp[0] == '#') ? hlist->timestamp + 1: hlist->timestamp); never executed: snprintf (timestr, sizeof (timestr), dcgettext (((void *)0), "%s: invalid timestamp" , 5) , (hlist->timestamp[0] == '#') ? hlist->timestamp + 1: hlist->timestamp); | 0 | ||||||||||||||||||||||||||||||
333 | else | - | ||||||||||||||||||||||||||||||
334 | strcpy (timestr, "??"); never executed: strcpy (timestr, "??"); | 0 | ||||||||||||||||||||||||||||||
335 | return timestr; never executed: return timestr; | 0 | ||||||||||||||||||||||||||||||
336 | } | - | ||||||||||||||||||||||||||||||
337 | - | |||||||||||||||||||||||||||||||
338 | static int | - | ||||||||||||||||||||||||||||||
339 | display_history (list) | - | ||||||||||||||||||||||||||||||
340 | WORD_LIST *list; | - | ||||||||||||||||||||||||||||||
341 | { | - | ||||||||||||||||||||||||||||||
342 | register int i; | - | ||||||||||||||||||||||||||||||
343 | intmax_t limit; | - | ||||||||||||||||||||||||||||||
344 | HIST_ENTRY **hlist; | - | ||||||||||||||||||||||||||||||
345 | char *histtimefmt, *timestr; | - | ||||||||||||||||||||||||||||||
346 | - | |||||||||||||||||||||||||||||||
347 | if (list)
| 1-14 | ||||||||||||||||||||||||||||||
348 | { | - | ||||||||||||||||||||||||||||||
349 | if (get_numeric_arg (list, 0, &limit) == 0)
| 0-1 | ||||||||||||||||||||||||||||||
350 | return (EXECUTION_FAILURE); never executed: return (1); | 0 | ||||||||||||||||||||||||||||||
351 | - | |||||||||||||||||||||||||||||||
352 | if (limit < 0)
| 0-1 | ||||||||||||||||||||||||||||||
353 | limit = -limit; never executed: limit = -limit; | 0 | ||||||||||||||||||||||||||||||
354 | } executed 1 time by 1 test: end of block Executed by:
| 1 | ||||||||||||||||||||||||||||||
355 | else | - | ||||||||||||||||||||||||||||||
356 | limit = -1; executed 14 times by 1 test: limit = -1; Executed by:
| 14 | ||||||||||||||||||||||||||||||
357 | - | |||||||||||||||||||||||||||||||
358 | hlist = history_list (); | - | ||||||||||||||||||||||||||||||
359 | - | |||||||||||||||||||||||||||||||
360 | if (hlist)
| 0-15 | ||||||||||||||||||||||||||||||
361 | { | - | ||||||||||||||||||||||||||||||
362 | for (i = 0; hlist[i]; i++)
| 15-135 | ||||||||||||||||||||||||||||||
363 | ; executed 135 times by 1 test: ; Executed by:
| 135 | ||||||||||||||||||||||||||||||
364 | - | |||||||||||||||||||||||||||||||
365 | if (0 <= limit && limit < i)
| 0-14 | ||||||||||||||||||||||||||||||
366 | i -= limit; executed 1 time by 1 test: i -= limit; Executed by:
| 1 | ||||||||||||||||||||||||||||||
367 | else | - | ||||||||||||||||||||||||||||||
368 | i = 0; executed 14 times by 1 test: i = 0; Executed by:
| 14 | ||||||||||||||||||||||||||||||
369 | - | |||||||||||||||||||||||||||||||
370 | histtimefmt = get_string_value ("HISTTIMEFORMAT"); | - | ||||||||||||||||||||||||||||||
371 | - | |||||||||||||||||||||||||||||||
372 | while (hlist[i])
| 15-122 | ||||||||||||||||||||||||||||||
373 | { | - | ||||||||||||||||||||||||||||||
374 | QUIT; never executed: termsig_handler (terminating_signal); never executed: throw_to_top_level ();
| 0-122 | ||||||||||||||||||||||||||||||
375 | - | |||||||||||||||||||||||||||||||
376 | timestr = (histtimefmt && *histtimefmt) ? histtime (hlist[i], histtimefmt) : (char *)NULL;
| 0-122 | ||||||||||||||||||||||||||||||
377 | printf ("%5d%c %s%s\n", i + history_base, | - | ||||||||||||||||||||||||||||||
378 | histdata(i) ? '*' : ' ', | - | ||||||||||||||||||||||||||||||
379 | ((timestr && *timestr) ? timestr : ""), | - | ||||||||||||||||||||||||||||||
380 | histline(i)); | - | ||||||||||||||||||||||||||||||
381 | i++; | - | ||||||||||||||||||||||||||||||
382 | } executed 122 times by 1 test: end of block Executed by:
| 122 | ||||||||||||||||||||||||||||||
383 | } executed 15 times by 1 test: end of block Executed by:
| 15 | ||||||||||||||||||||||||||||||
384 | - | |||||||||||||||||||||||||||||||
385 | return (EXECUTION_SUCCESS); executed 15 times by 1 test: return (0); Executed by:
| 15 | ||||||||||||||||||||||||||||||
386 | } | - | ||||||||||||||||||||||||||||||
387 | - | |||||||||||||||||||||||||||||||
388 | /* Remove the last entry in the history list and add each argument in | - | ||||||||||||||||||||||||||||||
389 | LIST to the history. */ | - | ||||||||||||||||||||||||||||||
390 | static void | - | ||||||||||||||||||||||||||||||
391 | push_history (list) | - | ||||||||||||||||||||||||||||||
392 | WORD_LIST *list; | - | ||||||||||||||||||||||||||||||
393 | { | - | ||||||||||||||||||||||||||||||
394 | char *s; | - | ||||||||||||||||||||||||||||||
395 | - | |||||||||||||||||||||||||||||||
396 | /* Delete the last history entry if it was a single entry added to the | - | ||||||||||||||||||||||||||||||
397 | history list (generally the `history -s' itself), or if `history -s' | - | ||||||||||||||||||||||||||||||
398 | is being used in a compound command and the compound command was | - | ||||||||||||||||||||||||||||||
399 | added to the history as a single element (command-oriented history). | - | ||||||||||||||||||||||||||||||
400 | If you don't want history -s to remove the compound command from the | - | ||||||||||||||||||||||||||||||
401 | history, change #if 0 to #if 1 below. */ | - | ||||||||||||||||||||||||||||||
402 | #if 0 | - | ||||||||||||||||||||||||||||||
403 | if (remember_on_history && hist_last_line_pushed == 0 && | - | ||||||||||||||||||||||||||||||
404 | hist_last_line_added && bash_delete_last_history () == 0) | - | ||||||||||||||||||||||||||||||
405 | #else | - | ||||||||||||||||||||||||||||||
406 | if (remember_on_history && hist_last_line_pushed == 0 &&
| 0-3 | ||||||||||||||||||||||||||||||
407 | (hist_last_line_added ||
| 0-3 | ||||||||||||||||||||||||||||||
408 | (current_command_line_count > 0 && current_command_first_line_saved && command_oriented_history))
| 0-3 | ||||||||||||||||||||||||||||||
409 | && bash_delete_last_history () == 0)
| 0 | ||||||||||||||||||||||||||||||
410 | #endif | - | ||||||||||||||||||||||||||||||
411 | return; never executed: return; | 0 | ||||||||||||||||||||||||||||||
412 | - | |||||||||||||||||||||||||||||||
413 | s = string_list (list); | - | ||||||||||||||||||||||||||||||
414 | /* Call check_add_history with FORCE set to 1 to skip the check against | - | ||||||||||||||||||||||||||||||
415 | current_command_line_count. If history -s is used in a compound | - | ||||||||||||||||||||||||||||||
416 | command, the above code will delete the compound command's history | - | ||||||||||||||||||||||||||||||
417 | entry and this call will add the line to the history as a separate | - | ||||||||||||||||||||||||||||||
418 | entry. Without FORCE=1, if current_command_line_count were > 1, the | - | ||||||||||||||||||||||||||||||
419 | line would be appended to the entry before the just-deleted entry. */ | - | ||||||||||||||||||||||||||||||
420 | check_add_history (s, 1); /* obeys HISTCONTROL, HISTIGNORE */ | - | ||||||||||||||||||||||||||||||
421 | - | |||||||||||||||||||||||||||||||
422 | hist_last_line_pushed = 1; /* XXX */ | - | ||||||||||||||||||||||||||||||
423 | free (s); | - | ||||||||||||||||||||||||||||||
424 | } executed 3 times by 1 test: end of block Executed by:
| 3 | ||||||||||||||||||||||||||||||
425 | - | |||||||||||||||||||||||||||||||
426 | #if defined (BANG_HISTORY) | - | ||||||||||||||||||||||||||||||
427 | static int | - | ||||||||||||||||||||||||||||||
428 | expand_and_print_history (list) | - | ||||||||||||||||||||||||||||||
429 | WORD_LIST *list; | - | ||||||||||||||||||||||||||||||
430 | { | - | ||||||||||||||||||||||||||||||
431 | char *s; | - | ||||||||||||||||||||||||||||||
432 | int r, result; | - | ||||||||||||||||||||||||||||||
433 | - | |||||||||||||||||||||||||||||||
434 | if (hist_last_line_pushed == 0 && hist_last_line_added && bash_delete_last_history () == 0)
| 0-5 | ||||||||||||||||||||||||||||||
435 | return EXECUTION_FAILURE; never executed: return 1; | 0 | ||||||||||||||||||||||||||||||
436 | result = EXECUTION_SUCCESS; | - | ||||||||||||||||||||||||||||||
437 | while (list)
| 7 | ||||||||||||||||||||||||||||||
438 | { | - | ||||||||||||||||||||||||||||||
439 | r = history_expand (list->word->word, &s); | - | ||||||||||||||||||||||||||||||
440 | if (r < 0)
| 1-6 | ||||||||||||||||||||||||||||||
441 | { | - | ||||||||||||||||||||||||||||||
442 | builtin_error (_("%s: history expansion failed"), list->word->word); | - | ||||||||||||||||||||||||||||||
443 | result = EXECUTION_FAILURE; | - | ||||||||||||||||||||||||||||||
444 | } executed 1 time by 1 test: end of block Executed by:
| 1 | ||||||||||||||||||||||||||||||
445 | else | - | ||||||||||||||||||||||||||||||
446 | { | - | ||||||||||||||||||||||||||||||
447 | fputs (s, stdout); | - | ||||||||||||||||||||||||||||||
448 | putchar ('\n'); | - | ||||||||||||||||||||||||||||||
449 | } executed 6 times by 1 test: end of block Executed by:
| 6 | ||||||||||||||||||||||||||||||
450 | FREE (s); executed 7 times by 1 test: sh_xfree((s), "./history.def", 450); Executed by:
| 0-7 | ||||||||||||||||||||||||||||||
451 | list = list->next; | - | ||||||||||||||||||||||||||||||
452 | } executed 7 times by 1 test: end of block Executed by:
| 7 | ||||||||||||||||||||||||||||||
453 | fflush (stdout); | - | ||||||||||||||||||||||||||||||
454 | return result; executed 7 times by 1 test: return result; Executed by:
| 7 | ||||||||||||||||||||||||||||||
455 | } | - | ||||||||||||||||||||||||||||||
456 | #endif /* BANG_HISTORY */ | - | ||||||||||||||||||||||||||||||
457 | #endif /* HISTORY */ | - | ||||||||||||||||||||||||||||||
Source code | Switch to Preprocessed file |