1 /* hist_purgecmd -- remove all instances of command or pattern from history
4 /* Copyright (C) 2011 Free Software Foundation, Inc.
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.
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.
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.
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/>.
22 #ifndef READLINE_LIBRARY
23 #define READLINE_LIBRARY 1
32 #ifdef READLINE_LIBRARY
35 # include <readline/history.h>
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))
44 #define PURGE_REGEXP 0x01
49 fprintf (stderr
, "hist_purgecmd: usage: hist_purgecmd [-r] [-t] [-f filename] command-spec\n");
63 while ((r
= getopt (argc
, argv
, "f:rt")) != -1)
71 flags
|= PURGE_REGEXP
;
74 history_write_timestamps
= 1;
84 fn
= getenv ("HISTFILE");
87 fprintf (stderr
, "hist_purgecmd: no history file\n");
91 if ((r
= read_history (fn
)) != 0)
93 fprintf (stderr
, "hist_purgecmd: read_history: %s: %s\n", fn
, strerror (r
));
97 for (r
= 0; r
< argc
; r
++)
98 hist_purgecmd (argv
[r
], flags
);
100 if ((r
= write_history (fn
)) != 0)
102 fprintf (stderr
, "hist_purgecmd: write_history: %s: %s\n", fn
, strerror (r
));
110 hist_purgecmd (cmd
, flags
)
116 regex_t regex
= { 0 };
118 if (flags
& PURGE_REGEXP
)
120 rflags
= REG_EXTENDED
|REG_NOSUB
;
121 if (regcomp (®ex
, cmd
, rflags
))
123 fprintf (stderr
, "hist_purgecmd: %s: invalid regular expression\n", cmd
);
130 r
= where_history ();
131 for (n
= 0; n
< r
; n
++)
133 temp
= history_get (n
+history_base
);
134 if (((flags
& PURGE_REGEXP
) && (regexec (®ex
, temp
->line
, 0, 0, 0) == 0)) ||
135 ((flags
& PURGE_REGEXP
) == 0 && STREQ (temp
->line
, cmd
)))
138 r
--; /* have to get one fewer now */
139 n
--; /* compensate for above increment */
140 history_offset
--; /* moving backwards in history list */
145 if (flags
& PURGE_REGEXP
)
This page took 0.031357 seconds and 4 git commands to generate.