/* DCL parsing of foreign command-lines
*/
/* Joe Meadows wrote the original code */
/* Modified by Joseph Huber: add check for MCR */
/*
 The purpose of this routine is to make DCL parsing available wether the 
 program has been called via a known DCL verb (in DCL command table), an
 external command (DCL symbol), or a "MCR filespec" command.
*/

#include 
#include 
#include 
#include 
#include 
#include  
#include "CLI_Invocation.h"

/*#define DEBUG*/
#ifdef DEBUG
#include stdio
#endif

static int cli_get_value(\
struct dsc$descriptor_s *s1, struct dsc$descriptor_s *s2, short *ll)
{
  int code=1;
  lib$establish(lib$sig_to_ret);
  code = cli$get_value(s1, s2, ll);
  lib$revert();
  return code;
}

/* "table" is a pointer to the internal CLD
   "name" is a text string containing the name of the verb
*/
int init_cli(char *table, char *name)
{
  static struct dsc$descriptor_s cmd = {0,DSC$K_DTYPE_T,DSC$K_CLASS_D,0};
  static $DESCRIPTOR(verb,"$VERB");
  static $DESCRIPTOR(line,"$LINE");
  int i,sts;
  short ll;
  int how=0;
 
  sts = CLI_Invocation ( &how);
  if (how == 0) return sts;
  
  sts = cli_get_value(&verb,&cmd,&ll);
  if (!(sts & 1)) return(sts);
#ifdef DEBUG
  printf("init_cli: verb=%s,length=%d\n",cmd.dsc$a_pointer,cmd.dsc$w_length);
#endif

#if 0
  if (strncmp(cmd.dsc$a_pointer,name,ll) == 0)
  {
    return 1; /* the command must have been properly defined by set command */
               /* in this case cli$dcl_parse not necessary */
  }
#endif
    sts = cli_get_value(&line,&cmd,&ll);
    if (!(sts & 1)) return(sts);
#ifdef DEBUG
  printf("init_cli: line=%s,length=%d\n",cmd.dsc$a_pointer,cmd.dsc$w_length);
#endif
/* check if the command is invoked by MCR filename param... 
   if so, fill space behind MCR to skip over verb
*/
    if (ll > 3) {
    if (strncasecmp (cmd.dsc$a_pointer,"MCR ",4) == 0) cmd.dsc$a_pointer[3]='X';
        }
    for (i = 0; (i < ll) &&
                (cmd.dsc$a_pointer[i] != ' ') &&
                (cmd.dsc$a_pointer[i] != '/'); ++i)
    cmd.dsc$a_pointer[i] = (i < strlen(name))?name[i]:' ';

    lib$establish(lib$sig_to_ret);
/*#define PROMPT*/
/* define PROMPT to prompt for required parameters,
   otherwise returns "missing required parameters" error.
*/
/* initialize the parse tables */
#ifdef PROMPT
    sts=cli$dcl_parse(&cmd,table,LIB$GET_INPUT,LIB$GET_INPUT);
#else
    sts=cli$dcl_parse(&cmd,table);
#endif
    lib$revert();
    return sts;
}