Automatic date update in version.in
[binutils-gdb.git] / readline / examples / histexamp.c
blob3b43674f79dd9142a7e4ae798a84fdecd7a684ac
1 /* histexamp.c - history library example program. */
3 /* Copyright (C) 1987-2009 Free Software Foundation, Inc.
5 This file is part of the GNU Readline Library (Readline), a library for
6 reading lines of text with interactive input and history editing.
8 Readline 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.
13 Readline 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.
18 You should have received a copy of the GNU General Public License
19 along with Readline. If not, see <http://www.gnu.org/licenses/>.
22 #include <stdio.h>
24 #ifdef READLINE_LIBRARY
25 # include "history.h"
26 #else
27 # include <readline/history.h>
28 #endif
30 #include <string.h>
32 main (argc, argv)
33 int argc;
34 char **argv;
36 char line[1024], *t;
37 int len, done;
39 line[0] = 0;
40 done = 0;
42 using_history ();
43 while (!done)
45 printf ("history$ ");
46 fflush (stdout);
47 t = fgets (line, sizeof (line) - 1, stdin);
48 if (t && *t)
50 len = strlen (t);
51 if (t[len - 1] == '\n')
52 t[len - 1] = '\0';
55 if (!t)
56 strcpy (line, "quit");
58 if (line[0])
60 char *expansion;
61 int result;
63 using_history ();
65 result = history_expand (line, &expansion);
66 if (result)
67 fprintf (stderr, "%s\n", expansion);
69 if (result < 0 || result == 2)
71 free (expansion);
72 continue;
75 add_history (expansion);
76 strncpy (line, expansion, sizeof (line) - 1);
77 free (expansion);
80 if (strcmp (line, "quit") == 0)
81 done = 1;
82 else if (strcmp (line, "save") == 0)
83 write_history ("history_file");
84 else if (strcmp (line, "read") == 0)
85 read_history ("history_file");
86 else if (strcmp (line, "list") == 0)
88 register HIST_ENTRY **the_list;
89 register int i;
90 time_t tt;
91 char timestr[128];
93 the_list = history_list ();
94 if (the_list)
95 for (i = 0; the_list[i]; i++)
97 tt = history_get_time (the_list[i]);
98 if (tt)
99 strftime (timestr, sizeof (timestr), "%a %R", localtime(&tt));
100 else
101 strcpy (timestr, "??");
102 printf ("%d: %s: %s\n", i + history_base, timestr, the_list[i]->line);
105 else if (strncmp (line, "delete", 6) == 0)
107 int which;
108 if ((sscanf (line + 6, "%d", &which)) == 1)
110 HIST_ENTRY *entry = remove_history (which);
111 if (!entry)
112 fprintf (stderr, "No such entry %d\n", which);
113 else
115 free (entry->line);
116 free (entry);
119 else
121 fprintf (stderr, "non-numeric arg given to `delete'\n");