home *** CD-ROM | disk | FTP | other *** search
/ Nebula / nebula.bin / SourceCode / libcs / hexarg.c < prev    next >
C/C++ Source or Header  |  1990-12-11  |  3KB  |  99 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. /*  hexarg  --  parse hexadecimal integer argument
  29.  *
  30.  *  Usage:  i = hexarg (ptr,brk,prompt,min,max,default)
  31.  *    unsigned int i,min,max,default;
  32.  *    char **ptr,*brk,*prompt;
  33.  *
  34.  *  Will attempt to parse an argument from the string pointed to
  35.  *  by "ptr", incrementing ptr to point to the next arg.  If
  36.  *  an arg is found, it is converted into an hexadecimal integer.  If there is
  37.  *  no arg or the value of the arg is not within the range
  38.  *  [min..max], then "gethex" is called to ask the user for an
  39.  *  hexadecimal integer value.
  40.  *  "Brk" is the list of characters which may terminate an argument;
  41.  *  if 0, then " " is used.
  42.  *
  43.  *  HISTORY
  44.  * $Log:    hexarg.c,v $
  45.  * Revision 1.2  90/12/11  17:56:19  mja
  46.  *     Add copyright/disclaimer for distribution.
  47.  * 
  48.  * 28-Apr-85  Steven Shafer (sas) at Carnegie-Mellon University
  49.  *    Modified for 4.2 BSD.  Now puts output on stderr.
  50.  *
  51.  * 20-Nov-79  Steven Shafer (sas) at Carnegie-Mellon University
  52.  *    Created for VAX.
  53.  *
  54.  */
  55.  
  56. #include <ctype.h>
  57. #include <stdio.h>
  58.  
  59. int gethex();
  60. char *nxtarg();
  61. unsigned int atoh();
  62. int strcmp();
  63.  
  64. int hexarg (ptr,brk,prompt,min,max,defalt)
  65. char **ptr;
  66. char *brk,*prompt;
  67. unsigned int min,max,defalt;
  68. {
  69.     register unsigned int i;
  70.     register char *arg,*p;
  71.  
  72.     arg = nxtarg (ptr,brk);
  73.     fflush (stdout);
  74.  
  75.     if (*arg != '\0') {        /* if there was an arg */
  76.         if (arg[0]=='0' && (arg[1]=='x' || arg[1]=='X'))
  77.             strcpy (arg,arg+2);
  78.         for (p=arg; *p && ((*p >= '0' && *p <= '9') ||
  79.             (*p >= 'a' && *p <= 'f') ||
  80.             (*p >= 'A' && *p <= 'F')); p++);
  81.         if (*p) {
  82.             if (strcmp(arg,"?") != 0)  fprintf (stderr,"%s not hexadecimal number.  ",arg);
  83.         } 
  84.         else {
  85.             i = atoh (arg);
  86.             if (i<min || i>max) {
  87.                 fprintf (stderr,"%s%x out of range.  ",
  88.                     (i ? "0x" : ""),i);
  89.             }
  90.         }
  91.     }
  92.  
  93.     if (*arg == '\0' || *p != '\0' || i<min || i>max) {
  94.         i = gethex (prompt,min,max,defalt);
  95.     }
  96.  
  97.     return (i);
  98. }
  99.