1119610Sache/* Copyright (C) 1987-2002 Free Software Foundation, Inc.
2119610Sache
3119610Sache   This file is part of the GNU Readline Library, a library for
4119610Sache   reading lines of text with interactive input and history editing.
5119610Sache
6119610Sache   The GNU Readline Library is free software; you can redistribute it
7119610Sache   and/or modify it under the terms of the GNU General Public License
8119610Sache   as published by the Free Software Foundation; either version 2, or
9119610Sache   (at your option) any later version.
10119610Sache
11119610Sache   The GNU Readline Library is distributed in the hope that it will be
12119610Sache   useful, but WITHOUT ANY WARRANTY; without even the implied warranty
13119610Sache   of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14119610Sache   GNU General Public License for more details.
15119610Sache
16119610Sache   The GNU General Public License is often shipped with GNU software, and
17119610Sache   is generally kept in a file called COPYING or LICENSE.  If you do not
18119610Sache   have a copy of the license, write to the Free Software Foundation,
19119610Sache   59 Temple Place, Suite 330, Boston, MA 02111 USA. */
20119610Sache
2175406Sache#include <stdio.h>
2275406Sache
2375406Sache#ifdef READLINE_LIBRARY
2475406Sache#  include "history.h"
2575406Sache#else
2675406Sache#  include <readline/history.h>
2775406Sache#endif
2875406Sache
29157184Sache#include <string.h>
30157184Sache
3175406Sachemain (argc, argv)
3275406Sache     int argc;
3375406Sache     char **argv;
3421308Sache{
3521308Sache  char line[1024], *t;
36136644Sache  int len, done;
3721308Sache
3821308Sache  line[0] = 0;
39136644Sache  done = 0;
4021308Sache
4121308Sache  using_history ();
4221308Sache  while (!done)
4321308Sache    {
4421308Sache      printf ("history$ ");
4521308Sache      fflush (stdout);
4621308Sache      t = fgets (line, sizeof (line) - 1, stdin);
4721308Sache      if (t && *t)
48136644Sache	{
49136644Sache	  len = strlen (t);
50136644Sache	  if (t[len - 1] == '\n')
51136644Sache	    t[len - 1] = '\0';
52136644Sache	}
5321308Sache
5421308Sache      if (!t)
55136644Sache	strcpy (line, "quit");
5621308Sache
5721308Sache      if (line[0])
58136644Sache	{
59136644Sache	  char *expansion;
60136644Sache	  int result;
6121308Sache
62136644Sache	  using_history ();
6321308Sache
64136644Sache	  result = history_expand (line, &expansion);
65136644Sache	  if (result)
66136644Sache	    fprintf (stderr, "%s\n", expansion);
6721308Sache
68136644Sache	  if (result < 0 || result == 2)
69136644Sache	    {
70136644Sache	      free (expansion);
71136644Sache	      continue;
72136644Sache	    }
7321308Sache
74136644Sache	  add_history (expansion);
75136644Sache	  strncpy (line, expansion, sizeof (line) - 1);
76136644Sache	  free (expansion);
77136644Sache	}
7821308Sache
7921308Sache      if (strcmp (line, "quit") == 0)
80136644Sache	done = 1;
8121308Sache      else if (strcmp (line, "save") == 0)
82136644Sache	write_history ("history_file");
8321308Sache      else if (strcmp (line, "read") == 0)
84136644Sache	read_history ("history_file");
8521308Sache      else if (strcmp (line, "list") == 0)
86136644Sache	{
87136644Sache	  register HIST_ENTRY **the_list;
88136644Sache	  register int i;
89136644Sache	  time_t tt;
90136644Sache	  char timestr[128];
9121308Sache
92136644Sache	  the_list = history_list ();
93136644Sache	  if (the_list)
94136644Sache	    for (i = 0; the_list[i]; i++)
95136644Sache	      {
96136644Sache	      	tt = history_get_time (the_list[i]);
97136644Sache		if (tt)
98136644Sache		  strftime (timestr, sizeof (timestr), "%a %R", localtime(&tt));
99136644Sache		else
100136644Sache		  strcpy (timestr, "??");
101136644Sache	        printf ("%d: %s: %s\n", i + history_base, timestr, the_list[i]->line);
102136644Sache	      }
103136644Sache	}
10421308Sache      else if (strncmp (line, "delete", 6) == 0)
105136644Sache	{
106136644Sache	  int which;
107136644Sache	  if ((sscanf (line + 6, "%d", &which)) == 1)
108136644Sache	    {
109136644Sache	      HIST_ENTRY *entry = remove_history (which);
110136644Sache	      if (!entry)
111136644Sache		fprintf (stderr, "No such entry %d\n", which);
112136644Sache	      else
113136644Sache		{
114136644Sache		  free (entry->line);
115136644Sache		  free (entry);
116136644Sache		}
117136644Sache	    }
118136644Sache	  else
119136644Sache	    {
120136644Sache	      fprintf (stderr, "non-numeric arg given to `delete'\n");
121136644Sache	    }
122136644Sache	}
12321308Sache    }
12421308Sache}
125