diff options
author | David Shaw <[email protected]> | 2004-04-16 15:19:35 +0000 |
---|---|---|
committer | David Shaw <[email protected]> | 2004-04-16 15:19:35 +0000 |
commit | 0a17966a21d9ab55cd4c5169bf23f5eccb4c0b23 (patch) | |
tree | fbcdb04293fad3e935e6eb34038e9b22669853a8 /g10/misc.c | |
parent | * main.h, misc.c (argsplit): Refactor argsep into argsplit and argsep so (diff) | |
download | gnupg-0a17966a21d9ab55cd4c5169bf23f5eccb4c0b23.tar.gz gnupg-0a17966a21d9ab55cd4c5169bf23f5eccb4c0b23.zip |
* main.h, misc.c (optsep, argsplit, optlen, parse_options): Simplify code
and properly handle a partial match against an option with an argument.
* keyserver-internal.h, keyserver.c (parse_keyserver_options): Use new
optsep and argsplit functions.
Diffstat (limited to 'g10/misc.c')
-rw-r--r-- | g10/misc.c | 74 |
1 files changed, 43 insertions, 31 deletions
diff --git a/g10/misc.c b/g10/misc.c index 150ed6438..9ffe09de7 100644 --- a/g10/misc.c +++ b/g10/misc.c @@ -644,10 +644,10 @@ compliance_failure(void) opt.compliance=CO_GNUPG; } -/* Break a string into option pieces. Accepts single word options and - key=value argument options. */ +/* Break a string into successive option pieces. Accepts single word + options and key=value argument options. */ char * -argsplit(char **stringp) +optsep(char **stringp) { char *tok,*end; @@ -692,47 +692,59 @@ argsplit(char **stringp) return tok; } -/* Break an option or key=value option into key and value */ +/* Breaks an option value into key and value. Returns NULL if there + is no value. Note that "string" is modified to remove the =value + part. */ char * -argsep(char **stringp,char **arg) +argsplit(char *string) { - char *tok; - - *arg=NULL; + char *equals,*arg=NULL; - tok=argsplit(stringp); - if(tok) + equals=strchr(string,'='); + if(equals) { - char *equals; - equals=strchr(tok,'='); - if(equals) - { - char *space; - - space=strchr(tok,' '); - if(space) - *space='\0'; - else - *equals='\0'; + char *space; - space=strrchr(equals+1,' '); - if(space) - *arg=space+1; - else - *arg=NULL; + space=strchr(string,' '); + if(space) + { + *space='\0'; + arg=space+1; } + else + { + *equals='\0'; + arg=equals+1; + } + + space=strrchr(arg,' '); + if(space) + arg=space+1; } - return tok; + return arg; +} + +/* Return the length of the initial token, leaving off any + argument. */ +static size_t +optlen(const char *s) +{ + char *end=strpbrk(s," ="); + + if(end) + return end-s; + else + return strlen(s); } int parse_options(char *str,unsigned int *options, struct parse_options *opts,int noisy) { - char *tok,*arg; + char *tok; - while((tok=argsep(&str,&arg))) + while((tok=optsep(&str))) { int i,rev=0; char *otok=tok; @@ -748,7 +760,7 @@ parse_options(char *str,unsigned int *options, for(i=0;opts[i].name;i++) { - size_t toklen=strlen(tok); + size_t toklen=optlen(tok); if(ascii_strncasecmp(opts[i].name,tok,toklen)==0) { @@ -778,7 +790,7 @@ parse_options(char *str,unsigned int *options, { *options|=opts[i].bit; if(opts[i].value) - *opts[i].value=arg?m_strdup(arg):NULL; + *opts[i].value=argsplit(tok); } break; } |