/*% cyntax -DTEST % && cc -DTEST -go # % */ #include "rc.h" #include "getflags.h" #include "fns.h" char *flagset[]={""}; char **flag[NFLAG]; char cmdline[NCMDLINE+1]; char *cmdname; static char *flagarg=""; static reverse(char**, char**); static scanflag(int, char*); static errn(char*, int); static errs(char*); static errc(int); static int reason; #define RESET 1 #define FEWARGS 2 #define FLAGSYN 3 #define BADFLAG 4 static int badflag; int getflags(int argc, char *argv[], char *flags, int stop) { char *s, *t; int i, j, c, count; flagarg=flags; if(cmdname==0) cmdname=argv[0]; s=cmdline; for(i=0;i!=argc;i++){ for(t=argv[i];*t;t++) if(s!=&cmdline[NCMDLINE]) *s++=*t; if(i!=argc-1 && s!=&cmdline[NCMDLINE]) *s++=' '; } *s='\0'; i=1; while(i!=argc){ if(argv[i][0]!='-' || argv[i][1]=='\0'){ if(stop) return argc; i++; continue; } s=argv[i]+1; while(*s){ c=*s++; count=scanflag(c, flags); if(count==-1) return -1; if(flag[c]){ reason=RESET; badflag=c; return -1; } if(count==0){ flag[c]=flagset; if(*s=='\0'){ for(j=i+1;j<=argc;j++) argv[j-1]=argv[j]; --argc; } } else{ if(*s=='\0'){ for(j=i+1;j<=argc;j++) argv[j-1]=argv[j]; --argc; s=argv[i]; } if(argc-i