home *** CD-ROM | disk | FTP | other *** search
/ Il CD di internet / CD.iso / SOURCE / AP / TERMNET / READLINE.0 / SEARCH.C < prev    next >
Encoding:
C/C++ Source or Header  |  1995-04-20  |  8.9 KB  |  368 lines

  1. /* search.c - code for non-incremental searching in emacs and vi modes. */
  2.  
  3. /* Copyright (C) 1992 Free Software Foundation, Inc.
  4.  
  5.    This file is part of the Readline Library (the Library), a set of
  6.    routines for providing Emacs style line input to programs that ask
  7.    for it.
  8.  
  9.    The Library is free software; you can redistribute it and/or modify
  10.    it under the terms of the GNU General Public License as published by
  11.    the Free Software Foundation; either version 1, or (at your option)
  12.    any later version.
  13.  
  14.    The Library is distributed in the hope that it will be useful, but
  15.    WITHOUT ANY WARRANTY; without even the implied warranty of
  16.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  17.    General Public License for more details.
  18.  
  19.    The GNU General Public License is often shipped with GNU software, and
  20.    is generally kept in a file called COPYING or LICENSE.  If you do not
  21.    have a copy of the license, write to the Free Software Foundation,
  22.    675 Mass Ave, Cambridge, MA 02139, USA. */
  23. #define READLINE_LIBRARY
  24.  
  25. #include <sys/types.h>
  26. #include <stdio.h>
  27.  
  28. #if defined (HAVE_UNISTD_H)
  29. #  include <unistd.h>
  30. #endif
  31.  
  32. #include "memalloc.h"
  33. #include "rldefs.h"
  34. #include "readline.h"
  35. #include "history.h"
  36.  
  37. #define STREQ(a, b)    (((a)[0] == (b)[0]) && (strcmp ((a), (b)) == 0))
  38. #define STREQN(a, b, n)    (((a)[0] == (b)[0]) && (strncmp ((a), (b), (n)) == 0))
  39.  
  40. #define abs(x)        (((x) > 0) ? (x) : -(x))
  41.  
  42. extern char *xmalloc (), *xrealloc ();
  43.  
  44. /* Variables imported from readline.c */
  45. extern int rl_point, rl_end, rl_line_buffer_len;
  46. extern Keymap _rl_keymap;
  47. extern char *rl_prompt;
  48. extern char *rl_line_buffer;
  49. extern HIST_ENTRY *saved_line_for_history;
  50. extern Function *rl_last_func;
  51.  
  52. /* Functions imported from the rest of the library. */
  53. extern int _rl_free_history_entry ();
  54.  
  55. static char *noninc_search_string = (char *) NULL;
  56. static int noninc_history_pos = 0;
  57. static char *prev_line_found = (char *) NULL;
  58.  
  59. /* Search the history list for STRING starting at absolute history position
  60.    POS.  If STRING begins with `^', the search must match STRING at the
  61.    beginning of a history line, otherwise a full substring match is performed
  62.    for STRING.  DIR < 0 means to search backwards through the history list,
  63.    DIR >= 0 means to search forward. */
  64. static int
  65. noninc_search_from_pos (string, pos, dir)
  66.      char *string;
  67.      int pos, dir;
  68. {
  69.   int ret, old;
  70.  
  71.   old = where_history ();
  72.   history_set_pos (pos);
  73.  
  74.   if (*string == '^')
  75.     ret = history_search_prefix (string + 1, dir);
  76.   else
  77.     ret = history_search (string, dir);
  78.  
  79.   if (ret != -1)
  80.     ret = where_history ();
  81.  
  82.   history_set_pos (old);
  83.   return (ret);
  84. }
  85.  
  86. /* Search for a line in the history containing STRING.  If DIR is < 0, the
  87.    search is backwards through previous entries, else through subsequent
  88.    entries. */
  89. static void
  90. noninc_dosearch (string, dir)
  91.      char *string;
  92.      int dir;
  93. {
  94.   int oldpos, pos;
  95.   HIST_ENTRY *entry;
  96.  
  97.   if (string == 0 || *string == 0 || noninc_history_pos < 0)
  98.     {
  99.       ding ();
  100.       return;
  101.     }
  102.  
  103.   pos = noninc_search_from_pos (string, noninc_history_pos + dir, dir);
  104.   if (pos == -1)
  105.     {
  106.       /* Search failed, current history position unchanged. */
  107.       maybe_unsave_line ();
  108.       rl_clear_message ();
  109.       rl_point = 0;
  110.       ding ();
  111.       return;
  112.     }
  113.  
  114.   noninc_history_pos = pos;
  115.  
  116.   oldpos = where_history ();
  117.   history_set_pos (noninc_history_pos);
  118.   entry = current_history ();
  119.   history_set_pos (oldpos);
  120.  
  121.   {
  122.     int line_len;
  123.  
  124.     line_len = strlen (entry->line);
  125.     if (line_len >= rl_line_buffer_len)
  126.       rl_extend_line_buffer (line_len);
  127.     strcpy (rl_line_buffer, entry->line);
  128.   }
  129.  
  130.   rl_undo_list = (UNDO_LIST *)entry->data;
  131.   rl_end = strlen (rl_line_buffer);
  132.   rl_point = 0;
  133.   rl_clear_message ();
  134.  
  135.   if (saved_line_for_history)
  136.     _rl_free_history_entry (saved_line_for_history);
  137.   saved_line_for_history = (HIST_ENTRY *)NULL;
  138. }
  139.  
  140. /* Search non-interactively through the history list.  DIR < 0 means to
  141.    search backwards through the history of previous commands; otherwise
  142.    the search is for commands subsequent to the current position in the
  143.    history list.  PCHAR is the character to use for prompting when reading
  144.    the search string; if not specified (0), it defaults to `:'. */
  145. static void
  146. noninc_search (dir, pchar)
  147.      int dir;
  148.      int pchar;
  149. {
  150.   int saved_point, c, pmtlen;
  151.   char *p;
  152.  
  153.   maybe_save_line ();
  154.   saved_point = rl_point;
  155.  
  156.   /* Use the line buffer to read the search string. */
  157.   rl_line_buffer[0] = 0;
  158.   rl_end = rl_point = 0;
  159.  
  160.   /* XXX - this needs fixing to work with the prompt expansion stuff - XXX */
  161.   pmtlen = (rl_prompt && *rl_prompt) ? strlen (rl_prompt) : 0;
  162.   p = xmalloc (2 + pmtlen);
  163.   if (pmtlen)
  164.     strcpy (p, rl_prompt);
  165.   p[pmtlen] = pchar ? pchar : ':';
  166.   p[pmtlen + 1]  = '\0';
  167.  
  168.   rl_message (p, 0, 0);
  169.   free (p);
  170.  
  171.   /* Read the search string. */
  172.   while (c = rl_read_key ())
  173.     {
  174.       switch (c)
  175.     {
  176.     case CTRL('H'):
  177.     case RUBOUT:
  178.       if (rl_point == 0)
  179.         {
  180.           maybe_unsave_line ();
  181.           rl_clear_message ();
  182.           rl_point = saved_point;
  183.           return;
  184.         }
  185.       rl_rubout (1);
  186.       break;
  187.  
  188.     case CTRL('W'):
  189.       rl_unix_word_rubout ();
  190.       break;
  191.  
  192.     case CTRL('U'):
  193.       rl_unix_line_discard ();
  194.       break;
  195.  
  196.     case RETURN:
  197.     case NEWLINE:
  198.       goto dosearch;
  199.       /* NOTREACHED */
  200.       break;
  201.  
  202.     case CTRL('C'):
  203.     case CTRL('G'):
  204.       maybe_unsave_line ();
  205.       rl_clear_message ();
  206.       rl_point = saved_point;
  207.       ding ();
  208.       return;
  209.  
  210.     default:
  211.       rl_insert (1, c);
  212.       break;
  213.     }
  214.       rl_redisplay ();
  215.     }
  216.  
  217.  dosearch:
  218.   /* If rl_point == 0, we want to re-use the previous search string and
  219.      start from the saved history position.  If there's no previous search
  220.      string, punt. */
  221.   if (rl_point == 0)
  222.     {
  223.       if (!noninc_search_string)
  224.     {
  225.       ding ();
  226.       return;
  227.     }
  228.     }
  229.   else
  230.     {
  231.       /* We want to start the search from the current history position. */
  232.       noninc_history_pos = where_history ();
  233.       if (noninc_search_string)
  234.     free (noninc_search_string);
  235.       noninc_search_string = savestring (rl_line_buffer);
  236.     }
  237.  
  238.   noninc_dosearch (noninc_search_string, dir);
  239. }
  240.  
  241. /* Search forward through the history list for a string.  If the vi-mode
  242.    code calls this, KEY will be `?'. */
  243. rl_noninc_forward_search (count, key)
  244.      int count, key;
  245. {
  246.   if (key == '?')
  247.     noninc_search (1, '?');
  248.   else
  249.     noninc_search (1, 0);
  250.   return 0;
  251. }
  252.  
  253. /* Reverse search the history list for a string.  If the vi-mode code
  254.    calls this, KEY will be `/'. */
  255. rl_noninc_reverse_search (count, key)
  256.      int count, key;
  257. {
  258.   if (key == '/')
  259.     noninc_search (-1, '/');
  260.   else
  261.     noninc_search (-1, 0);
  262.   return 0;
  263. }
  264.  
  265. /* Search forward through the history list for the last string searched
  266.    for.  If there is no saved search string, abort. */
  267. rl_noninc_forward_search_again (count, key)
  268.      int count, key;
  269. {
  270.   if (!noninc_search_string)
  271.     {
  272.       ding ();
  273.       return (-1);
  274.     }
  275.   noninc_dosearch (noninc_search_string, 1);
  276.   return 0;
  277. }
  278.  
  279. /* Reverse search in the history list for the last string searched
  280.    for.  If there is no saved search string, abort. */
  281. rl_noninc_reverse_search_again (count, key)
  282.      int count, key;
  283. {
  284.   if (!noninc_search_string)
  285.     {
  286.       ding ();
  287.       return (-1);
  288.     }
  289.   noninc_dosearch (noninc_search_string, -1);
  290.   return 0;
  291. }
  292.  
  293. static int
  294. rl_history_search_internal (count, direction)
  295.      int count, direction;
  296. {
  297.   HIST_ENTRY *temp, *old_temp;
  298.   int line_len;
  299.  
  300.   maybe_save_line ();
  301.  
  302.   temp = old_temp = (HIST_ENTRY *)NULL;
  303.   while (count)
  304.     {
  305.       temp = (direction < 0) ? previous_history () : next_history ();
  306.       if (!temp)
  307.         break;
  308.       if (STREQN (rl_line_buffer, temp->line, rl_point))
  309.     {
  310.       /* Don't find multiple instances of the same line. */
  311.       if (prev_line_found && STREQ (prev_line_found, temp->line))
  312.         continue;
  313.           if (direction < 0)
  314.             old_temp = temp;
  315.           prev_line_found = temp->line;
  316.           count--;
  317.     }
  318.     }
  319.  
  320.   if (!temp)
  321.     {
  322.       if (direction < 0 && old_temp)
  323.     temp = old_temp;
  324.       else
  325.     {
  326.       maybe_unsave_line ();
  327.       ding ();
  328.       return 1;
  329.     }
  330.     }
  331.  
  332.   line_len = strlen (temp->line);
  333.   if (line_len >= rl_line_buffer_len)
  334.     rl_extend_line_buffer (line_len);
  335.   strcpy (rl_line_buffer, temp->line);
  336.   rl_undo_list = (UNDO_LIST *)temp->data;
  337.   rl_end = line_len;
  338.   return 0;
  339. }
  340.  
  341. /* Search forward in the history for the string of characters
  342.    from the start of the line to rl_point.  This is a non-incremental
  343.    search. */
  344. int
  345. rl_history_search_forward (count, ignore)
  346.      int count, ignore;
  347. {
  348.   if (count == 0)
  349.     return (0);
  350.   if (rl_last_func != rl_history_search_forward)
  351.     prev_line_found = (char *)NULL;
  352.   return (rl_history_search_internal (abs (count), (count > 0) ? 1 : -1));
  353. }
  354.  
  355. /* Search backward through the history for the string of characters
  356.    from the start of the line to rl_point.  This is a non-incremental
  357.    search. */
  358. int
  359. rl_history_search_backward (count, ignore)
  360.      int count, ignore;
  361. {
  362.   if (count == 0)
  363.     return (0);
  364.   if (rl_last_func != rl_history_search_backward)
  365.     prev_line_found = (char *)NULL;
  366.   return (rl_history_search_internal (abs (count), (count > 0) ? -1 : 1));
  367. }
  368.