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 },
68 { "sort_field", OT_STR, STR_SORT_FIELD, UL "nick" },
73 static unsigned char bool_opts[BOOL_MAX];
74 static int int_opts[INT_MAXIMUM];
75 static char *str_opts[STR_MAX];
78 set_int(enum int_opts opt, int value)
80 assert(opt >= 0 && opt < INT_MAXIMUM);
82 int_opts[opt] = value;
86 set_bool(enum bool_opts opt, bool value)
88 assert(opt >= 0 && opt < BOOL_MAX);
90 bool_opts[opt] = value;
94 set_str(enum str_opts opt, char *value)
96 assert(opt >= 0 && opt < STR_MAX);
101 str_opts[opt] = strdup(value);
105 opt_get_int(enum int_opts opt)
107 assert(opt >= 0 && opt < INT_MAXIMUM);
109 return int_opts[opt];
113 opt_get_bool(enum bool_opts opt)
115 assert(opt >= 0 && opt < BOOL_MAX);
117 return bool_opts[opt];
121 opt_get_str(enum str_opts opt)
123 assert(opt >= 0 && opt < STR_MAX);
125 return str_opts[opt];
129 restore_default(struct option *p)
133 set_bool(p -> data, (bool)p -> init);
136 set_int(p -> data, (int)p -> init);
140 set_str(p -> data, (char *) p -> init);
152 for(i = 0; abook_vars[i].option; i++)
153 restore_default(&abook_vars[i]);
162 * only strings need to be freed
164 for(i = 0; i < STR_MAX; i++) {
179 opt_line_remove_comments(char *p)
181 bool in_quote = FALSE;
190 in_quote = !in_quote;
209 find_token_start(buffer *b)
213 for(; ISSPACE(*b -> ptr); b -> ptr ++);
217 find_token_end(buffer *b)
221 for(find_token_start(b); *(b -> ptr); b -> ptr ++) {
222 if(ISSPACE(*(b -> ptr))) {
229 opt_set_set_option(char *var, char *p, struct option *opt)
237 if(p[len - 1] == '\"' && *p == '\"') {
239 return "invalid value";
244 switch(opt -> type) {
246 set_str(opt -> data, p);
249 set_int(opt -> data, safe_atoi(p));
252 if(!strcasecmp(p, "true") || !strcasecmp(p, "on"))
253 set_bool(opt -> data, TRUE);
254 else if(!strcasecmp(p, "false") ||
255 !strcasecmp(p, "off"))
256 set_bool(opt -> data, FALSE);
258 return "invalid value";
266 opt_parse_set(buffer *b)
272 if((p = strchr(b -> ptr, '=')))
275 return "invalid value assignment";
279 for(i = 0;abook_vars[i].option; i++)
280 if(!strcmp(abook_vars[i].option, b -> ptr))
281 return opt_set_set_option(b -> ptr, p, &abook_vars[i]);
283 return "unknown option";
289 char * (*func) (buffer *line);
291 { "set", opt_parse_set },
296 opt_parse_line(char *line, int n, char *fn)
307 find_token_start(&b);
319 b.data = b.ptr = b.ptr;
321 for(i = 0; opt_parsers[i].token; i++)
322 if(!strcmp(opt_parsers[i].token, token)) {
323 if(!(err = opt_parsers[i].func(&b)))
328 fprintf(stderr, "%s: parse error at line %d: ", fn, n);
330 fprintf(stderr, "%s\n", err);
332 fprintf(stderr, "unknown token %s\n", token);
338 load_opts(char *filename)
345 if((in = fopen(filename, "r")) == NULL)
349 for(n = 1;!feof(in); n++) {
356 opt_line_remove_comments(line);
358 err += opt_parse_line(line, n, filename) ? 1:0;