home *** CD-ROM | disk | FTP | other *** search
/ Nebula 1995 August / NEBULA.bin / SourceCode / libcs / boolarg.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-12-11  |  2.9 KB  |  97 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. /*  boolarg  --  parse boolean from string
  29.  *
  30.  *  Usage:  i = boolarg (ptr,brk,prompt,defalt);
  31.  *    int i,defalt;
  32.  *    char **ptr,*brk,*prompt;
  33.  *
  34.  *  Boolarg will parse an argument from the string pointed to by "ptr",
  35.  *  bumping ptr to point to the next argument in the string.  The
  36.  *  argument parsed will be converted to a boolean (TRUE if it begins
  37.  *  with 'y' or 'Y'; FALSE for 'n' or 'N').  If there is no
  38.  *  argument, or if it is not a boolean value, then getbool() will
  39.  *  be used to ask the user for a boolean value.  In any event,
  40.  *  the boolean value obtained (from the string or from the user) is
  41.  *  returned.
  42.  *  "Brk" is the list of characters which may terminate an argument;
  43.  *  if 0, then " " is used.
  44.  *
  45.  *  HISTORY
  46.  * $Log:    boolarg.c,v $
  47.  * Revision 1.2  90/12/11  17:50:33  mja
  48.  *     Add copyright/disclaimer for distribution.
  49.  * 
  50.  * 28-Apr-85  Steven Shafer (sas) at Carnegie-Mellon University
  51.  *    Modified for 4.2 BSD.  Now puts output on stderr.
  52.  *
  53.  * 20-Nov-79  Steven Shafer (sas) at Carnegie-Mellon University
  54.  *    Created for VAX.
  55.  *
  56.  */
  57.  
  58. #include <stdio.h>
  59. #include <c.h>
  60.  
  61. int strcmp();
  62. char *nxtarg();
  63. int getbool();
  64.  
  65. int boolarg (ptr,brk,prompt,defalt)
  66. char **ptr,*brk,*prompt;
  67. int defalt;
  68. {
  69.     register char *arg;
  70.     register int valu;
  71.  
  72.     valu = 2;        /* meaningless value */
  73.     fflush (stdout);
  74.  
  75.     arg = nxtarg (ptr,brk);    /* parse an argument */
  76.     if (*arg) {        /* there was an argument */
  77.         switch (*arg) {
  78.         case 'n':
  79.         case 'N':
  80.             valu = FALSE;
  81.             break;
  82.         case 'y':
  83.         case 'Y':
  84.             valu = TRUE;
  85.             break;
  86.         case '?':
  87.             break;
  88.         default:
  89.             fprintf (stderr,"%s not 'yes' or 'no'.  ",arg);
  90.         }
  91.     }
  92.  
  93.     if (valu == 2)  valu = getbool (prompt,defalt);
  94.  
  95.     return (valu);
  96. }
  97.