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

   FILE:  MAIN_CNT.C
   USAGE: MAIN CONTROL_FILE_NAME

          This is a main routine for calling functions (named "do_it")
          that use a control file specified on the command line.
          The control file may have comments; it will be copied,
          minus its comments, to a temporary file, which will then
          serve as the actual control file.

                            Eric Firing
                             88-02-07

         Fri  03-17-1989  Added variable buffering; added
         removal of the temporary control file.  Both of
         these are controlled by preprocessor directives,
         since they are not available on all machines.

         Wed  07-11-1990 (JR)  Used tmpnam() to generate
         unique temporary control file name

         98/12/01 (EF) Changed the file mode to binary for
         the filtered control file handle that is passed to
         do_it(), so that ftell/fseek will work on DOS/WIN.
         The problem was first noticed with the mingw version
         of cygwin.

******************************************************************************/
#include "common.h"      /* malloc(), free(), exit(), VBUF */
#include "dbhost.h"      /* PROTOTYPE_ALLOWED */
#include "io_nc.h"       /* strip_comments() */
#include "ioserv.h"

#define VBUFSIZE 8192
#define CAN_DELETE 1    /* if ANSI.C remove() is available */

#if PROTOTYPE_ALLOWED
void do_it(FILE *fp_cnt);          /* the only user subroutine */
#else
void do_it();          /* the only user subroutine */
#endif

#if PROTOTYPE_ALLOWED
int main(int argc, char *argv[])
#else
int main(argc, argv)
int argc;
char *argv[];
#endif
{
     FILE *fp_cnt;
     FILE *fp_cnt_nc;
     char cntfile[40], tempcnt[L_tmpnam];

     if (argc < 2)
     {
          fprintf(stderr, "\nEnter control file name ==> ");
          scanf("%39s",cntfile);
     }
     else
        strcpy(cntfile,argv[1]);
     if ((fp_cnt = fopen(cntfile, "r")) == NULL)
     {
          fprintf(stderr, " \n%%%% ERROR: Can't open %s\n\n", cntfile);
          exit(-1);
     }
     tmpnam(tempcnt);
     if ((fp_cnt_nc = fopen(tempcnt, "w")) == NULL)
     {
          fprintf(stderr, "\n%%%% ERROR: Can't open %s\n\n", tempcnt);
          exit(-1);
     }
#ifdef VBUF
     if (    setvbuf(fp_cnt,    NULL, _IOFBF, VBUFSIZE)
          || setvbuf(fp_cnt_nc, NULL, _IOFBF, VBUFSIZE) )
     {
          fprintf(stderr, "\n%%%% ERROR: In setvbuf in main\n\n");
          exit(-1);
     }
#endif

     fprintf(stderr, "\nStripping comments from control file\n");
     strip_comments(fp_cnt, fp_cnt_nc);
     fclose(fp_cnt);
     fclose(fp_cnt_nc);

     if ((fp_cnt_nc = fopen(tempcnt, "rb")) == NULL)
     {
          fprintf(stderr, "\n%%%% ERROR: Can't open %s\n\n", tempcnt);
          exit(-1);
     }
     do_it(fp_cnt_nc);
     fclose(fp_cnt_nc);

#if CAN_DELETE == 1
     remove(tempcnt);
#else
     fp_cnt_nc = fopen(tempcnt, "w");
     fclose(fp_cnt_nc);
#endif
     return(0);
}
/* NOTE: The reason for closing fp_cnt_nc and then opening it
   again instead of just rewinding it is that the latter method
   leaves lost clusters on the disk if the program does not
   terminate normally.
*/

