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