home *** CD-ROM | disk | FTP | other *** search
/ World of Shareware - Software Farm 2 / wosw_2.zip / wosw_2 / CPROG / CMDPARSE.ZIP / CMDPARSE.C next >
Text File  |  1989-12-02  |  10KB  |  361 lines

  1. #include <string.h>
  2. #include <dos.h>
  3. #include <alloc.h>
  4.  
  5. /*
  6.    CMDPARSE - Contains several TURBO-C functions to handle the command line
  7.               passed on the call to a program.
  8.  
  9.               Several assumptions are made:
  10.  
  11.                   All parameters are separated by one or more spaces. The
  12.                    number of spaces is not significant and the spaces are
  13.                    not a part of the parameter.
  14.  
  15.                   No more than 20 parameters and options will be passed.
  16.  
  17.                   Options are indicated by a leading switch character,
  18.                    either a slash (/) or a dash (-).
  19.  
  20.                   Slash-delimited options may be appended to a field without
  21.                    a separating space, vis.
  22.  
  23.                             param/n        command/c
  24.  
  25.                   Dash-delimited options will always be preceded by a space.
  26.  
  27.                   Options always are terminated by a space.
  28.  
  29.                   Options can take one of three forms:
  30.  
  31.                            Switch+character    example:   /n
  32.                            Switch+string       example:   /noi
  33.                            Switch+expression   example:   /VIDEO=MONO
  34.  
  35.                   The switch+expression options may NOT have spaces before &/or
  36.                     after the = sign.
  37.  
  38.                   No case conversions are done.
  39.  
  40. */
  41.  
  42. #define TRUE 1
  43.  
  44. #define FALSE 0
  45.  
  46. /* ********************* Global variables ************************** */
  47.  
  48. /* combined_cmd holds the pieces of the command pointed to by cmd_arg[] */
  49.  
  50. static char combined_cmd[260];
  51.  
  52. /* cmd_arg[] is an array of pointers to the pieces of the command.  It
  53.    is accessible from the program using extern.
  54. */
  55.  
  56. char *cmd_arg[22];
  57.  
  58. /* arg_type[] is an array of flags indicating the type of each of the
  59.    arguments.  It is accessible from the program using extern.  The
  60.    flags are 0=parameter 1=option.
  61. */
  62.  
  63. int arg_type[22];
  64.  
  65.  
  66. /* *************************** Functions ************************** */
  67.  
  68. /*
  69.     CMD_INIT retrives and parses the command line into the arrays.
  70.  
  71.     Call:        status = cmd_init();
  72.  
  73.     Returns:     0 if error, non-zero if ok
  74.  
  75. */
  76.  
  77. int cmd_init(void)
  78. {
  79.     char *buffer;               /* Scratch buffer to assemble cmdline */
  80.  
  81.     int i;                      /* Index variable */
  82.  
  83.     char *cmdline, *outline;    /* Pointers used during parse */
  84.  
  85.  
  86.     /* Allocate temporary buffer to assemble command line */
  87.  
  88.     if ((buffer = (char *) calloc(260, sizeof(char))) == NULL)
  89.           return(0);
  90.  
  91.     if (_argc == 0) return(0);
  92.  
  93.     /* Assemble command line */
  94.  
  95.     strcpy(buffer,*_argv);              /* Command argument */
  96.  
  97.     for (i=1;i<_argc;i++)
  98.     {
  99.         strcat(buffer,"\v");             /* Param delimiter */
  100.         strcat(buffer,*(_argv+i));
  101.     }
  102.  
  103.     /* Now move the line into its final resting place, delimit params
  104.     */
  105.  
  106.     cmdline = buffer;                   /* Source pointer */
  107.  
  108.     outline = &combined_cmd[0];         /* Destination pointer */
  109.  
  110.     while (*cmdline != '\0')
  111.     {
  112.         if (*cmdline == '/' && *(cmdline-1) != '\v')
  113.         {
  114.             *outline = '\v';             /* Leading delimiter  */
  115.             outline += 1;
  116.         }
  117.         *outline = *cmdline;
  118.         outline += 1;
  119.         cmdline += 1;
  120.     }
  121.     *outline = '\0';                   /* Null-terminate string */
  122.  
  123.     free(buffer);                      /* Release temporary string */
  124.  
  125.     /* Now set up array */
  126.  
  127.     cmdline = &combined_cmd[0];
  128.  
  129.     cmd_arg[0] = cmdline;              /* program name  */
  130.     cmd_arg[1] = NULL;                 /* end of array test */
  131.  
  132.     i=1;
  133.  
  134.     while (*cmdline != '\0')
  135.     {
  136.         if (*cmdline == '\v')
  137.         {
  138.             *cmdline = '\0';
  139.             cmdline += 1;
  140.  
  141.             cmd_arg[i] = cmdline;     /* pointer to next */
  142.  
  143.             if (*(cmdline) == '/' || *(cmdline) == '-')
  144.                  arg_type[i] = 1;
  145.             else
  146.                  arg_type[i] = 0;
  147.  
  148.             i += 1;
  149.             cmd_arg[i] = NULL;         /* End of array test */
  150.         }
  151.         else
  152.             cmdline += 1;
  153.     }
  154.  
  155.  
  156.     return(1);
  157. }
  158.  
  159. /*
  160.      GET_PARAM - Returns a pointer to a read-only string containing the
  161.                  value of the parameter.
  162.  
  163.      Call:    ch_pointer = get_param(n)
  164.  
  165.                      n = number of the parameter (0 - n)
  166.  
  167.      Return:  Pointer to parameter in combined_cmd, NULL if not found
  168.  
  169. */
  170.  
  171. char *get_param(int pnum)
  172. {
  173.     int i,count;
  174.  
  175.     if (pnum == 0) return(cmd_arg[0]);
  176.  
  177.     count=0;
  178.  
  179.     for (i=1; cmd_arg[i] != NULL; i++)
  180.     {
  181.         if (arg_type[i] == 0)
  182.         {
  183.             count += 1;
  184.  
  185.             if (count == pnum)  return(cmd_arg[i]);       /* Return from here */
  186.         }
  187.     }
  188.  
  189.     return(NULL);
  190. }
  191.  
  192. /*
  193.    IF_OPTION - Scans all option fields and tests the first character(s)
  194.                following the switch to see if they match.  The number of
  195.                characters checked is determined by the number of characers
  196.                in the test string passed.
  197.  
  198.                The comparison is case sensitive.
  199.  
  200.    Call:   result = if_option("n");
  201.  
  202.            result = if_option("MVID");
  203.  
  204.    Result:       0 if not found
  205.                  array subscript in cmd_arg[] if found
  206.  
  207. */
  208.  
  209. int if_option(char *tst_str)
  210. {
  211.     int i;
  212.     size_t tsl;
  213.  
  214.     tsl = strlen(tst_str);
  215.  
  216.     for (i=1; cmd_arg[i] != NULL; i++)
  217.     {
  218.         if (arg_type[i] == 1)
  219.             if ((strlen(cmd_arg[i])-1) >= tsl)
  220.                 if ((strncmp(tst_str,(cmd_arg[i]+1),tsl)) == 0)
  221.                           return(i);                            /* Return
  222.                                                                    from
  223.                                                                    here
  224.                                                                    */
  225.     }
  226.  
  227.     return(0);
  228. }
  229.  
  230.  
  231. /*
  232.    IF_OPTIONI - Scans all option fields and tests the first character(s)
  233.                following the switch to see if they match.  The number of
  234.                characters checked is determined by the number of characers
  235.                in the test string passed.
  236.  
  237.                The comparison is case insensitive.
  238.  
  239.    Call:   result = if_option("n");
  240.  
  241.            result = if_option("MVID");
  242.  
  243.    Result:       0 if not found
  244.                  array subscript in cmd_arg[] if found
  245.  
  246. */
  247.  
  248. int if_optioni(char *tst_str)
  249. {
  250.     int i;
  251.     size_t tsl;
  252.  
  253.     tsl = strlen(tst_str);
  254.  
  255.     for (i=1; cmd_arg[i] != NULL; i++)
  256.     {
  257.         if (arg_type[i] == 1)
  258.             if ((strlen(cmd_arg[i])-1) >= tsl)
  259.                 if ((strnicmp(tst_str,(cmd_arg[i]+1),tsl)) == 0)
  260.                           return(i);                            /* Return
  261.                                                                    from
  262.                                                                    here
  263.                                                                    */
  264.     }
  265.  
  266.     return(0);
  267. }
  268.  
  269.  
  270. /*
  271.      OPT_VALUE - Returns a pointer to the read-only string containing the
  272.                  rvalue of a /optname=value option type. Scans all option
  273.                fields and tests the first character(s) following the switch
  274.                to see if they match.  The number of characters checked is
  275.                determined by the number of characers in the test string
  276.                passed.
  277.  
  278.                The comparison is case sensitive.
  279.  
  280.     Call:   c_pointer = opt_value("CHGSTR");
  281.  
  282.             c_pointer = opt_value("x");
  283.  
  284.     Returns:  Pointer to rvalue substring in combined_cmd.
  285.               NULL if not found.
  286.  
  287. */
  288.  
  289. char *opt_value(char *tst_str)
  290. {
  291.     int i;
  292.     size_t tsl;
  293.     char *rvalue;
  294.  
  295.     tsl = strlen(tst_str);
  296.  
  297.     for (i=1; cmd_arg[i] != NULL; i++)
  298.     {
  299.         if (arg_type[i] == 1)
  300.             if ((strlen(cmd_arg[i])-1) >= tsl)
  301.                 if ((strncmp(tst_str,(cmd_arg[i]+1),tsl)) == 0)
  302.                 {
  303.                     rvalue = strchr(cmd_arg[i],'=');
  304.                     if (rvalue != NULL) return(rvalue+1);       /* Return
  305.                                                                    from
  306.                                                                    here
  307.                                                                    */
  308.                 }
  309.  
  310.     }
  311.  
  312.     return(NULL);
  313. }
  314.  
  315.  
  316. /*
  317.      OPT_VALUEI - Returns a pointer to the read-only string containing the
  318.                  rvalue of a /optname=value option type. Scans all option
  319.                fields and tests the first character(s) following the switch
  320.                to see if they match.  The number of characters checked is
  321.                determined by the number of characers in the test string
  322.                passed.
  323.  
  324.                The comparison is case insensitive.
  325.  
  326.     Call:   c_pointer = opt_value("CHGSTR");
  327.  
  328.             c_pointer = opt_value("x");
  329.  
  330.     Returns:  Pointer to rvalue substring in combined_cmd.
  331.               NULL if not found.
  332.  
  333. */
  334.  
  335. char *opt_valuei(char *tst_str)
  336. {
  337.     int i;
  338.     size_t tsl;
  339.     char *rvalue;
  340.  
  341.     tsl = strlen(tst_str);
  342.  
  343.     for (i=1; cmd_arg[i] != NULL; i++)
  344.     {
  345.         if (arg_type[i] == 1)
  346.             if ((strlen(cmd_arg[i])-1) >= tsl)
  347.                 if ((strnicmp(tst_str,(cmd_arg[i]+1),tsl)) == 0)
  348.                 {
  349.                     rvalue = strchr(cmd_arg[i],'=');
  350.                     if (rvalue != NULL) return(rvalue+1);       /* Return
  351.                                                                    from
  352.                                                                    here
  353.                                                                    */
  354.                 }
  355.  
  356.     }
  357.  
  358.     return(NULL);
  359. }
  360.  
  361.