File:  [ELWIX - Embedded LightWeight unIX -] / embedaddon / readline / examples / hist_erasedups.c
Revision 1.1.1.2 (vendor branch): download - view: text, annotated - select for diffs - revision graph
Wed Mar 17 01:01:01 2021 UTC (3 years, 3 months ago) by misho
Branches: readline, MAIN
CVS tags: v8_2p0, v8_1p0, HEAD
readline 8.1

    1: /* hist_erasedups -- remove all duplicate entries from history file */
    2: 
    3: /* Copyright (C) 2011 Free Software Foundation, Inc.
    4: 
    5:    This file is part of the GNU Readline Library (Readline), a library for
    6:    reading lines of text with interactive input and history editing.
    7: 
    8:    Readline is free software: you can redistribute it and/or modify
    9:    it under the terms of the GNU General Public License as published by
   10:    the Free Software Foundation, either version 3 of the License, or
   11:    (at your option) any later version.
   12: 
   13:    Readline is distributed in the hope that it will be useful,
   14:    but WITHOUT ANY WARRANTY; without even the implied warranty of
   15:    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   16:    GNU General Public License for more details.
   17: 
   18:    You should have received a copy of the GNU General Public License
   19:    along with Readline.  If not, see <http://www.gnu.org/licenses/>.
   20: */
   21: #ifndef READLINE_LIBRARY
   22: #define READLINE_LIBRARY 1
   23: #endif
   24: 
   25: #include <unistd.h>
   26: #include <stdlib.h>
   27: #include <stdio.h>
   28: 
   29: #ifdef READLINE_LIBRARY
   30: #  include "history.h"
   31: #else
   32: #  include <readline/history.h>
   33: #endif
   34: 
   35: #include <string.h>
   36: 
   37: #define STREQ(a, b) ((a)[0] == (b)[0] && strcmp(a, b) == 0)
   38: #define STREQN(a, b, n) ((n == 0) ? (1) \
   39:                                   : ((a)[0] == (b)[0] && strncmp(a, b, n) == 0))
   40: 
   41: int hist_erasedups (void);
   42: 
   43: static void
   44: usage()
   45: {
   46:   fprintf (stderr, "hist_erasedups: usage: hist_erasedups [-t] [filename]\n");
   47:   exit (2);
   48: }
   49: 
   50: int
   51: main (argc, argv)
   52:      int argc;
   53:      char **argv;
   54: {
   55:   char *fn;
   56:   int r;
   57: 
   58:   while ((r = getopt (argc, argv, "t")) != -1)
   59:     {
   60:       switch (r)
   61: 	{
   62: 	case 't':
   63: 	  history_write_timestamps = 1;
   64: 	  break;
   65: 	default:
   66: 	  usage ();
   67: 	}
   68:     }
   69:   argv += optind;
   70:   argc -= optind;
   71: 
   72:   fn = argc ? argv[0] : getenv ("HISTFILE");
   73:   if (fn == 0)
   74:     {
   75:       fprintf (stderr, "hist_erasedups: no history file\n");
   76:       usage ();
   77:     }
   78: 
   79:   if ((r = read_history (fn)) != 0)
   80:     {
   81:       fprintf (stderr, "hist_erasedups: read_history: %s: %s\n", fn, strerror (r));
   82:       exit (1);
   83:     }
   84: 
   85:   hist_erasedups ();
   86: 
   87:   if ((r = write_history (fn)) != 0)
   88:     {
   89:       fprintf (stderr, "hist_erasedups: write_history: %s: %s\n", fn, strerror (r));
   90:       exit (1);
   91:     }
   92: 
   93:   exit (0);
   94: }
   95: 
   96: int
   97: hist_erasedups ()
   98: {
   99:   int r, n;
  100:   HIST_ENTRY *h, *temp;
  101: 
  102:   using_history ();
  103:   while (h = previous_history ())
  104:     {
  105:       r = where_history ();
  106:       for (n = 0; n < r; n++)
  107: 	{
  108: 	  temp = history_get (n+history_base);
  109: 	  if (STREQ (h->line, temp->line))
  110: 	    {
  111: 	      remove_history (n);
  112: 	      r--;			/* have to get one fewer now */
  113: 	      n--;			/* compensate for above increment */
  114: 	      history_offset--;		/* moving backwards in history list */
  115: 	    }
  116: 	}
  117:     }
  118:   using_history ();
  119: 
  120:   return r;
  121: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>