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