home *** CD-ROM | disk | FTP | other *** search
/ The Net: Ultimate Internet Guide / WWLCD1.ISO / pc / java / in4wjcxu / other / irc / common / match.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-08-14  |  9.4 KB  |  328 lines

  1. /************************************************************************
  2.  *   IRC - Internet Relay Chat, common/match.c
  3.  *   Copyright (C) 1990 Jarkko Oikarinen
  4.  *
  5.  *   This program is free software; you can redistribute it and/or modify
  6.  *   it under the terms of the GNU General Public License as published by
  7.  *   the Free Software Foundation; either version 1, or (at your option)
  8.  *   any later version.
  9.  *
  10.  *   This program is distributed in the hope that it will be useful,
  11.  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13.  *   GNU General Public License for more details.
  14.  *
  15.  *   You should have received a copy of the GNU General Public License
  16.  *   along with this program; if not, write to the Free Software
  17.  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18.  */
  19.  
  20. #ifndef lint
  21. static  char sccsid[] = "%W% %G% (C) 1988 University of Oulu, \
  22. Computing Center and Jarkko Oikarinen";
  23. #endif
  24.  
  25. #include "struct.h"
  26. #include "common.h"
  27. #include "sys.h"
  28.  
  29.  
  30. static    int    calls = 0;
  31. #define    MAX_CALLS    200
  32. /*
  33. **  Compare if a given string (name) matches the given
  34. **  mask (which can contain wild cards: '*' - match any
  35. **  number of chars, '?' - match any single character.
  36. **
  37. **    return    0, if match
  38. **        1, if no match
  39. */
  40.  
  41. /*
  42. ** _match()
  43. ** Iterative matching function, rather than recursive.
  44. ** Written by Douglas A Lewis (dalewis@acsu.buffalo.edu)
  45. */
  46.  
  47. static    int    _match(mask, name)
  48. char    *mask, *name;
  49. {
  50.     Reg1    u_char    *m = (u_char *)mask, *n = (u_char *)name;
  51.     char    *ma = mask, *na = name;
  52.     int    wild = 0, q = 0;
  53.  
  54.     while (1)
  55.         {
  56.         if (calls++ > MAX_CALLS)
  57.             return 1;
  58.         if (*m == '*')
  59.            {
  60.             while (*m == '*')
  61.                 m++;
  62.             wild = 1;
  63.             ma = (char *)m;
  64.             na = (char *)n;
  65.             }
  66.  
  67.         if (!*m)
  68.             {
  69.               if (!*n)
  70.                 return 0;
  71.               for (m--; (m > (u_char *)mask) && (*m == '?'); m--)
  72.                 ;
  73.             if ((*m == '*') && (m > (u_char *)mask) &&
  74.                 (m[-1] != '\\'))
  75.                 return 0;
  76.             if (!wild) 
  77.                 return 1;
  78.             m = (u_char *)ma;
  79.             n = (u_char *)++na;
  80.             }
  81.         else if (!*n)
  82.             {
  83.             while(*m == '*')
  84.                 m++;
  85.             return (*m != 0);
  86.             }
  87.         if ((*m == '\\') && ((m[1] == '*') || (m[1] == '?')))
  88.             {
  89.             m++;
  90.             q = 1;
  91.             }
  92.         else
  93.             q = 0;
  94.  
  95.         if ((tolower(*m) != tolower(*n)) && ((*m != '?') || q))
  96.             {
  97.             if (!wild)
  98.                 return 1;
  99.             m = (u_char *)ma;
  100.             n = (u_char *)++na;
  101.             }
  102.         else
  103.             {
  104.             if (*m)
  105.                 m++;
  106.             if (*n)
  107.                 n++;
  108.             }
  109.         }
  110. }
  111.  
  112. /*
  113.  * External interfaces to the above matching routine.
  114.  */
  115. int    match(ma, na)
  116. char    *ma, *na;
  117. {
  118.     calls = 0;
  119.     return _match(ma, na);
  120. }
  121.  
  122. int    matches(ma, na)
  123. char    *ma,*na;
  124. {
  125.     int    r;
  126.  
  127.     calls = 0;
  128.     return _match(ma, na);
  129. }
  130.  
  131.  
  132. /*
  133. ** collapse a pattern string into minimal components.
  134. ** This particular version is "in place", so that it changes the pattern
  135. ** which is to be reduced to a "minimal" size.
  136. */
  137. char    *collapse(pattern)
  138. char    *pattern;
  139. {
  140.     Reg1    char    *s = pattern, *s1, *t;
  141.  
  142.     if (BadPtr(pattern))
  143.         return pattern;
  144.     /*
  145.      * Collapse all \** into \*, \*[?]+\** into \*[?]+
  146.      */
  147.     for (; *s; s++)
  148.         if (*s == '\\')
  149.             if (!*(s + 1))
  150.                 break;
  151.             else
  152.                 s++;
  153.         else if (*s == '*')
  154.             {
  155.             if (*(t = s1 = s + 1) == '*')
  156.                 while (*t == '*')
  157.                     t++;
  158.             else if (*t == '?')
  159.                 for (t++, s1++; *t == '*' || *t == '?'; t++)
  160.                     if (*t == '?')
  161.                         *s1++ = *t;
  162.             while ((*s1++ = *t++))
  163.                 ;
  164.             }
  165.     return pattern;
  166. }
  167.  
  168.  
  169. /*
  170. **  Case insensitive comparison of two NULL terminated strings.
  171. **
  172. **    returns     0, if s1 equal to s2
  173. **        <0, if s1 lexicographically less than s2
  174. **        >0, if s1 lexicographically greater than s2
  175. */
  176. int    mycmp(s1, s2)
  177. char    *s1;
  178. char    *s2;
  179.     {
  180.     Reg1    unsigned char    *str1 = (unsigned char *)s1;
  181.     Reg2    unsigned char    *str2 = (unsigned char *)s2;
  182.     Reg3    int    res;
  183.  
  184.     while ((res = toupper(*str1) - toupper(*str2)) == 0)
  185.         {
  186.         if (*str1 == '\0')
  187.             return 0;
  188.         str1++;
  189.         str2++;
  190.         }
  191.     return (res);
  192.     }
  193.  
  194.  
  195. int    myncmp(str1, str2, n)
  196. char    *str1;
  197. char    *str2;
  198. int    n;
  199.     {
  200.     Reg1    unsigned char    *s1 = (unsigned char *)str1;
  201.     Reg2    unsigned char    *s2 = (unsigned char *)str2;
  202.     Reg3    int        res;
  203.  
  204.     while ((res = toupper(*s1) - toupper(*s2)) == 0)
  205.         {
  206.         s1++; s2++; n--;
  207.         if (n == 0 || (*s1 == '\0' && *s2 == '\0'))
  208.             return 0;
  209.         }
  210.     return (res);
  211.     }
  212.  
  213.  
  214. unsigned char tolowertab[] =
  215.         { 0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, 0x9, 0xa,
  216.           0xb, 0xc, 0xd, 0xe, 0xf, 0x10, 0x11, 0x12, 0x13, 0x14,
  217.           0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d,
  218.           0x1e, 0x1f,
  219.           ' ', '!', '"', '#', '$', '%', '&', 0x27, '(', ')',
  220.           '*', '+', ',', '-', '.', '/',
  221.           '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
  222.           ':', ';', '<', '=', '>', '?',
  223.           '@', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i',
  224.           'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's',
  225.           't', 'u', 'v', 'w', 'x', 'y', 'z', '{', '|', '}', '~',
  226.           '_',
  227.           '`', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i',
  228.           'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's',
  229.           't', 'u', 'v', 'w', 'x', 'y', 'z', '{', '|', '}', '~',
  230.           0x7f,
  231.           0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89,
  232.           0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f,
  233.           0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99,
  234.           0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f,
  235.           0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7, 0xa8, 0xa9,
  236.           0xaa, 0xab, 0xac, 0xad, 0xae, 0xaf,
  237.           0xb0, 0xb1, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7, 0xb8, 0xb9,
  238.           0xba, 0xbb, 0xbc, 0xbd, 0xbe, 0xbf,
  239.           0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7, 0xc8, 0xc9,
  240.           0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf,
  241.           0xd0, 0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7, 0xd8, 0xd9,
  242.           0xda, 0xdb, 0xdc, 0xdd, 0xde, 0xdf,
  243.           0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9,
  244.           0xea, 0xeb, 0xec, 0xed, 0xee, 0xef,
  245.           0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9,
  246.           0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff };
  247.  
  248. unsigned char touppertab[] =
  249.         { 0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, 0x9, 0xa,
  250.           0xb, 0xc, 0xd, 0xe, 0xf, 0x10, 0x11, 0x12, 0x13, 0x14,
  251.           0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d,
  252.           0x1e, 0x1f,
  253.           ' ', '!', '"', '#', '$', '%', '&', 0x27, '(', ')',
  254.           '*', '+', ',', '-', '.', '/',
  255.           '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
  256.           ':', ';', '<', '=', '>', '?',
  257.           '@', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I',
  258.           'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S',
  259.           'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '[', '\\', ']', '^',
  260.           0x5f,
  261.           '`', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I',
  262.           'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S',
  263.           'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '[', '\\', ']', '^',
  264.           0x7f,
  265.           0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89,
  266.           0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f,
  267.           0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99,
  268.           0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f,
  269.           0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7, 0xa8, 0xa9,
  270.           0xaa, 0xab, 0xac, 0xad, 0xae, 0xaf,
  271.           0xb0, 0xb1, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7, 0xb8, 0xb9,
  272.           0xba, 0xbb, 0xbc, 0xbd, 0xbe, 0xbf,
  273.           0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7, 0xc8, 0xc9,
  274.           0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf,
  275.           0xd0, 0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7, 0xd8, 0xd9,
  276.           0xda, 0xdb, 0xdc, 0xdd, 0xde, 0xdf,
  277.           0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9,
  278.           0xea, 0xeb, 0xec, 0xed, 0xee, 0xef,
  279.           0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9,
  280.           0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff };
  281.  
  282. unsigned char char_atribs[] = {
  283. /* 0-7 */    CNTRL, CNTRL, CNTRL, CNTRL, CNTRL, CNTRL, CNTRL, CNTRL,
  284. /* 8-12 */    CNTRL, CNTRL|SPACE, CNTRL|SPACE, CNTRL|SPACE, CNTRL|SPACE,
  285. /* 13-15 */    CNTRL|SPACE, CNTRL, CNTRL,
  286. /* 16-23 */    CNTRL, CNTRL, CNTRL, CNTRL, CNTRL, CNTRL, CNTRL, CNTRL,
  287. /* 24-31 */    CNTRL, CNTRL, CNTRL, CNTRL, CNTRL, CNTRL, CNTRL, CNTRL,
  288. /* space */    PRINT|SPACE,
  289. /* !"#$%&'( */    PRINT, PRINT, PRINT, PRINT, PRINT, PRINT, PRINT, PRINT,
  290. /* )*+,-./ */    PRINT, PRINT, PRINT, PRINT, PRINT, PRINT, PRINT,
  291. /* 0123 */    PRINT|DIGIT, PRINT|DIGIT, PRINT|DIGIT, PRINT|DIGIT,
  292. /* 4567 */    PRINT|DIGIT, PRINT|DIGIT, PRINT|DIGIT, PRINT|DIGIT,
  293. /* 89:; */    PRINT|DIGIT, PRINT|DIGIT, PRINT, PRINT,
  294. /* <=>? */    PRINT, PRINT, PRINT, PRINT,
  295. /* @ */        PRINT,
  296. /* ABC */    PRINT|ALPHA, PRINT|ALPHA, PRINT|ALPHA,
  297. /* DEF */    PRINT|ALPHA, PRINT|ALPHA, PRINT|ALPHA,
  298. /* GHI */    PRINT|ALPHA, PRINT|ALPHA, PRINT|ALPHA,
  299. /* JKL */    PRINT|ALPHA, PRINT|ALPHA, PRINT|ALPHA,
  300. /* MNO */    PRINT|ALPHA, PRINT|ALPHA, PRINT|ALPHA,
  301. /* PQR */    PRINT|ALPHA, PRINT|ALPHA, PRINT|ALPHA,
  302. /* STU */    PRINT|ALPHA, PRINT|ALPHA, PRINT|ALPHA,
  303. /* VWX */    PRINT|ALPHA, PRINT|ALPHA, PRINT|ALPHA,
  304. /* YZ[ */    PRINT|ALPHA, PRINT|ALPHA, PRINT|ALPHA,
  305. /* \]^ */    PRINT|ALPHA, PRINT|ALPHA, PRINT|ALPHA,
  306. /* _   */    PRINT,
  307. /* abc */    PRINT|ALPHA, PRINT|ALPHA, PRINT|ALPHA,
  308. /* def */    PRINT|ALPHA, PRINT|ALPHA, PRINT|ALPHA,
  309. /* ghi */    PRINT|ALPHA, PRINT|ALPHA, PRINT|ALPHA,
  310. /* jkl */    PRINT|ALPHA, PRINT|ALPHA, PRINT|ALPHA,
  311. /* mno */    PRINT|ALPHA, PRINT|ALPHA, PRINT|ALPHA,
  312. /* pqr */    PRINT|ALPHA, PRINT|ALPHA, PRINT|ALPHA,
  313. /* stu */    PRINT|ALPHA, PRINT|ALPHA, PRINT|ALPHA,
  314. /* vwx */    PRINT|ALPHA, PRINT|ALPHA, PRINT|ALPHA,
  315. /* yz{ */    PRINT|ALPHA, PRINT|ALPHA, PRINT|ALPHA,
  316. /* \}~ */    PRINT|ALPHA, PRINT|ALPHA, PRINT|ALPHA,
  317. /* del */    0,
  318. /* 80-8f */    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  319. /* 90-9f */    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  320. /* a0-af */    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  321. /* b0-bf */    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  322. /* c0-cf */    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  323. /* d0-df */    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  324. /* e0-ef */    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  325. /* f0-ff */    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
  326.         };
  327.  
  328.