5 * by JH <jheinonen@users.sourceforge.net>
7 * Copyright (C) Jaakko Heinonen
27 #define UL (unsigned long)
46 static struct option abook_vars[] = {
47 { "autosave", OT_BOOL, BOOL_AUTOSAVE, TRUE },
49 { "show_all_emails", OT_BOOL, BOOL_SHOW_ALL_EMAILS, TRUE },
50 { "emailpos", OT_INT, INT_EMAILPOS, 25 },
51 { "extra_column", OT_STR, STR_EXTRA_COLUMN, UL "phone" },
52 { "extra_alternative", OT_STR, STR_EXTRA_ALTERNATIVE, UL "-1" },
53 { "extrapos", OT_INT, INT_EXTRAPOS, 65 },
55 { "mutt_command", OT_STR, STR_MUTT_COMMAND, UL "mutt" },
56 { "mutt_return_all_emails", OT_BOOL, BOOL_MUTT_RETURN_ALL_EMAILS,
59 { "print_command", OT_STR, STR_PRINT_COMMAND, UL "lpr" },
61 { "www_command", OT_STR, STR_WWW_COMMAND, UL "lynx" },
63 { "address_style", OT_STR, STR_ADDRESS_STYLE, UL "eu" },
65 { "use_ascii_only", OT_BOOL, BOOL_USE_ASCII_ONLY, FALSE },
67 { "add_email_prevent_duplicates", OT_BOOL, BOOL_ADD_EMAIL_PREVENT_DUPLICATES, FALSE },
72 static unsigned char bool_opts[BOOL_MAX];
73 static int int_opts[INT_MAXIMUM];
74 static char *str_opts[STR_MAX];
77 set_int(enum int_opts opt, int value)
79 assert(opt >= 0 && opt < INT_MAXIMUM);
81 int_opts[opt] = value;
85 set_bool(enum bool_opts opt, bool value)
87 assert(opt >= 0 && opt < BOOL_MAX);
89 bool_opts[opt] = value;
93 set_str(enum str_opts opt, char *value)
95 assert(opt >= 0 && opt < STR_MAX);
100 str_opts[opt] = strdup(value);
104 opt_get_int(enum int_opts opt)
106 assert(opt >= 0 && opt < INT_MAXIMUM);
108 return int_opts[opt];
112 opt_get_bool(enum bool_opts opt)
114 assert(opt >= 0 && opt < BOOL_MAX);
116 return bool_opts[opt];
120 opt_get_str(enum str_opts opt)
122 assert(opt >= 0 && opt < STR_MAX);
124 return str_opts[opt];
128 restore_default(struct option *p)
132 set_bool(p -> data, (bool)p -> init);
135 set_int(p -> data, (int)p -> init);
139 set_str(p -> data, (char *) p -> init);
151 for(i = 0; abook_vars[i].option; i++)
152 restore_default(&abook_vars[i]);
161 * only strings need to be freed
163 for(i = 0; i < STR_MAX; i++) {
178 opt_line_remove_comments(char *p)
180 bool in_quote = FALSE;
189 in_quote = !in_quote;
208 find_token_start(buffer *b)
212 for(; ISSPACE(*b -> ptr); b -> ptr ++);
216 find_token_end(buffer *b)
220 for(find_token_start(b); *(b -> ptr); b -> ptr ++) {
221 if(ISSPACE(*(b -> ptr))) {
228 opt_set_set_option(char *var, char *p, struct option *opt)
236 if(p[len - 1] == '\"' && *p == '\"') {
238 return "invalid value";
243 switch(opt -> type) {
245 set_str(opt -> data, p);
248 set_int(opt -> data, safe_atoi(p));
251 if(!strcasecmp(p, "true") || !strcasecmp(p, "on"))
252 set_bool(opt -> data, TRUE);
253 else if(!strcasecmp(p, "false") ||
254 !strcasecmp(p, "off"))
255 set_bool(opt -> data, FALSE);
257 return "invalid value";
265 opt_parse_set(buffer *b)
271 if((p = strchr(b -> ptr, '=')))
274 return "invalid value assignment";
278 for(i = 0;abook_vars[i].option; i++)
279 if(!strcmp(abook_vars[i].option, b -> ptr))
280 return opt_set_set_option(b -> ptr, p, &abook_vars[i]);
282 return "unknown option";
288 char * (*func) (buffer *line);
290 { "set", opt_parse_set },
295 opt_parse_line(char *line, int n, char *fn)
306 find_token_start(&b);
318 b.data = b.ptr = b.ptr;
320 for(i = 0; opt_parsers[i].token; i++)
321 if(!strcmp(opt_parsers[i].token, token)) {
322 if(!(err = opt_parsers[i].func(&b)))
327 fprintf(stderr, "%s: parse error at line %d: ", fn, n);
329 fprintf(stderr, "%s\n", err);
331 fprintf(stderr, "unknown token %s\n", token);
337 load_opts(char *filename)
344 if((in = fopen(filename, "r")) == NULL)
348 for(n = 1;!feof(in); n++) {
355 opt_line_remove_comments(line);
357 err += opt_parse_line(line, n, filename) ? 1:0;