Imported from ../bash-3.0.tar.gz.
[platform/upstream/bash.git] / builtins / history.def
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
81 static char *histtime __P((HIST_ENTRY *, const char *));
82 static void display_history __P((WORD_LIST *));
83 static int delete_histent __P((int));
84 static int delete_last_history __P((void));
85 static void push_history __P((WORD_LIST *));
86 static int expand_and_print_history __P((WORD_LIST *));
87
88 #define AFLAG   0x01
89 #define RFLAG   0x02
90 #define WFLAG   0x04
91 #define NFLAG   0x08
92 #define SFLAG   0x10
93 #define PFLAG   0x20
94 #define CFLAG   0x40
95 #define DFLAG   0x80
96
97 int
98 history_builtin (list)
99      WORD_LIST *list;
100 {
101   int flags, opt, result, old_history_lines, obase;
102   char *filename, *delete_arg;
103   intmax_t delete_offset;
104
105   flags = 0;
106   reset_internal_getopt ();
107   while ((opt = internal_getopt (list, "acd:npsrw")) != -1)
108     {
109       switch (opt)
110         {
111         case 'a':
112           flags |= AFLAG;
113           break;
114         case 'c':
115           flags |= CFLAG;
116           break;
117         case 'n':
118           flags |= NFLAG;
119           break;
120         case 'r':
121           flags |= RFLAG;
122           break;
123         case 'w':
124           flags |= WFLAG;
125           break;
126         case 's':
127           flags |= SFLAG;
128           break;
129         case 'd':
130           flags |= DFLAG;
131           delete_arg = list_optarg;
132           break;
133         case 'p':
134 #if defined (BANG_HISTORY)
135           flags |= PFLAG;
136 #endif
137           break;
138         default:
139           builtin_usage ();
140           return (EX_USAGE);
141         }
142     }
143   list = loptend;
144
145   opt = flags & (AFLAG|RFLAG|WFLAG|NFLAG);
146   if (opt && opt != AFLAG && opt != RFLAG && opt != WFLAG && opt != NFLAG)
147     {
148       builtin_error (_("cannot use more than one of -anrw"));
149       return (EXECUTION_FAILURE);
150     }
151
152   /* clear the history, but allow other arguments to add to it again. */
153   if (flags & CFLAG)
154     {
155       clear_history ();
156       if (list == 0)
157         return (EXECUTION_SUCCESS);
158     }
159
160   if (flags & SFLAG)
161     {
162       if (list)
163         push_history (list);
164       return (EXECUTION_SUCCESS);
165     }
166 #if defined (BANG_HISTORY)
167   else if (flags & PFLAG)
168     {
169       if (list)
170         return (expand_and_print_history (list));
171       return (EXECUTION_SUCCESS);
172     }
173 #endif
174   else if (flags & DFLAG)
175     {
176       if ((legal_number (delete_arg, &delete_offset) == 0)
177           || (delete_offset < history_base)
178           || (delete_offset > (history_base + history_length)))
179         {
180           sh_erange (delete_arg, _("history position"));
181           return (EXECUTION_FAILURE);
182         }
183       opt = delete_offset;
184       result = delete_histent (opt - history_base);
185       /* Since remove_history changes history_length, this can happen if
186          we delete the last history entry. */
187       if (where_history () > history_length)
188         history_set_pos (history_length);
189       return (result ? EXECUTION_SUCCESS : EXECUTION_FAILURE);
190     }
191   else if ((flags & (AFLAG|RFLAG|NFLAG|WFLAG|CFLAG)) == 0)
192     {
193       display_history (list);
194       return (EXECUTION_SUCCESS);
195     }
196
197   filename = list ? list->word->word : get_string_value ("HISTFILE");
198   result = EXECUTION_SUCCESS;
199
200   if (flags & AFLAG)            /* Append session's history to file. */
201     result = maybe_append_history (filename);
202   else if (flags & WFLAG)       /* Write entire history. */
203     result = write_history (filename);
204   else if (flags & RFLAG)       /* Read entire file. */
205     result = read_history (filename);
206   else if (flags & NFLAG)       /* Read `new' history from file. */
207     {
208       /* Read all of the lines in the file that we haven't already read. */
209       old_history_lines = history_lines_in_file;
210       obase = history_base;
211
212       using_history ();
213       result = read_history_range (filename, history_lines_in_file, -1);
214       using_history ();
215
216       history_lines_in_file = where_history ();
217       /* The question is whether we reset history_lines_this_session to 0,
218          losing any history entries we had before we read the new entries
219          from the history file, or whether we count the new entries we just
220          read from the file as history lines added during this session.
221          Right now, we do the latter.  This will cause these history entries
222          to be written to the history file along with any intermediate entries
223          we add when we do a `history -a', but the alternative is losing
224          them altogether. */
225       history_lines_this_session += history_lines_in_file - old_history_lines +
226                                     history_base - obase;
227     }
228
229   return (result ? EXECUTION_FAILURE : EXECUTION_SUCCESS);
230 }
231
232 /* Accessors for HIST_ENTRY lists that are called HLIST. */
233 #define histline(i) (hlist[(i)]->line)
234 #define histdata(i) (hlist[(i)]->data)
235
236 static char *
237 histtime (hlist, histtimefmt)
238      HIST_ENTRY *hlist;
239      const char *histtimefmt;
240 {
241   static char timestr[128];
242   time_t t;
243
244   t = history_get_time (hlist);
245   if (t)
246     strftime (timestr, sizeof (timestr), histtimefmt, localtime (&t));
247   else
248     strcpy (timestr, "??");
249   return timestr;
250 }
251
252 static void
253 display_history (list)
254      WORD_LIST *list;
255 {
256   register int i;
257   intmax_t limit;
258   HIST_ENTRY **hlist;
259   char *histtimefmt, *timestr;
260
261   if (list)
262     {
263       limit = get_numeric_arg (list, 0);
264       if (limit < 0)
265         limit = -limit;
266     }
267   else
268     limit = -1;
269
270   hlist = history_list ();
271
272   if (hlist)
273     {
274       for (i = 0;  hlist[i]; i++)
275         ;
276
277       if (0 <= limit && limit < i)
278         i -= limit;
279       else
280         i = 0;
281
282
283       histtimefmt = get_string_value ("HISTTIMEFORMAT");
284
285       while (hlist[i])
286         {
287           QUIT;
288
289           timestr = (histtimefmt && *histtimefmt) ? histtime (hlist[i], histtimefmt) : (char *)NULL;
290           printf ("%5d%c %s%s\n", i + history_base,
291                   histdata(i) ? '*' : ' ',
292                   ((timestr && *timestr) ? timestr : ""),
293                   histline(i));
294           i++;
295         }
296     }
297 }
298
299 /* Delete and free the history list entry at offset I. */
300 static int
301 delete_histent (i)
302      int i;
303 {
304   HIST_ENTRY *discard;
305
306   discard = remove_history (i);
307   if (discard)
308     free_history_entry (discard);
309
310   return 1;
311 }
312
313 static int
314 delete_last_history ()
315 {
316   register int i;
317   HIST_ENTRY **hlist, *histent;
318   int r;
319
320   hlist = history_list ();
321   if (hlist == NULL)
322     return 0;
323
324   for (i = 0; hlist[i]; i++)
325     ;
326   i--;
327
328   /* History_get () takes a parameter that must be offset by history_base. */
329   histent = history_get (history_base + i);     /* Don't free this */
330   if (histent == NULL)
331     return 0;
332
333   r = delete_histent (i);
334
335   if (where_history () > history_length)
336     history_set_pos (history_length);
337
338   return r;
339 }
340
341 /* Remove the last entry in the history list and add each argument in
342    LIST to the history. */
343 static void
344 push_history (list)
345      WORD_LIST *list;
346 {
347   char *s;
348
349   /* Delete the last history entry if it was a single entry added to the
350      history list (generally the `history -s' itself), or if `history -s'
351      is being used in a compound command and the compound command was
352      added to the history as a single element (command-oriented history).
353      If you don't want history -s to remove the compound command from the
354      history, change #if 0 to #if 1 below. */
355 #if 0
356   if (hist_last_line_added && delete_last_history () == 0)
357 #else
358   if ((hist_last_line_added || (current_command_line_count > 0 && current_command_first_line_saved && command_oriented_history))
359       && delete_last_history () == 0)
360 #endif
361       return;
362
363   s = string_list (list);
364   /* Call check_add_history with FORCE set to 1 to skip the check against
365      current_command_line_count.  If history -s is used in a compound
366      command, the above code will delete the compound command's history
367      entry and this call will add the line to the history as a separate
368      entry.  Without FORCE=1, if current_command_line_count were > 1, the
369      line would be appended to the entry before the just-deleted entry. */
370   check_add_history (s, 1);     /* obeys HISTCONTROL, HISTIGNORE */
371   free (s);
372 }
373
374 #if defined (BANG_HISTORY)
375 static int
376 expand_and_print_history (list)
377      WORD_LIST *list;
378 {
379   char *s;
380   int r, result;
381
382   if (hist_last_line_added && delete_last_history () == 0)
383     return EXECUTION_FAILURE;
384   result = EXECUTION_SUCCESS;
385   while (list)
386     {
387       r = history_expand (list->word->word, &s);
388       if (r < 0)
389         {
390           builtin_error (_("%s: history expansion failed"), list->word->word);
391           result = EXECUTION_FAILURE;
392         }
393       else
394         {
395           fputs (s, stdout);
396           putchar ('\n');
397         }
398       FREE (s);
399       list = list->next;
400     }
401   fflush (stdout);
402   return result;
403 }
404 #endif /* BANG_HISTORY */
405 #endif /* HISTORY */