/*--- EDIT # 0004	31-MAR-1986 11:25   SYS$SYSROOT:[RVT.UTILS]XRFI.C;29 */
/*--- Previous edit 	9 Apr 1984   18:57:58	DB2:[310,61]XRFI.C;6  */

/*
 *                      ***************
 *                      * 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.10 RBD		14-Oct-80  Allow wildcards in RT11
 * Version V1.11 RBD		26-Dec-80  Registers which hold characters
 *				           MUST be 'register char'. Clean up
 *				           RT-11 usage message. 
*/

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


extern long int time_of_day;
static char *tim_str;		/* Ptr to time/date string. */
/*
 * Set up RSX file names, open them, and initialize the page header strings
*/


initinfile(in)
char *in;

{
   char wrkbuf[256];                             /* Working string buffer */

   name(wrkbuf, in, "c", 0);               /* Make input file name */
   if ((src = fwild(wrkbuf, "r")) == NULL)
      error("Cannot open:", wrkbuf);
   if (time_of_day == 0) {
      time(&time_of_day);	/* Get time (seconds) */
      tim_str = ctime(&time_of_day);
      tim_str[24] = NULL;
   }
}

initoutfile(out)
char	*out;
{
   char wrkbuf[256];
   char firstin[256];

   if (out != NULL)
      name(wrkbuf, out, "x", 0);
   else {
      fgetname(src, firstin);
      name(wrkbuf, firstin, "x", 1);            /* Make list file name */
   }
   if ((lst = fopen(wrkbuf, "w")) == NULL)       /* Try to open it */
      error("Cannot open listing file:", wrkbuf);
}

nextinfile()
{
   extern dflag;
   register char	*wp;
   register char	c;
   char			wrkbuf[40];

   if ((src = fnext(src)) == NULL)
      return(0);
   fgetname(src, wrkbuf);
   if (dflag)
      fprintf (stderr, "%s\n", wrkbuf);
   for (wp = wrkbuf; (c = *wp++) && c != ':';)
      ;
   if (c == 0)
      wp = wrkbuf;
   strcpy(pghead, "\fCross Ref of: ");
   strcat(pghead, wp);
   if (strlen(pghead) >= 40)
      strcat(pghead, "\n\t\t\t\t");
   strcat(pghead, "\t");
   strcat(pghead, tim_str);
   strcat(pghead, "\tPage ");
   return(1);
}



/*
 * 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 char c;                             /* Fast char. buffer */
   int in_br;			/* Inside directory []'s (VAX) */

   in_br = 0;
   p1 = sysnam;                                 /* p1 --> output string */
   p2 = file;                                   /* p2 --> input string */
   while (c = *p2++) {		/* Copy up to '.' */
      if (!in_br && c == '.')
	 break;
      if (c == '[')
	 ++in_br;
      if (c == ']')
	 in_br = 0;
      *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';
}


/*************************************************************************
 *
 * Give user help on program useage.
*/

useage()
{
#ifdef rt11
   fprintf(stderr, "Usage (short):  .RUN XR infile\n\n");
   fprintf(stderr, "Usage (long) :  .RUN XR\n");
   fprintf(stderr, "                Argv: [-n] [-o outfile] infiles\n");
#else
   fprintf(stderr, "Usage: xrf [-slxn] [-o outfile] infiles\n\n");
#endif
#ifdef rsx
   fprintf(stderr, "		    -s   Spool output (native RSX only)\n");
#endif
   fprintf(stderr, "		    -n   Narrow (80 column) output\n");
   fprintf(stderr, "		    -x   Do not produce x-ref list\n");
   fprintf(stderr, "		    -l   Do not produce line list (xref only)\n");
   fprintf(stderr, "		    -o   Output to \"outfile\"\n\n");
   fprintf(stderr, "Default input is .c filetype\n");
   fprintf(stderr, "Default output is first input filename, .x filetype\n");
   fprintf(stderr, "Input files may have wild-card names\n");
   error("?XRF-E-parameter error", NULL);
}
