3b43674f79dd9142a7e4ae798a84fdecd7a684ac
[deliverable/binutils-gdb.git] / readline / examples / histexamp.c
1 /* histexamp.c - history library example program. */
2
3 /* Copyright (C) 1987-2009 Free Software Foundation, Inc.
4
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.
7
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.
12
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.
17
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/>.
20 */
21
22 #include <stdio.h>
23
24 #ifdef READLINE_LIBRARY
25 # include "history.h"
26 #else
27 # include <readline/history.h>
28 #endif
29
30 #include <string.h>
31
32 main (argc, argv)
33 int argc;
34 char **argv;
35 {
36 char line[1024], *t;
37 int len, done;
38
39 line[0] = 0;
40 done = 0;
41
42 using_history ();
43 while (!done)
44 {
45 printf ("history$ ");
46 fflush (stdout);
47 t = fgets (line, sizeof (line) - 1, stdin);
48 if (t && *t)
49 {
50 len = strlen (t);
51 if (t[len - 1] == '\n')
52 t[len - 1] = '\0';
53 }
54
55 if (!t)
56 strcpy (line, "quit");
57
58 if (line[0])
59 {
60 char *expansion;
61 int result;
62
63 using_history ();
64
65 result = history_expand (line, &expansion);
66 if (result)
67 fprintf (stderr, "%s\n", expansion);
68
69 if (result < 0 || result == 2)
70 {
71 free (expansion);
72 continue;
73 }
74
75 add_history (expansion);
76 strncpy (line, expansion, sizeof (line) - 1);
77 free (expansion);
78 }
79
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)
87 {
88 register HIST_ENTRY **the_list;
89 register int i;
90 time_t tt;
91 char timestr[128];
92
93 the_list = history_list ();
94 if (the_list)
95 for (i = 0; the_list[i]; i++)
96 {
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);
103 }
104 }
105 else if (strncmp (line, "delete", 6) == 0)
106 {
107 int which;
108 if ((sscanf (line + 6, "%d", &which)) == 1)
109 {
110 HIST_ENTRY *entry = remove_history (which);
111 if (!entry)
112 fprintf (stderr, "No such entry %d\n", which);
113 else
114 {
115 free (entry->line);
116 free (entry);
117 }
118 }
119 else
120 {
121 fprintf (stderr, "non-numeric arg given to `delete'\n");
122 }
123 }
124 }
125 }
This page took 0.03264 seconds and 3 git commands to generate.