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