getflags: import from 4e, with usage

fixes #6 http://bitbucket.org/rsc/plan9port/issue/6/

http://codereview.appspot.com/95043
This commit is contained in:
Russ Cox 2009-07-15 02:48:37 -04:00
parent 9bea9069bf
commit 3cd77ae679
7 changed files with 231 additions and 485 deletions

View file

@ -1,267 +0,0 @@
#include <u.h>
#include <libc.h>
#include <ctype.h>
#include "getflags.h"
char **flag[NFLAG];
char cmdline[NCMDLINE+1];
char *cmdname;
char *flagset[];
char *flagset[]={"<flag>"};
static char *flagarg="";
static void reverse(char **, char **);
static int scanflag(int, char *);
static int reason;
#define RESET 1
#define ARGCCOUNT 2
#define FLAGSYN 3
#define BADFLAG 4
static int badflag;
char *getflagsargv[NGETFLAGSARGV+2]; /* original argv stored here for people who need it */
int
getflags(int argc, char *argv[], char *flags)
{
char *s, *t;
int i, j, c, count;
flagarg=flags;
if(cmdname==0){
cmdname=argv[0];
for(i=0;i!=argc && i!=NGETFLAGSARGV;i++) getflagsargv[i]=argv[i];
if(argc>NGETFLAGSARGV) getflagsargv[i++]="...";
getflagsargv[i]=0;
}
s=cmdline;
for(i=0;i!=argc;i++){
for(t=argv[i];*t;)
if(s!=&cmdline[NCMDLINE])
*s++=*t++;
else
break;
if(i!=argc-1 && s!=&cmdline[NCMDLINE])
*s++=' ';
}
*s='\0';
i=1;
while(i!=argc && argv[i][0]=='-'){
s=argv[i]+1;
if(*s=='\0'){ /* if argument is "-", stop scanning and delete it */
for(j=i+1;j<=argc;j++)
argv[j-1]=argv[j];
return argc-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<count){
reason=ARGCCOUNT;
badflag=c;
return -1;
}
reverse(argv+i, argv+argc);
reverse(argv+i, argv+argc-count);
reverse(argv+argc-count+1, argv+argc);
argc-=count;
flag[c]=argv+argc+1;
flag[c][0]=s;
s="";
}
}
}
return argc;
}
void
static reverse(char **p, char **q)
{
register char *t;
for(;p<q;p++,--q){ t=*p; *p=*q; *q=t; }
}
static int
scanflag(int c, char *f)
{
int fc, count;
if(0<=c && c<NFLAG) while(*f){
if(*f==' '){
f++;
continue;
}
fc=*f++;
if(*f==':'){
f++;
if(!isdigit((uchar)*f)){ reason=FLAGSYN; return -1; }
count=strtol(f, &f, 10);
}
else
count=0;
if(*f=='['){
int depth=1;
do{
f++;
if(*f=='\0'){ reason=FLAGSYN; return -1; }
if(*f=='[') depth++;
if(*f==']') depth--;
}while(depth>0);
f++;
}
if(c==fc) return count;
}
reason=BADFLAG;
badflag=c;
return -1;
}
static void errn(char *, int), errs(char *), errc(int);
void
usage(char *tail)
{
char *s, *t, c;
int count, nflag=0;
switch(reason){
case RESET:
errs("Flag -");
errc(badflag);
errs(": set twice\n");
break;
case ARGCCOUNT:
errs("Flag -");
errc(badflag);
errs(": too few arguments\n");
break;
case FLAGSYN:
errs("Bad argument to getflags!\n");
break;
case BADFLAG:
errs("Illegal flag -");
errc(badflag);
errc('\n');
break;
}
errs("Usage: ");
errs(cmdname);
for(s=flagarg;*s;){
c=*s;
if(*s++==' ') continue;
if(*s==':'){
s++;
count=strtol(s, &s, 10);
}
else count=0;
if(count==0){
if(nflag==0) errs(" [-");
nflag++;
errc(c);
}
if(*s=='['){
int depth=1;
s++;
for(;*s!='\0' && depth>0; s++)
if (*s==']') depth--;
else if (*s=='[') depth++;
}
}
if(nflag) errs("]");
for(s=flagarg;*s;){
c=*s;
if(*s++==' ') continue;
if(*s==':'){
s++;
count=strtol(s, &s, 10);
}
else count=0;
if(count!=0){
errs(" [-");
errc(c);
if(*s=='['){
int depth=1;
s++;
t=s;
for(;*s!='\0' && depth>0; s++)
if (*s==']') depth--;
else if (*s=='[') depth++;
errs(" ");
errn(t, s-t);
}
else
while(count--) errs(" arg");
errs("]");
}
else if(*s=='['){
int depth=1;
s++;
for(;*s!='\0' && depth>0; s++)
if (*s==']') depth--;
else if (*s=='[') depth++;
}
}
if(tail){
errs(" ");
errs(tail);
}
errs("\n");
exits("usage");
}
static void
errn(char *s, int count)
{
while(count){ errc(*s++); --count; }
}
static void
errs(char *s)
{
while(*s) errc(*s++);
}
#define NBUF 80
static char buf[NBUF], *bufp=buf;
static void
errc(int c){
*bufp++=c;
if(bufp==&buf[NBUF] || c=='\n'){
write(2, buf, bufp-buf);
bufp=buf;
}
}
#ifdef TEST
#include <stdio.h>
main(int argc, char *argv[])
{
int c, i, n;
if(argc<3){
fprint(2, "Usage: %s flags cmd ...\n", argv[0]);
exits("usage");
}
n=getflags(argc-2, argv+2, argv[1]);
if(n<0) usage("...");
putchar('\n');
for(c=0;c!=128;c++) if(flag[c]){
print("\t-.%c. ", c);
n=scanflag(c, argv[1]);
for(i=0;i!=n;i++) print(" <%s>", flag[c][i]);
putchar('\n');
}
}
#endif

View file

@ -1,199 +0,0 @@
/*% cyntax % && cc -go # %
* getflags: process flags for command files
* Usage: ifs='' eval `{getflags [-s] flagfmt [arg ...]} # rc
* Usage: IFS= eval `getflags -b [-s] flagfmt [arg...]` # Bourne shell
* -b means give Bourne-shell compatible output
*/
#include <u.h>
#include <libc.h>
#include "getflags.h"
/* predefine functions */
void bourneprint(int, char *[]);
void bournearg(char *);
void rcprint(int, char *[]);
void usmsg(char *);
int count(int, char *);
void rcarg(char *);
void
main(int argc, char *argv[])
{
int bourne;
argc=getflags(argc, argv, "b");
if(argc<2) usage("flagfmt [arg ...]");
bourne=flag['b']!=0;
flag['b']=0;
if((argc=getflags(argc-1, argv+1, argv[1]))<0){
usmsg(argv[1]);
exits(0);
}
if(bourne) bourneprint(argc, argv);
else rcprint(argc, argv);
exits(0);
}
void
bourneprint(int argc, char *argv[])
{
register int c, i, n;
for(c=0;c!=NFLAG;c++) if(flag[c]){
print("FLAG%c=", c); /* bug -- c could be a bad char */
n=count(c, argv[1]);
if(n==0)
print("1\n");
else{
print("'");
bournearg(flag[c][0]);
for(i=1;i!=n;i++){
print(" ");
bournearg(flag[c][i]);
}
print("'\n");
}
}
print("set --");
for(c=1;c!=argc;c++){
print(" ");
bournearg(argv[c+1]);
}
print("\n");
}
void
bournearg(char *s)
{
for(;*s;s++)
if(*s=='\'')
print("'\\''");
else
print("%c", *s);
}
void
rcprint(int argc, char *argv[])
{
int c, i, n;
for(c=0;c!=NFLAG;c++) if(flag[c]){
print("FLAG%c=", c); /* bug -- c could be a bad char */
n=count(c, argv[1]);
if(n==0)
print("''");
else if(n==1)
rcarg(flag[c][0]);
else{
print("(");
rcarg(flag[c][0]);
for(i=1;i!=n;i++){
print(" ");
rcarg(flag[c][i]);
}
print(")");
}
print("\n");
}
print("*=");
if(argc==1) print("()");
else if(argc==2) rcarg(argv[2]);
else{
print("(");
rcarg(argv[2]);
for(c=2;c!=argc;c++){
print(" ");
rcarg(argv[c+1]);
}
print(")");
}
print("\n");
}
void
usmsg(char *flagarg)
{
char *s, *t, c;
int count, nflag=0;
print("echo Usage: $0'");
for(s=flagarg;*s;){
c=*s;
if(*s++==' ') continue;
if(*s==':')
count = strtol(s+1, &s, 10);
else count=0;
if(count==0){
if(nflag==0) print(" [-");
nflag++;
print("%c", c);
}
if(*s=='['){
int depth=1;
s++;
for(;*s!='\0' && depth>0; s++)
if (*s==']') depth--;
else if (*s=='[') depth++;
}
}
if(nflag) print("]");
for(s=flagarg;*s;){
c=*s;
if(*s++==' ') continue;
if(*s==':')
count = strtol(s+1, &s, 10);
else count=0;
if(count!=0){
print(" [-");
print("%c", c);
if(*s=='['){
int depth=1;
s++;
t=s;
for(;*s!='\0' && depth>0; s++)
if (*s==']') depth--;
else if (*s=='[') depth++;
print(" ");
write(1, t, s - t);
}
else
while(count--) print(" arg");
print("]");
}
else if(*s=='['){
int depth=1;
s++;
for(;*s!='\0' && depth>0; s++)
if (*s==']') depth--;
else if (*s=='[') depth++;
}
}
print("' $usage;\n");
print("exit 'usage'\n");
}
int
count(int flag, char *flagarg)
{
char *s, c;
int n;
for(s=flagarg;*s;){
c=*s;
if(*s++==' ') continue;
if(*s==':')
n = strtol(s+1, &s, 10);
else n=0;
if(*s=='['){
int depth=1;
s++;
for(;*s!='\0' && depth>0; s++)
if (*s==']') depth--;
else if (*s=='[') depth++;
}
if(c==flag) return n;
}
return -1; /* never happens */
}
void
rcarg(char *s)
{
if(*s=='\0' || strpbrk(s, "\n \t#;&|^$=`'{}()<>?")){
print("\'");
for(;*s;s++)
if(*s=='\'') print("''");
else print("%c", *s);
print("\'");
}
else print("%s", s);
}

View file

@ -1,10 +0,0 @@
#define NFLAG 128
#define NCMDLINE 512
#define NGETFLAGSARGV 256
extern char **flag[NFLAG];
extern char cmdline[NCMDLINE+1];
extern char *cmdname;
extern char *flagset[];
extern char *getflagsargv[NGETFLAGSARGV+2];
int getflags(int, char *[], char *);
void usage(char *);

View file

@ -1,9 +0,0 @@
<$PLAN9/src/mkhdr
TARG=getflags
OFILES=\
getflags.$O\
funcgetflags.$O\
<$PLAN9/src/mkone