/* getparms() gets parameters from the command line.  String values returned
   are:

   source:     Source filespec with wildcards included.  E.g., if the user 
               wishes to convert all ARC files in subdirectory c:\tmp, he 
               would type "c:\tmp\*.arc".

   tempdir:    A directory name.  This will serve as a temporary directory.
               If the directory name doesn't already end with a ":" or a
               "/" or a "\", then we add a "\" to it one here before 
               returning it.  Thus concatenating a filename to it will
               always make sense.

   progname:   Command to use to extract a given source file.  For example,
               command could be "pkxarc" or "arce" or "arc x".

   Integer values returned are:

   bell, wait, rename, test, help

   Function return value:  -1 if error else 0
*/ 

#include <assert.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include "atoz_f.h"     /* function definitions:  atoz functions        */

#define  NEXT_ARG    {if (!(--argc)) goto insuf_args; ++argv;}

int getparms (argc, argv, source, tempdir, progname, 
               bell, wait, rename, test, help)
int argc;
char **argv;
char **source, **tempdir, **progname;
int *bell, *wait, *rename, *test, *help;
{
   char *option;              /* option typed by user */
   char *temp;                /* misc. temporary pointer */
   enum choice {              /* must agree with option_list */
      NONE=-1, SOURCE=0, TEMPDIR=8, PROGNAME=19, BELL=28, WAIT=34,
      RENAME=40, TEST=48, NOBELL=54, NOWAIT=62, NORENAME=70, NOTEST=80,
      HELP=88
   };
   static char option_list[] =   /* must agree with enum choice */
      "-source -temporary -program -bell -wait -rename -test -nobell -nowait -norename -notest -help";
      /* 0       8          19      28    34    40      48    54      62      70        80      89*/

   enum choice cmd;
   int i;
   int errors;

   /* defaults */
   *bell = *wait = *rename = 1;
   *test = *help = 0;
   *progname = *source = *tempdir = NULL;

   /* Get values from environment. These override defaults */
   temp = getenv ("ATOZ$BELL");
   if (temp != NULL && tolower (*temp) == 'n')
      *bell = 0;
   temp = getenv ("ATOZ$WAIT");
   if (temp != NULL && tolower (*temp) == 'n')
      *wait = 0;
   temp = getenv ("ATOZ$RENAME");
   if (temp != NULL && tolower (*temp) == 'n')
      *rename = 0;
   temp = getenv ("ATOZ$TEST");
   if (temp != NULL && tolower (*temp) == 'y')
      *test = 1;

   *tempdir = getenv ("ATOZ$TEMP");
   *progname = getenv ("ATOZ$PROG");

   if (*tempdir == NULL)
      *tempdir = getenv ("TEMP");
   if (*tempdir == NULL)
      *tempdir = getenv ("TMP");
   if (*tempdir == NULL)
      *tempdir = getenv ("ARCTEMP");
   if (*tempdir == NULL)
      *tempdir = getenv ("PKARCTMP");

   *source = NULL;

   /* Now for command-line parameters, which will supersede defaults and
   any values obtained from the environment */

   cmd = NONE;                            /* assume none by default */
   errors = 0;                            /* none so far */
   argv++; argc--;                        /* skip argv[0] */

   while (argc > 0) {
      option = *argv;
      cmd = (enum choice) index (option_list, strlwr(option), 0);

      if (*option == '-' && (int) cmd == -1) {
         prterror ('e', "[%s] is invalid.\n", option);
         errors++;
      } else if (*option=='-'&&index(option_list,option, (int) cmd + 1) != -1) {
         prterror ('e', "[%s] is ambiguous.\n", option);
         errors++;
      } else { /* find argument */
         switch (cmd) {
            case SOURCE:   NEXT_ARG;  *source = *argv;   break;
            case TEMPDIR:  NEXT_ARG;  *tempdir = *argv;  break;
            case PROGNAME: NEXT_ARG;  *progname = *argv; break;

            case BELL:     *bell = 1; break;
            case NOBELL:   *bell = 0; break;

            case WAIT:     *wait = 1; break;
            case NOWAIT:   *wait = 0; break;

            case RENAME:   *rename = 1; break;
            case NORENAME: *rename = 0; break;

            case TEST:     *test = 1; break;
            case NOTEST:   *test = 0; break;

            case HELP:     *help = 1; break;
            default:       *source = *argv; break;
         }
      }
      ++argv;  --argc;
   }
   if (errors > 0)
      return (-1);
   else
      return (0);

insuf_args:
   prterror ('w', "Something missing after [%s].\n", option);
   return (-1);
} /* getparms() */
