Import readline 7.0 (patch 5)
[deliverable/binutils-gdb.git] / readline / examples / hist_purgecmd.c
1 /* hist_purgecmd -- remove all instances of command or pattern from history
2 file */
3
4 /* Copyright (C) 2011 Free Software Foundation, Inc.
5
6 This file is part of the GNU Readline Library (Readline), a library for
7 reading lines of text with interactive input and history editing.
8
9 Readline is free software: you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation, either version 3 of the License, or
12 (at your option) any later version.
13
14 Readline is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with Readline. If not, see <http://www.gnu.org/licenses/>.
21 */
22 #ifndef READLINE_LIBRARY
23 #define READLINE_LIBRARY 1
24 #endif
25
26 #include <unistd.h>
27 #include <stdlib.h>
28 #include <stdio.h>
29
30 #include <regex.h>
31
32 #ifdef READLINE_LIBRARY
33 # include "history.h"
34 #else
35 # include <readline/history.h>
36 #endif
37
38 #include <string.h>
39
40 #define STREQ(a, b) ((a)[0] == (b)[0] && strcmp(a, b) == 0)
41 #define STREQN(a, b, n) ((n == 0) ? (1) \
42 : ((a)[0] == (b)[0] && strncmp(a, b, n) == 0))
43
44 #define PURGE_REGEXP 0x01
45
46 static void
47 usage()
48 {
49 fprintf (stderr, "hist_purgecmd: usage: hist_purgecmd [-r] [-t] [-f filename] command-spec\n");
50 exit (2);
51 }
52
53 int
54 main (argc, argv)
55 int argc;
56 char **argv;
57 {
58 char *fn;
59 int r, flags;
60
61 flags = 0;
62 fn = 0;
63 while ((r = getopt (argc, argv, "f:rt")) != -1)
64 {
65 switch (r)
66 {
67 case 'f':
68 fn = optarg;
69 break;
70 case 'r':
71 flags |= PURGE_REGEXP;
72 break;
73 case 't':
74 history_write_timestamps = 1;
75 break;
76 default:
77 usage ();
78 }
79 }
80 argv += optind;
81 argc -= optind;
82
83 if (fn == 0)
84 fn = getenv ("HISTFILE");
85 if (fn == 0)
86 {
87 fprintf (stderr, "hist_purgecmd: no history file\n");
88 usage ();
89 }
90
91 if ((r = read_history (fn)) != 0)
92 {
93 fprintf (stderr, "hist_purgecmd: read_history: %s: %s\n", fn, strerror (r));
94 exit (1);
95 }
96
97 for (r = 0; r < argc; r++)
98 hist_purgecmd (argv[r], flags);
99
100 if ((r = write_history (fn)) != 0)
101 {
102 fprintf (stderr, "hist_purgecmd: write_history: %s: %s\n", fn, strerror (r));
103 exit (1);
104 }
105
106 exit (0);
107 }
108
109 int
110 hist_purgecmd (cmd, flags)
111 char *cmd;
112 int flags;
113 {
114 int r, n, rflags;
115 HIST_ENTRY *temp;
116 regex_t regex = { 0 };
117
118 if (flags & PURGE_REGEXP)
119 {
120 rflags = REG_EXTENDED|REG_NOSUB;
121 if (regcomp (&regex, cmd, rflags))
122 {
123 fprintf (stderr, "hist_purgecmd: %s: invalid regular expression\n", cmd);
124 return -1;
125 }
126 }
127
128 r = 0;
129 using_history ();
130 r = where_history ();
131 for (n = 0; n < r; n++)
132 {
133 temp = history_get (n+history_base);
134 if (((flags & PURGE_REGEXP) && (regexec (&regex, temp->line, 0, 0, 0) == 0)) ||
135 ((flags & PURGE_REGEXP) == 0 && STREQ (temp->line, cmd)))
136 {
137 remove_history (n);
138 r--; /* have to get one fewer now */
139 n--; /* compensate for above increment */
140 history_offset--; /* moving backwards in history list */
141 }
142 }
143 using_history ();
144
145 if (flags & PURGE_REGEXP)
146 regfree (&regex);
147
148 return r;
149 }
This page took 0.033289 seconds and 5 git commands to generate.