5 * by JH <jheinonen@users.sourceforge.net>
7 * Copyright (C) Jaakko Heinonen
30 #define UL (unsigned long)
49 static struct option abook_vars[] = {
50 { "autosave", OT_BOOL, BOOL_AUTOSAVE, TRUE },
52 { "show_all_emails", OT_BOOL, BOOL_SHOW_ALL_EMAILS, TRUE },
53 { "index_format", OT_STR, STR_INDEX_FORMAT, UL " {name:22} {email:40} {phone:12|workphone|mobile}" },
54 { "mutt_command", OT_STR, STR_MUTT_COMMAND, UL "mutt" },
55 { "mutt_return_all_emails", OT_BOOL, BOOL_MUTT_RETURN_ALL_EMAILS,
58 { "print_command", OT_STR, STR_PRINT_COMMAND, UL "lpr" },
60 { "www_command", OT_STR, STR_WWW_COMMAND, UL "lynx" },
62 { "address_style", OT_STR, STR_ADDRESS_STYLE, UL "eu" },
64 { "use_ascii_only", OT_BOOL, BOOL_USE_ASCII_ONLY, FALSE },
66 { "add_email_prevent_duplicates", OT_BOOL, BOOL_ADD_EMAIL_PREVENT_DUPLICATES, FALSE },
67 { "preserve_fields", OT_STR, STR_PRESERVE_FIELDS, UL "standard" },
68 { "sort_field", OT_STR, STR_SORT_FIELD, UL "nick" },
69 { "show_cursor", OT_BOOL, BOOL_SHOW_CURSOR, FALSE },
70 { "use_mouse", OT_BOOL, BOOL_USE_MOUSE, FALSE },
71 { "use_colors", OT_BOOL, BOOL_USE_COLORS, FALSE },
72 { "color_header_fg", OT_STR, STR_COLOR_HEADER_FG, UL "blue" },
73 { "color_header_fg", OT_STR, STR_COLOR_HEADER_FG, UL "blue" },
74 { "color_header_bg", OT_STR, STR_COLOR_HEADER_BG, UL "red" },
75 { "color_footer_fg", OT_STR, STR_COLOR_FOOTER_FG, UL "red" },
76 { "color_footer_bg", OT_STR, STR_COLOR_FOOTER_BG, UL "default" },
77 { "color_list_even_fg", OT_STR, STR_COLOR_LIST_EVEN_FG, UL "yellow" },
78 { "color_list_even_bg", OT_STR, STR_COLOR_LIST_EVEN_BG, UL "default" },
79 { "color_list_odd_fg", OT_STR, STR_COLOR_LIST_ODD_FG, UL "default" },
80 { "color_list_odd_bg", OT_STR, STR_COLOR_LIST_ODD_BG, UL "default" },
81 { "color_list_header_fg", OT_STR, STR_COLOR_LIST_HEADER_FG, UL "white" },
82 { "color_list_header_bg", OT_STR, STR_COLOR_LIST_HEADER_BG, UL "blue" },
83 { "color_list_highlight_fg", OT_STR, STR_COLOR_LIST_HIGHLIGHT_FG, UL "black" },
84 { "color_list_highlight_bg", OT_STR, STR_COLOR_LIST_HIGHLIGHT_BG, UL "green" },
85 { "color_tab_border_fg", OT_STR, STR_COLOR_TAB_BORDER_FG, UL "cyan" },
86 { "color_tab_border_bg", OT_STR, STR_COLOR_TAB_BORDER_BG, UL "default" },
87 { "color_tab_label_fg", OT_STR, STR_COLOR_TAB_LABEL_FG, UL "magenta" },
88 { "color_tab_label_bg", OT_STR, STR_COLOR_TAB_LABEL_BG, UL "default" },
89 { "color_field_name_fg", OT_STR, STR_COLOR_FIELD_NAME_FG, UL "yellow" },
90 { "color_field_name_bg", OT_STR, STR_COLOR_FIELD_NAME_BG, UL "default" },
91 { "color_field_value_fg", OT_STR, STR_COLOR_FIELD_VALUE_FG, UL "green" },
92 { "color_field_value_bg", OT_STR, STR_COLOR_FIELD_VALUE_BG, UL "default" },
96 static unsigned char bool_opts[BOOL_MAX];
97 static int int_opts[INT_MAXIMUM];
98 static char *str_opts[STR_MAX];
101 set_int(enum int_opts opt, int value)
103 assert(opt >= 0 && opt < INT_MAXIMUM);
105 int_opts[opt] = value;
109 set_bool(enum bool_opts opt, bool value)
111 assert(opt >= 0 && opt < BOOL_MAX);
113 bool_opts[opt] = value;
117 set_str(enum str_opts opt, char *value)
119 assert(opt >= 0 && opt < STR_MAX);
124 str_opts[opt] = xstrdup(value);
128 opt_get_int(enum int_opts opt)
130 assert(opt >= 0 && opt < INT_MAXIMUM);
132 return int_opts[opt];
136 opt_get_bool(enum bool_opts opt)
138 assert(opt >= 0 && opt < BOOL_MAX);
140 return bool_opts[opt];
144 opt_get_str(enum str_opts opt)
146 assert(opt >= 0 && opt < STR_MAX);
148 return str_opts[opt];
152 restore_default(struct option *p)
156 set_bool(p -> data, (bool)p -> init);
159 set_int(p -> data, (int)p -> init);
163 set_str(p -> data, (char *) p -> init);
175 for(i = 0; abook_vars[i].option; i++)
176 restore_default(&abook_vars[i]);
185 * only strings need to be freed
187 for(i = 0; i < STR_MAX; i++) {
202 opt_line_remove_comments(char *p)
204 bool in_quote = FALSE;
213 in_quote = !in_quote;
230 * - b->data points to the found token, or NULL is end of parsing
231 * - b->ptr points to the begining of next token
233 * If the TOKEN_ALLOC option is used, the original string is not mangled
234 * and memory is allocated for the token.
237 get_token(buffer *b, int options)
245 if(*b->ptr && strchr("\"'", *b->ptr))
257 ((options & TOKEN_EQUAL) && (c == '=')) ||
258 ((options & TOKEN_COMMA) && (c == ',')))
262 } else if(c == quote) {
272 return _("quote mismatch");
274 if(options & (TOKEN_EQUAL | TOKEN_COMMA))
275 SKIPWS(b->ptr); /* whitespaces can precede the sign */
277 if((options & TOKEN_EQUAL) && (*b->ptr != '='))
278 return _("no assignment character found");
280 if((options & TOKEN_COMMA) && *b->ptr && (*b->ptr != ','))
281 return _("error in comma separated list");
283 if(b->ptr == b->data) {
285 return NULL; /* no error, just end of parsing */
288 if(options & TOKEN_ALLOC) /* freeing is the caller's responsibility */
289 b->data = xstrndup(b->data, end - b->data);
293 b->ptr++; /* advance to next token */
300 opt_set_set_option(char *p, struct option *opt)
309 if(*p == '\"' && p[len - 1] == '\"') {
311 return _("invalid value");
316 switch(opt -> type) {
318 set_str(opt -> data, p);
321 set_int(opt -> data, safe_atoi(p));
324 if(!strcasecmp(p, "true") || !strcasecmp(p, "on"))
325 set_bool(opt -> data, TRUE);
326 else if(!strcasecmp(p, "false") ||
327 !strcasecmp(p, "off"))
328 set_bool(opt -> data, FALSE);
330 return _("invalid value");
340 opt_set_option(char *var, char *p)
347 for(i = 0; abook_vars[i].option; i++)
348 if(!strcmp(abook_vars[i].option, var))
349 return opt_set_set_option(p, &abook_vars[i]);
351 return _("unknown option");
360 str = opt_get_str(STR_PRESERVE_FIELDS);
361 if(strcasecmp(str, "all") && strcasecmp(str, "none") &&
362 strcasecmp(str, "standard")) {
363 fprintf(stderr, _("valid values for the 'preserve_fields' "
364 "option are 'all', 'standard' "
365 "(default), and 'none'\n"));
366 restore_default(&abook_vars[STR_PRESERVE_FIELDS]);
369 str = opt_get_str(STR_ADDRESS_STYLE);
370 if(strcasecmp(str, "eu") && strcasecmp(str, "uk") &&
371 strcasecmp(str, "us")) {
372 fprintf(stderr, _("valid values for the 'address_style' "
373 "option are 'eu' (default), 'uk', "
375 restore_default(&abook_vars[STR_ADDRESS_STYLE]);
383 * syntax: set <option> = <value>
386 opt_parse_set(buffer *b)
390 if((err = get_token(b, TOKEN_EQUAL)))
393 if((var = b->data) == NULL)
394 return _("invalid value assignment");
396 return opt_set_option(var, b->ptr);
400 opt_parse_customfield(buffer *b)
402 return _("customfield: obsolete command - please use the "
403 "'field' and 'view' commands instead");
406 #include "views.h" /* needed for add_field_to_view */
409 * syntax: view <tab name> = <field1> [ , <field2>, ... ]
412 opt_parse_view(buffer *b)
416 if((err = get_token(b, TOKEN_EQUAL)))
419 if((view = b->data) == NULL)
420 return _("no view name provided");
423 if((err = get_token(b, TOKEN_COMMA)))
429 if((err = add_field_to_view(view, b->data)))
436 #include "database.h" /* needed for declare_new_field */
439 * syntax: field <identifier> = <human readable name> [ , <type> ]
442 opt_parse_field(buffer *b)
444 char *err, *field, *name;
446 if((err = get_token(b, TOKEN_EQUAL)))
449 if((field = b->data) == NULL)
450 return _("no field identifier provided");
452 if((err = get_token(b, TOKEN_COMMA)))
455 if((name = b->data) == NULL)
456 return _("no field name provided");
458 if((err = declare_new_field(field,
461 0 /* reject "standard" fields */)))
470 const char * (*func) (buffer *line);
472 { "set", opt_parse_set },
473 { "customfield", opt_parse_customfield }, /* obsolete */
474 { "view", opt_parse_view },
475 { "field", opt_parse_field },
480 opt_parse_line(char *line, int n, char *fn)
483 const char *err = NULL;
491 if((err = get_token(&b, 0))) {
492 fprintf(stderr, "%s\n", err);
503 b.data = b.ptr = b.ptr;
505 for(i = 0; opt_parsers[i].token; i++)
506 if(!strcmp(opt_parsers[i].token, token)) {
507 if(!(err = opt_parsers[i].func(&b)))
512 fprintf(stderr, _("%s: parse error at line %d: "), fn, n);
514 fprintf(stderr, "%s\n", err);
516 fprintf(stderr, _("unknown token %s\n"), token);
522 load_opts(char *filename)
529 if((in = fopen(filename, "r")) == NULL)
532 for(n = 1;!feof(in); n++) {
539 opt_line_remove_comments(line);
541 err += opt_parse_line(line, n, filename) ? 1:0;
550 /* post-initialization */
551 err += check_options();
552 if(!strcasecmp(opt_get_str(STR_PRESERVE_FIELDS), "standard"))
553 init_standard_fields();