home *** CD-ROM | disk | FTP | other *** search
/ Software Recommendations - 1998 Season 1 / DNBCD4.iso / share / DOS / ipxcopy / SRC.ZIP / SRC / CMDLINE.C < prev    next >
Encoding:
C/C++ Source or Header  |  1996-09-11  |  6.8 KB  |  320 lines

  1. /*
  2.  
  3.    CMDLINE.C
  4.  
  5.  
  6.    number:
  7.  
  8.    {'$'|[{+|─}]['0'][{'x'|'X'}]}[digits][{'k'|'K'}]
  9.  
  10.    If the first character is 0 and the second character is not
  11.    'x' or 'X', the string is interpreted as an octal integer.
  12.    Otherwise, it is interpreted as a decimal number.
  13.  
  14.  
  15.    Beginnt die Zahl mit '$' oder '0x', wird die Basis 16 verwendet,
  16.    bei '0' die Basis 8, sonst die Basis 10. Endet eine Zahl mit 'k'
  17.    oder 'K' wird sie mit 1024 multipliziert.
  18.  
  19.  
  20. */
  21.  
  22. #include <stdio.h>
  23. #include <stdlib.h>
  24. #include <string.h>
  25. #include <ctype.h>
  26. #include "cmdline.h"
  27.  
  28. char cl_file_list[CL_FILE_MAX][_MAX_PATH];
  29. int cl_file_cnt = 0;
  30.  
  31.  
  32. int cl_strcmp(s1, s2)
  33.    char *s1; 
  34.    char *s2;
  35. {
  36.    for(;;)
  37.    {
  38.       if ( *s1 == '\0' )   /* name aus der tabelle */
  39.          return 0;
  40.       if ( *s2 == '\0' )   /* name von der commandline */
  41.          return -1;
  42.  
  43.       if ( toupper(*s1) < toupper(*s2) )
  44.          return -1;
  45.       if ( toupper(*s1) > toupper(*s2) )
  46.          return 1;
  47.       s1++;
  48.       s2++;
  49.    }
  50. }
  51.  
  52. long cl_strtol( nptr, endptr )
  53.    char *nptr;
  54.    char **endptr;
  55. {
  56.    long res = 0L;
  57.    while( isspace(*nptr) )
  58.       nptr++;
  59.    if ( *nptr == '$' )
  60.    {
  61.       nptr++;
  62.       for(;;)
  63.       {
  64.          if ( *nptr >= '0' && *nptr <= '9' )
  65.          {
  66.             res *= 16L;
  67.             res += (long)(*nptr-'0');
  68.          }
  69.          else if ( *nptr >= 'a' && *nptr <= 'f' )
  70.          {
  71.             res *= 16L;
  72.             res += (long)(*nptr-'a');
  73.             res += 10L;
  74.          }
  75.          else if ( *nptr >= 'A' && *nptr <= 'F' )
  76.          {
  77.             res *= 16L;
  78.             res += (long)(*nptr-'A');
  79.             res += 10L;
  80.          }
  81.          else
  82.          {
  83.             break;
  84.          }
  85.          nptr++;
  86.       }
  87.    }
  88.    else
  89.    {
  90.       res = strtol( nptr, &nptr, 0 );
  91.    }
  92.    if ( *nptr == 'k' || *nptr == 'K' )
  93.    {
  94.       res *= 1024;
  95.       nptr++;
  96.    }
  97.    if ( endptr != NULL )
  98.       *endptr = nptr;
  99.    return res;
  100. }
  101.  
  102. int cl_GetOptionNo(list, option)
  103.    cl_entry_struct *list; 
  104.    char **option;
  105. {
  106.    int i;
  107.    if ( list == NULL || option == NULL )
  108.       return -3;
  109.    while( isspace(**option) )
  110.       (*option)++;
  111. #ifdef UNIX
  112.    if ( **option == '-' )
  113. #else
  114.    if ( **option == '/' || **option == '-' )
  115. #endif
  116.    {
  117.       i = 0;
  118.       while( list[i].typ != CL_TYP_NONE )
  119.       {
  120.          if ( cl_strcmp(list[i].name, (*option)+1) == 0 )
  121.          {
  122.             (*option) += 1 + strlen(list[i].name);
  123.             return i;
  124.          }
  125.          i++;
  126.       }
  127.    }
  128.    else
  129.    {
  130.       return -2;
  131.    }
  132.    return -1;
  133. }
  134.  
  135. int cl_Convert(entry, option, is_string_space )
  136.    cl_entry_struct *entry;
  137.    char **option;
  138.    int is_string_space;
  139. {
  140.    size_t i;
  141.    switch(entry->typ)
  142.    {
  143.       case CL_TYP_NONE:
  144.          return -1;
  145.       case CL_TYP_ON:
  146.          *(int *)entry->var_ptr = 1;
  147.          return 1;
  148.       case CL_TYP_OFF:
  149.          *(int *)entry->var_ptr = 1;
  150.          return 1;
  151.       case CL_TYP_SET:
  152.          *(long *)entry->var_ptr = (long)entry->var_size;
  153.          return 1;
  154.       case CL_TYP_LONG:
  155.          if ( **option == '\0' )
  156.             return 0;
  157.          *(long *)entry->var_ptr = cl_strtol( *option, option );
  158.          return 1;
  159.       case CL_TYP_PATH:
  160.       case CL_TYP_STRING:
  161.          if ( **option == '\0' )
  162.             return 0;
  163.          i = 0;
  164.          /*
  165.          if ( **option == '\"' )
  166.             (*option)++;
  167.          */
  168.          for(;;)
  169.          {
  170.             if ( isspace(**option) && !is_string_space )
  171.                break;
  172.             /*
  173.             if ( **option == '\"' )
  174.                break;
  175.             */
  176.             if ( **option == '\0' )
  177.                break;
  178.             if ( i >= entry->var_size-1 )
  179.                break;
  180.             ((char *)entry->var_ptr)[i] = **option;
  181.             (*option)++;
  182.             i++;
  183.          }
  184.          if ( i >= entry->var_size )
  185.             ((char *)entry->var_ptr)[entry->var_size-1] = '\0';
  186.          else
  187.             ((char *)entry->var_ptr)[i] = '\0';
  188.          return 1;
  189.    }
  190.    return -1;
  191. }
  192.  
  193. int cl_String(list, s)
  194.    cl_entry_struct *list;
  195.    char *s;
  196. {
  197.    int n;
  198.    if ( s == NULL )
  199.       return 0;
  200.    while( *s != '\0' )
  201.    {
  202.       while ( isspace(*s) )
  203.          s++;
  204.       n = cl_GetOptionNo(list, &s);
  205.       if ( n < 0 )
  206.       {
  207.          while ( !isspace(*s) )
  208.             s++;
  209.       }
  210.       else
  211.       {
  212.          cl_Convert(list+n, &s, 1);
  213.       }
  214.    }
  215.    return 1;
  216. }
  217.  
  218. int cl_Do(list, argc, argv)
  219.    cl_entry_struct *list; 
  220.    int argc; 
  221.    char **argv;
  222. {
  223.    int i;
  224.    int n;
  225.    int ret = 1;
  226.    char *s;
  227.    i = 1;
  228.    cl_file_cnt = 0;
  229.    while( argv[i] != NULL )
  230.    {
  231.       s = argv[i];
  232.       while( *s != '\0' )
  233.       {
  234.          n = cl_GetOptionNo(list, &s);
  235.          if ( n < 0 )
  236.          {
  237.             if ( n == -2 && cl_file_cnt < CL_FILE_MAX )
  238.             {
  239.                strcpy(cl_file_list[cl_file_cnt++], s);
  240.                break;
  241.             }
  242.             else
  243.             {
  244.                ret = 0;       /* unbekannte option */
  245.                break;
  246.             }
  247.          }
  248.          switch( cl_Convert(list+n, &s, 0) )
  249.          {
  250.             case -1:
  251.                ret = 0;       /* fehler */
  252.                break;
  253.             case 1:
  254.                break;
  255.             case 0:
  256.                i++;
  257.                s = argv[i];
  258.                if ( s == NULL )
  259.                {
  260.                   ret = 0;       /* fehler */
  261.                   break;
  262.                }
  263.                if ( cl_Convert(list+n, &s, 1 ) != 1 )
  264.                {
  265.                   ret = 0;       /* fehler */
  266.                   break;
  267.                }
  268.                break;
  269.          }
  270.          if ( argv[i] == NULL )
  271.             break;
  272.       }
  273.       if ( argv[i] == NULL )
  274.          break;
  275.       i++;
  276.       if ( argv[i] == NULL )
  277.          break;
  278.    }
  279.    return ret;
  280. }
  281.  
  282.  
  283.  
  284. /* #define cmdline_main_test */
  285. #ifdef cmdline_main_test
  286.  
  287. long com_port = -1;
  288. long port_adr = 0;
  289. int  boolval = 0;
  290. long vmode = -1;
  291.  
  292. cl_entry_struct cl_list[] =
  293. {
  294.    { CL_TYP_SET,  "25",  &vmode,  -2 },
  295.    { CL_TYP_SET,  "43",  &vmode,  -3 },
  296.    { CL_TYP_SET,  "50",  &vmode,  -4 },
  297.    { CL_TYP_SET,  "d",   &vmode,  -1 },
  298.    { CL_TYP_LONG, "v",   &vmode,  0 },
  299.    { CL_TYP_LONG, "com",  &com_port, 0 },
  300.    { CL_TYP_LONG, "port", &port_adr, 0 },
  301.    { CL_TYP_ON,   "on",   &boolval,  0 },
  302.    CL_ENTRY_LAST
  303. };
  304.  
  305.  
  306.  
  307. void main(int argc, char *argv[])
  308. {
  309.    int i;
  310.    /* printf("cl_Do: %d\n", cl_Do(cl_list, argc, argv)); */
  311.    cl_String(cl_list, "-com5 test -port");
  312.    printf("com_port: %ld\n", com_port);
  313.    printf("port_adr: %ld\n", port_adr);
  314.    printf("boolval: %d\n", boolval);
  315.    for( i = 0; i < cl_file_cnt; i++ )
  316.       printf("%d.: %s\n", i, cl_file_list[i]);
  317. }
  318.  
  319. #endif
  320.