5 * by JH <jheinonen@users.sourceforge.net>
7 * Copyright (C) Jaakko Heinonen
28 #define UL (unsigned long)
47 static struct option abook_vars[] = {
48 { "autosave", OT_BOOL, BOOL_AUTOSAVE, TRUE },
50 { "show_all_emails", OT_BOOL, BOOL_SHOW_ALL_EMAILS, TRUE },
51 { "emailpos", OT_INT, INT_EMAILPOS, 25 },
52 { "extra_column", OT_STR, STR_EXTRA_COLUMN, UL "phone" },
53 { "extra_alternative", OT_STR, STR_EXTRA_ALTERNATIVE, UL "-1" },
54 { "extrapos", OT_INT, INT_EXTRAPOS, 65 },
56 { "mutt_command", OT_STR, STR_MUTT_COMMAND, UL "mutt" },
57 { "mutt_return_all_emails", OT_BOOL, BOOL_MUTT_RETURN_ALL_EMAILS,
60 { "print_command", OT_STR, STR_PRINT_COMMAND, UL "lpr" },
62 { "www_command", OT_STR, STR_WWW_COMMAND, UL "lynx" },
64 { "address_style", OT_STR, STR_ADDRESS_STYLE, UL "eu" },
66 { "use_ascii_only", OT_BOOL, BOOL_USE_ASCII_ONLY, FALSE },
68 { "add_email_prevent_duplicates", OT_BOOL, BOOL_ADD_EMAIL_PREVENT_DUPLICATES, FALSE },
69 { "sort_field", OT_STR, STR_SORT_FIELD, UL "nick" },
70 { "show_cursor", OT_BOOL, BOOL_SHOW_CURSOR, FALSE },
75 static unsigned char bool_opts[BOOL_MAX];
76 static int int_opts[INT_MAXIMUM];
77 static char *str_opts[STR_MAX];
80 set_int(enum int_opts opt, int value)
82 assert(opt >= 0 && opt < INT_MAXIMUM);
84 int_opts[opt] = value;
88 set_bool(enum bool_opts opt, bool value)
90 assert(opt >= 0 && opt < BOOL_MAX);
92 bool_opts[opt] = value;
96 set_str(enum str_opts opt, char *value)
98 assert(opt >= 0 && opt < STR_MAX);
103 str_opts[opt] = xstrdup(value);
107 opt_get_int(enum int_opts opt)
109 assert(opt >= 0 && opt < INT_MAXIMUM);
111 return int_opts[opt];
115 opt_get_bool(enum bool_opts opt)
117 assert(opt >= 0 && opt < BOOL_MAX);
119 return bool_opts[opt];
123 opt_get_str(enum str_opts opt)
125 assert(opt >= 0 && opt < STR_MAX);
127 return str_opts[opt];
131 restore_default(struct option *p)
135 set_bool(p -> data, (bool)p -> init);
138 set_int(p -> data, (int)p -> init);
142 set_str(p -> data, (char *) p -> init);
154 for(i = 0; abook_vars[i].option; i++)
155 restore_default(&abook_vars[i]);
164 * only strings need to be freed
166 for(i = 0; i < STR_MAX; i++) {
181 opt_line_remove_comments(char *p)
183 bool in_quote = FALSE;
192 in_quote = !in_quote;
211 find_token_start(buffer *b)
215 for(; ISSPACE(*b -> ptr); b -> ptr ++);
219 find_token_end(buffer *b)
223 for(find_token_start(b); *(b -> ptr); b -> ptr ++) {
224 if(ISSPACE(*(b -> ptr))) {
231 opt_set_set_option(char *var, char *p, struct option *opt)
239 if(p[len - 1] == '\"' && *p == '\"') {
241 return "invalid value";
246 switch(opt -> type) {
248 set_str(opt -> data, p);
251 set_int(opt -> data, safe_atoi(p));
254 if(!strcasecmp(p, "true") || !strcasecmp(p, "on"))
255 set_bool(opt -> data, TRUE);
256 else if(!strcasecmp(p, "false") ||
257 !strcasecmp(p, "off"))
258 set_bool(opt -> data, FALSE);
260 return "invalid value";
270 opt_parse_set(buffer *b)
276 if((p = strchr(b -> ptr, '=')))
279 return "invalid value assignment";
283 for(i = 0;abook_vars[i].option; i++)
284 if(!strcmp(abook_vars[i].option, b -> ptr))
285 return opt_set_set_option(b -> ptr, p, &abook_vars[i]);
287 return "unknown option";
290 #include "database.h" /* needed for change_custom_field_name */
293 opt_parse_customfield(buffer *b)
303 memset(num, 0, sizeof(num));
305 len = (b -> ptr - p);
306 strncpy(num, p, min(sizeof(num) - 1, len));
311 if(change_custom_field_name(b->ptr, n) == -1)
312 return "invalid custom field number";
319 char * (*func) (buffer *line);
321 { "set", opt_parse_set },
322 { "customfield", opt_parse_customfield },
327 opt_parse_line(char *line, int n, char *fn)
338 find_token_start(&b);
350 b.data = b.ptr = b.ptr;
352 for(i = 0; opt_parsers[i].token; i++)
353 if(!strcmp(opt_parsers[i].token, token)) {
354 if(!(err = opt_parsers[i].func(&b)))
359 fprintf(stderr, "%s: parse error at line %d: ", fn, n);
361 fprintf(stderr, "%s\n", err);
363 fprintf(stderr, "unknown token %s\n", token);
369 load_opts(char *filename)
376 if((in = fopen(filename, "r")) == NULL)
380 for(n = 1;!feof(in); n++) {
387 opt_line_remove_comments(line);
389 err += opt_parse_line(line, n, filename) ? 1:0;