]> git.ipfire.org Git - thirdparty/bash.git/blame - lib/readline/examples/histexamp.c
Imported from ../bash-4.0-rc1.tar.gz.
[thirdparty/bash.git] / lib / readline / examples / histexamp.c
CommitLineData
3185942a 1/* histexamp.c - history library example program. */
7117c2d2 2
3185942a
JA
3/* Copyright (C) 1987-2009 Free Software Foundation, Inc.
4
5 This file is part of the GNU Readline Library (Readline), a library for
7117c2d2
JA
6 reading lines of text with interactive input and history editing.
7
3185942a
JA
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
7117c2d2
JA
11 (at your option) any later version.
12
3185942a
JA
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
7117c2d2
JA
16 GNU General Public License for more details.
17
3185942a
JA
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*/
7117c2d2 21
28ef6c31
JA
22#include <stdio.h>
23
24#ifdef READLINE_LIBRARY
25# include "history.h"
26#else
27# include <readline/history.h>
28#endif
29
95732b49
JA
30#include <string.h>
31
28ef6c31
JA
32main (argc, argv)
33 int argc;
34 char **argv;
726f6388
JA
35{
36 char line[1024], *t;
b80f6443 37 int len, done;
726f6388
JA
38
39 line[0] = 0;
b80f6443 40 done = 0;
726f6388
JA
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)
b80f6443
JA
49 {
50 len = strlen (t);
51 if (t[len - 1] == '\n')
52 t[len - 1] = '\0';
53 }
726f6388
JA
54
55 if (!t)
b80f6443 56 strcpy (line, "quit");
726f6388
JA
57
58 if (line[0])
b80f6443
JA
59 {
60 char *expansion;
61 int result;
726f6388 62
b80f6443 63 using_history ();
726f6388 64
b80f6443
JA
65 result = history_expand (line, &expansion);
66 if (result)
67 fprintf (stderr, "%s\n", expansion);
726f6388 68
b80f6443
JA
69 if (result < 0 || result == 2)
70 {
71 free (expansion);
72 continue;
73 }
726f6388 74
b80f6443
JA
75 add_history (expansion);
76 strncpy (line, expansion, sizeof (line) - 1);
77 free (expansion);
78 }
726f6388
JA
79
80 if (strcmp (line, "quit") == 0)
b80f6443 81 done = 1;
726f6388 82 else if (strcmp (line, "save") == 0)
b80f6443 83 write_history ("history_file");
726f6388 84 else if (strcmp (line, "read") == 0)
b80f6443 85 read_history ("history_file");
726f6388 86 else if (strcmp (line, "list") == 0)
b80f6443
JA
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 }
726f6388 105 else if (strncmp (line, "delete", 6) == 0)
b80f6443
JA
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 }
726f6388
JA
124 }
125}