/*************************************************************************
*	"find.cci" : パターン検索（コンソール用）
*-------------------------------------------------------------------------
*	使い方：
*		CCI find -- <検索パターン> <ファイル名>
*************************************************************************/

#include	"ccilib.h"

int			FlgI = 0;
char		*Ptn = NULL;
char		*RePtn = NULL;

int		find( char *fn )
{
	char	*fp;
	char	buf[BUFSIZ];
	long	ln;				/* 行番号	*/

	if ( (fp = fopen(fn,"r")) == NULL )
	{
		fprintf(stderr,"File open error!! (%s)\n", fn );
		return (ERR);
	}

	ln = 0;
	while ( fgets(buf,BUFSIZ,fp) != NULL )
	{
		++ln;
		if ( RePtnMatch( RePtn, buf ) >= 0 )
		{	/* マッチング */
			printf("%6d : %s", ln, buf );
		}
	}

	fclose(fp);
	return (NORMAL);
}

int		main( int argc, char **argv)
{
	int		i, ch;
	char	*s;

	for ( i = 1; i < argc; ++i )
	{
		s = argv[i];
		if ( *s == '-' || *s == '/' )
		{	/* オプションパラメータ */
			++s;
			while ( *s )
			{
				ch = toupper(*s++);
				switch ( ch )
				{
					case 'I':
						FlgI = 1;
						break;
				}
			}
		} else
		{
			if ( Ptn == NULL )
			{	/* 検索パターン	*/
				Ptn = s;
				/* 検索パターンのコンパイル	*/
				if ( (RePtn = RePtnAlloc( Ptn, FlgI )) == NULL )
				{
					fprintf(stderr,"Pattern error!!\n");
					exit(1);
				}
			} else
				find( s );
		}
	}
	return (0);
}
