X-Git-Url: https://git.deb.at/w?a=blobdiff_plain;f=options.c;fp=options.c;h=e9e0b3660ff7e49899e11c38ef5fcef86ba6ee72;hb=7751b3edc145646e6b55e2fda82eb5619fbe0074;hp=3b8c338b059c316796ebb2a7a3a38e9b371ed89a;hpb=627bebd233fee0136a6ffbef005da3e62a87f99b;p=pkg%2Fabook.git diff --git a/options.c b/options.c index 3b8c338..e9e0b36 100644 --- a/options.c +++ b/options.c @@ -1,6 +1,6 @@ /* - * $Id: options.c,v 1.27.4.1 2006/04/09 18:57:34 jheinonen Exp $ + * $Id: options.c,v 1.30 2006/08/07 11:39:53 jheinonen Exp $ * * by JH * @@ -17,6 +17,7 @@ #include "abook.h" #include "gettext.h" #include "misc.h" +#include "views.h" #include "xmalloc.h" #ifndef FALSE @@ -67,6 +68,7 @@ static struct option abook_vars[] = { { "use_ascii_only", OT_BOOL, BOOL_USE_ASCII_ONLY, FALSE }, { "add_email_prevent_duplicates", OT_BOOL, BOOL_ADD_EMAIL_PREVENT_DUPLICATES, FALSE }, + { "preserve_fields", OT_STR, STR_PRESERVE_FIELDS, UL "standard" }, { "sort_field", OT_STR, STR_SORT_FIELD, UL "nick" }, { "show_cursor", OT_BOOL, BOOL_SHOW_CURSOR, FALSE }, @@ -206,28 +208,78 @@ opt_line_remove_comments(char *p) } } -void -find_token_start(buffer *b) +/* After calling, + * - b->data points to the found token, or NULL is end of parsing + * - b->ptr points to the begining of next token + * + * If the TOKEN_ALLOC option is used, the original string is not mangled + * and memory is allocated for the token. + */ +static char * +get_token(buffer *b, int options) { + char quote = 0, c; + char *end = NULL; + assert(b); - for(; ISSPACE(*b -> ptr); b -> ptr ++); -} + SKIPWS(b->ptr); + if(*b->ptr && strchr("\"'", *b->ptr)) + quote = *(b->ptr++); + b->data = b->ptr; -void -find_token_end(buffer *b) -{ - assert(b); + while(1) { + if(!(c = *b->ptr)) { + end = b->ptr; + break; + } - for(find_token_start(b); *(b -> ptr); b -> ptr ++) { - if(ISSPACE(*(b -> ptr))) { + if(!quote && ( + ISSPACE(c) || + ((options & TOKEN_EQUAL) && (c == '=')) || + ((options & TOKEN_COMMA) && (c == ','))) + ) { + end = b->ptr; + break; + } else if(c == quote) { + quote = 0; + end = b->ptr++; break; } + + b->ptr++; } + + if(quote) + return _("quote mismatch"); + + if(options & (TOKEN_EQUAL | TOKEN_COMMA)) + SKIPWS(b->ptr); /* whitespaces can precede the sign */ + + if((options & TOKEN_EQUAL) && (*b->ptr != '=')) + return _("no assignment character found"); + + if((options & TOKEN_COMMA) && *b->ptr && (*b->ptr != ',')) + return _("error in comma separated list"); + + if(b->ptr == b->data) { + b->data = NULL; + return NULL; /* no error, just end of parsing */ + } + + if(options & TOKEN_ALLOC) /* freeing is the caller's responsibility */ + b->data = xstrndup(b->data, end - b->data); + else + *end = 0; + + b->ptr++; /* advance to next token */ + SKIPWS(b->ptr); + + return NULL; } static const char * -opt_set_set_option(char *var, char *p, struct option *opt) +opt_set_set_option(char *p, struct option *opt) { int len; @@ -266,59 +318,139 @@ opt_set_set_option(char *var, char *p, struct option *opt) } static const char * -opt_parse_set(buffer *b) +opt_set_option(char *var, char *p) { int i; - char *p; - find_token_start(b); - if((p = strchr(b -> ptr, '='))) - *p++ = 0; - else - return _("invalid value assignment"); + for(i = 0; abook_vars[i].option; i++) + if(!strcmp(abook_vars[i].option, var)) + return opt_set_set_option(p, &abook_vars[i]); - strtrim(b -> ptr); + return _("unknown option"); +} - for(i = 0;abook_vars[i].option; i++) - if(!strcmp(abook_vars[i].option, b -> ptr)) - return opt_set_set_option(b -> ptr, p, &abook_vars[i]); +static int +check_options() +{ + char *str; + int err = 0; - return _("unknown option"); + str = opt_get_str(STR_PRESERVE_FIELDS); + if(strcasecmp(str, "all") && strcasecmp(str, "none") && + strcasecmp(str, "standard")) { + fprintf(stderr, _("valid values for the 'preserve_fields' " + "option are 'all', 'standard' " + "(default), and 'none'\n")); + restore_default(&abook_vars[STR_PRESERVE_FIELDS]); + err++; + } + str = opt_get_str(STR_ADDRESS_STYLE); + if(strcasecmp(str, "eu") && strcasecmp(str, "uk") && + strcasecmp(str, "us")) { + fprintf(stderr, _("valid values for the 'address_style' " + "option are 'eu' (default), 'uk', " + "and 'us'\n")); + restore_default(&abook_vars[STR_ADDRESS_STYLE]); + err++; + } + + return err; } -#include "database.h" /* needed for change_custom_field_name */ +/* + * syntax: set