#module	IdxCmd		"1-001"
/*
 ***********************************************************************
 *                                                                     *
 * 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:	IdxCmd
 *
 * Author:	R L Aurbach	CR&DS MIS Group	    13-Jun-1986
 *
 * Function:
 *
 *	This routine parses the IdxTeX Command Line, and returns the name of
 *	the input file and a flag which indicates whether to add an entry to
 *	the table of contents for the Index.
 *
 * Modification History:
 *
 * Version     Initials	   Date		Description
 * ------------------------------------------------------------------------
 * 1-001	RLA	13-Jun-1986	Original Code, based on the CRDVLIB
 *					Library routine LIB_PARSE_FOREIGN
-*/

/*
 * Module IdxCmd - Module-Wide Data Description Section
 *
 * Include Files:
 */
#include descrip
#include "IdxDef.H"

/*
 * Module Definitions:
 */
#define TRUE		1		/* Success value		*/
#define FALSE		0		/* Failure value		*/
#define str_dyn		{ 0, DSC$K_DTYPE_T, DSC$K_CLASS_D, 0 }

/*
 * Global Declarations:
 */

/*
 * Static Declarations:
 */
    static $DESCRIPTOR	(prefix, "IdxTeX ");
    static struct dsc$descriptor    command_line = str_dyn;
    static struct dsc$descriptor    file_desc = str_dyn;
    static struct dsc$descriptor    toc_value = str_dyn;
    static $DESCRIPTOR	(p1, "File");
    static $DESCRIPTOR	(toc, "TOC");

/*
 * External References:
 */
    globalref int	idxtex_table;
    globalref int	lib$get_input;
    globalref int	lib$sig_to_stop;

/*
 * Functions Called:
 */
    int		lib$get_foreign();	/* Get foreign command line	*/
    int		cli$dcl_parse();	/* Parse a DCL command line	*/
    int		str$prefix();		/* Prefix a string		*/
    int		lib$establish();	/* Establish a condition handler*/

/*+
 * Function Idx_Commmand - Documentation Section
 *
 * Discussion:
 *	This routine uses lib$get_foreign to read the command line.  It 
 *	prefixes the command line with "IdxTeX ", so that the CLI routines
 *	will have the appropriate information on which to operate.  It then
 *	parses the command line and returns the appropriate information to the
 *	program.
 *
 *	Since this routine establishes LIB$SIG_TO_STOP as a condition handler,
 *	syntax errors detected during processing result in program exit.
 *
 * Calling Synopsis:
 *	Call Idx_Command (file, toc_flag)
 *
 * Inputs:
 *	none
 *
 * Outputs:
 *	file	    ->	is the input file specified in the command line.  This
 *			is a required parameter.  This variable is an ASCIZ
 *			string passed by reference.  The calling program is
 *			responsible for allocating a sufficiently large 
 *			string (i.e., char file[256]).
 *
 *	toc_flag    ->	indicates whether the /TOC qualifier was specified.
 *			Values returned are:
 *			    IDX_K_NONE    - /TOC not specified.
 *			    IDX_K_ARTICLE - /TOC:ARTICLE specified.
 *			    IDX_K_REPORT  - /TOC:REPORT specified.
 *
 * Return Value:
 *	none
 *
 * Global Data:
 *	none
 *
 * Files Used:
 *	none
 *
 * Assumed Entry State:
 *	IdxTeX is invokes as a Foreign DCL command.
 *
 * Normal Exit State:
 *	Routine returns to caller.  The "file" and "toc_flag" variables are
 *	set up correctly.
 *
 * Error Conditions:
 *	Since the routine establishes LIB$SIG_TO_STOP as its exception handler,
 *	syntax and parsing errors detected by the CLI$ routines (which signal
 *	errors) cause the program to exit.  That is, all syntax errors are
 *	fatal.

 *
 * Algorithm:
 *	A. Call Lib$Get_Foreign to retrieve the command line.
 *	B. Prefix the command line with the command verb.
 *	C. Call Cli$DCL_Parse to parse the command line.
 *	D. Process the file name.
 *	    1. Get the file name.
 *	    2. Convert it to an ASCIZ string.
 *	E. Process the /TOC qualifier.
 *	    1. If /TOC is not specified,
 *		a. toc_flag = 0.
 *	    2. If /TOC:ARTICLE specified,
 *		a. toc_flag = 1.
 *	    3. If /TOC:REPORT specified.
 *		a. toc_flag = 2.
 *
 * Special Notes:
 *	This routine is based on the LIB_PARSE_FOREIGN routine, suitably
 *	modified.
-*/

/*
 * Function Idx_Command - Code Section
 */

void	idx_command (file, toc_flag)

    char	*file;			/* File name parameter		*/
    int		*toc_flag;		/* /TOC flag			*/
{
/*
 * Local Declarations:
 */
    int		status;			/* return status		*/
/*
 * Module Body:
 */

/* Establish a condition handler					*/

lib$establish(&lib$sig_to_stop);

/* Get the command line							*/

status = lib$get_foreign(&command_line);
if (!status)	lib$signal(status);

str$prefix(&command_line, &prefix);

/* Parse the command line						*/

cli$dcl_parse(&command_line, &idxtex_table, &lib$get_input);

/* Get the filename string						*/

cli$get_value(&p1,&file_desc);
strncpy (file, file_desc.dsc$a_pointer, file_desc.dsc$w_length);
file[file_desc.dsc$w_length] = '\0';

/* Process the /TOC qualifier						*/

*toc_flag = IDX_K_NONE;
if ((cli$present(&toc) & TRUE) != 0)
    {
    cli$get_value(&toc, &toc_value);
    if (toc_value.dsc$a_pointer[0] == 'A')  *toc_flag = IDX_K_ARTICLE;
    if (toc_value.dsc$a_pointer[0] == 'R')  *toc_flag = IDX_K_REPORT;
    }
}
