/********** gpmmain.c *************************************************/

/*
** General driver for gpm variants. Only accesses gpm() externally.
** Exports FileName and LineNumber for error messages.
** Exports PrintErrorLine().
**
** argv[x] is made NULL if gpmmain thinks it's a flag that
** it can interpret. Then argv[] is passed to gpminit,
** which can read more flags.
*/

#define GPMMAIN_C_

#include "gpmdefs.h"
#include "gpmmain.h"
#include "gpm.h"


#define NAMESIZE	128	/* Longest exec() name for us */

#ifdef CAREFUL
#include <sys/types.h>
#include <sys/stat.h>
#endif

int LineNumber=0;		/* for Monitor */
char *FileName=(char *)NULL;	/* for Monitor */
char ExeName[NAMESIZE];		/* Space for storing exec() name */

int Special[SpecialCount];

#ifdef ESCAPE
char *SpecialDefault="{|<~>}`\\";
int SpecialSet[CHARSETSIZE];
#else
char *SpecialDefault="{|<~>}`";
#endif

#ifdef VERBOSESET
# ifdef VERBOSEDEFAULT
static int VerboseLevel = 1;
# else
static int VerboseLevel = 0;
# endif
#endif
#ifdef QUOTEMSET
# ifdef QUOTEMDEF
int QuoteMLevel = 1;
# else
int QuoteMLevel = 0;
# endif
#endif

#ifdef AUTHOR
void Author()
{
	fprintf(stderr,"\nTo complain, send E-mail to:\n\n");
	fprintf(stderr,"to: adridg@sci.kun.nl\nor : wupper@cs.kun.nl\n");
	fprintf(stderr,"\n(Valid per Mar 24, 1995)\n");
}
#endif

void PrintErrorLine()
{
/*
** BAD PRACTICE!! This hard-coded size for s must be big
** enough to hold any of the messages printed to it in
** this function.
*/
	char s[128];
	s[0]=0;		/* Make sure it terminates */

#ifdef FILENAME
# ifdef LINENUMBER
	sprintf(s,"(line %d of file %s)\n",LineNumber,FileName);
# else
	sprintf(s,"(file %s)\n",FileName);
# endif
#else
# ifdef LINENUMBER
	sprintf(s,"(line %d)\n",LineNumber);
# endif
#endif
	fprintf(stderr,"\n%s\n",s);
}

void Usage()
{
	fprintf(stderr,"Usage: %s [options] [files]\n     : %s -h for help.\n",
	  ExeName,ExeName);
}

int main(argc, argv, envp)
int argc;
char **argv;
char **envp;
{
	int c;
	int in_file_count=0;			/* number of files to process */
	int dryrun=0;				/* flag a no-process run */

#ifdef CAREFUL
	int force=0;				/* flag for force overwrite */
	struct stat out_stat;
#endif

	char *in_file = (char *) NULL;	/* name of each file to process */
	char *out_file = (char *) NULL;	/* -o file */
	char *special_chars = (char *) NULL;	/* -s chars */

	strcpy(ExeName,argv[0]);		/* get exec(2v) name passed in */

	for (c=1; c<argc; c++)
	{
	  if (argv[c][0]=='-')
	  {
	    switch(argv[c][1])
	    {
	      case '-' : in_file_count++; break;
#ifdef DRYRUN
	      case 'd' : dryrun=1; argv[c]=NULL; break;
#endif
#ifdef VERBOSESET
	      case 'v' : VerboseLevel = (argv[c][2]=='-' ? 0 : 1);
	                 argv[c]=NULL; break;
#endif
#ifdef CAREFUL
	      case 'O' : force=1;
#endif
	      case 'o' : out_file=argv[c+1]; 
	                 if (out_file==NULL)
	                 {
	                   fprintf(stderr,"Bad output file\n");
	                   exit(EACCES);
	                 }
	                 argv[c]=argv[c+1]=NULL; c=c+1; break;
	      case 's' : special_chars=argv[c+1];
	                 if (special_chars==NULL)
	                 {
	                   fprintf(stderr,"Bad special chars\n");
	                   exit(-1);
	                 }
	                 argv[c]=argv[c+1]=NULL; c=c+1; break;
	      case 'h' : Usage(); Help(); exit(0);
#ifdef AUTHOR
	      case 'A' : Usage(); Author(); exit(0);
#endif
#ifdef QUOTEMSET
	case 'Q' : QuoteMLevel = (argv[c][2]=='-' ? 0 : 1);
	                 argv[c]=NULL; break;
#endif
	    }
	  }
	  else
	  {
	    in_file_count++;
	  }
	}

	if (out_file!=NULL && dryrun==0)
	{
#ifdef CAREFUL
	  if ((stat(out_file,&out_stat)!=(-1)) && force==0)
	  {
	    if ((S_ISREG(out_stat.st_mode)) && (out_stat.st_size != 0))
	    {
	      fprintf(stderr,"Won't overwrite file %s\n",out_file);
	      exit(EACCES);
	    }
	  }
#endif
	  if (freopen(out_file,"w",stdout)==NULL)
	  {
	    fprintf(stderr,"Can't open output file %s\n",out_file);
	    exit(EACCES);
	  }
	}

/* Check for presence of -s flag. Use default if necessary. Check len of special chars. */

	if (special_chars==NULL) special_chars=SpecialDefault;
	Special[0]=SpecialMarker;
	if (strlen(special_chars)!=(SpecialCount-1))
	{
	  fprintf(stderr,"Must have %d special chars\n",(SpecialCount-1));
	  exit(-1);
	}

/* Copy special chars into array */

#ifndef ESCAPE
	for (c=0; c<(SpecialCount-1); c++) Special[c+1]=(int)special_chars[c];
#else
	for (c=0; c<CHARSETSIZE; c++) SpecialSet[c]=0;
	for (c=0; c<(SpecialCount-1); c++) 
	{
	  Special[c+1]=(special_chars[c])+SpecialMark;
	  SpecialSet[(int)special_chars[c]]=1;
	}
#endif

	gpmInit(argc,argv,out_file);

/* Now run filter gpm() */

	if (in_file_count==0)
	{
		/* no infiles specified, so read stdin and exit */
		LineNumber=0;
		FileName="stdin";
		gpm();
		return(0);
	}
	else
	{
	  for (c=1; c<argc; c++)
	  {
	    if (argv[c]!=NULL)
	    {
	      if (argv[c][0]=='-') 
	      {
	         in_file="/dev/tty"; 
	         FileName="stdin"; 
	      }
	      else 
	      { 
	        in_file=argv[c]; 
	        FileName=in_file; 
	      }
	      if (freopen(in_file,"r",stdin)==NULL)
	      {
	        fprintf(stderr,"Cannot open %s input\n",in_file);
	        exit(EACCES);
	      }

#define PROCESSING fprintf(stderr,"Processing: %s\n",in_file)

#ifndef DRYRUN
#ifdef VERBOSESET
	      if (VerboseLevel) PROCESSING;
#else
# ifdef VERBOSEDEFAULT
	      if (out_file != NULL) PROCESSING;
# endif
#endif
#else
#ifdef VERBOSESET
	      if (dryrun || VerboseLevel) PROCESSING;
#else
# ifdef VERBOSEDEFAULT
	      if (dryrun || out_file != NULL) PROCESSING;
# else
	      if (dryrun) PROCESSING;
# endif
#endif
#endif
#undef PROCESSING

	      LineNumber=0;
#ifdef DRYRUN
	      if (!dryrun) gpm();
#else
	      gpm();
#endif
	    }
	  }
	  return(0);
	}
}

