]> git.ipfire.org Git - thirdparty/bash.git/blame - bashhist.c
commit bash-20050623 snapshot
[thirdparty/bash.git] / bashhist.c
CommitLineData
726f6388
JA
1/* bashhist.c -- bash interface to the GNU history library. */
2
762a763b 3/* Copyright (C) 1993-2004 Free Software Foundation, Inc.
726f6388
JA
4
5 This file is part of GNU Bash, the Bourne Again SHell.
6
7 Bash is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
10 version.
11
12 Bash is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
16
17 You should have received a copy of the GNU General Public License along
18 with Bash; see the file COPYING. If not, write to the Free Software
bb70624e 19 Foundation, 59 Temple Place, Suite 330, Boston, MA 02111 USA. */
726f6388 20
ccc6cda3
JA
21#include "config.h"
22
23#if defined (HISTORY)
24
25#if defined (HAVE_UNISTD_H)
cce855bc
JA
26# ifdef _MINIX
27# include <sys/types.h>
28# endif
ccc6cda3
JA
29# include <unistd.h>
30#endif
31
32#include "bashtypes.h"
726f6388
JA
33#include <stdio.h>
34#include <errno.h>
35#include "bashansi.h"
36#include "posixstat.h"
37#include "filecntl.h"
d166f048 38
5e13499c
CR
39#include "bashintl.h"
40
726f6388
JA
41#include "shell.h"
42#include "flags.h"
ccc6cda3
JA
43#include "input.h"
44#include "parser.h" /* for the struct dstack stuff. */
45#include "pathexp.h" /* for the struct ignorevar stuff */
f73dda09 46#include "bashhist.h" /* matching prototypes and declarations */
ccc6cda3 47#include "builtins/common.h"
d166f048 48
726f6388 49#include <readline/history.h>
f73dda09
JA
50#include <glob/glob.h>
51#include <glob/strmatch.h>
ccc6cda3
JA
52
53#if defined (READLINE)
54# include "bashline.h"
762a763b 55extern int rl_done, rl_dispatching; /* should really include readline.h */
ccc6cda3
JA
56#endif
57
58#if !defined (errno)
59extern int errno;
60#endif
61
f73dda09 62static int histignore_item_func __P((struct ign *));
7117c2d2 63static int check_history_control __P((char *));
d3a24ed2 64static void hc_erasedups __P((char *));
7117c2d2 65static void really_add_history __P((char *));
ccc6cda3
JA
66
67static struct ignorevar histignore =
68{
69 "HISTIGNORE",
70 (struct ign *)0,
71 0,
72 (char *)0,
f73dda09 73 (sh_iv_item_func_t *)histignore_item_func,
ccc6cda3
JA
74};
75
76#define HIGN_EXPAND 0x01
726f6388
JA
77
78/* Declarations of bash history variables. */
79/* Non-zero means to remember lines typed to the shell on the history
80 list. This is different than the user-controlled behaviour; this
81 becomes zero when we read lines from a file, for example. */
82int remember_on_history = 1;
83
d3a24ed2
CR
84/* The number of lines that Bash has added to this history session. The
85 difference between the number of the top element in the history list
86 (offset from history_base) and the number of lines in the history file.
87 Appending this session's history to the history file resets this to 0. */
ccc6cda3 88int history_lines_this_session;
726f6388
JA
89
90/* The number of lines that Bash has read from the history file. */
ccc6cda3 91int history_lines_in_file;
726f6388 92
ccc6cda3 93#if defined (BANG_HISTORY)
726f6388
JA
94/* Non-zero means do no history expansion on this line, regardless
95 of what history_expansion says. */
ccc6cda3
JA
96int history_expansion_inhibited;
97#endif
726f6388 98
7117c2d2
JA
99/* With the old default, every line was saved in the history individually.
100 I.e., if the user enters:
726f6388 101 bash$ for i in a b c
ccc6cda3
JA
102 > do
103 > echo $i
104 > done
726f6388
JA
105 Each line will be individually saved in the history.
106 bash$ history
107 10 for i in a b c
ccc6cda3
JA
108 11 do
109 12 echo $i
110 13 done
111 14 history
726f6388
JA
112 If the variable command_oriented_history is set, multiple lines
113 which form one command will be saved as one history entry.
114 bash$ for i in a b c
ccc6cda3
JA
115 > do
116 > echo $i
117 > done
118 bash$ history
726f6388
JA
119 10 for i in a b c
120 do
121 echo $i
122 done
ccc6cda3 123 11 history
726f6388
JA
124 The user can then recall the whole command all at once instead
125 of just being able to recall one line at a time.
7117c2d2
JA
126
127 This is now enabled by default.
726f6388 128 */
ccc6cda3
JA
129int command_oriented_history = 1;
130
7117c2d2
JA
131/* Set to 1 if the first line of a possibly-multi-line command was saved
132 in the history list. Managed by maybe_add_history(), but global so
133 the history-manipluating builtins can see it. */
134int current_command_first_line_saved = 0;
135
ccc6cda3
JA
136/* Non-zero means to store newlines in the history list when using
137 command_oriented_history rather than trying to use semicolons. */
138int literal_history;
139
140/* Non-zero means to append the history to the history file at shell
141 exit, even if the history has been stifled. */
142int force_append_history;
726f6388 143
d3a24ed2
CR
144/* A nit for picking at history saving. Flags have the following values:
145
146 Value == 0 means save all lines parsed by the shell on the history.
147 Value & HC_IGNSPACE means save all lines that do not start with a space.
148 Value & HC_IGNDUPS means save all lines that do not match the last
149 line saved.
150 Value & HC_ERASEDUPS means to remove all other matching lines from the
151 history list before saving the latest line. */
ccc6cda3
JA
152int history_control;
153
d166f048
JA
154/* Set to 1 if the last command was added to the history list successfully
155 as a separate history entry; set to 0 if the line was ignored or added
156 to a previous entry as part of command-oriented-history processing. */
157int hist_last_line_added;
158
ccc6cda3
JA
159#if defined (READLINE)
160/* If non-zero, and readline is being used, the user is offered the
161 chance to re-edit a failed history expansion. */
162int history_reediting;
163
164/* If non-zero, and readline is being used, don't directly execute a
165 line with history substitution. Reload it into the editing buffer
166 instead and let the user further edit and confirm with a newline. */
167int hist_verify;
d166f048
JA
168
169#endif /* READLINE */
726f6388 170
f73dda09
JA
171/* Non-zero means to not save function definitions in the history list. */
172int dont_save_function_defs;
173
726f6388 174/* Variables declared in other files used here. */
726f6388 175extern int current_command_line_count;
726f6388 176
ccc6cda3
JA
177extern struct dstack dstack;
178
f73dda09
JA
179static int bash_history_inhibit_expansion __P((char *, int));
180#if defined (READLINE)
181static void re_edit __P((char *));
182#endif
183static int history_expansion_p __P((char *));
184static int shell_comment __P((char *));
185static int should_expand __P((char *));
186static HIST_ENTRY *last_history_entry __P((void));
187static char *expand_histignore_pattern __P((char *));
188static int history_should_ignore __P((char *));
ccc6cda3 189
d166f048
JA
190/* Is the history expansion starting at string[i] one that should not
191 be expanded? */
192static int
193bash_history_inhibit_expansion (string, i)
194 char *string;
195 int i;
196{
197 /* The shell uses ! as a pattern negation character in globbing [...]
198 expressions, so let those pass without expansion. */
199 if (i > 0 && (string[i - 1] == '[') && member (']', string + i + 1))
200 return (1);
201 /* The shell uses ! as the indirect expansion character, so let those
202 expansions pass as well. */
203 else if (i > 1 && string[i - 1] == '{' && string[i - 2] == '$' &&
204 member ('}', string + i + 1))
205 return (1);
cce855bc
JA
206#if defined (EXTENDED_GLOB)
207 else if (extended_glob && i > 1 && string[i+1] == '(' && member (')', string + i + 2))
208 return (1);
209#endif
d166f048
JA
210 else
211 return (0);
212}
213
ccc6cda3
JA
214void
215bash_initialize_history ()
216{
217 history_quotes_inhibit_expansion = 1;
218 history_search_delimiter_chars = ";&()|<>";
d166f048 219 history_inhibit_expansion_function = bash_history_inhibit_expansion;
0f445e6c 220#if defined (BANG_HISTORY)
5e13499c 221 sv_histchars ("histchars");
0f445e6c 222#endif
ccc6cda3
JA
223}
224
225void
226bash_history_reinit (interact)
227 int interact;
228{
229#if defined (BANG_HISTORY)
230 history_expansion = interact != 0;
231 history_expansion_inhibited = 1;
232#endif
233 remember_on_history = interact != 0;
d166f048 234 history_inhibit_expansion_function = bash_history_inhibit_expansion;
ccc6cda3
JA
235}
236
237void
238bash_history_disable ()
239{
240 remember_on_history = 0;
241#if defined (BANG_HISTORY)
242 history_expansion_inhibited = 1;
243#endif
244}
245
246void
247bash_history_enable ()
248{
249 remember_on_history = 1;
250#if defined (BANG_HISTORY)
251 history_expansion_inhibited = 0;
252#endif
d166f048 253 history_inhibit_expansion_function = bash_history_inhibit_expansion;
ccc6cda3
JA
254 sv_history_control ("HISTCONTROL");
255 sv_histignore ("HISTIGNORE");
256}
726f6388
JA
257
258/* Load the history list from the history file. */
259void
260load_history ()
261{
262 char *hf;
ccc6cda3 263 struct stat buf;
726f6388
JA
264
265 /* Truncate history file for interactive shells which desire it.
266 Note that the history file is automatically truncated to the
267 size of HISTSIZE if the user does not explicitly set the size
268 differently. */
269 set_if_not ("HISTFILESIZE", get_string_value ("HISTSIZE"));
ccc6cda3 270 sv_histsize ("HISTFILESIZE");
726f6388
JA
271
272 /* Read the history in HISTFILE into the history list. */
273 hf = get_string_value ("HISTFILE");
274
ccc6cda3 275 if (hf && *hf && stat (hf, &buf) == 0)
726f6388 276 {
ccc6cda3
JA
277 read_history (hf);
278 using_history ();
279 history_lines_in_file = where_history ();
726f6388
JA
280 }
281}
282
d166f048 283#ifdef INCLUDE_UNUSED
726f6388
JA
284/* Write the existing history out to the history file. */
285void
286save_history ()
287{
ccc6cda3
JA
288 char *hf;
289 struct stat buf;
726f6388 290
ccc6cda3
JA
291 hf = get_string_value ("HISTFILE");
292 if (hf && *hf && stat (hf, &buf) == 0)
726f6388 293 {
ccc6cda3
JA
294 /* Append only the lines that occurred this session to
295 the history file. */
296 using_history ();
726f6388 297
ccc6cda3
JA
298 if (history_lines_this_session < where_history () || force_append_history)
299 append_history (history_lines_this_session, hf);
300 else
301 write_history (hf);
726f6388 302
ccc6cda3
JA
303 sv_histsize ("HISTFILESIZE");
304 }
305}
d166f048 306#endif
ccc6cda3
JA
307
308int
309maybe_append_history (filename)
310 char *filename;
311{
312 int fd, result;
313 struct stat buf;
314
315 result = EXECUTION_SUCCESS;
316 if (history_lines_this_session && (history_lines_this_session < where_history ()))
317 {
318 /* If the filename was supplied, then create it if necessary. */
319 if (stat (filename, &buf) == -1 && errno == ENOENT)
320 {
b72432fd 321 fd = open (filename, O_WRONLY|O_CREAT, 0600);
ccc6cda3
JA
322 if (fd < 0)
323 {
5e13499c 324 builtin_error (_("%s: cannot create: %s"), filename, strerror (errno));
ccc6cda3
JA
325 return (EXECUTION_FAILURE);
326 }
327 close (fd);
726f6388 328 }
ccc6cda3
JA
329 result = append_history (history_lines_this_session, filename);
330 history_lines_in_file += history_lines_this_session;
331 history_lines_this_session = 0;
726f6388 332 }
ccc6cda3 333 return (result);
726f6388
JA
334}
335
336/* If this is an interactive shell, then append the lines executed
337 this session to the history file. */
338int
339maybe_save_shell_history ()
340{
ccc6cda3
JA
341 int result;
342 char *hf;
343 struct stat buf;
726f6388 344
ccc6cda3 345 result = 0;
726f6388
JA
346 if (history_lines_this_session)
347 {
ccc6cda3 348 hf = get_string_value ("HISTFILE");
726f6388
JA
349
350 if (hf && *hf)
351 {
726f6388
JA
352 /* If the file doesn't exist, then create it. */
353 if (stat (hf, &buf) == -1)
354 {
ccc6cda3 355 int file;
b72432fd 356 file = open (hf, O_CREAT | O_TRUNC | O_WRONLY, 0600);
726f6388
JA
357 if (file != -1)
358 close (file);
359 }
360
361 /* Now actually append the lines if the history hasn't been
362 stifled. If the history has been stifled, rewrite the
363 history file. */
364 using_history ();
ccc6cda3 365 if (history_lines_this_session <= where_history () || force_append_history)
726f6388
JA
366 {
367 result = append_history (history_lines_this_session, hf);
368 history_lines_in_file += history_lines_this_session;
369 }
370 else
371 {
372 result = write_history (hf);
373 history_lines_in_file = history_lines_this_session;
374 }
375 history_lines_this_session = 0;
ccc6cda3
JA
376
377 sv_histsize ("HISTFILESIZE");
726f6388
JA
378 }
379 }
380 return (result);
381}
382
ccc6cda3 383#if defined (READLINE)
726f6388
JA
384/* Tell readline () that we have some text for it to edit. */
385static void
386re_edit (text)
387 char *text;
388{
ccc6cda3 389 if (bash_input.type == st_stdin)
726f6388 390 bash_re_edit (text);
ccc6cda3 391}
726f6388 392#endif /* READLINE */
ccc6cda3
JA
393
394/* Return 1 if this line needs history expansion. */
395static int
396history_expansion_p (line)
397 char *line;
398{
399 register char *s;
400
401 for (s = line; *s; s++)
402 if (*s == history_expansion_char || *s == history_subst_char)
403 return 1;
404 return 0;
726f6388 405}
726f6388
JA
406
407/* Do pre-processing on LINE. If PRINT_CHANGES is non-zero, then
408 print the results of expanding the line if there were any changes.
409 If there is an error, return NULL, otherwise the expanded line is
410 returned. If ADDIT is non-zero the line is added to the history
411 list after history expansion. ADDIT is just a suggestion;
412 REMEMBER_ON_HISTORY can veto, and does.
413 Right now this does history expansion. */
414char *
415pre_process_line (line, print_changes, addit)
416 char *line;
417 int print_changes, addit;
418{
419 char *history_value;
420 char *return_value;
ccc6cda3 421 int expanded;
726f6388
JA
422
423 return_value = line;
ccc6cda3 424 expanded = 0;
726f6388
JA
425
426# if defined (BANG_HISTORY)
427 /* History expand the line. If this results in no errors, then
428 add that line to the history if ADDIT is non-zero. */
ccc6cda3 429 if (!history_expansion_inhibited && history_expansion && history_expansion_p (line))
726f6388
JA
430 {
431 expanded = history_expand (line, &history_value);
432
433 if (expanded)
434 {
435 if (print_changes)
436 {
437 if (expanded < 0)
cce855bc 438 internal_error ("%s", history_value);
d166f048 439#if defined (READLINE)
bb70624e 440 else if (hist_verify == 0 || expanded == 2)
d166f048
JA
441#else
442 else
443#endif
726f6388
JA
444 fprintf (stderr, "%s\n", history_value);
445 }
446
447 /* If there was an error, return NULL. */
448 if (expanded < 0 || expanded == 2) /* 2 == print only */
449 {
762a763b
CR
450# if defined (READLINE)
451 if (expanded == 2 && rl_dispatching == 0 && *history_value)
452# else
453 if (expanded == 2 && *history_value)
454# endif /* !READLINE */
455 maybe_add_history (history_value);
456
726f6388
JA
457 free (history_value);
458
ccc6cda3 459# if defined (READLINE)
726f6388
JA
460 /* New hack. We can allow the user to edit the
461 failed history expansion. */
762a763b 462 if (history_reediting && expanded < 0 && rl_done)
ccc6cda3
JA
463 re_edit (line);
464# endif /* READLINE */
465 return ((char *)NULL);
466 }
467
468# if defined (READLINE)
469 if (hist_verify && expanded == 1)
470 {
471 re_edit (history_value);
726f6388
JA
472 return ((char *)NULL);
473 }
ccc6cda3 474# endif
726f6388
JA
475 }
476
477 /* Let other expansions know that return_value can be free'ed,
478 and that a line has been added to the history list. Note
479 that we only add lines that have something in them. */
480 expanded = 1;
481 return_value = history_value;
482 }
483# endif /* BANG_HISTORY */
484
485 if (addit && remember_on_history && *return_value)
486 maybe_add_history (return_value);
487
d166f048 488#if 0
ccc6cda3 489 if (expanded == 0)
726f6388 490 return_value = savestring (line);
d166f048 491#endif
726f6388
JA
492
493 return (return_value);
494}
495
bb70624e
JA
496/* Return 1 if the first non-whitespace character in LINE is a `#', indicating
497 * that the line is a shell comment. */
498static int
499shell_comment (line)
500 char *line;
501{
502 char *p;
503
504 for (p = line; p && *p && whitespace (*p); p++)
505 ;
506 return (p && *p == '#');
507}
508
f73dda09 509#ifdef INCLUDE_UNUSED
bb70624e
JA
510/* Remove shell comments from LINE. A `#' and anything after it is a comment.
511 This isn't really useful yet, since it doesn't handle quoting. */
512static char *
513filter_comments (line)
514 char *line;
515{
516 char *p;
517
518 for (p = line; p && *p && *p != '#'; p++)
519 ;
520 if (p && *p == '#')
521 *p = '\0';
522 return (line);
523}
f73dda09 524#endif
bb70624e 525
7117c2d2
JA
526/* Check LINE against what HISTCONTROL says to do. Returns 1 if the line
527 should be saved; 0 if it should be discarded. */
528static int
529check_history_control (line)
726f6388
JA
530 char *line;
531{
ccc6cda3 532 HIST_ENTRY *temp;
7117c2d2 533 int r;
ccc6cda3 534
d3a24ed2
CR
535 if (history_control == 0)
536 return 1;
537
538 /* ignorespace or ignoreboth */
539 if ((history_control & HC_IGNSPACE) && *line == ' ')
540 return 0;
541
542 /* ignoredups or ignoreboth */
543 if (history_control & HC_IGNDUPS)
7117c2d2 544 {
7117c2d2
JA
545 using_history ();
546 temp = previous_history ();
547
548 r = (temp == 0 || STREQ (temp->line, line) == 0);
549
550 using_history ();
d3a24ed2
CR
551
552 if (r == 0)
553 return r;
7117c2d2
JA
554 }
555
d3a24ed2
CR
556 return 1;
557}
558
559/* Remove all entries matching LINE from the history list. Triggered when
560 HISTCONTROL includes `erasedups'. */
561static void
562hc_erasedups (line)
563 char *line;
564{
565 HIST_ENTRY *temp;
566 int r;
567
568 using_history ();
569 while (temp = previous_history ())
570 {
571 if (STREQ (temp->line, line))
572 {
573 r = where_history ();
574 remove_history (r);
575 }
576 }
577 using_history ();
7117c2d2
JA
578}
579
580/* Add LINE to the history list, handling possibly multi-line compound
581 commands. We note whether or not we save the first line of each command
582 (which is usually the entire command and history entry), and don't add
583 the second and subsequent lines of a multi-line compound command if we
584 didn't save the first line. We don't usually save shell comment lines in
585 compound commands in the history, because they could have the effect of
586 commenting out the rest of the command when the entire command is saved as
587 a single history entry (when COMMAND_ORIENTED_HISTORY is enabled). If
588 LITERAL_HISTORY is set, we're saving lines in the history with embedded
589 newlines, so it's OK to save comment lines. We also make sure to save
590 multiple-line quoted strings or other constructs. */
591void
592maybe_add_history (line)
593 char *line;
594{
28ef6c31 595 hist_last_line_added = 0;
726f6388
JA
596
597 /* Don't use the value of history_control to affect the second
cce855bc
JA
598 and subsequent lines of a multi-line command (old code did
599 this only when command_oriented_history is enabled). */
cce855bc 600 if (current_command_line_count > 1)
ccc6cda3 601 {
7117c2d2 602 if (current_command_first_line_saved &&
28ef6c31 603 (literal_history || dstack.delimiter_depth != 0 || shell_comment (line) == 0))
bb70624e 604 bash_add_history (line);
ccc6cda3
JA
605 return;
606 }
726f6388 607
28ef6c31
JA
608 /* This is the first line of a (possible multi-line) command. Note whether
609 or not we should save the first line and remember it. */
7117c2d2
JA
610 current_command_first_line_saved = check_add_history (line, 0);
611}
28ef6c31 612
7117c2d2
JA
613/* Just check LINE against HISTCONTROL and HISTIGNORE and add it to the
614 history if it's OK. Used by `history -s' as well as maybe_add_history().
615 Returns 1 if the line was saved in the history, 0 otherwise. */
616int
617check_add_history (line, force)
618 char *line;
619 int force;
620{
621 if (check_history_control (line) && history_should_ignore (line) == 0)
726f6388 622 {
d3a24ed2
CR
623 /* We're committed to saving the line. If the user has requested it,
624 remove other matching lines from the history. */
625 if (history_control & HC_ERASEDUPS)
626 hc_erasedups (line);
627
7117c2d2
JA
628 if (force)
629 {
630 really_add_history (line);
631 using_history ();
632 }
633 else
634 bash_add_history (line);
635 return 1;
726f6388 636 }
7117c2d2 637 return 0;
726f6388
JA
638}
639
640/* Add a line to the history list.
641 The variable COMMAND_ORIENTED_HISTORY controls the style of history
642 remembering; when non-zero, and LINE is not the first line of a
643 complete parser construct, append LINE to the last history line instead
644 of adding it as a new line. */
d166f048 645void
726f6388
JA
646bash_add_history (line)
647 char *line;
648{
ccc6cda3
JA
649 int add_it, offset, curlen;
650 HIST_ENTRY *current, *old;
651 char *chars_to_add, *new_line;
726f6388 652
ccc6cda3 653 add_it = 1;
726f6388
JA
654 if (command_oriented_history && current_command_line_count > 1)
655 {
ccc6cda3 656 chars_to_add = literal_history ? "\n" : history_delimiting_chars ();
726f6388
JA
657
658 using_history ();
726f6388
JA
659 current = previous_history ();
660
661 if (current)
662 {
663 /* If the previous line ended with an escaped newline (escaped
664 with backslash, but otherwise unquoted), then remove the quoted
665 newline, since that is what happens when the line is parsed. */
726f6388
JA
666 curlen = strlen (current->line);
667
ccc6cda3 668 if (dstack.delimiter_depth == 0 && current->line[curlen - 1] == '\\' &&
726f6388
JA
669 current->line[curlen - 2] != '\\')
670 {
671 current->line[curlen - 1] = '\0';
672 curlen--;
673 chars_to_add = "";
674 }
675
f73dda09
JA
676 new_line = (char *)xmalloc (1
677 + curlen
678 + strlen (line)
679 + strlen (chars_to_add));
726f6388 680 sprintf (new_line, "%s%s%s", current->line, chars_to_add, line);
ccc6cda3 681 offset = where_history ();
726f6388
JA
682 old = replace_history_entry (offset, new_line, current->data);
683 free (new_line);
684
685 if (old)
d3a24ed2
CR
686 free_history_entry (old);
687
726f6388
JA
688 add_it = 0;
689 }
690 }
691
692 if (add_it)
7117c2d2
JA
693 really_add_history (line);
694
726f6388
JA
695 using_history ();
696}
697
7117c2d2
JA
698static void
699really_add_history (line)
700 char *line;
701{
702 hist_last_line_added = 1;
703 add_history (line);
704 history_lines_this_session++;
705}
706
726f6388
JA
707int
708history_number ()
709{
710 using_history ();
ccc6cda3
JA
711 return (get_string_value ("HISTSIZE") ? history_base + where_history () : 1);
712}
713
714static int
715should_expand (s)
716 char *s;
717{
718 char *p;
719
720 for (p = s; p && *p; p++)
721 {
722 if (*p == '\\')
723 p++;
724 else if (*p == '&')
725 return 1;
726 }
727 return 0;
728}
729
730static int
731histignore_item_func (ign)
732 struct ign *ign;
733{
734 if (should_expand (ign->val))
735 ign->flags |= HIGN_EXPAND;
736 return (0);
737}
738
739void
740setup_history_ignore (varname)
741 char *varname;
742{
743 setup_ignore_patterns (&histignore);
744}
745
746static HIST_ENTRY *
747last_history_entry ()
748{
749 HIST_ENTRY *he;
750
751 using_history ();
752 he = previous_history ();
753 using_history ();
754 return he;
755}
756
757char *
758last_history_line ()
759{
760 HIST_ENTRY *he;
761
762 he = last_history_entry ();
763 if (he == 0)
764 return ((char *)NULL);
765 return he->line;
766}
767
768static char *
769expand_histignore_pattern (pat)
770 char *pat;
771{
772 HIST_ENTRY *phe;
bb70624e 773 char *ret;
ccc6cda3
JA
774
775 phe = last_history_entry ();
776
777 if (phe == (HIST_ENTRY *)0)
778 return (savestring (pat));
779
bb70624e 780 ret = strcreplace (pat, '&', phe->line, 1);
ccc6cda3 781
ccc6cda3
JA
782 return ret;
783}
784
785/* Return 1 if we should not put LINE into the history according to the
786 patterns in HISTIGNORE. */
787static int
788history_should_ignore (line)
789 char *line;
790{
791 register int i, match;
792 char *npat;
793
794 if (histignore.num_ignores == 0)
795 return 0;
796
797 for (i = match = 0; i < histignore.num_ignores; i++)
798 {
799 if (histignore.ignores[i].flags & HIGN_EXPAND)
800 npat = expand_histignore_pattern (histignore.ignores[i].val);
801 else
802 npat = histignore.ignores[i].val;
803
f73dda09 804 match = strmatch (npat, line, FNMATCH_EXTFLAG) != FNM_NOMATCH;
ccc6cda3
JA
805
806 if (histignore.ignores[i].flags & HIGN_EXPAND)
807 free (npat);
808
809 if (match)
810 break;
811 }
812
813 return match;
726f6388 814}
ccc6cda3 815#endif /* HISTORY */