home *** CD-ROM | disk | FTP | other *** search
/ Oakland CPM Archive / oakcpm.iso / cpm / cpm68k / arc68k.arc / ARCMATCH.C < prev    next >
Encoding:
Text File  |  1987-11-27  |  3.9 KB  |  176 lines

  1. /* -*-c,save-*- */
  2. /*
  3.  *      arcmatch.c      1.1
  4.  *
  5.  *      Author: Thom Henderson
  6.  *      Original System V port: Mike Stump
  7.  *      Enhancements, Bug fixes, and cleanup: Chris Seaman
  8.  *      Date: Fri Mar 20 09:57:02 1987
  9.  *      Last Mod.       3/22/87
  10.  *    removed typedef INT BOOLEAN (turned into typdef short char) 7-5-87
  11.  *    removed all instances of BOOLEAN
  12.  *
  13.  */
  14.  
  15. /*
  16.  * ARC - Archive utility - ARCMATCH
  17.  * 
  18.  * Version 2.17, created on 12/17/85) at 20:32:18
  19.  * 
  20.  * (C) COPYRIGHT 1985 by System Enhancement Associates; ALL RIGHTS RESERVED
  21.  * 
  22.  *     Description:
  23.  *          This file contains service routines needed to maintain an archive.
  24.  */
  25.  
  26. #include "arc.h"
  27. /*  #include <sys/dir.h>        */
  28.  
  29. #define ASTERISK '*'            /* The '*' metacharacter */
  30. #define QUESTION '?'            /* The '?' metacharacter */
  31. #define BACK_SLASH '\\'         /* The '\' metacharacter */
  32. #define LEFT_BRACKET '['        /* The '[' metacharacter */
  33. #define RIGHT_BRACKET ']'       /* The ']' metacharacter */
  34.  
  35. #define IS_OCTAL(ch) (ch >= '0' && ch <= '7')
  36.  
  37. #define VOID short
  38. #define TRUE 1
  39. #define FALSE 0
  40. #define EOS '\000'
  41.  
  42. char nextch();
  43.  
  44. int match(string, pattern)
  45. char *string;
  46. char *pattern;
  47. {
  48.     register int ismatch;
  49.  
  50.     ismatch = FALSE;
  51.     switch (*pattern)
  52.     {
  53.     case ASTERISK:
  54.         pattern++;
  55.         do
  56.         {
  57.             ismatch = match (string, pattern);
  58.         }
  59.         while (!ismatch && *string++ != EOS);
  60.         break;
  61.     case QUESTION:
  62.         if (*string != EOS)
  63.             ismatch = match (++string, ++pattern);
  64.         break;
  65.     case EOS:
  66.         if (*string == EOS)
  67.             ismatch = TRUE;
  68.         break;
  69.     case LEFT_BRACKET:
  70.         if (*string != EOS)
  71.             ismatch = do_list (string, pattern);
  72.         break;
  73.     case BACK_SLASH:
  74.         pattern++;
  75.     default:
  76. #ifdef CPM68K
  77.         if (toupper(*string) == toupper(*pattern))
  78. #else
  79.         if (*string == *pattern)
  80. #endif
  81.         {
  82.             string++;
  83.             pattern++;
  84.             ismatch = match (string, pattern);
  85.         }
  86.         else
  87.             ismatch = FALSE;
  88.         break;
  89.     }
  90.     return(ismatch);
  91. }
  92.  
  93. do_list (string, pattern)
  94. register char *string;
  95. char *pattern;
  96. {
  97.     register  ismatch;
  98.     register  if_found;
  99.     register  if_not_found;
  100.     char lower;
  101.     char upper;
  102.  
  103.     pattern++;
  104.     if (*pattern == '!')
  105.     {
  106.         if_found = FALSE;
  107.         if_not_found = TRUE;
  108.         pattern++;
  109.     }
  110.     else
  111.     {
  112.         if_found = TRUE;
  113.         if_not_found = FALSE;
  114.     }
  115.     ismatch = if_not_found;
  116.     while (*pattern != ']' && *pattern != EOS)
  117.     {
  118.         list_parse(&pattern, &lower, &upper);
  119.         if (*string >= lower && *string <= upper)
  120.         {
  121.             ismatch = if_found;
  122.             while (*pattern != ']' && *pattern != EOS) pattern++;
  123.         }
  124.     }
  125.  
  126.     if (*pattern++ != ']')
  127.         abort("Character class error");
  128.     else
  129.         if (ismatch)
  130.             ismatch = match (++string, pattern);
  131.  
  132.     return(ismatch);
  133. }
  134.  
  135. static VOID list_parse (patp, lowp, highp)
  136. char **patp;
  137. char *lowp;
  138. char *highp;
  139. {
  140.     *lowp = nextch (patp);
  141.     if (**patp == '-')
  142.     {
  143.         (*patp)++;
  144.         *highp = nextch(patp);
  145.     }
  146.     else
  147.         *highp = *lowp;
  148. }
  149.  
  150. char nextch (patp)
  151. char **patp;
  152. {
  153.     register char ch;
  154.     register char chsum;
  155.     register INT count;
  156.  
  157.     ch = *(*patp)++;
  158.     if (ch == '\\')
  159.     {
  160.         ch = *(*patp)++;
  161.         if (IS_OCTAL(ch))
  162.         {
  163.             chsum = 0;
  164.             for (count = 0; count < 3 && IS_OCTAL(ch); count++)
  165.             {
  166.                 chsum *= 8;
  167.                 chsum += ch - '0';
  168.                 ch = *(*patp)++;
  169.             }
  170.             (*patp)--;
  171.             ch = chsum;
  172.         }
  173.     }
  174.     return(ch);
  175. }
  176.