home *** CD-ROM | disk | FTP | other *** search
/ Nebula / nebula.bin / SourceCode / libcs / stablk.c < prev    next >
C/C++ Source or Header  |  1990-12-11  |  5KB  |  140 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. /*  stablk  --  string table lookup
  29.  *
  30.  *  Usage:  i = stablk (arg,table,quiet);
  31.  *
  32.  *    int i;
  33.  *    char *arg,**table;
  34.  *    int quiet;
  35.  *
  36.  *  Stablk looks for a string in "table" which matches
  37.  *  "arg".  Table is declared like this:
  38.  *    char *table[] = {"string1","string2",...,0};
  39.  *  Each string in the table is checked via stablk() to determine
  40.  *  if its initial characters match arg.  If exactly one such
  41.  *  string matches arg, then the index of that string is returned.
  42.  *  If none match arg, or if several match, then -1 (respectively -2)
  43.  *  is returned.  Also, for either of these errors, if quiet is
  44.  *  FALSE, the user will be asked if he wants a list of the possible
  45.  *  strings.  In the case of multiple matches, the matching strings
  46.  *  will be marked specially.
  47.  *
  48.  *  HISTORY
  49.  * $Log:    stablk.c,v $
  50.  * Revision 1.2  90/12/11  17:59:32  mja
  51.  *     Add copyright/disclaimer for distribution.
  52.  * 
  53.  * 28-Apr-85  Steven Shafer (sas) at Carnegie-Mellon University
  54.  *    Modified for 4.2 BSD.  Now puts output on std. error using fprintf and
  55.  *    fprstab.
  56.  *
  57.  * 08-Sep-81  Steven Shafer (sas) at Carnegie-Mellon University
  58.  *    Now handles case of multiple exact matches just like case of
  59.  *    multiple initial-substring matches:  returns -2 if "quiet", else
  60.  *    asks user which one (as if it matters).
  61.  *
  62.  * 19-May-80  Steven Shafer (sas) at Carnegie-Mellon University
  63.  *    Added exactmatch and code to recognize exact match in case of
  64.  *    ambiguity from initial prefix matching.
  65.  *
  66.  * 16-Apr-80  Steven Shafer (sas) at Carnegie-Mellon University
  67.  *    Changed listing code to use prstab() instead of just printing
  68.  *    table -- this uses multiple columns when appropriate.  To do this,
  69.  *    it was necessary to add the "matches" array.  Too bad!
  70.  *
  71.  * 20-Nov-79  Steven Shafer (sas) at Carnegie-Mellon University
  72.  *    Rewritten for VAX from Ken Greer's routine.  The error messages are
  73.  *    different now.
  74.  *
  75.  *  Originally from klg (Ken Greer) on IUS/SUS UNIX.
  76.  */
  77.  
  78. #include <c.h>
  79. #include <strings.h>
  80. #include <stdio.h>
  81.  
  82. #define NOTFOUND -1
  83. #define AMBIGUOUS -2
  84. #define MAXSTRINGS 500
  85.  
  86. int stlmatch();
  87. int strcmp();
  88.  
  89. int stablk (arg,table,quiet)
  90. char *arg, **table;
  91. int quiet;
  92. {
  93.     register int i,ix,count;
  94.     int wantlist;
  95.     char *matches[MAXSTRINGS];
  96.     int exactmatch;
  97.  
  98.     count = 0;
  99.     exactmatch = 0;
  100.     for (i=0; table[i] != 0 && exactmatch == 0; i++) {
  101.         if (stlmatch (table[i],arg)) {
  102.             ix = i;        /* index of last match */
  103.             matches[count++] = table[i];
  104.             if (strcmp(table[i],arg) == 0)  exactmatch = 1;
  105.         }
  106.     }
  107.     matches[count] = 0;
  108.  
  109.     if (exactmatch) {    /* i-th entry is exact match */
  110.         --i;        /* (actually, i-1th entry) */
  111.         matches[0] = table[i];
  112.         count = 1;
  113.         for (i=i+1; table[i] != 0; i++) {
  114.             if (strcmp(table[i],arg) == 0)  {
  115.                 matches[count++] = table[i];
  116.                 ix = i;
  117.             }
  118.         }
  119.         matches[count] = 0;
  120.     }
  121.  
  122.     if (count == 1)  return (ix);
  123.  
  124.     if (!quiet) {
  125.         if (strcmp(arg,"?") == 0) {
  126.             wantlist = TRUE;
  127.         }
  128.         else {
  129.             fprintf (stderr,"%s is %s.  ",arg,(count ? "ambiguous" : "unknown"));
  130.             wantlist = getbool ("Do you want a list?",TRUE);
  131.         }
  132.         if (wantlist) {
  133.             fprintf (stderr,"Must match one of these:\n");
  134.             if (count)  fprstab (stderr,matches);
  135.             else        fprstab (stderr,table);
  136.         }
  137.     }
  138.     return (count ? AMBIGUOUS : NOTFOUND);
  139. }
  140.