xref: /openbsd-src/gnu/lib/libreadline/examples/histexamp.c (revision 15b117eae8ea0caffec2fbd5cbd366a5ccee20ab)
1*15b117eaSkettenis /* Copyright (C) 1987-2002 Free Software Foundation, Inc.
2*15b117eaSkettenis 
3*15b117eaSkettenis    This file is part of the GNU Readline Library, a library for
4*15b117eaSkettenis    reading lines of text with interactive input and history editing.
5*15b117eaSkettenis 
6*15b117eaSkettenis    The GNU Readline Library is free software; you can redistribute it
7*15b117eaSkettenis    and/or modify it under the terms of the GNU General Public License
8*15b117eaSkettenis    as published by the Free Software Foundation; either version 2, or
9*15b117eaSkettenis    (at your option) any later version.
10*15b117eaSkettenis 
11*15b117eaSkettenis    The GNU Readline Library is distributed in the hope that it will be
12*15b117eaSkettenis    useful, but WITHOUT ANY WARRANTY; without even the implied warranty
13*15b117eaSkettenis    of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14*15b117eaSkettenis    GNU General Public License for more details.
15*15b117eaSkettenis 
16*15b117eaSkettenis    The GNU General Public License is often shipped with GNU software, and
17*15b117eaSkettenis    is generally kept in a file called COPYING or LICENSE.  If you do not
18*15b117eaSkettenis    have a copy of the license, write to the Free Software Foundation,
19*15b117eaSkettenis    59 Temple Place, Suite 330, Boston, MA 02111 USA. */
20*15b117eaSkettenis 
21*15b117eaSkettenis #include <stdio.h>
22*15b117eaSkettenis 
23*15b117eaSkettenis #ifdef READLINE_LIBRARY
24*15b117eaSkettenis #  include "history.h"
25*15b117eaSkettenis #else
26*15b117eaSkettenis #  include <readline/history.h>
27*15b117eaSkettenis #endif
28*15b117eaSkettenis 
main(argc,argv)29*15b117eaSkettenis main (argc, argv)
30*15b117eaSkettenis      int argc;
31*15b117eaSkettenis      char **argv;
321acd27e7Smillert {
331acd27e7Smillert   char line[1024], *t;
341acd27e7Smillert   int len, done = 0;
351acd27e7Smillert 
361acd27e7Smillert   line[0] = 0;
371acd27e7Smillert 
381acd27e7Smillert   using_history ();
391acd27e7Smillert   while (!done)
401acd27e7Smillert     {
411acd27e7Smillert       printf ("history$ ");
421acd27e7Smillert       fflush (stdout);
431acd27e7Smillert       t = fgets (line, sizeof (line) - 1, stdin);
441acd27e7Smillert       if (t && *t)
451acd27e7Smillert         {
461acd27e7Smillert           len = strlen (t);
471acd27e7Smillert           if (t[len - 1] == '\n')
481acd27e7Smillert             t[len - 1] = '\0';
491acd27e7Smillert         }
501acd27e7Smillert 
511acd27e7Smillert       if (!t)
521acd27e7Smillert         strcpy (line, "quit");
531acd27e7Smillert 
541acd27e7Smillert       if (line[0])
551acd27e7Smillert         {
561acd27e7Smillert           char *expansion;
571acd27e7Smillert           int result;
581acd27e7Smillert 
591acd27e7Smillert           using_history ();
601acd27e7Smillert 
611acd27e7Smillert           result = history_expand (line, &expansion);
621acd27e7Smillert           if (result)
631acd27e7Smillert             fprintf (stderr, "%s\n", expansion);
641acd27e7Smillert 
651acd27e7Smillert           if (result < 0 || result == 2)
661acd27e7Smillert             {
671acd27e7Smillert               free (expansion);
681acd27e7Smillert               continue;
691acd27e7Smillert             }
701acd27e7Smillert 
711acd27e7Smillert           add_history (expansion);
721acd27e7Smillert           strncpy (line, expansion, sizeof (line) - 1);
731acd27e7Smillert           free (expansion);
741acd27e7Smillert         }
751acd27e7Smillert 
761acd27e7Smillert       if (strcmp (line, "quit") == 0)
771acd27e7Smillert         done = 1;
781acd27e7Smillert       else if (strcmp (line, "save") == 0)
791acd27e7Smillert         write_history ("history_file");
801acd27e7Smillert       else if (strcmp (line, "read") == 0)
811acd27e7Smillert         read_history ("history_file");
821acd27e7Smillert       else if (strcmp (line, "list") == 0)
831acd27e7Smillert         {
841acd27e7Smillert           register HIST_ENTRY **the_list;
851acd27e7Smillert           register int i;
861acd27e7Smillert 
871acd27e7Smillert           the_list = history_list ();
881acd27e7Smillert           if (the_list)
891acd27e7Smillert             for (i = 0; the_list[i]; i++)
901acd27e7Smillert               printf ("%d: %s\n", i + history_base, the_list[i]->line);
911acd27e7Smillert         }
921acd27e7Smillert       else if (strncmp (line, "delete", 6) == 0)
931acd27e7Smillert         {
941acd27e7Smillert           int which;
951acd27e7Smillert           if ((sscanf (line + 6, "%d", &which)) == 1)
961acd27e7Smillert             {
971acd27e7Smillert               HIST_ENTRY *entry = remove_history (which);
981acd27e7Smillert               if (!entry)
991acd27e7Smillert                 fprintf (stderr, "No such entry %d\n", which);
1001acd27e7Smillert               else
1011acd27e7Smillert                 {
1021acd27e7Smillert                   free (entry->line);
1031acd27e7Smillert                   free (entry);
1041acd27e7Smillert                 }
1051acd27e7Smillert             }
1061acd27e7Smillert           else
1071acd27e7Smillert             {
1081acd27e7Smillert               fprintf (stderr, "non-numeric arg given to `delete'\n");
1091acd27e7Smillert             }
1101acd27e7Smillert         }
1111acd27e7Smillert     }
1121acd27e7Smillert }
113