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