Category : C Source Code
Archive   : CXRF.ZIP
Filename : XRF0.C

 
Output of file : XRF0.C contained in archive : CXRF.ZIP

/*------------------------------------------------------------------------
XRF0.C

This is the mainline program for the C cross referencer and lister.
It handles the following tasks:

- Scans the command line
- Opens the appropriate files
- Calls some initialization functions
- Invokes line by line processing
- Prints the cross-reference index
- Finishes up processing as appropriate


Usage:

xrf [-n] [-o out_file] in_files

Flags:
-s Print C source (with linenumbers) as well.
-c Concatanate C source files (treat as one).
-cfile Concatanate source files and write to .
-lxxx Set page length to xxx lines.
-wxxx Set page width to xxx columns.
-txx Expand tabs to xx spaces.
-f Strip form feed characters.

Written By:

Bob Denny
Creative System Design Co.
3452 E. Foothill Blvd. << Suite 601 >>
Pasadena, Ca. 91107
(213) 355-6836

Modified for MSDOS By:

Mike Cole
Digital Equipment Co.
PC Customer Information & Support Centre,
Jays Close,
Basingstoke,
Hampshire RG22 4DE,
ENGLAND. (44) 256-59200

Modified for Turbo C By:

Steve Boerner
P.O. Box 364
Brockport, NY 14420

Experimental Version X1.0 5-May-80
Split off initialization X1.1 7-May-80
Prototype. Y1.2 9-May-80
Release. V1.3 9-May-80
Chg. for DECUS OTS. & Cmplr. V1.4 1-Jul-80
Add support for RT-11 V1.5 3-Jul-80
Conditionalized spool call V1.6 MM 9-Jul-80
Added narrow, outfile, etc. V1.7 MM 10-Jul-80
Added multiple infiles V1.8 MM 22-Jul-80
Fspool now in the library V1.9 MM 2-Aug-80
Modify for CI86 under MSDOS V1.10MC 3-Dec-84
add Print spooler (-s opt) V1.11MC 4-Jan-85
add -i, -p options V1.12MC 4-Jan-85
add -c option V1.13MC 8-Jan-85
add -v option 8-Jan-85
allow for mixed case keys V1.14MC 12-Jan-85
& flag functions with '()' 12-Jan-85
modify for Turbo C V1.15SMB 12-Dec-89
remove -i, -s options 12-Dec-89
allow separate outfiles 15-Dec-89
advanced command line parsing 20-Dec-89
add -l, -w, -t, -f options 21-Dec-89
modify -c option, -p becomes -s 21-Dec-89
bug fixed in lstline 23-Dec-89
------------------------------------------------------------------------*/
#include
#include
#include
#include
#include "xrf.h"

static int pageno; /* Current listing page no. */
static char *outfile; /* Output filename pointer */
/*------------------------------------------------------------------------
Functions: newpage

Prototype in: xrf.h

Description: Start a new printout page, with header.

Returns: void
------------------------------------------------------------------------*/
void newpage(void) /* Start new page */
{
++pageno; /* Increment page number */
linpg = 0; /* Reset line-in-page count */
fprintf(lst, "%s%d\n\n", pghead, pageno); /* Output page heading */
}

/*------------------------------------------------------------------------
Function: detab

Prototype In: Local to this module.

Description: Replace tabs with blanks.

Returns: Did expanded string fit?
------------------------------------------------------------------------*/
static BOOL detab(char *src, char *dst, int dstsize)
{
int srcpos, dstpos, j;

dstpos = 0;
for (srcpos = 0; src[srcpos] != '\0'; srcpos++)
if (src[srcpos] == '\t')
for (j = tabsize - dstpos % tabsize; j > 0; j--) {
if (dstpos >= dstsize - 1) {
dst[dstpos] = '\0';
return (FALSE);
}
dst[dstpos++] = ' ';
}
else {
if (dstpos >= dstsize - 1) {
dst[dstpos] = '\0';
return (FALSE);
}
dst[dstpos++] = src[srcpos];
}
dst[dstpos] = '\0';
return (TRUE);
}

/*------------------------------------------------------------------------
Function: lstline

Prototype In: Local to this module.

Description: Write a line of text to the destination file.

Returns: void
------------------------------------------------------------------------*/
static void lstline(void) /* Write listing line out */
{
static char linebf[LWIDTH + 1];

if (linpg >= plength)
newpage();
if (tabflag && strchr(scanbf, '\t')) {
if (!detab(scanbf, linebf, sizeof(linebf)))
fatal("Line too long, can't use \"-t\" option\n");
strcpy(scanbf, linebf);
}
fprintf(lst, "%4d:\t%s", lineno, scanbf);
linpg++;
}

/*------------------------------------------------------------------------
Function: klower

Prototype In: xrf.h

Description: Make a fixed length lower case key from mixed case
variable length key.

Returns: Pointer to lower case key string.
------------------------------------------------------------------------*/
static char *klower(char *c1)
{
int i=0;
static char ktemp[CPS + FFLAG + 1]; /* Lose function marker */

while(*c1)
if (*c1 != '(' && *c1 != ')') /* Move in key as lowercase */
ktemp[i++] = tolower(*c1++);
else
c1++; /* Unless function id '()' */
while (i < CPS + FFLAG)
ktemp[i++]=' '; /* add trail spaces */
ktemp[i]='\0'; /* add terminator */
return ktemp;
}

/*------------------------------------------------------------------------
Function: main

Description: Main program function.
------------------------------------------------------------------------*/
void main(int argc, char *argv[])
{ /* Start the main prog. */
int i; /* Arg count */
BOOL nofiles; /* Flag "got a file" */
char keystr[12 + CPS + CPS + 1]; /* constructed key hold */

/* Initialize variables */
root = NULL; /* No tree yet */
nofiles = TRUE; /* No files yet */
outfile = NULL; /* No output file yet */
lastsym[0] = '\0'; /* no prev symbol [XRF3] */

myfree(); /* Clear out storage */
date(thedate); /* get date & time */
time(thetime);
fprintf(stderr, "C Program Cross Referencer\n\n");

/* Parse the command line */
if ((i = parsedos(argc, argv, option, OPTS)) == 0) {
useage(argv[0]);
exit(0);
}
if (conflag)
rperline = (lwidth - (CPS + FFLAG) - 14) / RSIZE;
else
rperline = (lwidth - (CPS + FFLAG) - 2) / RSIZE;
/* Process all filenames */
while (i < argc) {
nextinfile(argv[i++]);
if (conflag && nofiles) { /* If concatanation */
outfile = nextoutfile(outfile); /* Open outfile */
nofiles = 0;
}
do {
lineno = pageno = 0; /* Init. page & line number */
if (!conflag) { /* If NO concatanation ... */
outfile = nextoutfile(NULL);/* Open default outfile */
nofiles = 0;
myfree(); /* Clear out storage */
root = NULL; /* No tree yet */
lastsym[0]='\0'; /* no prev symbol [XRF3] */
}
if (prnflag)
newpage(); /* Start first page */
/* Process each line of source file */
while (fgets(scanbf, lwidth, src) != NULL) {
++lineno; /* Increment line number */
scanp = scanbf; /* Reset scan pointer */
while (getid()) { /* scanp --> id or NULL */
/* Construct key */
concat(keystr, klower(idbuf), idbuf, progname, NULL);
root = search(keystr, root); /* Process identifier */
} /* Till done with this line */
if(prnflag)
lstline(); /* List this line */
} /* Till end of source file */
concat(pghead, "\fXRF Cross Reference Listing: ", strupr(outfile),
"\t", thedate, " ", thetime, "\tPage ", NULL);
if (!conflag) { /* Process if NOT concat */
pageno = 0;
newpage(); /* Start index on new page */
prtree(root); /* Print the index */
fclose(lst); /* Close outfile */
}
} while (nextinfile(NULL));
}
if (conflag) { /* Process now if concat */
pageno = 0;
newpage(); /* Start index on new page */
prtree(root); /* Print the index */
}
if (nofiles)
useage(argv[0]); /* Give help */
myfree(); /* Clear out storage */
exit(0); /* Exit closing files */
} /***** END MAIN *****/

/*------------------------------------------------------------------------
Function: setconcat, setpageln, setpagewd, settab

Prototype In: xrf.h

Description: Functions to handle command line options.

Returns: 1 for success, 0 for failure.
------------------------------------------------------------------------*/
int setconcat(char *op) /* Handle 'c' option */
{
op++;
if (*op)
outfile = op;
return 1;
}

int setpageln(char *op) /* Handle 'l' option */
{
op++;
if (isdigit(*op)) {
plength = atoi(op);
if (plength < 0)
plength = 0;
return 1;
}
return 0;
}

int setpagewd(char *op) /* Handle 'w' option */
{
op++;
if (isdigit(*op)) {
lwidth = atoi(op);
if (lwidth > 132)
lwidth = 132;
if (lwidth < 80)
lwidth = 80;
return 1;
}
return 0;
}

int settab(char *op) /* Handle 't' option */
{
op++;
if (isdigit(*op)) {
tabsize = atoi(op);
return 1;
}
return 0;
}

/* End XRF0.C */