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
46 int hist_purgecmd (char *, int);
51 fprintf (stderr
, "hist_purgecmd: usage: hist_purgecmd [-r] [-t] [-f filename] command-spec\n");
65 while ((r
= getopt (argc
, argv
, "f:rt")) != -1)
73 flags
|= PURGE_REGEXP
;
76 history_write_timestamps
= 1;
86 fn
= getenv ("HISTFILE");
89 fprintf (stderr
, "hist_purgecmd: no history file\n");
93 if ((r
= read_history (fn
)) != 0)
95 fprintf (stderr
, "hist_purgecmd: read_history: %s: %s\n", fn
, strerror (r
));
99 for (r
= 0; r
< argc
; r
++)
100 hist_purgecmd (argv
[r
], flags
);
102 if ((r
= write_history (fn
)) != 0)
104 fprintf (stderr
, "hist_purgecmd: write_history: %s: %s\n", fn
, strerror (r
));
112 hist_purgecmd (cmd
, flags
)
118 regex_t regex
= { 0 };
120 if (flags
& PURGE_REGEXP
)
122 rflags
= REG_EXTENDED
|REG_NOSUB
;
123 if (regcomp (®ex
, cmd
, rflags
))
125 fprintf (stderr
, "hist_purgecmd: %s: invalid regular expression\n", cmd
);
132 r
= where_history ();
133 for (n
= 0; n
< r
; n
++)
135 temp
= history_get (n
+history_base
);
136 if (((flags
& PURGE_REGEXP
) && (regexec (®ex
, temp
->line
, 0, 0, 0) == 0)) ||
137 ((flags
& PURGE_REGEXP
) == 0 && STREQ (temp
->line
, cmd
)))
140 r
--; /* have to get one fewer now */
141 n
--; /* compensate for above increment */
142 history_offset
--; /* moving backwards in history list */
147 if (flags
& PURGE_REGEXP
)
This page took 0.037402 seconds and 4 git commands to generate.