+
+#include "database.h" /* needed for declare_new_field */
+
+/*
+ * syntax: field <identifier> = <human readable name> [ , <type> ]
+ */
+static const char *
+opt_parse_field(buffer *b)
+{
+ char *err, *field, *name;
+
+ if((err = get_token(b, TOKEN_EQUAL)))
+ return err;
+
+ if((field = b->data) == NULL)
+ return _("no field identifier provided");
+
+ if((err = get_token(b, TOKEN_COMMA)))
+ return err;
+
+ if((name = b->data) == NULL)
+ return _("no field name provided");
+
+ if((err = declare_new_field(field,
+ name,
+ b->ptr,
+ 0 /* reject "standard" fields */)))
+ return err;
+
+ return NULL;
+}
+
+
+static struct {
+ char *token;
+ const char * (*func) (buffer *line);
+} opt_parsers[] = {
+ { "set", opt_parse_set },
+ { "customfield", opt_parse_customfield }, /* obsolete */
+ { "view", opt_parse_view },
+ { "field", opt_parse_field },
+ { NULL }
+};
+
+static bool
+opt_parse_line(char *line, int n, char *fn)
+{
+ int i;
+ const char *err = NULL;
+ char *token;
+ buffer b;
+
+ assert(line && fn);
+
+ b.ptr = line;
+
+ if((err = get_token(&b, 0))) {
+ fprintf(stderr, "%s\n", err);
+ return FALSE;
+ }
+
+ if(b.data == NULL)
+ return FALSE;
+
+ strtrim(b.data);
+ strtrim(b.ptr);
+
+ token = b.data;
+ b.data = b.ptr = b.ptr;
+
+ for(i = 0; opt_parsers[i].token; i++)
+ if(!strcmp(opt_parsers[i].token, token)) {
+ if(!(err = opt_parsers[i].func(&b)))
+ return FALSE;
+ break;
+ }
+
+ fprintf(stderr, _("%s: parse error at line %d: "), fn, n);
+ if(err)
+ fprintf(stderr, "%s\n", err);
+ else
+ fprintf(stderr, _("unknown token %s\n"), token);
+
+ return TRUE;
+}
+
+int
+load_opts(char *filename)
+{
+ FILE *in;
+ char *line = NULL;
+ int n;
+ int err = 0;
+
+ if((in = fopen(filename, "r")) == NULL)
+ return -1;
+
+ for(n = 1;!feof(in); n++) {
+ line = getaline(in);
+
+ if(feof(in))
+ break;
+
+ if(line && *line) {
+ opt_line_remove_comments(line);
+ if(*line)
+ err += opt_parse_line(line, n, filename) ? 1:0;
+ }
+
+ free(line);
+ line = NULL;
+ }
+
+ free(line);
+
+ /* post-initialization */
+ err += check_options();
+ if(!strcasecmp(opt_get_str(STR_PRESERVE_FIELDS), "standard"))
+ init_standard_fields();
+
+ return err;
+}
+