home *** CD-ROM | disk | FTP | other *** search
/ Nebula 1995 August / NEBULA.bin / SourceCode / libcs / chrarg.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-12-11  |  2.7 KB  |  87 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. /*  chrarg  --  parse character and return its index
  29.  *
  30.  *  Usage:  i = chrarg (ptr,brk,prompt,legals,defalt);
  31.  *    int i;
  32.  *    char **ptr,*brk,*prompt,*legals,defalt;
  33.  *
  34.  *  Chrarg will parse an argument from the string pointed to by "ptr",
  35.  *  bumping ptr to point to the next argument.  The first character
  36.  *  of the arg will be searched for in "legals", and its index
  37.  *  returned; if it is not found, or if there is no argument, then
  38.  *  getchr() will be used to ask the user for a character.
  39.  *  "Brk" is the list of characters which may terminate an argument;
  40.  *  if it is 0, then " " is used.
  41.  *
  42.  *  HISTORY
  43.  * $Log:    chrarg.c,v $
  44.  * Revision 1.2  90/12/11  17:50:49  mja
  45.  *     Add copyright/disclaimer for distribution.
  46.  * 
  47.  * 28-Apr-85  Steven Shafer (sas) at Carnegie-Mellon University
  48.  *    Modified for 4.2 BSD.  Now puts output on stderr.
  49.  *
  50.  * 20-Nov-79  Steven Shafer (sas) at Carnegie-Mellon University
  51.  *    Rewritten for VAX.
  52.  *
  53.  */
  54.  
  55. #include <stdio.h>
  56.  
  57. int strcmp(), getchr();
  58. char *index(),*nxtarg();
  59.  
  60. int chrarg (ptr,brk,prompt,legals,defalt)
  61. char **ptr, *brk, *prompt, *legals, defalt;
  62. {
  63.     register int i;
  64.     register char *arg,*p;
  65.  
  66.     i = -1;            /* bogus value */
  67.     fflush (stdout);
  68.  
  69.     arg = nxtarg (ptr,brk);    /* parse argument */
  70.  
  71.     if (*arg) {        /* there was an arg */
  72.         p = index (legals,*arg);
  73.         if (p) {
  74.             i = p - legals;
  75.         } 
  76.         else if (strcmp("?",arg) != 0) {
  77.             fprintf (stderr,"%s: not valid.  ",arg);
  78.         }
  79.     }
  80.  
  81.     if (i < 0) {
  82.         i = getchr (prompt,legals,defalt);
  83.     }
  84.  
  85.     return (i);
  86. }
  87.