#module    IdxTeX    "1-002"
/*
 ***********************************************************************
 *                                                                     *
 * The software was developed at the Monsanto Company and is provided  *
 * "as-is".  Monsanto Company and the auther disclaim all warranties   *
 * on the software, including without limitation, all implied warran-  *
 * ties or merchantabilitiy and fitness.                               *
 *                                                                     *
 * This software does not contain any technical data or information    *
 * that is proprietary in nature.  It may be copied, modified, and     *
 * distributed on a non-profit basis and with the inclusion of this    *
 * notice.                                                             *
 *                                                                     *
 ***********************************************************************
 */

/*+
 * Module Name:	IdxTeX
 *
 * Author:	R L Aurbach	CR&DS MIS Group    26-Apr-1986
 *
 * Function:
 *	Main Line of the IdxTeX program.  This program processes .idx files
 *	produced by LaTeX and generates files which will produce a properly
 *	formatted index.
 *
 * Modification History:
 *
 * Version     Initials	   Date		Description
 * ------------------------------------------------------------------------
 * 1-001	RLA	26-Apr-1986	Original Code
 * 1-002	RLA	13-Jun-1986	Add support for /TOC qualifier
-*/

/*
 * Module IdxTeX - Module-Wide Data Description Section
 *
 * Include Files:
 */
#include	    descrip
#include	    stdio
#include	    "IdxDef.H"

/*
 * Module Definitions:
 */
#define		    TRUE	1
#define		    FALSE	0
#define		    linebfsize	133	/* Max size of a line		*/

/*
 * Global Declarations:
 */
    TREE_PTR	    root = 0;		/* Root of the Index Tree	*/

/*
 * Static Declarations:
 */

/*
 * External References:
 */

/*
 * Functions Called:
 */

/*+
 * Function IdxTeX - Documentation Section
 *
 * Discussion:
 *	This is the main line of the IdxTeX program.  It reads the input file,
 *	parsing the information, and generates an output file based on the 
 *	information from the input file.
 *
 * Calling Synopsis:
 *	$ IdxTeX :== $Crl_Public:IdxTeX
 *	$ IdxTeX filespec [/TOC:{ARTICLE | REPORT}]
 *
 * Inputs:
 *	filespec	    ->	Name of the input file to be processed.  The
 *				default filespec SYS$DISK:[].IDX is processed
 *				against this specification.
 *
 * Outputs:
 *	filespec	    ->	The program produces an output file which is
 *				the resultant filespec from processing .IND as
 *				the primary file specification and the fully-
 *				qualified input filespec as the default.
 *
 * Return Value:
 *	returns SUCCESS
 *
 * Global Data:
 *	none
 *
 * Files Used:
 *	reads an input file as specified above.
 *	produces an output file as specified above.
 *
 * Assumed Entry State:
 *	Called from DCL level.
 *
 * Normal Exit State:
 *	Returns to DCL level.
 *
 * Error Conditions:
 *	Input file not found	-- program issues a message and exits 
 *				    immediately.
 *	Input file format error -- program ignores any lines of the input file
 *				    which it does not understand.

 *
 * Algorithm:
 *	A. Open the input file.
 *	B. For all records in the input file,
 *	    1. Read the record.
 *	    2. Parse it into item, subitem, subsubitem, and page fields.
 *	    3. Add data to Index Tree.
 *	C. Close the input file.
 *	D. Open the output file.
 *	E. For all nodes of the Index Tree,
 *	    1. Generate appropriate output.
 *	F. Close the output file.
 *
 * Special Notes:
 *	none
-*/

/*
 * Function IdxTeX - Code Section
 */

main()
{
/*
 * Local Declarations
 */
    FILE	*file;			/* file pointer			*/
    char	dna[133];		/* Default filename arg		*/
    char	linebf[linebfsize];	/* I/O line buffer		*/
    char	token_1[linebfsize];	/* First token in field		*/
    char	token_2[linebfsize];	/* Second token in field	*/
    char	token_3[linebfsize];	/* Third token in field		*/
    char	page_no[linebfsize];	/* Page reference token		*/
    int		token_ct;		/* Token level in current line	*/
    char	filename[256];		/* Input file name		*/
    int		toc_flag;		/* /TOC qualifier flag		*/
/*
 * Module Body
 */

/* Process the command line.  The routine will force exit on error.	*/

idx_command(filename, &toc_flag);

printf("\nIdxTeX, the automatic index generator for LaTeX, version 1.1\n");

/* 
 * Process the input file:
 *  A. Open the file.
 *  B. For all records in the file,
 *	1. Read the next record.
 *	2. Call Idx_Parse to parse the record into tokens.
 *	3. Call Idx_Build_Tree to enter the information into the Index Tree.
 *  C. Close the file.
 */

if ((file = fopen(filename, "r", "dna = sys$disk:[].idx")) == NULL)
    {
    printf("Could not open the input file\n");
    exit();
    }
while (fgets(linebf, linebfsize, file) != 0)
    {
    idx_parse (linebf, token_1, token_2, token_3, page_no, &token_ct);
    idx_build_tree (token_1, token_2, token_3, page_no, token_ct);
    }
fclose(file);

/* Now generate the output file...					*/

sprintf(dna, "dna = %s", filename);
if ((file = fopen("sys$disk:[].ind", "w", "rat=cr", "rfm=var", dna)) == NULL)
    {
    printf("Could not open the output file\n");
    exit();
    }
idx_generate (file, toc_flag);
fclose(file);

exit();
}
