blob: 7912cce311871da3766979ffe60178a4d03ce06d [file] [log] [blame]
Jari Aalto726f6381996-08-26 18:22:31 +00001/* bashhist.c -- bash interface to the GNU history library. */
2
Chet Rameya0c0a002016-09-15 16:59:08 -04003/* Copyright (C) 1993-2015 Free Software Foundation, Inc.
Jari Aalto726f6381996-08-26 18:22:31 +00004
5 This file is part of GNU Bash, the Bourne Again SHell.
6
Jari Aalto31859422009-01-12 13:36:28 +00007 Bash is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
Jari Aalto726f6381996-08-26 18:22:31 +000011
Jari Aalto31859422009-01-12 13:36:28 +000012 Bash is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
Jari Aalto726f6381996-08-26 18:22:31 +000016
Jari Aalto31859422009-01-12 13:36:28 +000017 You should have received a copy of the GNU General Public License
18 along with Bash. If not, see <http://www.gnu.org/licenses/>.
19*/
Jari Aalto726f6381996-08-26 18:22:31 +000020
Jari Aaltoccc6cda1996-12-23 17:02:34 +000021#include "config.h"
22
23#if defined (HISTORY)
24
25#if defined (HAVE_UNISTD_H)
Jari Aaltocce855b1998-04-17 19:52:44 +000026# ifdef _MINIX
Chet Rameyac50fba2014-02-26 09:36:43 -050027 # include <sys/types.h>
Jari Aaltocce855b1998-04-17 19:52:44 +000028# endif
Jari Aaltoccc6cda1996-12-23 17:02:34 +000029# include <unistd.h>
30#endif
31
32#include "bashtypes.h"
Jari Aalto726f6381996-08-26 18:22:31 +000033#include <stdio.h>
34#include <errno.h>
35#include "bashansi.h"
36#include "posixstat.h"
37#include "filecntl.h"
Jari Aaltod166f041997-06-05 14:59:13 +000038
Jari Aaltob80f6442004-07-27 13:29:18 +000039#include "bashintl.h"
40
Chet Ramey00018032011-11-21 20:51:19 -050041#if defined (SYSLOG_HISTORY)
42# include <syslog.h>
43#endif
44
Jari Aalto726f6381996-08-26 18:22:31 +000045#include "shell.h"
46#include "flags.h"
Chet Rameyd233b482019-01-07 09:27:52 -050047#include "parser.h"
Jari Aaltoccc6cda1996-12-23 17:02:34 +000048#include "input.h"
49#include "parser.h" /* for the struct dstack stuff. */
50#include "pathexp.h" /* for the struct ignorevar stuff */
Jari Aaltof73dda02001-11-13 17:56:06 +000051#include "bashhist.h" /* matching prototypes and declarations */
Jari Aaltoccc6cda1996-12-23 17:02:34 +000052#include "builtins/common.h"
Jari Aaltod166f041997-06-05 14:59:13 +000053
Jari Aalto726f6381996-08-26 18:22:31 +000054#include <readline/history.h>
Jari Aaltof73dda02001-11-13 17:56:06 +000055#include <glob/glob.h>
56#include <glob/strmatch.h>
Jari Aaltoccc6cda1996-12-23 17:02:34 +000057
58#if defined (READLINE)
59# include "bashline.h"
Jari Aaltob80f6442004-07-27 13:29:18 +000060extern int rl_done, rl_dispatching; /* should really include readline.h */
Jari Aaltoccc6cda1996-12-23 17:02:34 +000061#endif
62
Chet Rameyd233b482019-01-07 09:27:52 -050063#ifndef HISTSIZE_DEFAULT
64# define HISTSIZE_DEFAULT "500"
65#endif
66
Jari Aaltoccc6cda1996-12-23 17:02:34 +000067#if !defined (errno)
68extern int errno;
69#endif
70
Jari Aaltof73dda02001-11-13 17:56:06 +000071static int histignore_item_func __P((struct ign *));
Jari Aalto7117c2d2002-07-17 14:10:11 +000072static int check_history_control __P((char *));
Jari Aaltob80f6442004-07-27 13:29:18 +000073static void hc_erasedups __P((char *));
Jari Aalto7117c2d2002-07-17 14:10:11 +000074static void really_add_history __P((char *));
Jari Aaltoccc6cda1996-12-23 17:02:34 +000075
76static struct ignorevar histignore =
77{
78 "HISTIGNORE",
79 (struct ign *)0,
80 0,
81 (char *)0,
Jari Aaltof73dda02001-11-13 17:56:06 +000082 (sh_iv_item_func_t *)histignore_item_func,
Jari Aaltoccc6cda1996-12-23 17:02:34 +000083};
84
85#define HIGN_EXPAND 0x01
Jari Aalto726f6381996-08-26 18:22:31 +000086
87/* Declarations of bash history variables. */
88/* Non-zero means to remember lines typed to the shell on the history
89 list. This is different than the user-controlled behaviour; this
90 becomes zero when we read lines from a file, for example. */
Chet Rameya0c0a002016-09-15 16:59:08 -040091int remember_on_history = 0;
92int enable_history_list = 0; /* value for `set -o history' */
Jari Aalto726f6381996-08-26 18:22:31 +000093
Jari Aaltob80f6442004-07-27 13:29:18 +000094/* The number of lines that Bash has added to this history session. The
95 difference between the number of the top element in the history list
96 (offset from history_base) and the number of lines in the history file.
97 Appending this session's history to the history file resets this to 0. */
Jari Aaltoccc6cda1996-12-23 17:02:34 +000098int history_lines_this_session;
Jari Aalto726f6381996-08-26 18:22:31 +000099
100/* The number of lines that Bash has read from the history file. */
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000101int history_lines_in_file;
Jari Aalto726f6381996-08-26 18:22:31 +0000102
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000103#if defined (BANG_HISTORY)
Jari Aalto726f6381996-08-26 18:22:31 +0000104/* Non-zero means do no history expansion on this line, regardless
105 of what history_expansion says. */
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000106int history_expansion_inhibited;
Chet Rameya0c0a002016-09-15 16:59:08 -0400107/* If non-zero, double quotes can quote the history expansion character. */
108int double_quotes_inhibit_history_expansion = 0;
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000109#endif
Jari Aalto726f6381996-08-26 18:22:31 +0000110
Jari Aalto7117c2d2002-07-17 14:10:11 +0000111/* With the old default, every line was saved in the history individually.
112 I.e., if the user enters:
Jari Aalto726f6381996-08-26 18:22:31 +0000113 bash$ for i in a b c
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000114 > do
115 > echo $i
116 > done
Jari Aalto726f6381996-08-26 18:22:31 +0000117 Each line will be individually saved in the history.
118 bash$ history
119 10 for i in a b c
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000120 11 do
121 12 echo $i
122 13 done
123 14 history
Jari Aalto726f6381996-08-26 18:22:31 +0000124 If the variable command_oriented_history is set, multiple lines
125 which form one command will be saved as one history entry.
126 bash$ for i in a b c
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000127 > do
128 > echo $i
129 > done
130 bash$ history
Jari Aalto726f6381996-08-26 18:22:31 +0000131 10 for i in a b c
132 do
133 echo $i
134 done
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000135 11 history
Jari Aalto726f6381996-08-26 18:22:31 +0000136 The user can then recall the whole command all at once instead
137 of just being able to recall one line at a time.
Jari Aalto7117c2d2002-07-17 14:10:11 +0000138
139 This is now enabled by default.
Jari Aalto726f6381996-08-26 18:22:31 +0000140 */
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000141int command_oriented_history = 1;
142
Jari Aalto7117c2d2002-07-17 14:10:11 +0000143/* Set to 1 if the first line of a possibly-multi-line command was saved
144 in the history list. Managed by maybe_add_history(), but global so
145 the history-manipluating builtins can see it. */
146int current_command_first_line_saved = 0;
147
Chet Rameyd233b482019-01-07 09:27:52 -0500148/* Set to the number of the most recent line of a possibly-multi-line command
149 that contains a shell comment. Used by bash_add_history() to determine
150 whether to add a newline or a semicolon. */
151int current_command_line_comment = 0;
152
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000153/* Non-zero means to store newlines in the history list when using
154 command_oriented_history rather than trying to use semicolons. */
155int literal_history;
156
157/* Non-zero means to append the history to the history file at shell
158 exit, even if the history has been stifled. */
159int force_append_history;
Jari Aalto726f6381996-08-26 18:22:31 +0000160
Jari Aaltob80f6442004-07-27 13:29:18 +0000161/* A nit for picking at history saving. Flags have the following values:
162
163 Value == 0 means save all lines parsed by the shell on the history.
164 Value & HC_IGNSPACE means save all lines that do not start with a space.
165 Value & HC_IGNDUPS means save all lines that do not match the last
166 line saved.
167 Value & HC_ERASEDUPS means to remove all other matching lines from the
168 history list before saving the latest line. */
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000169int history_control;
170
Jari Aaltod166f041997-06-05 14:59:13 +0000171/* Set to 1 if the last command was added to the history list successfully
172 as a separate history entry; set to 0 if the line was ignored or added
173 to a previous entry as part of command-oriented-history processing. */
174int hist_last_line_added;
175
Jari Aalto95732b42005-12-07 14:08:12 +0000176/* Set to 1 if builtins/history.def:push_history added the last history
177 entry. */
178int hist_last_line_pushed;
179
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000180#if defined (READLINE)
181/* If non-zero, and readline is being used, the user is offered the
182 chance to re-edit a failed history expansion. */
183int history_reediting;
184
185/* If non-zero, and readline is being used, don't directly execute a
186 line with history substitution. Reload it into the editing buffer
187 instead and let the user further edit and confirm with a newline. */
188int hist_verify;
Jari Aaltod166f041997-06-05 14:59:13 +0000189
190#endif /* READLINE */
Jari Aalto726f6381996-08-26 18:22:31 +0000191
Jari Aaltof73dda02001-11-13 17:56:06 +0000192/* Non-zero means to not save function definitions in the history list. */
193int dont_save_function_defs;
194
Chet Rameya0c0a002016-09-15 16:59:08 -0400195#if defined (BANG_HISTORY)
Jari Aaltof73dda02001-11-13 17:56:06 +0000196static int bash_history_inhibit_expansion __P((char *, int));
Chet Rameya0c0a002016-09-15 16:59:08 -0400197#endif
Jari Aaltof73dda02001-11-13 17:56:06 +0000198#if defined (READLINE)
199static void re_edit __P((char *));
200#endif
201static int history_expansion_p __P((char *));
202static int shell_comment __P((char *));
203static int should_expand __P((char *));
204static HIST_ENTRY *last_history_entry __P((void));
205static char *expand_histignore_pattern __P((char *));
206static int history_should_ignore __P((char *));
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000207
Chet Rameya0c0a002016-09-15 16:59:08 -0400208#if defined (BANG_HISTORY)
Jari Aaltod166f041997-06-05 14:59:13 +0000209/* Is the history expansion starting at string[i] one that should not
210 be expanded? */
211static int
212bash_history_inhibit_expansion (string, i)
213 char *string;
214 int i;
215{
Chet Rameyd233b482019-01-07 09:27:52 -0500216 int t, si;
Chet Rameya0c0a002016-09-15 16:59:08 -0400217 char hx[2];
218
219 hx[0] = history_expansion_char;
220 hx[1] = '\0';
221
Jari Aaltod166f041997-06-05 14:59:13 +0000222 /* The shell uses ! as a pattern negation character in globbing [...]
223 expressions, so let those pass without expansion. */
224 if (i > 0 && (string[i - 1] == '[') && member (']', string + i + 1))
225 return (1);
226 /* The shell uses ! as the indirect expansion character, so let those
227 expansions pass as well. */
228 else if (i > 1 && string[i - 1] == '{' && string[i - 2] == '$' &&
229 member ('}', string + i + 1))
230 return (1);
Chet Ramey495aee42011-11-22 19:11:26 -0500231 /* The shell uses $! as a defined parameter expansion. */
232 else if (i > 1 && string[i - 1] == '$' && string[i] == '!')
233 return (1);
Jari Aaltocce855b1998-04-17 19:52:44 +0000234#if defined (EXTENDED_GLOB)
235 else if (extended_glob && i > 1 && string[i+1] == '(' && member (')', string + i + 2))
236 return (1);
237#endif
Chet Rameya0c0a002016-09-15 16:59:08 -0400238
Chet Rameyd233b482019-01-07 09:27:52 -0500239 si = 0;
240 /* If we're supposed to be in single-quoted string, skip over the
241 single-quoted part and then look at what's left. */
242 if (history_quoting_state == '\'')
243 {
244 si = skip_to_delim (string, 0, "'", SD_NOJMP|SD_HISTEXP);
245 if (string[si] == 0 || si >= i)
246 return (1);
247 si++;
248 }
249
Chet Rameya0c0a002016-09-15 16:59:08 -0400250 /* Make sure the history expansion should not be skipped by quoting or
251 command/process substitution. */
Chet Rameyd233b482019-01-07 09:27:52 -0500252 if ((t = skip_to_histexp (string, si, hx, SD_NOJMP|SD_HISTEXP)) > 0)
Chet Rameya0c0a002016-09-15 16:59:08 -0400253 {
254 /* Skip instances of history expansion appearing on the line before
255 this one. */
256 while (t < i)
257 {
258 t = skip_to_histexp (string, t+1, hx, SD_NOJMP|SD_HISTEXP);
259 if (t <= 0)
260 return 0;
261 }
262 return (t > i);
263 }
Jari Aaltod166f041997-06-05 14:59:13 +0000264 else
265 return (0);
266}
Chet Rameya0c0a002016-09-15 16:59:08 -0400267#endif
Jari Aaltod166f041997-06-05 14:59:13 +0000268
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000269void
270bash_initialize_history ()
271{
272 history_quotes_inhibit_expansion = 1;
273 history_search_delimiter_chars = ";&()|<>";
Jari Aalto95732b42005-12-07 14:08:12 +0000274#if defined (BANG_HISTORY)
Chet Rameya0c0a002016-09-15 16:59:08 -0400275 history_inhibit_expansion_function = bash_history_inhibit_expansion;
Jari Aaltob80f6442004-07-27 13:29:18 +0000276 sv_histchars ("histchars");
Jari Aalto95732b42005-12-07 14:08:12 +0000277#endif
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000278}
279
280void
281bash_history_reinit (interact)
282 int interact;
283{
284#if defined (BANG_HISTORY)
Chet Rameyd233b482019-01-07 09:27:52 -0500285 history_expansion = (interact == 0) ? histexp_flag : HISTEXPAND_DEFAULT;
286 history_expansion_inhibited = (interact == 0) ? 1 - histexp_flag : 0; /* changed in bash_history_enable() */
Jari Aaltod166f041997-06-05 14:59:13 +0000287 history_inhibit_expansion_function = bash_history_inhibit_expansion;
Chet Rameya0c0a002016-09-15 16:59:08 -0400288#endif
289 remember_on_history = enable_history_list;
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000290}
291
292void
293bash_history_disable ()
294{
295 remember_on_history = 0;
296#if defined (BANG_HISTORY)
297 history_expansion_inhibited = 1;
298#endif
299}
300
301void
302bash_history_enable ()
303{
Chet Rameya0c0a002016-09-15 16:59:08 -0400304 remember_on_history = enable_history_list = 1;
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000305#if defined (BANG_HISTORY)
306 history_expansion_inhibited = 0;
Jari Aaltod166f041997-06-05 14:59:13 +0000307 history_inhibit_expansion_function = bash_history_inhibit_expansion;
Chet Rameya0c0a002016-09-15 16:59:08 -0400308#endif
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000309 sv_history_control ("HISTCONTROL");
310 sv_histignore ("HISTIGNORE");
311}
Jari Aalto726f6381996-08-26 18:22:31 +0000312
313/* Load the history list from the history file. */
314void
315load_history ()
316{
317 char *hf;
318
319 /* Truncate history file for interactive shells which desire it.
320 Note that the history file is automatically truncated to the
321 size of HISTSIZE if the user does not explicitly set the size
322 differently. */
Chet Rameyd233b482019-01-07 09:27:52 -0500323 set_if_not ("HISTSIZE", HISTSIZE_DEFAULT);
Jari Aalto06285672006-10-10 14:15:34 +0000324 sv_histsize ("HISTSIZE");
325
Jari Aalto726f6381996-08-26 18:22:31 +0000326 set_if_not ("HISTFILESIZE", get_string_value ("HISTSIZE"));
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000327 sv_histsize ("HISTFILESIZE");
Jari Aalto726f6381996-08-26 18:22:31 +0000328
329 /* Read the history in HISTFILE into the history list. */
330 hf = get_string_value ("HISTFILE");
331
Jari Aalto31859422009-01-12 13:36:28 +0000332 if (hf && *hf && file_exists (hf))
Jari Aalto726f6381996-08-26 18:22:31 +0000333 {
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000334 read_history (hf);
Chet Rameya0c0a002016-09-15 16:59:08 -0400335 /* We have read all of the lines from the history file, even if we
336 read more lines than $HISTSIZE. Remember the total number of lines
337 we read so we don't count the last N lines as new over and over
338 again. */
339 history_lines_in_file = history_lines_read_from_file;
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000340 using_history ();
Chet Rameya0c0a002016-09-15 16:59:08 -0400341 /* history_lines_in_file = where_history () + history_base - 1; */
Jari Aalto726f6381996-08-26 18:22:31 +0000342 }
343}
344
Jari Aalto31859422009-01-12 13:36:28 +0000345void
346bash_clear_history ()
347{
348 clear_history ();
349 history_lines_this_session = 0;
Chet Rameya0c0a002016-09-15 16:59:08 -0400350 /* XXX - reset history_lines_read_from_file? */
Jari Aalto31859422009-01-12 13:36:28 +0000351}
352
353/* Delete and free the history list entry at offset I. */
354int
355bash_delete_histent (i)
356 int i;
357{
358 HIST_ENTRY *discard;
359
360 discard = remove_history (i);
361 if (discard)
362 free_history_entry (discard);
363 history_lines_this_session--;
364
365 return 1;
366}
367
368int
Chet Rameyd233b482019-01-07 09:27:52 -0500369bash_delete_history_range (first, last)
370 int first, last;
371{
372 register int i;
373 HIST_ENTRY **discard_list;
374
375 discard_list = remove_history_range (first, last);
376 for (i = 0; discard_list && discard_list[i]; i++)
377 free_history_entry (discard_list[i]);
378 history_lines_this_session -= i;
379
380 return 1;
381}
382
383int
Jari Aalto31859422009-01-12 13:36:28 +0000384bash_delete_last_history ()
385{
386 register int i;
387 HIST_ENTRY **hlist, *histent;
388 int r;
389
390 hlist = history_list ();
391 if (hlist == NULL)
392 return 0;
393
394 for (i = 0; hlist[i]; i++)
395 ;
396 i--;
397
398 /* History_get () takes a parameter that must be offset by history_base. */
399 histent = history_get (history_base + i); /* Don't free this */
400 if (histent == NULL)
401 return 0;
402
403 r = bash_delete_histent (i);
404
405 if (where_history () > history_length)
406 history_set_pos (history_length);
407
408 return r;
409}
410
Jari Aaltod166f041997-06-05 14:59:13 +0000411#ifdef INCLUDE_UNUSED
Jari Aalto726f6381996-08-26 18:22:31 +0000412/* Write the existing history out to the history file. */
413void
414save_history ()
415{
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000416 char *hf;
Chet Rameyac50fba2014-02-26 09:36:43 -0500417 int r;
Jari Aalto726f6381996-08-26 18:22:31 +0000418
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000419 hf = get_string_value ("HISTFILE");
Jari Aalto31859422009-01-12 13:36:28 +0000420 if (hf && *hf && file_exists (hf))
Jari Aalto726f6381996-08-26 18:22:31 +0000421 {
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000422 /* Append only the lines that occurred this session to
423 the history file. */
424 using_history ();
Jari Aalto726f6381996-08-26 18:22:31 +0000425
Chet Ramey495aee42011-11-22 19:11:26 -0500426 if (history_lines_this_session <= where_history () || force_append_history)
Chet Rameyac50fba2014-02-26 09:36:43 -0500427 r = append_history (history_lines_this_session, hf);
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000428 else
Chet Rameyac50fba2014-02-26 09:36:43 -0500429 r = write_history (hf);
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000430 sv_histsize ("HISTFILESIZE");
Jari Aalto726f6381996-08-26 18:22:31 +0000431 }
432}
Jari Aaltod166f041997-06-05 14:59:13 +0000433#endif
Jari Aalto726f6381996-08-26 18:22:31 +0000434
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000435int
436maybe_append_history (filename)
437 char *filename;
438{
439 int fd, result;
440 struct stat buf;
441
442 result = EXECUTION_SUCCESS;
Chet Rameya0c0a002016-09-15 16:59:08 -0400443 if (history_lines_this_session > 0 && (history_lines_this_session <= where_history ()))
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000444 {
445 /* If the filename was supplied, then create it if necessary. */
446 if (stat (filename, &buf) == -1 && errno == ENOENT)
447 {
Jari Aaltob72432f1999-02-19 17:11:39 +0000448 fd = open (filename, O_WRONLY|O_CREAT, 0600);
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000449 if (fd < 0)
450 {
Jari Aaltob80f6442004-07-27 13:29:18 +0000451 builtin_error (_("%s: cannot create: %s"), filename, strerror (errno));
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000452 return (EXECUTION_FAILURE);
453 }
454 close (fd);
455 }
456 result = append_history (history_lines_this_session, filename);
Chet Rameya0c0a002016-09-15 16:59:08 -0400457 /* Pretend we already read these lines from the file because we just
458 added them */
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000459 history_lines_in_file += history_lines_this_session;
460 history_lines_this_session = 0;
461 }
Chet Rameya0c0a002016-09-15 16:59:08 -0400462 else
463 history_lines_this_session = 0; /* reset if > where_history() */
464
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000465 return (result);
466}
467
Jari Aalto726f6381996-08-26 18:22:31 +0000468/* If this is an interactive shell, then append the lines executed
469 this session to the history file. */
470int
471maybe_save_shell_history ()
472{
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000473 int result;
474 char *hf;
Jari Aalto726f6381996-08-26 18:22:31 +0000475
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000476 result = 0;
Chet Rameya0c0a002016-09-15 16:59:08 -0400477 if (history_lines_this_session > 0)
Jari Aalto726f6381996-08-26 18:22:31 +0000478 {
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000479 hf = get_string_value ("HISTFILE");
Jari Aalto726f6381996-08-26 18:22:31 +0000480
481 if (hf && *hf)
482 {
Jari Aalto726f6381996-08-26 18:22:31 +0000483 /* If the file doesn't exist, then create it. */
Jari Aalto31859422009-01-12 13:36:28 +0000484 if (file_exists (hf) == 0)
Jari Aalto726f6381996-08-26 18:22:31 +0000485 {
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000486 int file;
Jari Aaltob72432f1999-02-19 17:11:39 +0000487 file = open (hf, O_CREAT | O_TRUNC | O_WRONLY, 0600);
Jari Aalto726f6381996-08-26 18:22:31 +0000488 if (file != -1)
489 close (file);
490 }
491
492 /* Now actually append the lines if the history hasn't been
493 stifled. If the history has been stifled, rewrite the
494 history file. */
495 using_history ();
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000496 if (history_lines_this_session <= where_history () || force_append_history)
Jari Aalto726f6381996-08-26 18:22:31 +0000497 {
498 result = append_history (history_lines_this_session, hf);
499 history_lines_in_file += history_lines_this_session;
500 }
501 else
502 {
503 result = write_history (hf);
Chet Rameya0c0a002016-09-15 16:59:08 -0400504 history_lines_in_file = history_lines_written_to_file;
505 /* history_lines_in_file = where_history () + history_base - 1; */
Jari Aalto726f6381996-08-26 18:22:31 +0000506 }
507 history_lines_this_session = 0;
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000508
509 sv_histsize ("HISTFILESIZE");
Jari Aalto726f6381996-08-26 18:22:31 +0000510 }
511 }
512 return (result);
513}
514
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000515#if defined (READLINE)
Jari Aalto726f6381996-08-26 18:22:31 +0000516/* Tell readline () that we have some text for it to edit. */
517static void
518re_edit (text)
519 char *text;
520{
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000521 if (bash_input.type == st_stdin)
Jari Aalto726f6381996-08-26 18:22:31 +0000522 bash_re_edit (text);
Jari Aalto726f6381996-08-26 18:22:31 +0000523}
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000524#endif /* READLINE */
525
526/* Return 1 if this line needs history expansion. */
527static int
528history_expansion_p (line)
529 char *line;
530{
531 register char *s;
532
533 for (s = line; *s; s++)
534 if (*s == history_expansion_char || *s == history_subst_char)
535 return 1;
536 return 0;
537}
Jari Aalto726f6381996-08-26 18:22:31 +0000538
539/* Do pre-processing on LINE. If PRINT_CHANGES is non-zero, then
540 print the results of expanding the line if there were any changes.
541 If there is an error, return NULL, otherwise the expanded line is
542 returned. If ADDIT is non-zero the line is added to the history
543 list after history expansion. ADDIT is just a suggestion;
544 REMEMBER_ON_HISTORY can veto, and does.
545 Right now this does history expansion. */
546char *
547pre_process_line (line, print_changes, addit)
548 char *line;
549 int print_changes, addit;
550{
551 char *history_value;
552 char *return_value;
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000553 int expanded;
Jari Aalto726f6381996-08-26 18:22:31 +0000554
555 return_value = line;
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000556 expanded = 0;
Jari Aalto726f6381996-08-26 18:22:31 +0000557
558# if defined (BANG_HISTORY)
559 /* History expand the line. If this results in no errors, then
560 add that line to the history if ADDIT is non-zero. */
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000561 if (!history_expansion_inhibited && history_expansion && history_expansion_p (line))
Jari Aalto726f6381996-08-26 18:22:31 +0000562 {
Chet Rameyd233b482019-01-07 09:27:52 -0500563 /* If we are expanding the second or later line of a multi-line
564 command, decrease history_length so references to history expansions
565 in these lines refer to the previous history entry and not the
566 current command. */
567 if (history_length > 0 && command_oriented_history && current_command_first_line_saved && current_command_line_count > 1)
568 history_length--;
Jari Aalto726f6381996-08-26 18:22:31 +0000569 expanded = history_expand (line, &history_value);
Chet Rameyd233b482019-01-07 09:27:52 -0500570 if (history_length >= 0 && command_oriented_history && current_command_first_line_saved && current_command_line_count > 1)
571 history_length++;
Jari Aalto726f6381996-08-26 18:22:31 +0000572
573 if (expanded)
574 {
575 if (print_changes)
576 {
577 if (expanded < 0)
Jari Aaltocce855b1998-04-17 19:52:44 +0000578 internal_error ("%s", history_value);
Jari Aaltod166f041997-06-05 14:59:13 +0000579#if defined (READLINE)
Jari Aaltobb706242000-03-17 21:46:59 +0000580 else if (hist_verify == 0 || expanded == 2)
Jari Aaltod166f041997-06-05 14:59:13 +0000581#else
582 else
583#endif
Jari Aalto726f6381996-08-26 18:22:31 +0000584 fprintf (stderr, "%s\n", history_value);
585 }
586
587 /* If there was an error, return NULL. */
588 if (expanded < 0 || expanded == 2) /* 2 == print only */
589 {
Jari Aaltob80f6442004-07-27 13:29:18 +0000590# if defined (READLINE)
591 if (expanded == 2 && rl_dispatching == 0 && *history_value)
592# else
593 if (expanded == 2 && *history_value)
594# endif /* !READLINE */
595 maybe_add_history (history_value);
596
Jari Aalto726f6381996-08-26 18:22:31 +0000597 free (history_value);
598
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000599# if defined (READLINE)
Jari Aalto726f6381996-08-26 18:22:31 +0000600 /* New hack. We can allow the user to edit the
601 failed history expansion. */
Jari Aaltob80f6442004-07-27 13:29:18 +0000602 if (history_reediting && expanded < 0 && rl_done)
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000603 re_edit (line);
604# endif /* READLINE */
Jari Aalto726f6381996-08-26 18:22:31 +0000605 return ((char *)NULL);
606 }
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000607
608# if defined (READLINE)
609 if (hist_verify && expanded == 1)
610 {
611 re_edit (history_value);
Chet Rameya0c0a002016-09-15 16:59:08 -0400612 free (history_value);
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000613 return ((char *)NULL);
614 }
615# endif
Jari Aalto726f6381996-08-26 18:22:31 +0000616 }
617
618 /* Let other expansions know that return_value can be free'ed,
619 and that a line has been added to the history list. Note
620 that we only add lines that have something in them. */
621 expanded = 1;
622 return_value = history_value;
623 }
624# endif /* BANG_HISTORY */
625
626 if (addit && remember_on_history && *return_value)
627 maybe_add_history (return_value);
628
Jari Aaltod166f041997-06-05 14:59:13 +0000629#if 0
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000630 if (expanded == 0)
Jari Aalto726f6381996-08-26 18:22:31 +0000631 return_value = savestring (line);
Jari Aaltod166f041997-06-05 14:59:13 +0000632#endif
Jari Aalto726f6381996-08-26 18:22:31 +0000633
634 return (return_value);
635}
636
Jari Aaltobb706242000-03-17 21:46:59 +0000637/* Return 1 if the first non-whitespace character in LINE is a `#', indicating
Chet Rameyd233b482019-01-07 09:27:52 -0500638 that the line is a shell comment. Return 2 if there is a comment after the
639 first non-whitespace character. Return 0 if the line does not contain a
640 comment. */
Jari Aaltobb706242000-03-17 21:46:59 +0000641static int
642shell_comment (line)
643 char *line;
644{
645 char *p;
Chet Rameyd233b482019-01-07 09:27:52 -0500646 int n;
Jari Aaltobb706242000-03-17 21:46:59 +0000647
Chet Rameyd233b482019-01-07 09:27:52 -0500648 if (line == 0)
649 return 0;
Jari Aaltobb706242000-03-17 21:46:59 +0000650 for (p = line; p && *p && whitespace (*p); p++)
651 ;
Chet Rameyd233b482019-01-07 09:27:52 -0500652 if (p && *p == '#')
653 return 1;
654 n = skip_to_delim (line, p - line, "#", SD_NOJMP|SD_GLOB|SD_EXTGLOB|SD_COMPLETE);
655 return (line[n] == '#') ? 2 : 0;
Jari Aaltobb706242000-03-17 21:46:59 +0000656}
657
Jari Aaltof73dda02001-11-13 17:56:06 +0000658#ifdef INCLUDE_UNUSED
Jari Aaltobb706242000-03-17 21:46:59 +0000659/* Remove shell comments from LINE. A `#' and anything after it is a comment.
660 This isn't really useful yet, since it doesn't handle quoting. */
661static char *
662filter_comments (line)
663 char *line;
664{
665 char *p;
666
667 for (p = line; p && *p && *p != '#'; p++)
668 ;
669 if (p && *p == '#')
670 *p = '\0';
671 return (line);
672}
Jari Aaltof73dda02001-11-13 17:56:06 +0000673#endif
Jari Aaltobb706242000-03-17 21:46:59 +0000674
Jari Aalto7117c2d2002-07-17 14:10:11 +0000675/* Check LINE against what HISTCONTROL says to do. Returns 1 if the line
676 should be saved; 0 if it should be discarded. */
677static int
678check_history_control (line)
679 char *line;
680{
681 HIST_ENTRY *temp;
682 int r;
683
Jari Aaltob80f6442004-07-27 13:29:18 +0000684 if (history_control == 0)
685 return 1;
686
687 /* ignorespace or ignoreboth */
688 if ((history_control & HC_IGNSPACE) && *line == ' ')
689 return 0;
690
691 /* ignoredups or ignoreboth */
692 if (history_control & HC_IGNDUPS)
Jari Aalto7117c2d2002-07-17 14:10:11 +0000693 {
Jari Aalto7117c2d2002-07-17 14:10:11 +0000694 using_history ();
695 temp = previous_history ();
696
697 r = (temp == 0 || STREQ (temp->line, line) == 0);
698
699 using_history ();
Jari Aaltob80f6442004-07-27 13:29:18 +0000700
701 if (r == 0)
702 return r;
Jari Aalto7117c2d2002-07-17 14:10:11 +0000703 }
704
Jari Aaltob80f6442004-07-27 13:29:18 +0000705 return 1;
706}
707
708/* Remove all entries matching LINE from the history list. Triggered when
709 HISTCONTROL includes `erasedups'. */
710static void
711hc_erasedups (line)
712 char *line;
713{
714 HIST_ENTRY *temp;
715 int r;
716
717 using_history ();
718 while (temp = previous_history ())
719 {
720 if (STREQ (temp->line, line))
721 {
722 r = where_history ();
Chet Rameya0c0a002016-09-15 16:59:08 -0400723 temp = remove_history (r);
724 if (temp)
725 free_history_entry (temp);
Jari Aaltob80f6442004-07-27 13:29:18 +0000726 }
727 }
728 using_history ();
Jari Aalto7117c2d2002-07-17 14:10:11 +0000729}
730
731/* Add LINE to the history list, handling possibly multi-line compound
732 commands. We note whether or not we save the first line of each command
733 (which is usually the entire command and history entry), and don't add
734 the second and subsequent lines of a multi-line compound command if we
735 didn't save the first line. We don't usually save shell comment lines in
736 compound commands in the history, because they could have the effect of
737 commenting out the rest of the command when the entire command is saved as
738 a single history entry (when COMMAND_ORIENTED_HISTORY is enabled). If
739 LITERAL_HISTORY is set, we're saving lines in the history with embedded
Chet Rameyac50fba2014-02-26 09:36:43 -0500740 newlines, so it's OK to save comment lines. If we're collecting the body
741 of a here-document, we should act as if literal_history is enabled, because
742 we want to save the entire contents of the here-document as it was
743 entered. We also make sure to save multiple-line quoted strings or other
744 constructs. */
Jari Aalto726f6381996-08-26 18:22:31 +0000745void
746maybe_add_history (line)
747 char *line;
748{
Chet Rameyd233b482019-01-07 09:27:52 -0500749 int is_comment;
750
Jari Aalto28ef6c32001-04-06 19:14:31 +0000751 hist_last_line_added = 0;
Chet Rameyd233b482019-01-07 09:27:52 -0500752 is_comment = shell_comment (line);
Jari Aalto726f6381996-08-26 18:22:31 +0000753
754 /* Don't use the value of history_control to affect the second
Jari Aaltocce855b1998-04-17 19:52:44 +0000755 and subsequent lines of a multi-line command (old code did
756 this only when command_oriented_history is enabled). */
Jari Aaltocce855b1998-04-17 19:52:44 +0000757 if (current_command_line_count > 1)
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000758 {
Jari Aalto7117c2d2002-07-17 14:10:11 +0000759 if (current_command_first_line_saved &&
Chet Rameyd233b482019-01-07 09:27:52 -0500760 ((parser_state & PST_HEREDOC) || literal_history || dstack.delimiter_depth != 0 || is_comment != 1))
Jari Aaltobb706242000-03-17 21:46:59 +0000761 bash_add_history (line);
Chet Rameyd233b482019-01-07 09:27:52 -0500762 current_command_line_comment = is_comment ? current_command_line_count : -2;
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000763 return;
764 }
Jari Aalto726f6381996-08-26 18:22:31 +0000765
Jari Aalto28ef6c32001-04-06 19:14:31 +0000766 /* This is the first line of a (possible multi-line) command. Note whether
767 or not we should save the first line and remember it. */
Chet Rameyd233b482019-01-07 09:27:52 -0500768 current_command_line_comment = is_comment ? current_command_line_count : -2;
Jari Aalto7117c2d2002-07-17 14:10:11 +0000769 current_command_first_line_saved = check_add_history (line, 0);
770}
Jari Aalto28ef6c32001-04-06 19:14:31 +0000771
Jari Aalto7117c2d2002-07-17 14:10:11 +0000772/* Just check LINE against HISTCONTROL and HISTIGNORE and add it to the
773 history if it's OK. Used by `history -s' as well as maybe_add_history().
774 Returns 1 if the line was saved in the history, 0 otherwise. */
775int
776check_add_history (line, force)
777 char *line;
778 int force;
779{
780 if (check_history_control (line) && history_should_ignore (line) == 0)
Jari Aalto726f6381996-08-26 18:22:31 +0000781 {
Jari Aaltob80f6442004-07-27 13:29:18 +0000782 /* We're committed to saving the line. If the user has requested it,
783 remove other matching lines from the history. */
784 if (history_control & HC_ERASEDUPS)
785 hc_erasedups (line);
786
Jari Aalto7117c2d2002-07-17 14:10:11 +0000787 if (force)
788 {
789 really_add_history (line);
790 using_history ();
791 }
792 else
793 bash_add_history (line);
794 return 1;
Jari Aalto726f6381996-08-26 18:22:31 +0000795 }
Jari Aalto7117c2d2002-07-17 14:10:11 +0000796 return 0;
Jari Aalto726f6381996-08-26 18:22:31 +0000797}
798
Chet Ramey00018032011-11-21 20:51:19 -0500799#if defined (SYSLOG_HISTORY)
800#define SYSLOG_MAXLEN 600
801
Chet Rameya0c0a002016-09-15 16:59:08 -0400802#ifndef OPENLOG_OPTS
803#define OPENLOG_OPTS 0
804#endif
805
Chet Rameyd233b482019-01-07 09:27:52 -0500806#if defined (SYSLOG_SHOPT)
807int syslog_history = SYSLOG_SHOPT;
808#else
809int syslog_history = 1;
810#endif
811
Chet Ramey00018032011-11-21 20:51:19 -0500812void
813bash_syslog_history (line)
814 const char *line;
815{
816 char trunc[SYSLOG_MAXLEN];
Chet Rameya0c0a002016-09-15 16:59:08 -0400817 static int first = 1;
818
819 if (first)
820 {
821 openlog (shell_name, OPENLOG_OPTS, SYSLOG_FACILITY);
822 first = 0;
823 }
Chet Ramey00018032011-11-21 20:51:19 -0500824
825 if (strlen(line) < SYSLOG_MAXLEN)
826 syslog (SYSLOG_FACILITY|SYSLOG_LEVEL, "HISTORY: PID=%d UID=%d %s", getpid(), current_user.uid, line);
827 else
828 {
829 strncpy (trunc, line, SYSLOG_MAXLEN);
830 trunc[SYSLOG_MAXLEN - 1] = '\0';
831 syslog (SYSLOG_FACILITY|SYSLOG_LEVEL, "HISTORY (TRUNCATED): PID=%d UID=%d %s", getpid(), current_user.uid, trunc);
832 }
833}
834#endif
835
Jari Aalto726f6381996-08-26 18:22:31 +0000836/* Add a line to the history list.
837 The variable COMMAND_ORIENTED_HISTORY controls the style of history
838 remembering; when non-zero, and LINE is not the first line of a
839 complete parser construct, append LINE to the last history line instead
840 of adding it as a new line. */
Jari Aaltod166f041997-06-05 14:59:13 +0000841void
Jari Aalto726f6381996-08-26 18:22:31 +0000842bash_add_history (line)
843 char *line;
844{
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000845 int add_it, offset, curlen;
846 HIST_ENTRY *current, *old;
847 char *chars_to_add, *new_line;
Jari Aalto726f6381996-08-26 18:22:31 +0000848
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000849 add_it = 1;
Jari Aalto726f6381996-08-26 18:22:31 +0000850 if (command_oriented_history && current_command_line_count > 1)
851 {
Chet Rameyac50fba2014-02-26 09:36:43 -0500852 /* The second and subsequent lines of a here document have the trailing
853 newline preserved. We don't want to add extra newlines here, but we
854 do want to add one after the first line (which is the command that
855 contains the here-doc specifier). parse.y:history_delimiting_chars()
856 does the right thing to take care of this for us. We don't want to
857 add extra newlines if the user chooses to enable literal_history,
858 so we have to duplicate some of what that function does here. */
859 if ((parser_state & PST_HEREDOC) && literal_history && current_command_line_count > 2 && line[strlen (line) - 1] == '\n')
860 chars_to_add = "";
Chet Rameyd233b482019-01-07 09:27:52 -0500861 else if (current_command_line_count == current_command_line_comment+1)
862 chars_to_add = "\n";
863 else if (literal_history)
864 chars_to_add = "\n";
Chet Rameyac50fba2014-02-26 09:36:43 -0500865 else
Chet Rameyd233b482019-01-07 09:27:52 -0500866 chars_to_add = history_delimiting_chars (line);
Jari Aalto726f6381996-08-26 18:22:31 +0000867
868 using_history ();
Jari Aalto726f6381996-08-26 18:22:31 +0000869 current = previous_history ();
870
Chet Rameyd233b482019-01-07 09:27:52 -0500871 current_command_line_comment = shell_comment (line) ? current_command_line_count : -2;
872
Jari Aalto726f6381996-08-26 18:22:31 +0000873 if (current)
874 {
875 /* If the previous line ended with an escaped newline (escaped
876 with backslash, but otherwise unquoted), then remove the quoted
877 newline, since that is what happens when the line is parsed. */
Jari Aalto726f6381996-08-26 18:22:31 +0000878 curlen = strlen (current->line);
879
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000880 if (dstack.delimiter_depth == 0 && current->line[curlen - 1] == '\\' &&
Jari Aalto726f6381996-08-26 18:22:31 +0000881 current->line[curlen - 2] != '\\')
882 {
883 current->line[curlen - 1] = '\0';
884 curlen--;
885 chars_to_add = "";
886 }
887
Chet Ramey495aee42011-11-22 19:11:26 -0500888 /* If we're not in some kind of quoted construct, the current history
889 entry ends with a newline, and we're going to add a semicolon,
890 don't. In some cases, it results in a syntax error (e.g., before
891 a close brace), and it should not be needed. */
892 if (dstack.delimiter_depth == 0 && current->line[curlen - 1] == '\n' && *chars_to_add == ';')
893 chars_to_add++;
894
Jari Aaltof73dda02001-11-13 17:56:06 +0000895 new_line = (char *)xmalloc (1
896 + curlen
897 + strlen (line)
898 + strlen (chars_to_add));
Jari Aalto726f6381996-08-26 18:22:31 +0000899 sprintf (new_line, "%s%s%s", current->line, chars_to_add, line);
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000900 offset = where_history ();
Jari Aalto726f6381996-08-26 18:22:31 +0000901 old = replace_history_entry (offset, new_line, current->data);
902 free (new_line);
903
904 if (old)
Jari Aaltob80f6442004-07-27 13:29:18 +0000905 free_history_entry (old);
906
Jari Aalto726f6381996-08-26 18:22:31 +0000907 add_it = 0;
908 }
909 }
910
911 if (add_it)
Jari Aalto7117c2d2002-07-17 14:10:11 +0000912 really_add_history (line);
913
Chet Ramey00018032011-11-21 20:51:19 -0500914#if defined (SYSLOG_HISTORY)
Chet Rameyd233b482019-01-07 09:27:52 -0500915 if (syslog_history)
916 bash_syslog_history (line);
Chet Ramey00018032011-11-21 20:51:19 -0500917#endif
918
Jari Aalto726f6381996-08-26 18:22:31 +0000919 using_history ();
920}
921
Jari Aalto7117c2d2002-07-17 14:10:11 +0000922static void
923really_add_history (line)
924 char *line;
925{
926 hist_last_line_added = 1;
Jari Aalto95732b42005-12-07 14:08:12 +0000927 hist_last_line_pushed = 0;
Jari Aalto7117c2d2002-07-17 14:10:11 +0000928 add_history (line);
929 history_lines_this_session++;
930}
931
Jari Aalto726f6381996-08-26 18:22:31 +0000932int
933history_number ()
934{
935 using_history ();
Jari Aalto95732b42005-12-07 14:08:12 +0000936 return (remember_on_history ? history_base + where_history () : 1);
Jari Aalto726f6381996-08-26 18:22:31 +0000937}
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000938
939static int
940should_expand (s)
941 char *s;
942{
943 char *p;
944
945 for (p = s; p && *p; p++)
946 {
947 if (*p == '\\')
948 p++;
949 else if (*p == '&')
950 return 1;
951 }
952 return 0;
953}
954
955static int
956histignore_item_func (ign)
957 struct ign *ign;
958{
959 if (should_expand (ign->val))
960 ign->flags |= HIGN_EXPAND;
961 return (0);
962}
963
964void
965setup_history_ignore (varname)
966 char *varname;
967{
968 setup_ignore_patterns (&histignore);
969}
970
971static HIST_ENTRY *
972last_history_entry ()
973{
974 HIST_ENTRY *he;
975
976 using_history ();
977 he = previous_history ();
978 using_history ();
979 return he;
980}
981
982char *
983last_history_line ()
984{
985 HIST_ENTRY *he;
986
987 he = last_history_entry ();
988 if (he == 0)
989 return ((char *)NULL);
990 return he->line;
991}
992
993static char *
994expand_histignore_pattern (pat)
995 char *pat;
996{
997 HIST_ENTRY *phe;
Jari Aaltobb706242000-03-17 21:46:59 +0000998 char *ret;
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000999
1000 phe = last_history_entry ();
1001
1002 if (phe == (HIST_ENTRY *)0)
1003 return (savestring (pat));
1004
Jari Aaltobb706242000-03-17 21:46:59 +00001005 ret = strcreplace (pat, '&', phe->line, 1);
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001006
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001007 return ret;
1008}
1009
1010/* Return 1 if we should not put LINE into the history according to the
1011 patterns in HISTIGNORE. */
1012static int
1013history_should_ignore (line)
1014 char *line;
1015{
1016 register int i, match;
1017 char *npat;
1018
1019 if (histignore.num_ignores == 0)
1020 return 0;
1021
1022 for (i = match = 0; i < histignore.num_ignores; i++)
1023 {
1024 if (histignore.ignores[i].flags & HIGN_EXPAND)
1025 npat = expand_histignore_pattern (histignore.ignores[i].val);
1026 else
1027 npat = histignore.ignores[i].val;
1028
Jari Aaltof73dda02001-11-13 17:56:06 +00001029 match = strmatch (npat, line, FNMATCH_EXTFLAG) != FNM_NOMATCH;
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001030
1031 if (histignore.ignores[i].flags & HIGN_EXPAND)
1032 free (npat);
1033
1034 if (match)
1035 break;
1036 }
1037
1038 return match;
1039}
1040#endif /* HISTORY */