[comp.sys.amiga] Wildcard Support in dos.library

AXTBF%ALASKA.BITNET@cunyvm.cuny.edu (Tim Friest - programmer at large) (02/22/89)

Where is the AmigaDOS equivilent to STR$MATCH_WILD (in VMS)??
I know, there ain't one.  Why not?!   (don't tell me that ARP has one,
because that ain't good enough).

So, AmigaDOS 1.4 has one right (HINT HINT HINT)!!!
If not, I'll do it if you'll tell me you'll use it (this for CATS)...
comments to: AXTBF@ALASKA.BITNET
diclaimer: You heard it from me, no one else...

peter@sugar.uu.net (Peter da Silva) (02/23/89)

In article <9303@louie.udel.EDU>, AXTBF%ALASKA.BITNET@cunyvm.cuny.edu (Tim Friest - programmer at large) writes:
> Where is the AmigaDOS equivilent to STR$MATCH_WILD (in VMS)??

-----8<----- it slices, it dices, it juliennes ------------------------------
echo x - PatMatch.c
sed 's/^X//' > PatMatch.c << '//END'
X/* PatMatch.c - Implements AmigaDos Regular Expression Pattern Matching.
X**
X**  This program will test whether a string is an AmigaDos  regular expression
X**  It may be used to implement wild expressions such as:
X**
X**    "copy #?.c to <dir>" to copy any file ending in .c
X**
X**  The program has two entry points: CmplPat, and Match.
X**
X**    CmplPat - takes a pattern and returns an auxilliary integer vector
X**              which is used by Match.  The pattern is not modified in
X**              any way.  CmplPat returns 1 if no errors were detected
X**              while compiling the pattern; otherwise it returns 0;
X**
X**    Match   - takes the pattern, the auxilliary vector, and the string
X**              to be matched.  It returns 1 if the string matches the
X**              pattern; otherwise it returns 0;
X**
X**  Translated from BCPL by:
X**              Jeff Lydiatt
X**              Richmond B.C. Canada
X**              16 May 1986.
X**
X**  Source: "A Compact Function for Regular Expression Pattern Matching",
X**           Software - Practice and Experience, September 1979.
X**
X**  Useage:
X**     To test if "file.c" matches the regular expression "#?.c"
X**     char *Pat = "#?.c";
X**     char *Str = "file.c";
X**     WORD Aux[128];
X**
X**     if ( CmplPat( Pat, Aux ) == 0 )
X**        {
X**           printf("Bad Wildcard Expression\n");
X**           exit(1);
X**        }
X**     if ( Match( Pat, Aux, Str ) == 1 )
X**        printf("String matches the pattern\n");
X**     else
X**        printf("String does NOT match the pattern\n");
X**/
X
X/*--- Included files ----*/
X
X#include <stdio.h>
X#include <exec/types.h>
X#include <ctype.h>
X
X#define  EOS '\0'
X
X/*--- Global Variables  ---*/
X
Xstatic char     Ch;      /* The current character in Pattern */
Xstatic char     *Pat;    /* Pointer to the Pattern */
Xstatic int      *Aux;    /* Pointer to returned auxilliary vector */
Xstatic int      PatP;    /* Current position in Pat */
Xstatic int      Patlen;  /* strlen(pat) */
Xstatic BOOL     Errflag; /* TRUE if error */
Xstatic int      *Work;   /* Pointer to Active work area */
Xstatic int      Wp;      /* Current position in work */
Xstatic BOOL     Succflag;/* True if "str" matches "pat" */
X
X/*----------------------------------------------------------------*/
X/*                   The Interpreter                              */
X/*----------------------------------------------------------------*/
X
Xstatic void Put(N)
Xint N;
X{
X   register int *ip;
X   register int *to;
X
X   if ( N == 0 )
X      Succflag = TRUE;
X   else
X      {
X	for ( ip = &Work[ 1 ], to = &Work[ Wp ]; ip <= to; ip++)
X	   if ( *ip == N )
X	      return;
X	Work[ ++Wp ] = N;
X      }
X}
X
Xint Match( Pat, Aux, Str )
Xchar Pat[];
Xint  Aux[];
Xchar Str[];
X{
X   static int W[ 128 ];
X   int  S = 0;
X   int  I, N, Q, P, Strlength;
X   char K;
X   int  strlen();
X   void Put();
X
X   Work = W;
X   Wp = 0;
X   Succflag = FALSE;
X   Strlength = strlen( Str );
X   Put( 1 );
X
X   if ( Aux[ 0 ] != 0 )
X      Put( Aux[ 0 ] );
X
X   for(;;)
X      {
X        /* First complete the closure */
X        for( N=1; N <= Wp; N++ )
X          {
X	     P = Work[ N ];
X	     K = Pat[ P-1 ];
X	     Q = Aux[ P ];
X	     switch( K )
X	   	{
X		  case '#': Put( P + 1 );
X		  case '%': Put( Q );
X		  default : break;
X		  case '(':
X		  case '|': Put( P + 1);
X			    if ( Q != 0 )
X			       Put( Q );
X		}
X	   }
X
X	if ( S >= Strlength )
X	   return Succflag;
X	if ( Wp == 0 )
X	   return FALSE;
X	Ch = Str[ S++ ];
X
X	/* Now deal with the match items */
X
X	N = Wp;
X	Wp = 0;
X	Succflag = FALSE;
X
X	for ( I = 1; I <= N; I++)
X	  {
X	     P = Work[ I ];
X	     K = Pat[ P - 1 ];
X	     switch( K )
X	       {
X		 case '#':
X		 case '|':
X		 case '%':
X		 case '(': break;
X		 case '\'': K = Pat[ P ];
X		 default : if ( _toupper( Ch ) != _toupper( K ) )
X			      break;
X		 case '?': /* Successful match */
X		 	   Put ( Aux[ P ] );
X		} /* End Switch */
X	  } /* End For I */
X     } /* End for(;;) */
X}
X
X
X/*----------------------------------------------------------------*/
X/*                     The Compiler                               */
X/*----------------------------------------------------------------*/
X
Xstatic void  Rch() /* Read next character from Pat */
X{
X   if ( PatP >= Patlen )
X      Ch = EOS;
X   else
X      Ch = Pat[ PatP++ ];
X}
X
Xstatic void Nextitem() /* Get next char from Pat; recognize the ' escape char */
X{
X   if ( Ch == '\'' )
X      Rch();
X   Rch();
X}
X
Xstatic void Setexits( List, Val )
Xint List;
Xint Val;
X{
X   int A;
X
X   do
X     {
X	A = Aux[ List ];
X	Aux[ List ] = Val;
X	List = A;
X     }
X	while ( List != 0 );
X}
X
Xstatic int Join( A, B )
Xint A, B;
X{
X    int T = A;
X
X    if ( A == 0 )
X	return B;
X    while ( Aux[ A ] != 0 )
X	A = Aux[ A ];
X    Aux[ A ] = B;
X    return T;
X}
X
Xstatic int Prim()      /* Parse a Prim symbol */
X{
X   int   A = PatP;
X   char Op = Ch;
X   int  Exp();
X   void Setexits(), Nextitem();
X
X   Nextitem();
X   switch( Op )
X     {
X        case EOS:
X        case ')':
X        case '|': Errflag = TRUE;
X        default : return A;
X        case '#': Setexits( Prim(), A ); return A;
X        case '(': A = Exp( A );
X		  if ( Ch != ')' )
X		    {
X			Errflag = TRUE;
X		    }
X		  Nextitem();
X		  return A;
X     }
X}
X
Xstatic int Exp( AltP )    /* Parse an expression */
Xint AltP;
X{
X   int Exits = 0;
X   int A;
X   int Prim(), Exits(), Join();
X   void Nextitem(), Setexits();
X
X   for (;;)
X	{
X	   A = Prim();
X	   if ( Ch == '|' || Ch == ')' || Ch == EOS )
X	      {
X		Exits = Join( Exits, A );
X		if ( Ch != '|' )
X		   return Exits;
X		Aux[ AltP ] = PatP;
X		AltP = PatP;
X		Nextitem();
X	      }
X	   else
X	      Setexits( A, PatP );
X	}
X}
X
Xint CmplPat( Pattern, CmplPattern)
Xchar Pattern[];
Xint  CmplPattern[];
X{
X   int i, strlen();
X   void Rch(), Setexits();
X
X   Pat = Pattern;
X   Aux = CmplPattern;
X   PatP = 0;
X   Patlen = strlen( Pat );
X   Errflag = FALSE;
X
X   for ( i = 0; i <= Patlen; i++ )
X      Aux[ i ] = 0;
X   Rch();
X   Setexits( Exp(0), 0 );
X   return (!Errflag);
X}
//END
-- 
Peter "Have you hugged your wolf today" da Silva  `-_-'  Hackercorp.
...texbell!sugar!peter, or peter@sugar.uu.net      'U`

AXTBF%ALASKA.BITNET@cunyvm.cuny.edu (Tim Friest - programmer at large) (02/24/89)

>microsoft!w-colinp@uunet.UU.NET

>You wrote:
>> Where is the AmigaDOS equivilent to STR$MATCH_WILD (in VMS)??
>> I know, there ain't one.  Why not?!   (don't tell me that ARP has one,
>> because that ain't good enough).

>*Why* isn't the ARP one good enough?

ARP is not good enough because I don't have it on my system.  A lot of people
don't have it on their system.  I don't want to right programs for general
distribution that don't work on a lot (a majority) of systems.  If there
was a routine provided with DOS, I could use it, all the DOS commands that
use pattern matching (or those that should and don't like protect), would
be able to use it without growing in size (there should be an overall savings
in disk space).
I didn't mean that ARP did not handle string matching well....

>(BTW, what are the semantics of STR$MATCH_WILD? If it just does a pattern
>match in two strings, *IT DOESN'T BELONG IN DOS*.  It's a general string
>globbing routine, and has nothing in particular to do with file I/O.

Yep that's it: if (str$match_wild(string, pattern) == STR$_MATCH) then

>As a utility, sure, but it belongs in dos.library even less than LoadSeg,
>CreateProc, and Execute.

I don't see where you want to do string pattern matching using the DOS
string pattern matching patterns outside of DOS???

>If CATS wanted one, they could easily get the one from ARP.  Or Henry
>Spencer's regular expression library.  You do have a point that it's a
>useful utility.  Maybe someone should come up with a utils.library.

My vote is for CATS to get one, or write one, or have me write one, or
have you write one... and add it to the OS where it belongs!  They can
decide if it goes in EXEC.LIBRARY or DOS.LIBRARY or whatever.library.
Having it come with the distribution disks would make it usable.

>--
>    -Colin (uunet!microsoft!w-colinp)

Tim Friest = AXTBF@ALASKA.BITNET

Disclaimer: Quote from my employer, "What's an Amiga?"

disd@hubcap.UUCP (Gary Heffelfinger) (02/25/89)

From article <9499@louie.udel.EDU>, by AXTBF%ALASKA.BITNET@cunyvm.cuny.edu (Tim Friest - programmer at large):
>>microsoft!w-colinp@uunet.UU.NET
> 
>>*Why* isn't the ARP one good enough?
> 
> ARP is not good enough because I don't have it on my system.  A lot of people
> don't have it on their system.  I don't want to right programs for general
> distribution that don't work on a lot (a majority) of systems.  If there

But, the ARP library is not all that large and I'm sure it wouldn't be
tough to get permission to distribute it with your program.  A script
used to install your program could install the appropriate library in
LIBS: just as easily.

> was a routine provided with DOS, I could use it, all the DOS commands that
> use pattern matching (or those that should and don't like protect), would
> be able to use it without growing in size (there should be an overall savings
> in disk space).

I won't argue with that.  I just don't see why it is a problem to
include the arp.library in your distribution.



Gary



-- 
Gary R Heffelfinger   -  Not speaking for Clemson University           
disd@hubcap.clemson.edu       -- FIX the Holodeck --
       Furman Paladins --- National Champs!!