home *** CD-ROM | disk | FTP | other *** search
/ Nebula / nebula.bin / SourceCode / libcs / getchr.c < prev    next >
C/C++ Source or Header  |  1990-12-11  |  3KB  |  86 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. /*  getchr  --  ask user to select a character
  29.  *
  30.  *  Usage:  i = getchr (prompt,legals,defalt)
  31.  *    int i;
  32.  *    char *prompt, *legals, defalt;
  33.  *
  34.  *  Prints the message:        prompt  (legals)  [defalt]
  35.  *  and allows the user to type a line.  The first character
  36.  *  the user types is searched for in the string "legals"; if it
  37.  *  is there, its index is returned; otherwise, the user must
  38.  *  try again.  If the user just types carriage return, the
  39.  *  "defalt" character will be searched for.  In any case, note
  40.  *  that is the INDEX (i.e. 0, 1, 2, ...) of the
  41.  *  character that is returned.
  42.  *  On error or EOF in the standard input, the default is used.
  43.  *
  44.  *  HISTORY
  45.  * $Log:    getchr.c,v $
  46.  * Revision 1.2  90/12/11  17:53:49  mja
  47.  *     Add copyright/disclaimer for distribution.
  48.  * 
  49.  * 28-Apr-85  Steven Shafer (sas) at Carnegie-Mellon University
  50.  *    Modified for 4.2 BSD.  Now uses stderr for output.
  51.  *
  52.  * 23-Oct-82  Steven Shafer (sas) at Carnegie-Mellon University
  53.  *    Added code to use default value on EOF or error in standard input.
  54.  *
  55.  * 20-Nov-79  Steven Shafer (sas) at Carnegie-Mellon University
  56.  *    Rewritten for VAX.
  57.  *
  58.  */
  59.  
  60. #include <stdio.h>
  61. char *index();
  62.  
  63. int getchr (prompt, legals, defalt)
  64. char *prompt, *legals, defalt;
  65. {
  66.     register int i;
  67.     register char *p;
  68.     char input [200];
  69.  
  70.     fflush (stdout);
  71.     do {
  72.         fprintf (stderr,"%s  (%s)  [%c]  ",prompt,legals,defalt);
  73.         fflush (stderr);
  74.         if (gets (input) == NULL)  *input = defalt;
  75.         if (*input == '\0')  *input = defalt;
  76.         p = index (legals, *input);
  77.         if (p == 0)
  78.             fprintf (stderr,"Must be one of: %s\n",legals);
  79.         else
  80.             i = (p - legals);
  81.     } 
  82.     while (p == 0);
  83.  
  84.     return (i);
  85. }
  86.