/*
 *                      ***************
 *                      * X R F I . C *
 *                      ***************
 *
 * Do initialization things.
 * NOTE: This is once-only code. It should be run in an overlay,
 *       along with FOPEN. The root need only contain XRF0 and XRFD.
 *
 * Version V1.4                  1-Jul-80
 * Version V1.5                  3-Jul-80  Conditionaled date/time stuff.
 * Version V1.6 MM		 9-Jul-80  Changed date/time stuff
 * Version V1.7 MM		10-Jul-80  File name changes
 * Version V1.8 MM		21-Jul-80  Ctime() changed
 * Version V1.9 MM		22-Jul-80  Redid initialization
 * Version V1.11 RBD		26-Dec-80  Cleanup rt11 usage message
 * Version V1.12 MM		22-Dec-81  Rtime isn't in vax library
 * Version V1.13 MM		16-Feb-82  ctime fixup
 */

#include <stdio.h>
#include "xrf.h"

extern long time_of_day;




















/*
 * Set up file names, open them, and initialize the page header strings
 */


initoutfile()
   {

   if (debug) {printf("xrf: beginning init out file name\n");}
   if (lst_arg != NULL)
      {
      name(lst_name, lst_arg, "x", 0);
      }
   else
      {
      name(lst_name, src_arg, "x", 1);
      }
   if((lst = fopen(lst_name, "w")) == NULL)  {
      fprintf(stderr,"Cannot open listing file %s\n", lst_name);
      exit(-1);
   }
   if (debug) {printf("xrf: list file %s openned\n",lst_name);}
   }

initinfile()
{
   char	*timetemp;
#ifndef AMIGA
   char	*ctime();
#endif
   FILE *fopen();

   if (debug) {printf("xrf: beginning init source file\n");}
   name(src_name, src_arg, "c", 0);
   if((src = fopen(src_name, "r")) == NULL) {
      fprintf(stderr,"Cannot open source file %s\n", src_name);
      exit(-1);
   } else {
#ifdef AMIGA
	sprintf(pghead,"\fCross Reference of: %s \tPage ",
	    src_name);
#else
       if (time_of_day == 0) {
           time(&time_of_day);
       }
       timetemp = ctime(&time_of_day);
       timetemp[24] = '\0';
	sprintf(pghead,"\fCross Reference of: %s \t %s \tPage ",
	    src_name,timetemp);
#endif
	if (debug) {printf("xrf: source file %s openned\n",src_name);}
   }
}

/*
 * Make a file name.
 * The mode argument is either 0 which means use type as default extension,
 * or 1, which means force the extension to be type. Argument file is the
 * 'raw' file name, sysnam is the final filename string. All arg's except
 * mode are pointers, as usual.
 *
 */

name(sysnam, file, type, mode)
char *sysnam, *file, *type;

int mode;

   {
   register char *p1, *p2;                      /* Fast pointers */
   register int c;                              /* Fast char. buffer */

   if (debug) {printf("xrf: begin name build\n");}
   p1 = sysnam;                                 /* p1 --> output string */
   p2 = file;                                   /* p2 --> input string */
   while((c = *p2++) && c != '.')               /* Copy up to '.' */
      *p1++ = c;
   if( mode == 0 )                              /* Default extension */
      {
      if( c == '.' )                            /* Extension explicitly given */
         {
           do                                   /* Copy '.' + any ext. */
              {
              *p1++ = c;
              } while( c = *p2++ );
         }
      else                                      /* No ext. given, default */
         {
         *p1++ = '.';
         p2 = type;
         while( c = *p2++ )
            *p1++ = c;
         }
      }
   else                                 /* Force extension */
      {
      *p1++ = '.';
      p2 = type;
      while( c = *p2++ )
         *p1++ = c;
      }
   *p1 = '\0';                          /* Terminate with eos */
   if (debug) {printf("xrf: name %s built\n",sysnam);}
   }
/*
 *****************************************************************************
 *
 * Give user help on program useage.
 */

useage()
   {
   fprintf(stderr, "Usage: xrf [-dn] [-o outfile] infiles\n\n");
   fprintf(stderr, "       -d  Enable debug printouts\n");
   fprintf(stderr, "       -n  Narrow (80 column) output\n");
   fprintf(stderr, "       -o  Output to \"outfile\"\n\n");
   fprintf(stderr, "Input files may have wild-card names\n");
   fprintf(stderr, "Default input filetype is \".c\"\n");
   fprintf(stderr, "Default output filename is \"<first_input_file>.x\"\n");
   exit(-1);
   }
