/* wild.c */
/* wild card routine(s) created by Lawrence Foard */
/* Snatched from tinymush for tinymage */
#include <stdio.h>
#include <ctype.h>
#include "config.h"
#include "externs.h"

char *wptr[10] = {NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL};
int wlen[10];
char wbuff[2000];

int wild(char *s, char *d, int p, int os)
{
  switch(*s)                               
    {
    case '?': /* match any character in d, note end of string is considered a match */
      /* if just in nonwildcard state record location of change */
      if (!os && (p < 10))
	wptr[p] = d; 
      return(wild(s + 1, (*d) ? d + 1 : d, p, 1));    
    case '*': /* match a range of characters */
      if (!os && (p < 10))
	{
	  wptr[p] = d;
	}
      return(wild(s + 1, d, p, 1) || ((*d) ? wild(s, d + 1, p, 1) : 0));
    default:
      if (os && (p < 10))
	{
	  wlen[p] = d - wptr[p];
	  p++;
	}
      return((toupper(*s) != toupper(*d)) ? 0 :
	     ((*s) ? wild(s + 1, d + 1, p, 0) : 1));
    }
}

int wild_match(char *s, const char *d)
{
  int a;

  for(a = 0; a < 10; a++)
    wptr[a] = NULL;
  switch(*s)
    {
    case '>':     
      s++;
      /* if both first letters are #'s then numeric compare */
      if (isdigit(s[0]) || (*s == '-'))
	return(atoi(s) < atoi(d));
      else
	return(strcmp(s, d) < 0);
      break;
    case '<':
      s++;
      if (isdigit(s[0]) || (*s == '-'))
	return(atoi(s) > atoi(d));
      else
	return(strcmp(s, d) > 0);
      break;
    default:
      if (wild(s, d, 0, 0))
	{          
	  int a, b;
	  char *e, *f = wbuff;
	  for(a = 0; a < 10; a++)
	    if (e = wptr[a])
	      {      
		wptr[a] = f;
		for(b = wlen[a]; b--; *f++ = *e++);
		*f++ = 0;
	      }
	  return(1);
	}
      else
	return(0);
    }
  return(2);
}