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_colors", OT_BOOL, BOOL_USE_COLORS, FALSE },
71 { "color_header_fg", OT_STR, STR_COLOR_HEADER_FG, UL "blue" },
72 { "color_header_fg", OT_STR, STR_COLOR_HEADER_FG, UL "blue" },
73 { "color_header_bg", OT_STR, STR_COLOR_HEADER_BG, UL "red" },
74 { "color_footer_fg", OT_STR, STR_COLOR_FOOTER_FG, UL "red" },
75 { "color_footer_bg", OT_STR, STR_COLOR_FOOTER_BG, UL "default" },
76 { "color_list_even_fg", OT_STR, STR_COLOR_LIST_EVEN_FG, UL "yellow" },
77 { "color_list_even_bg", OT_STR, STR_COLOR_LIST_EVEN_BG, UL "default" },
78 { "color_list_odd_fg", OT_STR, STR_COLOR_LIST_ODD_FG, UL "default" },
79 { "color_list_odd_bg", OT_STR, STR_COLOR_LIST_ODD_BG, UL "default" },
80 { "color_list_header_fg", OT_STR, STR_COLOR_LIST_HEADER_FG, UL "white" },
81 { "color_list_header_bg", OT_STR, STR_COLOR_LIST_HEADER_BG, UL "blue" },
82 { "color_list_highlight_fg", OT_STR, STR_COLOR_LIST_HIGHLIGHT_FG, UL "black" },
83 { "color_list_highlight_bg", OT_STR, STR_COLOR_LIST_HIGHLIGHT_BG, UL "green" },
84 { "color_tab_border_fg", OT_STR, STR_COLOR_TAB_BORDER_FG, UL "cyan" },
85 { "color_tab_border_bg", OT_STR, STR_COLOR_TAB_BORDER_BG, UL "default" },
86 { "color_tab_label_fg", OT_STR, STR_COLOR_TAB_LABEL_FG, UL "magenta" },
87 { "color_tab_label_bg", OT_STR, STR_COLOR_TAB_LABEL_BG, UL "default" },
88 { "color_field_name_fg", OT_STR, STR_COLOR_FIELD_NAME_FG, UL "yellow" },
89 { "color_field_name_bg", OT_STR, STR_COLOR_FIELD_NAME_BG, UL "default" },
90 { "color_field_value_fg", OT_STR, STR_COLOR_FIELD_VALUE_FG, UL "green" },
91 { "color_field_value_bg", OT_STR, STR_COLOR_FIELD_VALUE_BG, UL "default" },
95 static unsigned char bool_opts[BOOL_MAX];
96 static int int_opts[INT_MAXIMUM];
97 static char *str_opts[STR_MAX];
100 set_int(enum int_opts opt, int value)
102 assert(opt >= 0 && opt < INT_MAXIMUM);
104 int_opts[opt] = value;
108 set_bool(enum bool_opts opt, bool value)
110 assert(opt >= 0 && opt < BOOL_MAX);
112 bool_opts[opt] = value;
116 set_str(enum str_opts opt, char *value)
118 assert(opt >= 0 && opt < STR_MAX);
123 str_opts[opt] = xstrdup(value);
127 opt_get_int(enum int_opts opt)
129 assert(opt >= 0 && opt < INT_MAXIMUM);
131 return int_opts[opt];
135 opt_get_bool(enum bool_opts opt)
137 assert(opt >= 0 && opt < BOOL_MAX);
139 return bool_opts[opt];
143 opt_get_str(enum str_opts opt)
145 assert(opt >= 0 && opt < STR_MAX);
147 return str_opts[opt];
151 restore_default(struct option *p)
155 set_bool(p -> data, (bool)p -> init);
158 set_int(p -> data, (int)p -> init);
162 set_str(p -> data, (char *) p -> init);
174 for(i = 0; abook_vars[i].option; i++)
175 restore_default(&abook_vars[i]);
184 * only strings need to be freed
186 for(i = 0; i < STR_MAX; i++) {
201 opt_line_remove_comments(char *p)
203 bool in_quote = FALSE;
212 in_quote = !in_quote;
229 * - b->data points to the found token, or NULL is end of parsing
230 * - b->ptr points to the begining of next token
232 * If the TOKEN_ALLOC option is used, the original string is not mangled
233 * and memory is allocated for the token.
236 get_token(buffer *b, int options)
244 if(*b->ptr && strchr("\"'", *b->ptr))
256 ((options & TOKEN_EQUAL) && (c == '=')) ||
257 ((options & TOKEN_COMMA) && (c == ',')))
261 } else if(c == quote) {
271 return _("quote mismatch");
273 if(options & (TOKEN_EQUAL | TOKEN_COMMA))
274 SKIPWS(b->ptr); /* whitespaces can precede the sign */
276 if((options & TOKEN_EQUAL) && (*b->ptr != '='))
277 return _("no assignment character found");
279 if((options & TOKEN_COMMA) && *b->ptr && (*b->ptr != ','))
280 return _("error in comma separated list");
282 if(b->ptr == b->data) {
284 return NULL; /* no error, just end of parsing */
287 if(options & TOKEN_ALLOC) /* freeing is the caller's responsibility */
288 b->data = xstrndup(b->data, end - b->data);
292 b->ptr++; /* advance to next token */
299 opt_set_set_option(char *p, struct option *opt)
308 if(*p == '\"' && p[len - 1] == '\"') {
310 return _("invalid value");
315 switch(opt -> type) {
317 set_str(opt -> data, p);
320 set_int(opt -> data, safe_atoi(p));
323 if(!strcasecmp(p, "true") || !strcasecmp(p, "on"))
324 set_bool(opt -> data, TRUE);
325 else if(!strcasecmp(p, "false") ||
326 !strcasecmp(p, "off"))
327 set_bool(opt -> data, FALSE);
329 return _("invalid value");
339 opt_set_option(char *var, char *p)
346 for(i = 0; abook_vars[i].option; i++)
347 if(!strcmp(abook_vars[i].option, var))
348 return opt_set_set_option(p, &abook_vars[i]);
350 return _("unknown option");
359 str = opt_get_str(STR_PRESERVE_FIELDS);
360 if(strcasecmp(str, "all") && strcasecmp(str, "none") &&
361 strcasecmp(str, "standard")) {
362 fprintf(stderr, _("valid values for the 'preserve_fields' "
363 "option are 'all', 'standard' "
364 "(default), and 'none'\n"));
365 restore_default(&abook_vars[STR_PRESERVE_FIELDS]);
368 str = opt_get_str(STR_ADDRESS_STYLE);
369 if(strcasecmp(str, "eu") && strcasecmp(str, "uk") &&
370 strcasecmp(str, "us")) {
371 fprintf(stderr, _("valid values for the 'address_style' "
372 "option are 'eu' (default), 'uk', "
374 restore_default(&abook_vars[STR_ADDRESS_STYLE]);
382 * syntax: set <option> = <value>
385 opt_parse_set(buffer *b)
389 if((err = get_token(b, TOKEN_EQUAL)))
392 if((var = b->data) == NULL)
393 return _("invalid value assignment");
395 return opt_set_option(var, b->ptr);
399 opt_parse_customfield(buffer *b)
401 return _("customfield: obsolete command - please use the "
402 "'field' and 'view' commands instead");
405 #include "views.h" /* needed for add_field_to_view */
408 * syntax: view <tab name> = <field1> [ , <field2>, ... ]
411 opt_parse_view(buffer *b)
415 if((err = get_token(b, TOKEN_EQUAL)))
418 if((view = b->data) == NULL)
419 return _("no view name provided");
422 if((err = get_token(b, TOKEN_COMMA)))
428 if((err = add_field_to_view(view, b->data)))
435 #include "database.h" /* needed for declare_new_field */
438 * syntax: field <identifier> = <human readable name> [ , <type> ]
441 opt_parse_field(buffer *b)
443 char *err, *field, *name;
445 if((err = get_token(b, TOKEN_EQUAL)))
448 if((field = b->data) == NULL)
449 return _("no field identifier provided");
451 if((err = get_token(b, TOKEN_COMMA)))
454 if((name = b->data) == NULL)
455 return _("no field name provided");
457 if((err = declare_new_field(field,
460 0 /* reject "standard" fields */)))
469 const char * (*func) (buffer *line);
471 { "set", opt_parse_set },
472 { "customfield", opt_parse_customfield }, /* obsolete */
473 { "view", opt_parse_view },
474 { "field", opt_parse_field },
479 opt_parse_line(char *line, int n, char *fn)
482 const char *err = NULL;
490 if((err = get_token(&b, 0))) {
491 fprintf(stderr, "%s\n", err);
502 b.data = b.ptr = b.ptr;
504 for(i = 0; opt_parsers[i].token; i++)
505 if(!strcmp(opt_parsers[i].token, token)) {
506 if(!(err = opt_parsers[i].func(&b)))
511 fprintf(stderr, _("%s: parse error at line %d: "), fn, n);
513 fprintf(stderr, "%s\n", err);
515 fprintf(stderr, _("unknown token %s\n"), token);
521 load_opts(char *filename)
528 if((in = fopen(filename, "r")) == NULL)
531 for(n = 1;!feof(in); n++) {
538 opt_line_remove_comments(line);
540 err += opt_parse_line(line, n, filename) ? 1:0;
549 /* post-initialization */
550 err += check_options();
551 if(!strcasecmp(opt_get_str(STR_PRESERVE_FIELDS), "standard"))
552 init_standard_fields();