From 0a17966a21d9ab55cd4c5169bf23f5eccb4c0b23 Mon Sep 17 00:00:00 2001 From: David Shaw Date: Fri, 16 Apr 2004 15:19:35 +0000 Subject: [PATCH] * 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. --- g10/ChangeLog | 9 +++++ g10/keyserver-internal.h | 2 +- g10/keyserver.c | 10 ++++-- g10/main.h | 4 +-- g10/misc.c | 74 +++++++++++++++++++++++----------------- 5 files changed, 62 insertions(+), 37 deletions(-) diff --git a/g10/ChangeLog b/g10/ChangeLog index 8db74fdec..c4485be42 100644 --- a/g10/ChangeLog +++ b/g10/ChangeLog @@ -1,3 +1,12 @@ +2004-04-16 David Shaw + + * 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. + 2004-04-15 David Shaw * main.h, misc.c (argsplit): Refactor argsep into argsplit and diff --git a/g10/keyserver-internal.h b/g10/keyserver-internal.h index b1f22be32..42b78048d 100644 --- a/g10/keyserver-internal.h +++ b/g10/keyserver-internal.h @@ -8,7 +8,7 @@ #include "iobuf.h" #include "types.h" -void parse_keyserver_options(char *options); +int parse_keyserver_options(char *options); struct keyserver_spec *parse_keyserver_uri(char *uri, const char *configname, unsigned int configlineno); diff --git a/g10/keyserver.c b/g10/keyserver.c index c4c43b87b..e7c4c93a3 100644 --- a/g10/keyserver.c +++ b/g10/keyserver.c @@ -70,12 +70,13 @@ static struct parse_options keyserver_opts[]= static int keyserver_work(int action,STRLIST list, KEYDB_SEARCH_DESC *desc,int count); -void +int parse_keyserver_options(char *options) { - char *tok,*arg; + int ret=1; + char *tok; - while((tok=argsep(&options,&arg))) + while((tok=optsep(&options))) { if(tok[0]=='\0') continue; @@ -108,6 +109,7 @@ parse_keyserver_options(char *options) { /* All of the standard options have failed, so the option is destined for a keyserver plugin. */ + char *arg=argsplit(tok); if(arg) { @@ -126,6 +128,8 @@ parse_keyserver_options(char *options) add_to_strlist(&opt.keyserver_options.other,tok); } } + + return ret; } struct keyserver_spec * diff --git a/g10/main.h b/g10/main.h index 08b925084..500d45d4f 100644 --- a/g10/main.h +++ b/g10/main.h @@ -108,8 +108,8 @@ struct parse_options char **value; }; -char *argsplit(char **stringp); -char *argsep(char **stringp,char **arg); +char *optsep(char **stringp); +char *argsplit(char *string); int parse_options(char *str,unsigned int *options, struct parse_options *opts,int noisy); 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; + char *equals,*arg=NULL; - *arg=NULL; - - tok=argsplit(stringp); - if(tok) + equals=strchr(string,'='); + if(equals) { - char *equals; - equals=strchr(tok,'='); - if(equals) + char *space; + + space=strchr(string,' '); + if(space) { - char *space; - - space=strchr(tok,' '); - if(space) - *space='\0'; - else - *equals='\0'; - - space=strrchr(equals+1,' '); - if(space) - *arg=space+1; - else - *arg=NULL; + *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; }