home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 11 Util / 11-Util.zip / MONITOR1.ZIP / MATCH.C next >
Text File  |  1989-01-19  |  3KB  |  63 lines

  1. #include <stdlib.h>
  2. #include <string.h>
  3.  
  4. #include "match.h"
  5.  
  6.  
  7. /********************************************************************/
  8. /*                                                                  */
  9. /*   FUNCTION:                                                      */
  10. /*      short match(table,token)                                    */
  11. /*                                                                  */
  12. /*   DESCRIPTION:                                                   */
  13. /*      Identifies the position in "table" that matches "token".    */
  14. /*                                                                  */
  15. /*   PARAMETERS:                                                    */
  16. /*      table - pointer to pointers to command names.               */
  17. /*              Must be terminated by a NULL pointer.               */
  18. /*      token - pointer to incoming command.                        */
  19. /*                                                                  */
  20. /*                                                                  */
  21. /*   EXIT:                                                          */
  22. /*      Returns the integer position into "table", or NO_MATCH      */
  23. /*      if not found.                                               */
  24. /*                                                                  */
  25. /*   EXTERNAL EFFECTS:                                              */
  26. /*                                                                  */
  27. /*   WARNINGS:                                                      */
  28. /*                                                                  */
  29. /*   ROUTINES CALLED:                                               */
  30. /*                                                                  */
  31. /********************************************************************/
  32. short match(char *table[], char *token)
  33. {
  34.   short table_pos = 0;              /* Position in table             */
  35.   short token_len;                  /* Length of token to match      */
  36.   short abbrev = 0;                 /* Found an abbreviation flag    */
  37.   short i;                          /* simple counter                */
  38.  
  39.   token_len = strlen(token);
  40.   while (table[table_pos]){         /* As long as something is there */
  41.  
  42.     if ( strcmpi(table[table_pos],token) ){
  43.                                     /* If no match; try for abbrev.  */
  44.       for (i=0; (toupper(*(table[table_pos]+i))==toupper(token[i])) && i<token_len; ++i);
  45.       if (i==token_len){            /* Is it an abbreviation?        */
  46.         if (abbrev){                /* Yes, check if first           */
  47.           abbrev = -1;              /* Already saw an abbreviation   */
  48.         } else {
  49.           abbrev = table_pos+1;     /* Save position+1               */
  50.         } /* endif */
  51.       } /* endif */
  52.       table_pos += 1;               /*   then try next               */
  53.     } else {
  54.       return table_pos;             /* Eureka! we have it.           */
  55.     } /* endif */
  56.   } /* endwhile */
  57.   if (abbrev>0){                    /* Did we see ONE abbreviation?  */
  58.     return abbrev-1;                /* Yes, return the position.     */
  59.   } /* endif */
  60.   return NO_MATCH;                  /* Sorry, Charlie. Better luck
  61.                                        next time                     */
  62. }
  63.