//
// testing of regular expression matching category.
// Both GNU and BSD types, depending on compile-time
// flag (GNU_REGEXP or BSD_REGEXP).
#import <stdio.h>
#import <stdlib.h>
#import <signal.h>
#import <fcntl.h>
#import <string.h>
#import <sys/time.h>
#import <RCString.h>


int
main(int c, char **v)
{
	RCString *oString;
	char buf[512];
	char *bpMatch;
	RCString *oMatch;
	void usage();

#ifdef NeXT
	int signalHandler();

	// install a few signal handlers
	signal(SIGSEGV, signalHandler);
	signal(SIGBUS,  signalHandler);
	signal(SIGILL,  signalHandler);

	// turn on fairly high level of malloc error checking
	malloc_debug(16);
#endif NeXT

	if (c < 4) {
		usage(*v);
		exit(1);
	}

	// pause to allow use of MallocDebug 
	if (c > 4)
		gets(buf);

	oString = [RCString newFromString:*(v + 1)];
	if (oString) {
		printf("\"%s\" %s regular expression \"%s\"\n",
			[oString data],
			[oString matches:*(v + 2)]?"MATCHES" : "DOESN'T MATCH",
			*(v + 2));

		if ((bpMatch = [oString subStringMatching:*(v + 2)]))
		{
			printf("string that matches: \"%s\"\n", bpMatch);
			free(bpMatch);
		} else {
			fprintf(stderr, "NULL string from match\n");
		}

		if ((oMatch = [oString objectMatching:*(v + 2)]))
		{
			printf("string from object that matches: \"%s\"\n",
				[oMatch data]);
			[oMatch free];
		} else {
			fprintf(stderr, "NULL object from match\n");
		}

		printf("string from object (with replacement): \"%s\"\n",
			[[oString replaceSubStringMatching:*(v + 2) with:*(v + 3)] data]);
	
		free(oString);
	}

	// pause to allow use of MallocDebug 
	if (c > 4)
		gets(buf);

	return(0);
}

void
usage(char *command)
{
	fprintf(stderr, "%s: check regular expression stuff\n", command);
	fprintf(stderr, "usage: %s string regexp replacment_string\n", command);
}

#ifdef NeXT
int
signalHandler(int something)
{
	printf("signalHandler(%d) (0x%x)\n", something, (unsigned int)something);
	exit(0);
}
#endif
