glob.c #3

  • //
  • guest/
  • perforce_software/
  • jam/
  • src/
  • glob.c
  • View
  • Commits
  • Open Download .zip Download (2 KB)
/*
 * Copyright 1994 Christopher Seiwald.  All rights reserved. 
 *
 * This file is part of Jam - see jam.c for Copyright information.
 */

/*
 * glob.c - match a string against a simple pattern
 *
 * Understands the following patterns:
 *
 *	*	any number of characters
 *	?	any single character
 *	[a-z]	any single character in the range a-z
 *	[^a-z]	any single character not in the range a-z
 *	\x	match x
 *	
 * External functions:
 *
 *	glob() - match a string against a simple pattern
 *
 * Internal functions:
 *
 *	globchars() - build a bitlist to check for character group match
 */

# include "jam.h"

# define CHECK_BIT( tab, bit ) ( tab[ (bit)/8 ] & (1<<( (bit)%8 )) )
# define BITLISTSIZE 16	/* bytes used for [chars] in compiled expr */

static void globchars( const char *s, const char *e, char *b );

/*
 * glob() - match a string against a simple pattern
 */

int
glob(
	const char *c,
	const char *s )
{
	char bitlist[ BITLISTSIZE ];
	const char *here;

	for( ;; )
	    switch( *c++ )
	{
	case '\0':
		return *s ? -1 : 0;

	case '?':
		if( !*s++ )
		    return 1;
		break;

	case '[':
		/* scan for matching ] */

		here = c;
		do if( !*c++ )
			return 1;
		while( here == c || *c != ']' );
		c++;

		/* build character class bitlist */

		globchars( here, c, bitlist );

		if( !CHECK_BIT( bitlist, *(unsigned char *)s ) )
			return 1;
		s++;
		break;

	case '*':
		here = s;

		while( *s ) 
			s++;

		/* Try to match the rest of the pattern in a recursive */
		/* call.  If the match fails we'll back up chars, retrying. */

		while( s != here )
		{
			int r;

			/* A fast path for the last token in a pattern */

			r = *c ? glob( c, s ) : *s ? -1 : 0;

			if( !r )
				return 0;
			else if( r < 0 )
				return 1;

			--s;
		}
		break;

	case '\\':
		/* Force literal match of next char. */

		if( !*c || *s++ != *c++ )
		    return 1;
		break;

	default:
		if( *s++ != c[-1] )
		    return 1;
		break;
	}
}

/*
 * globchars() - build a bitlist to check for character group match
 */

static void
globchars( 
	const char *s, 
	const char *e, 
	char *b )
{
	int neg = 0;

	memset( b, '\0', BITLISTSIZE  );

	if( *s == '^') 
		neg++, s++;

	while( s < e )
	{
		int c;

		if( s+2 < e && s[1] == '-' )
		{
			for( c = s[0]; c <= s[2]; c++ )
				b[ c/8 ] |= (1<<(c%8));
			s += 3;
		} else {
			c = *s++;
			b[ c/8 ] |= (1<<(c%8));
		}
	}
			
	if( neg )
	{
		int i;
		for( i = 0; i < BITLISTSIZE; i++ )
			b[ i ] ^= 0377;
	}

	/* Don't include \0 in either $[chars] or $[^chars] */

	b[0] &= 0376;
}
# Change User Description Committed
#4 2493 rmg Rewrite the past: update all jam's source with comments to
reflect changes since about 2.3, very early 2001.

Whitespace only change.

=== computer:1666: Change 37660 by seiwald@play-seiwald on 2002/11/06 22:41:35

Note: I regenerated jamgram.c on my linux 7.3 system prior to
the submit, since patch was so unhappy trying to lay down the
changes from Christopher's change. Presumably this is just due to
different yacc/bison/whatever particulars on the system where
Christopher made the changes originally. - rmg
#3 2491 rmg Some consting in jam to make it more compilable by C++ compilers.

No functional change.

=== computer:1666: Change 37433 by perforce@perforce on 2002/10/30 16:08:51

Recreational const-ing of jam, for compilers that don't allow
"string" to be passed as a non-const char *.

This included a few places where we were modifying what could
possibly have been read-only storage, oddly enough.

No functional change.

=== computer:1666: Change 37602 by seiwald@play-seiwald on 2002/11/04 17:25:40
#2 486 Perforce staff Jam 2.3.
 See RELNOTES for a list of changes from 2.2.x.

Just about every source file was touched when jam got ANSI-fied.
#1 2 laura Add Jam/MR 2.2 source