X-Git-Url: https://git.deb.at/?a=blobdiff_plain;f=options.c;h=92d56703c22d1782728aa895597f83f41e3b87df;hb=242907dcb6220a23b620f3314e96581b5b472a0d;hp=8196bcd50a74e1498dc08c52a8593b8bfe79c6e0;hpb=c5d8ef5198f2bfd02f678b7a709b6538ef83cd44;p=pkg%2Fabook.git diff --git a/options.c b/options.c index 8196bcd..92d5670 100644 --- a/options.c +++ b/options.c @@ -1,6 +1,6 @@ /* - * $Id: options.c,v 1.21 2003/12/05 18:11:55 jheinonen Exp $ + * $Id: options.c,v 1.33 2006/09/06 08:48:33 jheinonen Exp $ * * by JH * @@ -15,7 +15,10 @@ #include #include "options.h" #include "abook.h" +#include "gettext.h" #include "misc.h" +#include "views.h" +#include "xmalloc.h" #ifndef FALSE # define FALSE 0 @@ -47,25 +50,23 @@ static struct option abook_vars[] = { { "autosave", OT_BOOL, BOOL_AUTOSAVE, TRUE }, { "show_all_emails", OT_BOOL, BOOL_SHOW_ALL_EMAILS, TRUE }, - { "emailpos", OT_INT, INT_EMAILPOS, 25 }, - { "extra_column", OT_STR, STR_EXTRA_COLUMN, UL "phone" }, - { "extra_alternative", OT_STR, STR_EXTRA_ALTERNATIVE, UL "-1" }, - { "extrapos", OT_INT, INT_EXTRAPOS, 65 }, - + { "index_format", OT_STR, STR_INDEX_FORMAT, UL " {name:22} {email:40} {phone:12|workphone|mobile}" }, { "mutt_command", OT_STR, STR_MUTT_COMMAND, UL "mutt" }, { "mutt_return_all_emails", OT_BOOL, BOOL_MUTT_RETURN_ALL_EMAILS, TRUE }, - + { "print_command", OT_STR, STR_PRINT_COMMAND, UL "lpr" }, { "www_command", OT_STR, STR_WWW_COMMAND, UL "lynx" }, - + { "address_style", OT_STR, STR_ADDRESS_STYLE, UL "eu" }, { "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 }, { NULL } }; @@ -98,7 +99,7 @@ set_str(enum str_opts opt, char *value) if(str_opts[opt]) free(str_opts[opt]); - str_opts[opt] = strdup(value); + str_opts[opt] = xstrdup(value); } int @@ -186,10 +187,8 @@ opt_line_remove_comments(char *p) for(; *p; p++) { switch(*p) { case '\"': - if(!escape) { + if(!escape) in_quote = !in_quote; - escape = FALSE; - } break; case '\\': escape = TRUE; @@ -205,38 +204,89 @@ 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) { - assert(b); - - for(; ISSPACE(*b -> ptr); b -> ptr ++); -} + char quote = 0, c; + char *end = NULL; -void -find_token_end(buffer *b) -{ assert(b); - for(find_token_start(b); *(b -> ptr); b -> ptr ++) { - if(ISSPACE(*(b -> ptr))) { + SKIPWS(b->ptr); + if(*b->ptr && strchr("\"'", *b->ptr)) + quote = *(b->ptr++); + b->data = b->ptr; + + while(1) { + if(!(c = *b->ptr)) { + end = b->ptr; + break; + } + + 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 char * -opt_set_set_option(char *var, char *p, struct option *opt) +static const char * +opt_set_set_option(char *p, struct option *opt) { int len; - - strtrim(p); + assert(p); + + strtrim(p); len = strlen(p); - if(p[len - 1] == '\"' && *p == '\"') { + if(*p == '\"' && p[len - 1] == '\"') { if(len < 3) - return "invalid value"; + return _("invalid value"); p[len - 1] = 0; p++; } @@ -255,69 +305,152 @@ opt_set_set_option(char *var, char *p, struct option *opt) !strcasecmp(p, "off")) set_bool(opt -> data, FALSE); else - return "invalid value"; + return _("invalid value"); break; default: assert(0); } - + return NULL; } -static char * -opt_parse_set(buffer *b) +static const char * +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"; - - strtrim(b -> ptr); - - 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]); - - return "unknown option"; + assert(var); + assert(p); + + for(i = 0; abook_vars[i].option; i++) + if(!strcmp(abook_vars[i].option, var)) + return opt_set_set_option(p, &abook_vars[i]); + + return _("unknown option"); } -#include "database.h" /* needed for change_custom_field_name */ +static int +check_options() +{ + char *str; + int err = 0; -static char * + 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; +} + +/* + * syntax: set