]> git.ipfire.org Git - thirdparty/bash.git/blob - lib/readline/examples/histexamp.c
fa0de1bea3ba5f521bb20e99e03712a71710eb61
[thirdparty/bash.git] / lib / readline / examples / histexamp.c
1 #include <stdio.h>
2
3 #ifdef READLINE_LIBRARY
4 # include "history.h"
5 #else
6 # include <readline/history.h>
7 #endif
8
9 main (argc, argv)
10 int argc;
11 char **argv;
12 {
13 char line[1024], *t;
14 int len, done = 0;
15
16 line[0] = 0;
17
18 using_history ();
19 while (!done)
20 {
21 printf ("history$ ");
22 fflush (stdout);
23 t = fgets (line, sizeof (line) - 1, stdin);
24 if (t && *t)
25 {
26 len = strlen (t);
27 if (t[len - 1] == '\n')
28 t[len - 1] = '\0';
29 }
30
31 if (!t)
32 strcpy (line, "quit");
33
34 if (line[0])
35 {
36 char *expansion;
37 int result;
38
39 using_history ();
40
41 result = history_expand (line, &expansion);
42 if (result)
43 fprintf (stderr, "%s\n", expansion);
44
45 if (result < 0 || result == 2)
46 {
47 free (expansion);
48 continue;
49 }
50
51 add_history (expansion);
52 strncpy (line, expansion, sizeof (line) - 1);
53 free (expansion);
54 }
55
56 if (strcmp (line, "quit") == 0)
57 done = 1;
58 else if (strcmp (line, "save") == 0)
59 write_history ("history_file");
60 else if (strcmp (line, "read") == 0)
61 read_history ("history_file");
62 else if (strcmp (line, "list") == 0)
63 {
64 register HIST_ENTRY **the_list;
65 register int i;
66
67 the_list = history_list ();
68 if (the_list)
69 for (i = 0; the_list[i]; i++)
70 printf ("%d: %s\n", i + history_base, the_list[i]->line);
71 }
72 else if (strncmp (line, "delete", 6) == 0)
73 {
74 int which;
75 if ((sscanf (line + 6, "%d", &which)) == 1)
76 {
77 HIST_ENTRY *entry = remove_history (which);
78 if (!entry)
79 fprintf (stderr, "No such entry %d\n", which);
80 else
81 {
82 free (entry->line);
83 free (entry);
84 }
85 }
86 else
87 {
88 fprintf (stderr, "non-numeric arg given to `delete'\n");
89 }
90 }
91 }
92 }