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