home *** CD-ROM | disk | FTP | other *** search
/ Crawly Crypt Collection 2 / crawlyvol2.bin / program / c / ios_src / match.c < prev    next >
C/C++ Source or Header  |  1993-01-31  |  4KB  |  240 lines

  1. /*********************************************************************
  2.  
  3.                     IOS - pattern matcher
  4.                     
  5.     Program : IOSmail
  6.     Source  : free
  7.     Author  : Rinaldo Visscher
  8.     Date    : 1991
  9.     
  10.     Check pattern on 2 string and compare them on byte for byte.
  11. **********************************************************************/
  12.  
  13.  
  14. #include    <stdio.h>
  15. #include    <string.h>
  16. #include    "portab.h"
  17. #include    "defs.h"
  18. #include    "ioslib.h"
  19.  
  20.  
  21. MLOCAL WORD addrmatch(BYTE *first, BYTE *second)
  22. {
  23.     if (!*second) return(!*first);
  24.     
  25.     if (*second == '.')
  26.     {
  27.         while (*first && *first != '.') first++;
  28.         
  29.         if (*first)
  30.             return(addrmatch(++first, ++second));
  31.         else
  32.             return(FALSE);
  33.     }
  34.     
  35.     if (*second == '*')
  36.     {
  37.         if (!*++second) return(TRUE);
  38.         
  39.         do
  40.         {
  41.             if ((*first == *second || *second == '?') &&
  42.                 addrmatch(first+1, second+1))
  43.                 return(TRUE);
  44.         } while (*first++);
  45.     }
  46.     else
  47.         if (*first && (*second == '?' || *second == *first))
  48.             return(addrmatch(++first, ++second));
  49.     
  50.     return(FALSE);
  51. }
  52.  
  53. BOOLEAN newmatch(BYTE *str, BYTE *pattern)
  54. {
  55.     BYTE        c,
  56.                 *cp;
  57.     BOOLEAN        done = FALSE,
  58.                 ret_code,
  59.                 ok;
  60.     
  61.     while (*pattern != EOS && !done && ((*str == EOS &&
  62.             (*pattern == '{' || *pattern == '*')) || *str != EOS))
  63.     {
  64.         switch (*pattern)
  65.         {
  66.             case '\\':
  67.                 
  68.                 pattern++;
  69.                 if (*pattern) pattern++;
  70.                 break;
  71.                 
  72.             case '*':
  73.                 
  74.                 pattern++;
  75.                 ret_code = FALSE;
  76.                 while (*str && (ret_code = newmatch(str++, pattern)) == FALSE);
  77.                 if (ret_code)
  78.                 {
  79.                     while (*str) str++;
  80.                     while (*pattern) pattern++;
  81.                 }
  82.                 break;
  83.                 
  84.             case '[':
  85.                 
  86.                 pattern++;
  87.                 
  88.                 repeat:
  89.                 
  90.                 if (*pattern == EOS || *pattern == ']')
  91.                 {
  92.                     done = TRUE;
  93.                     break;
  94.                 }
  95.                 
  96.                 if (*pattern == '\\')
  97.                 {
  98.                     pattern++;
  99.                     if (*pattern == EOS)
  100.                     {
  101.                         done = TRUE;
  102.                         break;
  103.                     }
  104.                 }
  105.                 
  106.                 if (*(pattern+1) == '-')
  107.                 {
  108.                     c = *pattern;
  109.                     pattern += 2;
  110.                     if (*pattern == ']')
  111.                     {
  112.                         done = TRUE;
  113.                         break;
  114.                     }
  115.                     if (*pattern == '\\')
  116.                     {
  117.                         pattern++;
  118.                         if (*pattern == EOS)
  119.                         {
  120.                             done = TRUE;
  121.                             break;
  122.                         }
  123.                     }
  124.                     if (*str < c || *str > *pattern)
  125.                     {
  126.                         pattern++;
  127.                         goto repeat;
  128.                     }
  129.                 }
  130.                 else if (*pattern != *str)
  131.                 {
  132.                     pattern++;
  133.                     goto repeat;
  134.                 }
  135.                 
  136.                 pattern++;
  137.                 
  138.                 while (*pattern && *pattern != ']')
  139.                 {
  140.                     if (*pattern == '\\' && *(pattern+1)) pattern++;
  141.                     pattern++;
  142.                 }
  143.                 
  144.                 if (*pattern)
  145.                 {
  146.                     pattern++;
  147.                     str++;
  148.                 }
  149.                 
  150.                 break;
  151.                 
  152.             case '?':
  153.                 
  154.                 pattern++;
  155.                 str++;
  156.                 break;
  157.                 
  158.             case '{':
  159.                 
  160.                 pattern++;
  161.                 
  162.                 while (*pattern != '{' && *pattern)
  163.                 {
  164.                     cp = str;
  165.                     ok = TRUE;
  166.                     
  167.                     while (ok && *cp && *pattern && *pattern != ',' && *pattern != '}')
  168.                     {
  169.                         if (*pattern == '\\') pattern++;
  170.                         ok = (*pattern == *cp);
  171.                         cp++;
  172.                         pattern++;
  173.                     }
  174.                     
  175.                     if (*pattern == EOS)
  176.                     {
  177.                         ok = FALSE;
  178.                         done = TRUE;
  179.                         break;
  180.                     }
  181.                     else if (ok)
  182.                     {
  183.                         str = cp;
  184.                         
  185.                         while (*pattern != '}' && *pattern)
  186.                         {
  187.                             pattern++;
  188.                             if (*pattern == '\\')
  189.                             {
  190.                                 pattern++;
  191.                                 if (*pattern == '}') pattern++;
  192.                             }
  193.                         }
  194.                     }
  195.                     else
  196.                     {
  197.                         while (*pattern != '}' && *pattern != ',' && *pattern)
  198.                         {
  199.                             pattern++;
  200.                             if (*pattern == '\\')
  201.                             {
  202.                                 pattern++;
  203.                                 if (*pattern == '}' || *pattern == ',') pattern++;
  204.                             }
  205.                         }
  206.                     }
  207.                     
  208.                     if (*pattern) pattern++;
  209.                 }
  210.                 
  211.                 break;
  212.                 
  213.             default:
  214.                 
  215.                 if (*str == *pattern)
  216.                 {
  217.                     str++;
  218.                     pattern++;
  219.                 }
  220.                 else
  221.                     done = TRUE;
  222.         }
  223.     }
  224.     
  225.     while (*pattern == '*') pattern++;
  226.     
  227.     return(*str == EOS && *pattern == EOS);
  228. }
  229.  
  230. WORD match(BYTE *first, BYTE *second)
  231. {
  232.     BYTE    f[128],
  233.             s[128];
  234.     
  235.     strcpy(f, expand(first));
  236.     strcpy(s, expand(second));
  237.     
  238.     return(newmatch(f, s));
  239. }
  240.