/**************************************************************************/
/*                             Check for no-good chars                           */
/**************************************************************************/


#include "srch.h"
#include <ctype.h>

#define FILE_OPENING_ERROR 3
#define FILENAME_MAXLEN 8
#define CR "\n"
#define FILE_SUFFIX ".xtr"
#define MAXLEN 60
#define LINE_LEN 80
#define NOARGS 1
#define INCREMENT 1
#define SPACE ' '
#define NOT '_'
#define SINGLE 1
#define MINLEN 4
#define NULL_ 0
#define ARGCNT 2

#define BUFFERSIZE 8192
/*******8K buffer******/

char ad[] =
"XTRACT tool by M\\Cooper, PO Box 237, St. David, AZ 85630-0237";

typedef enum { FALSE, TRUE } Boolean;

void getword( char *wrdfile );
void center( char *strng );
void parse( char *argument, char *lset, char *not_letterset );
Boolean wordtest( char *word );


void main(  int argc, char **argv )
{

   char letterset[ MAXLEN ],
	   n_letterset[ MAXLEN ],
	   input_set[ MAXLEN ],
           wfile[ MAXLEN ];  

	   *n_letterset = NULL_;

         if( argc < ARGCNT )
             strcpy( wfile, "word.lst" );
         else
             strcpy( wfile, *(argv+1) );

	 getword( wfile );
}



/**********************************WORDTEST********************************/
/*       Function tests if word is constructible from Letterset           */
/*                 Args in: char *letterset, char *word                   */
/*   Returns: error_flag == TRUE (1) if constructible, FALSE (0) if not   */
/**************************************************************************/

Boolean wordtest( char *word )
{
   char temp [MAXLEN],
	   *t;
   Boolean error_flag = TRUE;


	 strcpy( temp, word );  /*Preserve WORD*/
         t = temp;

	 while( *t )
	    {
            if( !islower( *t ) && *t != '\n' )
               return( FALSE );
            t++;
	    }

		return( error_flag );
}


/*************************************************************/

void getword( char *wordfile )
{

	char	l_set [ MAXLEN ],
		word [ MAXLEN ],
		tempstr [ LINE_LEN + 1 ],
		targetfile [ MAXLEN ],
		bar [ LINE_LEN + 1 ],
		double_bar [ LINE_LEN + 1 ],
  msg1 [ MAXLEN ], 
  msg2 [ MAXLEN ];

	FILE *fptr,
		*tfile;
	int fnamelen;
	long wcount = 0L;





	   if( !( fptr = fopen( wordfile, "rt" ) ) )
		 {
		 printf( "\7\7\7Cannot open Wordfile!" );
		 exit( FILE_OPENING_ERROR );
		 }


		 /*********************Main Loop*************/	 
		  while( fgets( word, MAXLEN, fptr ) != NULL_ )

			if( !wordtest( word ) )
				 {
				 printf( "%s", word );
				 wcount++;
				 }
		  /*******************************************/
           fclose( fptr );

}



void center( char *str )
{
   int padding;
   char st [ LINE_LEN + INCREMENT ];

	 padding = LINE_LEN / 2 - strlen( str ) / 2;
	 memset( st, SPACE, padding );
	 *( st + padding ) = NULL_;  /*Terminate
                                       string*/
	 strcat( st, str );
	 strcpy( str, st );

	 return;
}

void parse( char *arg, char *letterset, char *nls )
{
   char *p;


	 if( *arg == '_' )
	    {
	    *letterset = NULL_;
	    strcpy( nls, ++arg );
	    return;
	    }


	 if( ( p = strchr( arg, NOT ) ) )    
             {
             *p = NULL_;
             strcpy( letterset, arg );
	     strcpy( nls, p + 1 );
             }

/*	 p = strtok( NULL_, NULL_ ); 


	 if( *p )
	    strcpy( nls, p );
*/

  else
     {
     *nls = NULL_;
     strcpy( letterset, arg );
     }

	 return;
}
