]> git.deb.at Git - pkg/abook.git/blob - options.c
Obsoleted the emailpos, extra_column, extra_alternative and extrapos
[pkg/abook.git] / options.c
1
2 /*
3  * $Id$
4  *
5  * by JH <jheinonen@users.sourceforge.net>
6  *
7  * Copyright (C) Jaakko Heinonen
8  *
9  */
10
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <string.h>
14 #include <ctype.h>
15 #include <assert.h>
16 #include "options.h"
17 #include "abook.h"
18 #include "gettext.h"
19 #include "misc.h"
20 #include "views.h"
21 #include "xmalloc.h"
22
23 #ifndef FALSE
24 #       define FALSE    0
25 #endif
26 #ifndef TRUE
27 #       define TRUE     1
28 #endif
29
30 #define UL      (unsigned long)
31
32 /*
33  * option types
34  */
35
36 enum opt_type {
37         OT_BOOL,
38         OT_STR,
39         OT_INT
40 };
41
42 struct option {
43         char *option;
44         enum opt_type type;
45         unsigned int data;
46         unsigned long init;
47 };
48
49 static struct option abook_vars[] = {
50         { "autosave", OT_BOOL, BOOL_AUTOSAVE, TRUE },
51
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,
56                 TRUE },
57
58         { "print_command", OT_STR, STR_PRINT_COMMAND, UL "lpr" },
59
60         { "www_command", OT_STR, STR_WWW_COMMAND, UL "lynx" },
61
62         { "address_style", OT_STR, STR_ADDRESS_STYLE, UL "eu" },
63
64         { "use_ascii_only", OT_BOOL, BOOL_USE_ASCII_ONLY, FALSE },
65
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
71         { NULL }
72 };
73
74 static unsigned char bool_opts[BOOL_MAX];
75 static int int_opts[INT_MAXIMUM];
76 static char *str_opts[STR_MAX];
77
78 static void
79 set_int(enum int_opts opt, int value)
80 {
81         assert(opt >= 0 && opt < INT_MAXIMUM);
82
83         int_opts[opt] = value;
84 }
85
86 static void
87 set_bool(enum bool_opts opt, bool value)
88 {
89         assert(opt >= 0 && opt < BOOL_MAX);
90
91         bool_opts[opt] = value;
92 }
93
94 static void
95 set_str(enum str_opts opt, char *value)
96 {
97         assert(opt >= 0 && opt < STR_MAX);
98
99         if(str_opts[opt])
100                 free(str_opts[opt]);
101
102         str_opts[opt] = xstrdup(value);
103 }
104
105 int
106 opt_get_int(enum int_opts opt)
107 {
108         assert(opt >= 0 && opt < INT_MAXIMUM);
109
110         return int_opts[opt];
111 }
112
113 bool
114 opt_get_bool(enum bool_opts opt)
115 {
116         assert(opt >= 0 && opt < BOOL_MAX);
117
118         return bool_opts[opt];
119 }
120
121 char *
122 opt_get_str(enum str_opts opt)
123 {
124         assert(opt >= 0 && opt < STR_MAX);
125
126         return str_opts[opt];
127 }
128
129 static void
130 restore_default(struct option *p)
131 {
132         switch(p -> type) {
133                 case OT_BOOL:
134                         set_bool(p -> data, (bool)p -> init);
135                         break;
136                 case OT_INT:
137                         set_int(p -> data, (int)p -> init);
138                         break;
139                 case OT_STR:
140                         if(p -> init)
141                                 set_str(p -> data, (char *) p -> init);
142                         break;
143                 default:
144                         assert(0);
145         }
146 }
147
148 void
149 init_opts()
150 {
151         int i;
152
153         for(i = 0; abook_vars[i].option; i++)
154                 restore_default(&abook_vars[i]);
155 }
156
157 void
158 free_opts()
159 {
160         int i;
161
162         /*
163          * only strings need to be freed
164          */
165         for(i = 0; i < STR_MAX; i++) {
166                 free(str_opts[i]);
167                 str_opts[i] = NULL;
168         }
169 }
170
171 /*
172  * file parsing
173  */
174
175 typedef struct {
176         char *data, *ptr;
177 } buffer;
178
179 static void
180 opt_line_remove_comments(char *p)
181 {
182         bool in_quote = FALSE;
183         bool escape = FALSE;
184
185         assert(p != NULL);
186
187         for(; *p; p++) {
188                 switch(*p) {
189                         case '\"':
190                                 if(!escape)
191                                         in_quote = !in_quote;
192                                 break;
193                         case '\\':
194                                 escape = TRUE;
195                                 break;
196                         case '#':
197                                 if(!in_quote) {
198                                         *p = 0;
199                                         return;
200                                 }
201                         default:
202                                 escape = FALSE;
203                 }
204         }
205 }
206
207 /* After calling,
208  * - b->data points to the found token, or NULL is end of parsing
209  * - b->ptr  points to the begining of next token
210  *
211  * If the TOKEN_ALLOC option is used, the original string is not mangled
212  * and memory is allocated for the token.
213  */
214 static char *
215 get_token(buffer *b, int options)
216 {
217         char quote = 0, c;
218         char *end = NULL;
219
220         assert(b);
221
222         SKIPWS(b->ptr);
223         if(*b->ptr && strchr("\"'", *b->ptr))
224                 quote = *(b->ptr++);
225         b->data = b->ptr;
226
227         while(1) {
228                 if(!(c = *b->ptr)) {
229                         end = b->ptr;
230                         break;
231                 }
232
233                 if(!quote && (
234                                 ISSPACE(c) ||
235                                 ((options & TOKEN_EQUAL) && (c == '=')) ||
236                                 ((options & TOKEN_COMMA) && (c == ',')))
237                                 ) {
238                         end = b->ptr;
239                         break;
240                 } else if(c == quote) {
241                         quote = 0;
242                         end = b->ptr++;
243                         break;
244                 }
245
246                 b->ptr++;
247         }
248
249         if(quote)
250                 return _("quote mismatch");
251
252         if(options & (TOKEN_EQUAL | TOKEN_COMMA))
253                 SKIPWS(b->ptr); /* whitespaces can precede the sign */
254
255         if((options & TOKEN_EQUAL) && (*b->ptr != '='))
256                 return _("no assignment character found");
257
258         if((options & TOKEN_COMMA) && *b->ptr && (*b->ptr != ','))
259                 return _("error in comma separated list");
260
261         if(b->ptr == b->data) {
262                 b->data = NULL;
263                 return NULL; /* no error, just end of parsing */
264         }
265
266         if(options & TOKEN_ALLOC) /* freeing is the caller's responsibility */
267                 b->data = xstrndup(b->data, end - b->data);
268         else
269                 *end = 0;
270
271         b->ptr++; /* advance to next token */
272         SKIPWS(b->ptr);
273
274         return NULL;
275 }
276
277 static const char *
278 opt_set_set_option(char *p, struct option *opt)
279 {
280         int len;
281
282         strtrim(p);
283
284         len = strlen(p);
285
286         if(p[len - 1] == '\"' && *p == '\"') {
287                 if(len < 3)
288                         return _("invalid value");
289                 p[len - 1] = 0;
290                 p++;
291         }
292
293         switch(opt -> type) {
294                 case OT_STR:
295                         set_str(opt -> data, p);
296                         break;
297                 case OT_INT:
298                         set_int(opt -> data, safe_atoi(p));
299                         break;
300                 case OT_BOOL:
301                         if(!strcasecmp(p, "true") || !strcasecmp(p, "on"))
302                                 set_bool(opt -> data, TRUE);
303                         else if(!strcasecmp(p, "false") ||
304                                         !strcasecmp(p, "off"))
305                                 set_bool(opt -> data, FALSE);
306                         else
307                                 return _("invalid value");
308                         break;
309                 default:
310                         assert(0);
311         }
312
313         return NULL;
314 }
315
316 static const char *
317 opt_set_option(char *var, char *p)
318 {
319         int i;
320
321         for(i = 0; abook_vars[i].option; i++)
322                 if(!strcmp(abook_vars[i].option, var))
323                         return opt_set_set_option(p, &abook_vars[i]);
324
325         return _("unknown option");
326 }
327
328 static int
329 check_options()
330 {
331         char *str;
332         int err = 0;
333
334         str = opt_get_str(STR_PRESERVE_FIELDS);
335         if(strcasecmp(str, "all") && strcasecmp(str, "none") &&
336                         strcasecmp(str, "standard")) {
337                 fprintf(stderr, _("valid values for the 'preserve_fields' "
338                                         "option are 'all', 'standard' "
339                                         "(default), and 'none'\n"));
340                 restore_default(&abook_vars[STR_PRESERVE_FIELDS]);
341                 err++;
342         }
343         str = opt_get_str(STR_ADDRESS_STYLE);
344         if(strcasecmp(str, "eu") && strcasecmp(str, "uk") &&
345                         strcasecmp(str, "us")) {
346                 fprintf(stderr, _("valid values for the 'address_style' "
347                                         "option are 'eu' (default), 'uk', "
348                                         "and 'us'\n"));
349                 restore_default(&abook_vars[STR_ADDRESS_STYLE]);
350                 err++;
351         }
352
353         return err;
354 }
355
356 /*
357  * syntax: set <option> = <value>
358  */
359 static const char *
360 opt_parse_set(buffer *b)
361 {
362         char *var, *err;
363
364         if((err = get_token(b, TOKEN_EQUAL)))
365                 return err;
366
367         if((var = b->data) == NULL)
368                 return _("invalid value assignment");
369
370         return opt_set_option(var, b->ptr);
371 }
372
373 static const char *
374 opt_parse_customfield(buffer *b)
375 {
376         return _("customfield: obsolete command - please use the "
377                         "'field' and 'view' commands instead");
378 }
379
380 #include "views.h" /* needed for add_field_to_view */
381
382 /*
383  * syntax: view <tab name> = <field1> [ , <field2>, ... ]
384  */
385 static const char *
386 opt_parse_view(buffer *b)
387 {
388         char *err, *view;
389
390         if((err = get_token(b, TOKEN_EQUAL)))
391                 return err;
392
393         if((view = b->data) == NULL)
394                 return _("no view name provided");
395
396         while(1) {
397                 if((err = get_token(b, TOKEN_COMMA)))
398                         return err;
399
400                 if(b->data == NULL)
401                         break;
402
403                 if((err = add_field_to_view(view, b->data)))
404                         return err;
405         }
406
407         return NULL;
408 }
409
410 #include "database.h" /* needed for declare_new_field */
411
412 /*
413  * syntax: field <identifier> = <human readable name> [ , <type> ]
414  */
415 static const char *
416 opt_parse_field(buffer *b)
417 {
418         char *err, *field, *name;
419
420         if((err = get_token(b, TOKEN_EQUAL)))
421                 return err;
422
423         if((field = b->data) == NULL)
424                 return _("no field identifier provided");
425
426         if((err = get_token(b, TOKEN_COMMA)))
427                 return err;
428
429         if((name = b->data) == NULL)
430                 return _("no field name provided");
431
432         if((err = declare_new_field(field,
433                                         name,
434                                         b->ptr,
435                                         0 /* reject "standard" fields */)))
436                 return err;
437
438         return NULL;
439 }
440
441
442 static struct {
443         char *token;
444         const char * (*func) (buffer *line);
445 } opt_parsers[] = {
446         { "set", opt_parse_set },
447         { "customfield", opt_parse_customfield }, /* obsolete */
448         { "view", opt_parse_view },
449         { "field", opt_parse_field },
450         { NULL }
451 };
452
453 static bool
454 opt_parse_line(char *line, int n, char *fn)
455 {
456         int i;
457         const char *err = NULL;
458         char *token;
459         buffer b;
460
461         assert(line && fn);
462
463         b.ptr = line;
464
465         if((err = get_token(&b, 0))) {
466                 fprintf(stderr, "%s\n", err);
467                 return FALSE;
468         }
469
470         if(b.data == NULL)
471                 return FALSE;
472
473         strtrim(b.data);
474         strtrim(b.ptr);
475
476         token = b.data;
477         b.data = b.ptr = b.ptr;
478
479         for(i = 0; opt_parsers[i].token; i++)
480                 if(!strcmp(opt_parsers[i].token, token)) {
481                         if(!(err = opt_parsers[i].func(&b)))
482                                 return FALSE;
483                         break;
484                 }
485
486         fprintf(stderr, _("%s: parse error at line %d: "), fn, n);
487         if(err)
488                 fprintf(stderr, "%s\n", err);
489         else
490                 fprintf(stderr, _("unknown token %s\n"), token);
491
492         return TRUE;
493 }
494
495 int
496 load_opts(char *filename)
497 {
498         FILE *in;
499         char *line = NULL;
500         int n;
501         int err = 0;
502
503         if((in = fopen(filename, "r")) == NULL)
504                 return -1;
505
506         for(n = 1;!feof(in); n++) {
507                 line = getaline(in);
508
509                 if(feof(in))
510                         break;
511
512                 if(line && *line) {
513                         opt_line_remove_comments(line);
514                         if(*line)
515                                 err += opt_parse_line(line, n, filename) ? 1:0;
516                 }
517
518                 free(line);
519                 line = NULL;
520         }
521
522         free(line);
523
524         /* post-initialization */
525         err += check_options();
526         if(!strcasecmp(opt_get_str(STR_PRESERVE_FIELDS), "standard"))
527                 init_standard_fields();
528
529         return err;
530 }
531