]> git.ipfire.org Git - thirdparty/bash.git/blame - lib/readline/examples/histexamp.c
Imported from ../bash-2.05b.tar.gz.
[thirdparty/bash.git] / lib / readline / examples / histexamp.c
CommitLineData
7117c2d2
JA
1/* Copyright (C) 1987-2002 Free Software Foundation, Inc.
2
3 This file is part of the GNU Readline Library, a library for
4 reading lines of text with interactive input and history editing.
5
6 The GNU Readline Library is free software; you can redistribute it
7 and/or modify it under the terms of the GNU General Public License
8 as published by the Free Software Foundation; either version 2, or
9 (at your option) any later version.
10
11 The GNU Readline Library is distributed in the hope that it will be
12 useful, but WITHOUT ANY WARRANTY; without even the implied warranty
13 of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 The GNU General Public License is often shipped with GNU software, and
17 is generally kept in a file called COPYING or LICENSE. If you do not
18 have a copy of the license, write to the Free Software Foundation,
19 59 Temple Place, Suite 330, Boston, MA 02111 USA. */
20
28ef6c31
JA
21#include <stdio.h>
22
23#ifdef READLINE_LIBRARY
24# include "history.h"
25#else
26# include <readline/history.h>
27#endif
28
29main (argc, argv)
30 int argc;
31 char **argv;
726f6388
JA
32{
33 char line[1024], *t;
34 int len, done = 0;
35
36 line[0] = 0;
37
38 using_history ();
39 while (!done)
40 {
41 printf ("history$ ");
42 fflush (stdout);
43 t = fgets (line, sizeof (line) - 1, stdin);
44 if (t && *t)
45 {
46 len = strlen (t);
47 if (t[len - 1] == '\n')
48 t[len - 1] = '\0';
49 }
50
51 if (!t)
52 strcpy (line, "quit");
53
54 if (line[0])
55 {
56 char *expansion;
57 int result;
58
59 using_history ();
60
61 result = history_expand (line, &expansion);
62 if (result)
63 fprintf (stderr, "%s\n", expansion);
64
65 if (result < 0 || result == 2)
66 {
67 free (expansion);
68 continue;
69 }
70
71 add_history (expansion);
72 strncpy (line, expansion, sizeof (line) - 1);
73 free (expansion);
74 }
75
76 if (strcmp (line, "quit") == 0)
77 done = 1;
78 else if (strcmp (line, "save") == 0)
79 write_history ("history_file");
80 else if (strcmp (line, "read") == 0)
81 read_history ("history_file");
82 else if (strcmp (line, "list") == 0)
83 {
84 register HIST_ENTRY **the_list;
85 register int i;
86
87 the_list = history_list ();
88 if (the_list)
89 for (i = 0; the_list[i]; i++)
90 printf ("%d: %s\n", i + history_base, the_list[i]->line);
91 }
92 else if (strncmp (line, "delete", 6) == 0)
93 {
94 int which;
95 if ((sscanf (line + 6, "%d", &which)) == 1)
96 {
97 HIST_ENTRY *entry = remove_history (which);
98 if (!entry)
99 fprintf (stderr, "No such entry %d\n", which);
100 else
101 {
102 free (entry->line);
103 free (entry);
104 }
105 }
106 else
107 {
108 fprintf (stderr, "non-numeric arg given to `delete'\n");
109 }
110 }
111 }
112}