home *** CD-ROM | disk | FTP | other *** search
/ Nebula / nebula.bin / SourceCode / libcs / getsearch.c < prev    next >
C/C++ Source or Header  |  1990-12-11  |  3KB  |  84 lines

  1. /*
  2.  * Copyright (c) 1990 Carnegie Mellon University
  3.  * All Rights Reserved.
  4.  * 
  5.  * Permission to use, copy, modify and distribute this software and its
  6.  * documentation is hereby granted, provided that both the copyright
  7.  * notice and this permission notice appear in all copies of the
  8.  * software, derivative works or modified versions, and any portions
  9.  * thereof, and that both notices appear in supporting documentation.
  10.  *
  11.  * THE SOFTWARE IS PROVIDED "AS IS" AND CARNEGIE MELLON UNIVERSITY
  12.  * DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL
  13.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS.  IN NO EVENT
  14.  * SHALL CARNEGIE MELLON UNIVERSITY BE LIABLE FOR ANY SPECIAL, DIRECT,
  15.  * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
  16.  * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
  17.  * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
  18.  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  19.  *
  20.  * Users of this software agree to return to Carnegie Mellon any
  21.  * improvements or extensions that they make and grant Carnegie the
  22.  * rights to redistribute these changes.
  23.  *
  24.  * Export of this software is permitted only after complying with the
  25.  * regulations of the U.S. Deptartment of Commerce relating to the
  26.  * Export of Technical Data.
  27.  */
  28. /*  getsearch  --  ask user for string in table
  29.  *
  30.  *  Usage:  i = getsearch (prompt,table,defalt)
  31.  *    int i;
  32.  *    char *prompt, **table, *defalt;
  33.  *
  34.  *  Getsearch prints the messge:   prompt  [defalt]
  35.  *  and asks the user to type in a line.  This input text
  36.  *  is compared to all the strings in the table to see which
  37.  *  (if any) it matches; the stabsearch() routine is used
  38.  *  for the matching.  If the string is ambiguous or invalid
  39.  *  (i.e. matches zero strings, or more than one), the cycle
  40.  *  is repeated.  When a valid string is typed, the index
  41.  *  of the string it matches is returned.  If the user just
  42.  *  types carriage return, the default string is used for matching.
  43.  *  The default value is also used on error or EOF in standard input.
  44.  *  The string table may be declared in this way:
  45.  *     char *table[] = {"string1","string2",...,0};
  46.  *
  47.  *  HISTORY
  48.  * $Log:    getsearch.c,v $
  49.  * Revision 1.2  90/12/11  17:55:23  mja
  50.  *     Add copyright/disclaimer for distribution.
  51.  * 
  52.  * 28-Apr-85  Steven Shafer (sas) at Carnegie-Mellon University
  53.  *    Modified for 4.2 BSD.  Now uses stderr for output.
  54.  *
  55.  * 23-Oct-82  Steven Shafer (sas) at Carnegie-Mellon University
  56.  *    Added code to use default value on EOF or error in standard input.
  57.  *
  58.  * 23-Jan-80  Steven Shafer (sas) at Carnegie-Mellon University
  59.  *    Created.  This is just an alternative to getstab, but with funny
  60.  *    heuristic string-matching and the nifty "May I suggest..." stuff.
  61.  *
  62.  */
  63.  
  64. #include <strings.h>
  65. #include <stdio.h>
  66. #include <c.h>
  67.  
  68. int getsearch (prompt,table,defalt)
  69. char *prompt, **table, *defalt;
  70. {
  71.     char input[200];
  72.     register int ix;
  73.  
  74.     fflush (stdout);
  75.     do {
  76.         fprintf (stderr,"%s  [%s]  ",prompt, defalt);
  77.         fflush (stderr);
  78.         if (gets (input) == NULL)  strcpy (input,defalt);
  79.         if (*input == '\0')  strcpy (input,defalt);
  80.         ix = stabsearch (input,table,FALSE);
  81.     } 
  82.     while (ix < 0);
  83. }
  84.