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