home *** CD-ROM | disk | FTP | other *** search
/ Hall of Fame / HallofFameCDROM.cdr / proglc / zoo141_c.lzh / GETPARMS.C < prev    next >
Text File  |  1987-02-07  |  5KB  |  137 lines

  1. /* getparms() gets parameters from the command line.  String values returned
  2.    are:
  3.  
  4.    source:     Source filespec with wildcards included.  E.g., if the user 
  5.                wishes to convert all ARC files in subdirectory c:\tmp, he 
  6.                would type "c:\tmp\*.arc".
  7.  
  8.    tempdir:    A directory name.  This will serve as a temporary directory.
  9.                If the directory name doesn't already end with a ":" or a
  10.                "/" or a "\", then we add a "\" to it one here before 
  11.                returning it.  Thus concatenating a filename to it will
  12.                always make sense.
  13.  
  14.    progname:   Command to use to extract a given source file.  For example,
  15.                command could be "pkxarc" or "arce" or "arc x".
  16.  
  17.    Integer values returned are:
  18.  
  19.    bell, wait, rename, test, help
  20.  
  21.    Function return value:  -1 if error else 0
  22. */ 
  23.  
  24. #include <assert.h>
  25. #include <string.h>
  26. #include <stdio.h>
  27. #include <stdlib.h>
  28. #include "atoz_f.h"     /* function definitions:  atoz functions        */
  29.  
  30. #define  NEXT_ARG    {if (!(--argc)) goto insuf_args; ++argv;}
  31.  
  32. int getparms (argc, argv, source, tempdir, progname, 
  33.                bell, wait, rename, test, help)
  34. int argc;
  35. char **argv;
  36. char **source, **tempdir, **progname;
  37. int *bell, *wait, *rename, *test, *help;
  38. {
  39.    char *option;              /* option typed by user */
  40.    char *temp;                /* misc. temporary pointer */
  41.    enum choice {              /* must agree with option_list */
  42.       NONE=-1, SOURCE=0, TEMPDIR=8, PROGNAME=19, BELL=28, WAIT=34,
  43.       RENAME=40, TEST=48, NOBELL=54, NOWAIT=62, NORENAME=70, NOTEST=80,
  44.       HELP=88
  45.    };
  46.    static char option_list[] =   /* must agree with enum choice */
  47.       "-source -temporary -program -bell -wait -rename -test -nobell -nowait -norename -notest -help";
  48.       /* 0       8          19      28    34    40      48    54      62      70        80      89*/
  49.  
  50.    enum choice cmd;
  51.    int i;
  52.    int errors;
  53.  
  54.    /* defaults */
  55.    *bell = *wait = *rename = 1;
  56.    *test = *help = 0;
  57.    *progname = *source = *tempdir = NULL;
  58.  
  59.    /* Get values from environment. These override defaults */
  60.    temp = getenv ("ATOZ$BELL");
  61.    if (temp != NULL && tolower (*temp) == 'n')
  62.       *bell = 0;
  63.    temp = getenv ("ATOZ$WAIT");
  64.    if (temp != NULL && tolower (*temp) == 'n')
  65.       *wait = 0;
  66.    temp = getenv ("ATOZ$RENAME");
  67.    if (temp != NULL && tolower (*temp) == 'n')
  68.       *rename = 0;
  69.    temp = getenv ("ATOZ$TEST");
  70.    if (temp != NULL && tolower (*temp) == 'y')
  71.       *test = 1;
  72.  
  73.    *tempdir = getenv ("ATOZ$TEMP");
  74.    *progname = getenv ("ATOZ$PROG");
  75.  
  76.    if (*tempdir == NULL)
  77.       *tempdir = getenv ("TEMP");
  78.    if (*tempdir == NULL)
  79.       *tempdir = getenv ("TMP");
  80.    if (*tempdir == NULL)
  81.       *tempdir = getenv ("ARCTEMP");
  82.    if (*tempdir == NULL)
  83.       *tempdir = getenv ("PKARCTMP");
  84.  
  85.    *source = NULL;
  86.  
  87.    /* Now for command-line parameters, which will supersede defaults and
  88.    any values obtained from the environment */
  89.  
  90.    cmd = NONE;                            /* assume none by default */
  91.    errors = 0;                            /* none so far */
  92.    argv++; argc--;                        /* skip argv[0] */
  93.  
  94.    while (argc > 0) {
  95.       option = *argv;
  96.       cmd = (enum choice) index (option_list, strlwr(option), 0);
  97.  
  98.       if (*option == '-' && (int) cmd == -1) {
  99.          prterror ('e', "[%s] is invalid.\n", option);
  100.          errors++;
  101.       } else if (*option=='-'&&index(option_list,option, (int) cmd + 1) != -1) {
  102.          prterror ('e', "[%s] is ambiguous.\n", option);
  103.          errors++;
  104.       } else { /* find argument */
  105.          switch (cmd) {
  106.             case SOURCE:   NEXT_ARG;  *source = *argv;   break;
  107.             case TEMPDIR:  NEXT_ARG;  *tempdir = *argv;  break;
  108.             case PROGNAME: NEXT_ARG;  *progname = *argv; break;
  109.  
  110.             case BELL:     *bell = 1; break;
  111.             case NOBELL:   *bell = 0; break;
  112.  
  113.             case WAIT:     *wait = 1; break;
  114.             case NOWAIT:   *wait = 0; break;
  115.  
  116.             case RENAME:   *rename = 1; break;
  117.             case NORENAME: *rename = 0; break;
  118.  
  119.             case TEST:     *test = 1; break;
  120.             case NOTEST:   *test = 0; break;
  121.  
  122.             case HELP:     *help = 1; break;
  123.             default:       *source = *argv; break;
  124.          }
  125.       }
  126.       ++argv;  --argc;
  127.    }
  128.    if (errors > 0)
  129.       return (-1);
  130.    else
  131.       return (0);
  132.  
  133. insuf_args:
  134.    prterror ('w', "Something missing after [%s].\n", option);
  135.    return (-1);
  136. } /* getparms() */
  137.