814e7054664182d3b57aa6a67259095d6ce6a012
[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, 1989, 1991 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 1, 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, 675 Mass Ave, Cambridge, MA 02139, USA.
21
22 $PRODUCES history.c
23
24 $BUILTIN history
25 $FUNCTION history_builtin
26 $DEPENDS_ON HISTORY
27 $SHORT_DOC history [n] [ [-awrn] [filename]]
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.  Argument `-w' means to write out the current
31 history file;  `-r' means to read it instead.  Argument `-a' means
32 to append history lines from this session to the history file.
33 Argument `-n' means to read all history lines not already read
34 from the history file.  If FILENAME is given, then use that file,
35 else if $HISTFILE has a value, use that, else use ~/.bash_history.
36 $END
37
38 #include "../shell.h"
39 #if defined (HISTORY)
40 #include <sys/types.h>
41 #include <sys/file.h>
42 #include "../filecntl.h"
43 #include "../posixstat.h"
44 #include "../bashhist.h"
45 #include <readline/history.h>
46
47 /* History.  Arg of -w FILENAME means write file, arg of -r FILENAME
48    means read file.  Arg of N means only display that many items. */
49
50 history_builtin (list)
51      WORD_LIST *list;
52 {
53   register int i;
54   int limited = 0, limit = 0;
55   HIST_ENTRY **hlist;
56
57   while (list)
58     {
59       char *arg = list->word->word;
60
61       if ((arg[0] == '-') &&
62           (strlen (arg) == 2) &&
63           (member (arg[1], "rwan")))
64         {
65           char *file;
66           int result = EXECUTION_SUCCESS;
67
68           if (list->next)
69             file = list->next->word->word;
70           else
71             file = get_string_value ("HISTFILE");
72
73           switch (arg[1])
74             {
75             case 'a':           /* Append `new' lines to file. */
76               {
77                 if (history_lines_this_session)
78                   {
79                     void using_history ();
80
81                     if (history_lines_this_session < where_history ())
82                       {
83                         /* If the filename was supplied, then create it
84                            if it doesn't already exist. */
85                         if (file)
86                           {
87                             struct stat buf;
88
89                             if (stat (file, &buf) == -1)
90                               {
91                                 int tem;
92
93                                 tem = open (file, O_CREAT, 0666);
94                                 close (tem);
95                               }
96                           }
97
98                         result =
99                           append_history (history_lines_this_session, file);
100                         history_lines_in_file += history_lines_this_session;
101                         history_lines_this_session = 0;
102                       }
103                   }
104                 break;
105               }
106
107             case 'w':           /* Write entire history. */
108               {
109                 result = write_history (file);
110                 break;
111               }
112
113             case 'r':           /* Read entire file. */
114               {
115                 result = read_history (file);
116                 break;
117               }
118
119             case 'n':           /* Read `new' history from file. */
120               {
121                 /* Read all of the lines in the file that we haven't
122                    already read. */
123                 using_history ();
124                 result = read_history_range (file, history_lines_in_file, -1);
125                 using_history ();
126                 history_lines_in_file = where_history ();
127
128                 break;
129               }
130             }
131           return (result ? EXECUTION_FAILURE : EXECUTION_SUCCESS);
132         }
133       else if (strcmp (list->word->word, "--") == 0)
134         {
135           list = list->next;
136           break;
137         }
138       else if (*list->word->word == '-')
139         {
140           bad_option (list->word->word);
141           builtin_error ("usage: history [n] [-rwan [filename]]");
142           return (EX_USAGE);
143         }
144       else
145         break;
146     }
147
148   if (list)
149     {
150       limited = 1;
151       limit = get_numeric_arg (list);
152     }
153
154   hlist = history_list ();
155
156   if (hlist)
157     {
158       for (i = 0;  hlist[i]; i++);
159
160       if (limit < 0)
161         limit = -limit;
162
163       if (!limited)
164         i = 0;
165       else
166         if ((i -= limit) < 0)
167           i = 0;
168
169       while (hlist[i])
170         {
171           QUIT;
172           printf ("%5d%c %s\n", i + history_base,
173                   hlist[i]->data ? '*' : ' ', hlist[i]->line);
174           i++;
175         }
176     }
177   return (EXECUTION_SUCCESS);
178 }
179 #endif /* HISTORY */