home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / misc / volume6 / copt / demo.c < prev    next >
C/C++ Source or Header  |  1989-03-07  |  3KB  |  101 lines

  1. /* demonstration of _cset */
  2.  
  3. #include "cset.h"
  4.  
  5. OPTIONS
  6. {  "DEMO",   COMM_KWD, "a command name",
  7.    "Tesco",  COMM_KWD, "another command name",
  8.    "String", SVAL_KWD, "an arbitrary string",
  9.    "Number", NVAL_KWD, "a number, base 8, 10 or 16",
  10.    "Tabs",   MVAL_KWD, "list of tab stops",
  11.    "Wide",   PLUS_KWD, "turn an option on",
  12.    "Multi",  PLUS_KWD | DASH_KWD | BLNK_KWD, "option may be +, - or plain string",
  13.    "Hidden", HIDE_KWD | NVAL_KWD | 19, "hidden numeric base 19 argument!",
  14.    OPT_HELP(OPT_EXEC, "more README")
  15. };
  16.  
  17. #define STRING    0
  18. #define    DEMO    1
  19. #define    TESCO    (DEMO+1)
  20. #define STR    (TESCO+1)
  21. #define NUM    (STR+1)
  22. #define TABS    (NUM+1)
  23. #define WIDE    (TABS+1)
  24. #define MULTI    (WIDE+1)
  25. #define HID    (MULTI+1)
  26.  
  27. /* I'm sure I should be able to do this in one go..., but the compiler barfs */
  28. char *d1[] = {"demo", "st=asda", (char *)0};
  29. char *d2[] = {"demo", "tesco", "num=0xd", "l=57", "+g", (char *)0};
  30. char *d3[] = {"tesco", "+w", "-mu", "+mt", "muti", (char *)0};
  31. char *d4[] = {"demo", "h=beef t=09,9,0x9", (char *)0};
  32. char *d5[] = {"demo", "==", (char *)0};
  33. char *d6[] = {"demo", "=hi", "=+", "=#", (char *)0};
  34.  
  35. char **Demos[] =
  36. {  d1, d2, d3, d4, d5, d6,
  37.    (char **)0
  38. };
  39.  
  40. execute(cmd, args)
  41. char *cmd, **args;
  42. {  if( fork() )
  43.       wait(0);
  44.    else
  45.       execv(cmd, args);
  46. };
  47.  
  48. main(argc, argv) char **argv;
  49. {  _opt_desc *parsed;
  50.    char ***dp, **cmd;
  51.    int *p, num;
  52.  
  53.    if(argc <= 1)
  54.    {  /* no arguments, do demos */
  55.       dp = Demos;
  56.       while(*dp != (char **)0)
  57.       {  printf("\n************************\nExecuting:");
  58.          cmd = *dp;
  59.          while(*cmd != (char *)0) printf(" %s", *cmd++);
  60.          printf("\nGives:\n\n");
  61.      execute("./demo", *dp++);
  62.       }
  63.       return;
  64.    }
  65.  
  66.    parsed = _cset(argv, 1);
  67.  
  68.    while( parsed->_opt_num != -1 )
  69.    {  switch( parsed->_opt_num ) 
  70.       {  case STRING:
  71.              printf("Arbitrary string option: %s\n", parsed->_opt.sval); break;
  72.          case DEMO:
  73.            break;
  74.          case TESCO:
  75.            printf("Called using alternative command name \"tesco\"\n"); break;
  76.          case NUM:
  77.            printf("Numeric value: 0%o, %d, 0x%x\n", parsed->_opt.nval, parsed->_opt.nval, parsed->_opt.nval); break;
  78.          case STR:
  79.            printf("String valued option: %s\n", parsed->_opt.sval); break;
  80.          case HID:
  81.            printf("Hidden keyword, number input in base 19 = %d in decimal\n", parsed->_opt.nval);
  82.          case MULTI:
  83.            printf("Option may be +, - or blank, this time it was: '%c'\n", parsed->_opt_type);
  84.            break;
  85.          case WIDE:
  86.            printf("Wide option turned on\n"); break;
  87.          case TABS:
  88.            p = parsed->_opt.mval;
  89.            printf("Muliple numeric valued keyword with %d values:", (num = *p++));
  90.             while(num--) printf(" %d", *p++);
  91.            printf("\n");
  92.            break;
  93.      default:    /* out of range - looks like an option but didn't match */
  94.            printf("Unknown option: %s\n", parsed->_opt.sval );
  95.  
  96.       }
  97.       parsed++;
  98.    }
  99. }
  100.    
  101.