]> git.deb.at Git - pkg/abook.git/blobdiff - options.c
- i18n support
[pkg/abook.git] / options.c
index 3947a38a959855a5621c4673c00bafbb8b198033..8a0f7e9db71cccf22d879f29cac0d8bb537776cc 100644 (file)
--- a/options.c
+++ b/options.c
@@ -15,7 +15,9 @@
 #include <assert.h>
 #include "options.h"
 #include "abook.h"
+#include "gettext.h"
 #include "misc.h"
+#include "xmalloc.h"
 
 #ifndef FALSE
 #      define FALSE    0
@@ -55,28 +57,30 @@ static struct option abook_vars[] = {
        { "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 },
+       { "sort_field", OT_STR, STR_SORT_FIELD, UL "nick" },
+       { "show_cursor", OT_BOOL, BOOL_SHOW_CURSOR, FALSE },
 
        { NULL }
 };
 
 static unsigned char bool_opts[BOOL_MAX];
-static int int_opts[INT_MAX];
+static int int_opts[INT_MAXIMUM];
 static char *str_opts[STR_MAX];
 
 static void
 set_int(enum int_opts opt, int value)
 {
-       assert(opt >= 0 && opt < INT_MAX);
+       assert(opt >= 0 && opt < INT_MAXIMUM);
 
        int_opts[opt] = value;
 }
@@ -97,13 +101,13 @@ 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
 opt_get_int(enum int_opts opt)
 {
-       assert(opt >= 0 && opt < INT_MAX);
+       assert(opt >= 0 && opt < INT_MAXIMUM);
 
        return int_opts[opt];
 }
@@ -208,7 +212,7 @@ void
 find_token_start(buffer *b)
 {
        assert(b);
-       
+
        for(; ISSPACE(*b -> ptr); b -> ptr ++);
 }
 
@@ -228,14 +232,14 @@ static char *
 opt_set_set_option(char *var, char *p, struct option *opt)
 {
        int len;
-       
+
        strtrim(p);
 
        len = strlen(p);
 
        if(p[len - 1] == '\"' && *p == '\"') {
                if(len < 3)
-                       return "invalid value";
+                       return _("invalid value");
                p[len - 1] = 0;
                p++;
        }
@@ -254,10 +258,12 @@ 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;
 }
 
@@ -271,23 +277,50 @@ opt_parse_set(buffer *b)
        if((p = strchr(b -> ptr, '=')))
                *p++ = 0;
        else
-               return "invalid value assignment";
-       
+               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";
+
+       return _("unknown option");
 }
 
+#include "database.h" /* needed for change_custom_field_name */
+
+static char *
+opt_parse_customfield(buffer *b)
+{
+       char *p, num[5];
+       int n;
+       size_t len;
+
+       find_token_start(b);
+       p = b -> ptr;
+       find_token_end(b);
+
+       memset(num, 0, sizeof(num));
+
+       len = (b -> ptr - p);
+       strncpy(num, p, min(sizeof(num) - 1, len));
+       n = safe_atoi(num);
+
+       find_token_start(b);
+
+       if(change_custom_field_name(b->ptr, n) == -1)
+               return _("invalid custom field number");
+
+       return NULL;
+}
 
 static struct {
        char *token;
        char * (*func) (buffer *line);
 } opt_parsers[] = {
        { "set", opt_parse_set },
+       { "customfield", opt_parse_customfield },
        { NULL }
 };
 
@@ -298,7 +331,7 @@ opt_parse_line(char *line, int n, char *fn)
        char *err = NULL;
        char *token;
        buffer b;
-       
+
        assert(line && fn);
 
        b.ptr = line;
@@ -323,12 +356,12 @@ opt_parse_line(char *line, int n, char *fn)
                                return FALSE;
                        break;
                }
-       
-       fprintf(stderr, "%s: parse error at line %d: ", fn, n);
+
+       fprintf(stderr, _("%s: parse error at line %d: "), fn, n);
        if(err)
                fprintf(stderr, "%s\n", err);
        else
-               fprintf(stderr, "unknown token %s\n", token);
+               fprintf(stderr, _("unknown token %s\n"), token);
 
        return TRUE;
 }
@@ -339,12 +372,12 @@ load_opts(char *filename)
        FILE *in;
        char *line = NULL;
        int n;
-       bool err = FALSE;
-       
+       int err = 0;
+
        if((in = fopen(filename, "r")) == NULL)
                return -1;
 
-       
+
        for(n = 1;!feof(in); n++) {
                line = getaline(in);
 
@@ -354,19 +387,15 @@ load_opts(char *filename)
                if(line && *line) {
                        opt_line_remove_comments(line);
                        if(*line)
-                               err = opt_parse_line(line, n, filename);
+                               err += opt_parse_line(line, n, filename) ? 1:0;
                }
 
-               my_free(line);
+               free(line);
+               line = NULL;
        }
 
        free(line);
 
-       if(err) {
-               printf("Press enter to continue...\n");
-               fgetc(stdin);
-       }
-       
        return err;
 }