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 | ||
cb41b9e7 TT |
30 | #include <unistd.h> |
31 | #include <stdlib.h> | |
5bdf8622 DJ |
32 | #include <string.h> |
33 | ||
cb41b9e7 | 34 | int |
9255ee31 EZ |
35 | main (argc, argv) |
36 | int argc; | |
37 | char **argv; | |
d60d9f65 SS |
38 | { |
39 | char line[1024], *t; | |
5bdf8622 | 40 | int len, done; |
d60d9f65 SS |
41 | |
42 | line[0] = 0; | |
5bdf8622 | 43 | done = 0; |
d60d9f65 SS |
44 | |
45 | using_history (); | |
46 | while (!done) | |
47 | { | |
48 | printf ("history$ "); | |
49 | fflush (stdout); | |
50 | t = fgets (line, sizeof (line) - 1, stdin); | |
51 | if (t && *t) | |
5bdf8622 DJ |
52 | { |
53 | len = strlen (t); | |
54 | if (t[len - 1] == '\n') | |
55 | t[len - 1] = '\0'; | |
56 | } | |
d60d9f65 SS |
57 | |
58 | if (!t) | |
5bdf8622 | 59 | strcpy (line, "quit"); |
d60d9f65 SS |
60 | |
61 | if (line[0]) | |
5bdf8622 DJ |
62 | { |
63 | char *expansion; | |
64 | int result; | |
d60d9f65 | 65 | |
5bdf8622 | 66 | using_history (); |
d60d9f65 | 67 | |
5bdf8622 DJ |
68 | result = history_expand (line, &expansion); |
69 | if (result) | |
70 | fprintf (stderr, "%s\n", expansion); | |
d60d9f65 | 71 | |
5bdf8622 DJ |
72 | if (result < 0 || result == 2) |
73 | { | |
74 | free (expansion); | |
75 | continue; | |
76 | } | |
d60d9f65 | 77 | |
5bdf8622 DJ |
78 | add_history (expansion); |
79 | strncpy (line, expansion, sizeof (line) - 1); | |
80 | free (expansion); | |
81 | } | |
d60d9f65 SS |
82 | |
83 | if (strcmp (line, "quit") == 0) | |
5bdf8622 | 84 | done = 1; |
d60d9f65 | 85 | else if (strcmp (line, "save") == 0) |
5bdf8622 | 86 | write_history ("history_file"); |
d60d9f65 | 87 | else if (strcmp (line, "read") == 0) |
5bdf8622 | 88 | read_history ("history_file"); |
d60d9f65 | 89 | else if (strcmp (line, "list") == 0) |
5bdf8622 DJ |
90 | { |
91 | register HIST_ENTRY **the_list; | |
92 | register int i; | |
93 | time_t tt; | |
94 | char timestr[128]; | |
95 | ||
96 | the_list = history_list (); | |
97 | if (the_list) | |
98 | for (i = 0; the_list[i]; i++) | |
99 | { | |
100 | tt = history_get_time (the_list[i]); | |
101 | if (tt) | |
102 | strftime (timestr, sizeof (timestr), "%a %R", localtime(&tt)); | |
103 | else | |
104 | strcpy (timestr, "??"); | |
105 | printf ("%d: %s: %s\n", i + history_base, timestr, the_list[i]->line); | |
106 | } | |
107 | } | |
d60d9f65 | 108 | else if (strncmp (line, "delete", 6) == 0) |
5bdf8622 DJ |
109 | { |
110 | int which; | |
111 | if ((sscanf (line + 6, "%d", &which)) == 1) | |
112 | { | |
113 | HIST_ENTRY *entry = remove_history (which); | |
114 | if (!entry) | |
115 | fprintf (stderr, "No such entry %d\n", which); | |
116 | else | |
117 | { | |
118 | free (entry->line); | |
119 | free (entry); | |
120 | } | |
121 | } | |
122 | else | |
123 | { | |
124 | fprintf (stderr, "non-numeric arg given to `delete'\n"); | |
125 | } | |
126 | } | |
d60d9f65 SS |
127 | } |
128 | } |