home *** CD-ROM | disk | FTP | other *** search
/ DP Tool Club 15 / CD_ASCQ_15_070894.iso / vrac / mikmod43.zip / GETOPT.C < prev    next >
C/C++ Source or Header  |  1994-04-28  |  3KB  |  141 lines

  1. #include <errno.h>
  2. #include <string.h>
  3. #include <dos.h>
  4. #include <stdio.h>
  5. #include <ctype.h>
  6. #include <alloc.h>
  7. #include "getopt.h"
  8.  
  9. int  aindex;        // argument index
  10. int  oindex;        // option index
  11. int  margc;            // number of arguments
  12. char **margv;        // the arguments itself
  13. char *moptions;        // option string
  14. char switchchar;    // switch char
  15. char *op,*np;        //
  16. char dest[160];        // dest is large enough to hold a complete commandline
  17. char c;                // last option char
  18.  
  19.  
  20.  
  21. int GetNextPartial(void)
  22. /*
  23.     Copy next commandline argument into dest[]. This functions
  24.     splits arguments like BLAHBLAH/X/Y into separate argument i.e.
  25.     BLAHBLAH, /X and /Y. This makes parsing the switches a lot easier.
  26.  
  27.     returns:    1 on succes
  28.                 0 on end of commandline
  29. */
  30. {
  31.     if(op==NULL){
  32.         op=margv[aindex++];
  33.         if(op==NULL) return 0;
  34.     }
  35.  
  36.     if((np=strchr(op+1,switchchar))!=NULL){
  37.         *np=0;
  38.         strcpy(dest,op);
  39.         *np=switchchar;
  40.     }
  41.     else{
  42.         strcpy(dest,op);
  43.     }
  44.  
  45.     op=np;
  46.     return 1;
  47. }
  48.  
  49.  
  50.  
  51.  
  52. void InitOpt(int argc,char *argv[],char *options)
  53. /*
  54.     Initializes commandline parsing:
  55.  
  56.     input:    argc    -    standard argument count, as delivered to main()
  57.             argv[]    -    standard argument array, as delivered to main()
  58.             options    -    a string of option-characters to scan for. When an
  59.                         option is followed by ':' that option will require an
  60.                         argument.
  61. */
  62. {
  63.     _AX = 0x3700;          // get dos switch character via doscall
  64.     geninterrupt(0x21);
  65.     switchchar=_DL;
  66.     margc=argc;            // init global vars.
  67.     margv=argv;
  68.     moptions=options;
  69.     aindex=1;            // reset current argument index
  70.     oindex=1;            // reset current option index
  71.     op=NULL;
  72.     c=0;
  73. }
  74.  
  75.  
  76.  
  77.  
  78. int GetOpt(char *opt,char **arg)
  79. /*
  80.     Get next commandline element (option, option+argument or argument)
  81.  
  82.     returns:
  83.             GO_EOF                no more elements
  84.             GO_OPT                option char in opt, option argument in arg
  85.             GO_ARG                normal argument (..filename) in arg
  86.             GO_UNKNOWN_SWITCH    the option in opt is not valid
  87.             GO_SWITCH_NEEDS_ARG    the option in opt needs an argument
  88.  
  89.     Yes, yes.. this is a really messy routine.. but it does the job. If
  90.     anyone knows a cleaner or smaller method, let me know.
  91. */
  92. {
  93.     char *p,*marg;
  94.  
  95.     nextarg:
  96.  
  97.     if(c==0) if(!GetNextPartial()) return GO_EOF;
  98.  
  99.     if(dest[0]==switchchar){
  100.  
  101.         c=dest[oindex++];
  102.         *opt=c;
  103.         *arg=NULL;
  104.  
  105.         if(c==0){
  106.             if(oindex==2) return GO_UNKNOWN_SWITCH;    // no empty switch
  107.             oindex=1;
  108.             goto nextarg;    // <- don't try this at home kids!
  109.         }
  110.  
  111.         if(c==':' || (p=strchr(moptions,c))==NULL){
  112.             return GO_UNKNOWN_SWITCH;
  113.         }
  114.  
  115.         if(p[1]==':'){
  116.  
  117.             if(dest[oindex]==0){
  118.                 if(!GetNextPartial() || dest[0]==switchchar){
  119.                     return GO_SWITCH_NEEDS_ARG;
  120.                 }
  121.                 *arg=dest;
  122.             }
  123.             else{
  124.                 *arg=&dest[oindex];
  125.             }
  126.             oindex=1;
  127.             c=0;
  128.         }
  129.  
  130.         return GO_OPT;
  131.     }
  132.     else{
  133.         *arg=dest;
  134.         oindex=1;
  135.         c=0;
  136.         return GO_ARG;
  137.     }
  138. }
  139.  
  140.  
  141.