home *** CD-ROM | disk | FTP | other *** search
/ Nebula / nebula.bin / SourceCode / libcs / getstab.c < prev    next >
C/C++ Source or Header  |  1990-12-11  |  3KB  |  90 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. /*  getstab  --  ask user for string in table
  29.  *
  30.  *  Usage:  i = getstab (prompt,table,defalt)
  31.  *    int i;
  32.  *    char *prompt, **table, *defalt;
  33.  *
  34.  *  Getstab 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 stablk() 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 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:    getstab.c,v $
  49.  * Revision 1.2  90/12/11  17:55:53  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.  * 08-Mar-83  Steven Shafer (sas) at Carnegie-Mellon University
  56.  *    Fixed by adding missing "return" statement!  Somehow, it
  57.  *    worked on the VAX anyway ...
  58.  *
  59.  * 23-Oct-82  Steven Shafer (sas) at Carnegie-Mellon University
  60.  *    Added code to use default value on error or EOF in standard input.
  61.  *
  62.  * 20-Nov-79  Steven Shafer (sas) at Carnegie-Mellon University
  63.  *    Rewritten for VAX.  Now lets stablk() do the error-message printing.
  64.  *
  65.  *  Originally by klg (Ken Greer) on IUS/SUS UNIX.
  66.  */
  67.  
  68. #include <strings.h>
  69. #include <stdio.h>
  70. #include <c.h>
  71.  
  72. int getstab (prompt,table,defalt)
  73. char *prompt, **table, *defalt;
  74. {
  75.     char input[200];
  76.     register int ix;
  77.  
  78.     fflush (stdout);
  79.     do {
  80.         fprintf (stderr,"%s  [%s]  ",prompt, defalt);
  81.         fflush (stderr);
  82.         if (gets (input) == NULL)  strcpy (input,defalt);
  83.         if (*input == '\0')  strcpy (input,defalt);
  84.         ix = stablk (input,table,FALSE);
  85.     } 
  86.     while (ix < 0);
  87.  
  88.     return (ix);
  89. }
  90.