/*	#CMS Generation: 1                       29-AUG-89
 */
#include ctype
#include stdlib
#include string

struct segment
	{
	int	stat;
	char *head,*tail;
	};

#define NO_COM_END 1
#define FOUND_EOL 2

/* return pointer to end of matching string, NULL if no match */
/* ignore whitespace */
/* `~' in object is non-alpha character (ie, punctuation ) */

char *same(start,object)
char *start,*object;
{

			/* while stuff left to check */ 

	while ( (*start != '\0')&&(*object != '\0') )	
	{
		if (!isspace(*object))		/* if normal char */
		{
		 if(*object == '~')		/* match non-alpha */
		 {
		  if( ispunct(*start) )
		  {
			object++;		/* bump pointers */
			start++;
		  }
		  else return(NULL);                      
		 }     
		 else
		 if ( *object == *start)	/* and they match */
		  {
			object++;		/* bump pointers */
			start++;
		  }                      
		 else return (NULL);		/* else return no match*/
		}
		else
		{
		if ( !isspace(*start) ) return(NULL);	/* whitespace mismatch */
		 while( isspace(*(++object)) );		/* skip white */
		 while( isspace(*(++start)) );		/* skip white */
		}
	}

	if (*object == '\0') 
	{
	if (*(object-1) == '~') 
	 while( ispunct(*(start-1) )) start--;
	if (isspace(*(object-1)) ) 
	 while( isspace(*(start-1) )) start--;

	return(start);	/* checked all of pattern */
	}
	else return(NULL);			/* some left, so no match */
}


struct segment find_object(line,object)
struct segment line;
char *object;
{
	/* while not end of line and they do not match, 
	   check next position */
	
	while ( (*line.head != '\0') && ( (line.tail=same(line.head,object)) == NULL) )
	{	
	line.head++; 
	}

	if (line.tail == NULL) 			/* never matched up */
	 {
		line.stat=FOUND_EOL;               
  		line.head=NULL;
	}    	
	else 
	if (object[0] == '~')
	 while( ispunct(*line.head) ) line.head++;

	return(line);
}
                                  


char *index(line,object)
char *line,*object;
{ char *start,*ptr,first[2];
  int len;
  int done;
 int beg;

done=0;


	first[0]=object[0];
	first[1]='\0';
	len=strlen(object);	
	start=line;                                      

    	while( (start=strpbrk(start,first)) != NULL) 
	 {
		if(strncmp(start,object,len) == 0 ) return(start);
		else start++;
	 }
	return(NULL);
}

/*
 * return pointers to head and tail of first occurance of pattern in string 
 * and ignore stuff between comment delimeter
 */

struct segment find_string(pattern,string_seg,comment)
char *pattern;
struct segment string_seg;
struct { 
	char beg_comm,end_comm ;
	} comment;

{	struct segment ret_value;
	char *left_comment,*right_comment;
	char *temp;

	left_comment=strchr(string_seg.head,comment.beg_comm);	/* = zero if not found */
	if (!left_comment )	/* if not found */
	 {	
	 return(find_object(string_seg,pattern));
	 }                     
				/* search just the left part of string */

	*left_comment = '\0';			/* stop string at comment */
	ret_value = find_object(string_seg,pattern);	/* search */
	*left_comment = comment.beg_comm;	/* replace character */

	if (ret_value.stat == FOUND_EOL)	/* no match on left side */
	 {
		right_comment=strchr(left_comment+1,comment.end_comm);
		if (right_comment==0)	/* if no right comment */
		 {	string_seg.stat=NO_COM_END;
			return(string_seg);
		 }
		string_seg.head = right_comment;
		ret_value = find_object(string_seg,pattern); /* search */

	 }
	return(ret_value);
}

