2 libcore: library for basic data structures and algorithms.
3 Copyright (C) 2005 IPD Goos, Universit"at Karlsruhe, Germany
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with this library; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
28 #if defined(__FreeBSD__)
36 /* Includes to determine user's home directory */
40 #include <sys/types.h>
45 /* maximum length of a path. */
51 #include "lc_common_t.h"
52 #include "lc_opts_t.h"
53 #include "lc_opts_enum.h"
54 #include "lc_parser_t.h"
56 #include "lc_printf.h"
58 #define ERR_STRING "In argument \"%s\": "
62 #define HELP_TEMPL "%-15s %-10s %-45s"
63 #define HELP_TEMPL_VALS HELP_TEMPL " [%s] (%s)"
65 static struct obstack obst;
67 static void set_name(lc_opt_entry_t *ent, const char *name)
70 ent->hash = HASH_STR(name, strlen(name));
73 #define entry_matches(ent,hash_val,str) \
74 ((ent)->hash == hash_val && strcmp((ent)->name, (str)) == 0)
76 #define entries_equal(e1,e2) entry_matches(e1, (e2)->hash, (e2)->name)
78 static lc_opt_err_info_t *set_error(lc_opt_err_info_t *err, int error, const char *arg)
89 int lc_opt_raise_error(const lc_opt_err_info_t *err, lc_opt_error_handler_t *handler,
96 if(err && lc_opt_is_error(err)) {
100 vsnprintf(buf, sizeof(buf), fmt, args);
109 static lc_opt_entry_t *init_entry(lc_opt_entry_t *ent, lc_opt_entry_t *parent,
110 const char *name, const char *desc)
112 const char *copied_name;
113 const char *copied_desc;
115 obstack_grow0(&obst, name, strlen(name));
116 copied_name = obstack_finish(&obst);
117 obstack_grow0(&obst, desc, strlen(desc));
118 copied_desc = obstack_finish(&obst);
120 memset(ent, 0, sizeof(*ent));
121 set_name(ent, copied_name);
122 ent->desc = copied_desc;
123 ent->parent = parent;
127 static lc_opt_entry_t *init_grp(lc_opt_entry_t *ent, lc_opt_err_info_t *err)
130 INIT_LIST_HEAD(&ent->v.grp.grps);
131 INIT_LIST_HEAD(&ent->v.grp.opts);
133 set_error(err, lc_opt_err_none, "");
135 if(ent->parent->is_grp)
136 list_add(&ent->list, &lc_get_grp_special(ent->parent)->grps);
138 set_error(err, lc_opt_err_grp_expected, ent->parent->name);
144 static lc_opt_entry_t *init_opt(lc_opt_entry_t *ent,
146 void *val, size_t length,
147 lc_opt_callback_t *cb,
149 lc_opt_dump_vals_t *dump_vals,
150 lc_opt_err_info_t *err)
152 lc_opt_special_t *s = lc_get_opt_special(ent);
155 set_error(err, lc_opt_err_none, "");
156 list_add(&ent->list, &lc_get_grp_special(ent->parent)->opts);
162 s->dump_vals = dump_vals;
169 lc_opt_entry_t *lc_opt_root_grp(void)
171 static lc_opt_entry_t root_group;
172 static int inited = 0;
178 init_entry(&root_group, NULL, "root", "The root node");
179 init_grp(&root_group, NULL);
185 int lc_opt_grp_is_root(const lc_opt_entry_t *ent)
187 return ent->parent == NULL;
190 static const char *get_type_name(lc_opt_type_t type)
194 #define XXX(t) case lc_opt_type_ ## t: res = #t; break
202 case lc_opt_type_negbit: res = "bit"; break;
203 case lc_opt_type_negboolean: res = "boolean"; break;
212 const char *lc_opt_get_type_name(const lc_opt_entry_t *ent)
214 return get_type_name(lc_get_opt_special(ent)->type);
218 lc_opt_entry_t *lc_opt_find_grp(const lc_opt_entry_t *grp, const char *name, lc_opt_err_info_t *err);
219 lc_opt_entry_t *lc_opt_find_opt(const lc_opt_entry_t *grp, const char *name, lc_opt_err_info_t *err);
221 lc_opt_entry_t *lc_opt_get_grp(lc_opt_entry_t *parent, const char *name)
223 lc_opt_entry_t *ent = lc_opt_find_grp(parent, name, NULL);
226 ent = obstack_alloc(&obst, sizeof(*ent));
227 init_entry(ent, parent, name, "");
234 lc_opt_entry_t *lc_opt_add_opt(lc_opt_entry_t *parent,
235 const char *name, const char *desc,
236 lc_opt_type_t type, void *value, size_t length,
237 lc_opt_callback_t *cb, lc_opt_dump_t *dump,
238 lc_opt_dump_vals_t *dump_vals,
239 lc_opt_err_info_t *err)
241 lc_opt_entry_t *res = NULL;
244 lc_opt_entry_t *ent = lc_opt_find_opt(parent, name, NULL);
247 res = obstack_alloc(&obst, sizeof(*ent));
248 init_entry(res, parent, name, desc);
249 init_opt(res, type, value, length, cb, dump, dump_vals, err);
251 set_error(err, lc_opt_err_opt_already_there, name);
253 set_error(err, lc_opt_err_grp_expected, name);
259 static lc_opt_entry_t *lc_opt_find_ent(const struct list_head *head, const char *name,
260 int error_to_use, lc_opt_err_info_t *err)
262 lc_opt_entry_t *ent, *found = NULL;
263 int error = error_to_use;
264 unsigned hash = HASH_STR(name, strlen(name));
266 if(!list_empty(head)) {
267 list_for_each_entry(lc_opt_entry_t, ent, head, list) {
268 if(entry_matches(ent, hash, name)) {
269 error = lc_opt_err_none;
276 set_error(err, error, name);
280 lc_opt_entry_t *lc_opt_find_grp(const lc_opt_entry_t *grp, const char *name, lc_opt_err_info_t *err)
282 return grp ? lc_opt_find_ent(&lc_get_grp_special(grp)->grps,
283 name, lc_opt_err_grp_not_found, err) : NULL;
286 lc_opt_entry_t *lc_opt_find_opt(const lc_opt_entry_t *grp, const char *name, lc_opt_err_info_t *err)
288 return grp ? lc_opt_find_ent(&lc_get_grp_special(grp)->opts,
289 name, lc_opt_err_opt_not_found, err) : NULL;
292 static const lc_opt_entry_t *resolve_up_to_last(const lc_opt_entry_t *root,
293 const char * const *names, int pos, int n, lc_opt_err_info_t *err)
300 ent = lc_opt_find_grp(root, names[pos], err);
301 return ent ? resolve_up_to_last(ent, names, pos + 1, n, err) : NULL;
304 static const char *path_delim = "/.";
306 static lc_opt_entry_t *resolve_up_to_last_str_rec(lc_opt_entry_t *from,
308 const char **last_name)
311 lc_opt_entry_t *res = from;
312 size_t end = strcspn(path, path_delim);
314 if(path[end] != '\0') {
315 /* skip all delimiters */
316 size_t next = strspn(path + end, path_delim);
318 /* copy the part of the path into a buffer */
319 char *buf = malloc((end+1) * sizeof(buf[0]));
320 strncpy(buf, path, end);
323 /* resolve the group and free */
324 from = lc_opt_get_grp(from, buf);
327 res = resolve_up_to_last_str_rec(from, path + end + next, last_name);
330 else if(last_name != NULL) {
337 static lc_opt_entry_t *resolve_up_to_last_str(lc_opt_entry_t *root, const char *path, const char **last_name)
339 size_t next = strspn(path, path_delim);
341 /* if l != 0 we saw deliminators, so we resolve from the root */
343 root = lc_opt_root_grp();
345 return resolve_up_to_last_str_rec(root, path + next, last_name);
348 lc_opt_entry_t *lc_opt_resolve_grp(const lc_opt_entry_t *root,
349 const char * const *names, int n, lc_opt_err_info_t *err)
351 const lc_opt_entry_t *grp = resolve_up_to_last(root, names, 0, n - 1, err);
352 return lc_opt_find_grp(grp, names[n - 1], err);
355 lc_opt_entry_t *lc_opt_resolve_opt(const lc_opt_entry_t *root,
356 const char * const *names, int n, lc_opt_err_info_t *err)
358 const lc_opt_entry_t *grp = resolve_up_to_last(root, names, 0, n - 1, err);
359 return lc_opt_find_opt(grp, names[n - 1], err);
362 static char *strtolower(char *buf, size_t n, const char *str)
365 for(i = 0; i < n; ++i)
366 buf[i] = tolower(str[i]);
370 int lc_opt_std_cb(UNUSED(const char *name), lc_opt_type_t type, void *data, size_t length, ...)
376 va_start(args, length);
381 case lc_opt_type_bit:
382 integer = va_arg(args, int);
384 *((int *) data) |= length;
386 *((int *) data) &= ~length;
389 case lc_opt_type_negbit:
390 integer = va_arg(args, int);
392 *((int *) data) &= ~length;
394 *((int *) data) |= length;
397 case lc_opt_type_boolean:
398 *((int *) data) = va_arg(args, int);
401 case lc_opt_type_negboolean:
402 *((int *) data) = !va_arg(args, int);
405 case lc_opt_type_string:
406 strncpy(data, va_arg(args, const char *), length);
409 case lc_opt_type_int:
410 *((int *) data) = va_arg(args, int);
413 case lc_opt_type_double:
414 *((double *) data) = va_arg(args, double);
425 int lc_opt_std_dump(char *buf, size_t n, UNUSED(const char *name), lc_opt_type_t type, void *data, UNUSED(size_t length))
431 case lc_opt_type_bit:
432 case lc_opt_type_negbit:
433 res = snprintf(buf, n, "%x", *((int *) data));
435 case lc_opt_type_boolean:
436 case lc_opt_type_negboolean:
437 res = snprintf(buf, n, "%s", *((int *) data) ? "true" : "false");
439 case lc_opt_type_string:
440 strncpy(buf, data, n);
443 case lc_opt_type_int:
444 res = snprintf(buf, n, "%d", *((int *) data));
446 case lc_opt_type_double:
447 res = snprintf(buf, n, "%g", *((double *) data));
463 int lc_opt_bool_dump_vals(char *buf, size_t n, UNUSED(const char *name), UNUSED(lc_opt_type_t type), UNUSED(void *data), UNUSED(size_t length))
465 strncpy(buf, "true, false", n);
469 int lc_opt_occurs(lc_opt_entry_t *opt, const char *value, lc_opt_err_info_t *err)
471 static const struct {
486 int error = lc_opt_err_illegal_format;
487 lc_opt_special_t *s = lc_get_opt_special(opt);
492 } val_storage, *val = &val_storage;
495 set_error(err, lc_opt_err_opt_not_found, "");
500 set_error(err, lc_opt_err_no_callback, "");
507 case lc_opt_type_int:
508 if(sscanf(value, "%i", (int *) val)) {
509 error = lc_opt_err_unknown_value;
510 if (s->cb(opt->name, s->type, s->value, s->length, val->integer))
511 error = lc_opt_err_none;
515 case lc_opt_type_double:
516 if(sscanf(value, "%lf", (double *) val)) {
517 error = lc_opt_err_unknown_value;
518 if (s->cb(opt->name, s->type, s->value, s->length, val->dbl))
519 error = lc_opt_err_none;
523 case lc_opt_type_boolean:
524 case lc_opt_type_negboolean:
525 case lc_opt_type_bit:
526 case lc_opt_type_negbit:
527 strtolower(buf, sizeof(buf), value);
528 for(i = 0; i < LC_ARRSIZE(bool_strings); ++i) {
529 if(strcmp(buf, bool_strings[i].str) == 0) {
530 val->integer = bool_strings[i].val;
531 error = lc_opt_err_none;
536 if (error == lc_opt_err_none) {
537 error = lc_opt_err_unknown_value;
538 if (s->cb(opt->name, s->type, s->value, s->length, val->integer))
539 error = lc_opt_err_none;
544 case lc_opt_type_string:
545 case lc_opt_type_enum:
546 error = lc_opt_err_unknown_value;
547 if (s->cb(opt->name, s->type, s->value, s->length, value))
548 error = lc_opt_err_none;
552 set_error(err, error, value);
553 return error == lc_opt_err_none;
556 char *lc_opt_value_to_string(char *buf, size_t len, const lc_opt_entry_t *ent)
558 const lc_opt_special_t *s = lc_get_opt_special(ent);
560 s->dump(buf, len, ent->name, s->type, s->value, s->length);
562 strncpy(buf, "<n/a>", len);
567 char *lc_opt_values_to_string(char *buf, size_t len, const lc_opt_entry_t *ent)
569 const lc_opt_special_t *s = lc_get_opt_special(ent);
571 s->dump_vals(buf, len, ent->name, s->type, s->value, s->length);
576 static lc_opt_entry_t *resolve_up_to_last_str(lc_opt_entry_t *root, const char *path, const char **last_name);
578 int lc_opt_add_table(lc_opt_entry_t *root, const lc_opt_table_entry_t *table)
581 lc_opt_err_info_t err;
583 for(i = 0; table[i].name != NULL; ++i) {
585 const lc_opt_table_entry_t *tab = &table[i];
586 lc_opt_entry_t *grp = resolve_up_to_last_str(root, tab->name, &name);
588 lc_opt_add_opt(grp, name, tab->desc, tab->type, tab->value, tab->len, tab->cb, tab->dump, tab->dump_vals, &err);
589 if(err.error != lc_opt_err_none)
596 static void lc_opt_print_grp_path_rec(char *buf, size_t len, const lc_opt_entry_t *ent, char separator, lc_opt_entry_t *stop_ent)
600 if (!lc_opt_grp_is_root(ent)) {
602 lc_opt_print_grp_path_rec(buf, len, ent->parent, separator, stop_ent);
604 if (l > 0 && l < len-1) {
610 strncat(buf, ent->name, len);
613 static char *lc_opt_print_grp_path(char *buf, size_t len, const lc_opt_entry_t *ent, char separator, lc_opt_entry_t *stop_ent)
617 lc_opt_print_grp_path_rec(buf, len, ent, separator, stop_ent);
622 * dump the option tree.
623 * @param ent starting entity
624 * @param separator separator char
625 * @param stop_ent stop at this entity when dumping the name
626 * @param f output file
628 static void lc_opt_print_help_rec(lc_opt_entry_t *ent, char separator, lc_opt_entry_t *stop_ent, FILE *f)
630 lc_grp_special_t *s = lc_get_grp_special(ent);
636 if(!list_empty(&s->opts)) {
637 lc_opt_print_grp_path(grp_name, sizeof(grp_name), ent, separator, stop_ent);
640 fprintf(f, "%s:\n", grp_name);
642 list_for_each_entry(lc_opt_entry_t, e, &s->opts, list) {
645 lc_opt_value_to_string(value, sizeof(value), e);
646 lc_opt_values_to_string(values, sizeof(values), e);
647 fprintf(f, HELP_TEMPL_VALS "\n", e->name, lc_opt_get_type_name(e), e->desc, value, values);
651 list_for_each_entry(lc_opt_entry_t, e, &s->grps, list) {
652 lc_opt_print_help_rec(e, separator, stop_ent, f);
657 void lc_opt_print_help(lc_opt_entry_t *ent, FILE *f)
659 fprintf(f, HELP_TEMPL_VALS "\n", "option", "type", "description", "default", "possible options");
660 lc_opt_print_help_rec(ent, '.', NULL, f);
663 void lc_opt_print_help_for_entry(lc_opt_entry_t *ent, char separator, FILE *f)
665 fprintf(f, HELP_TEMPL_VALS "\n", "option", "type", "description", "default", "possible options");
666 lc_opt_print_help_rec(ent, separator, ent, f);
670 static void indent(FILE *f, int n)
673 for(i = 0; i < n; ++i)
677 static void lc_opt_print_tree_lc_opt_indent(lc_opt_entry_t *ent, FILE *f, int level)
680 lc_opt_special_t *s = lc_get_opt_special(ent);
683 fprintf(f, "%c%s(\"%s\"):%s = %s\n", s->is_set ? '+' : '-', ent->name,
684 ent->desc, lc_opt_get_type_name(ent), lc_opt_value_to_string(buf, sizeof(buf), ent));
687 static void lc_opt_print_tree_grp_indent(lc_opt_entry_t *ent, FILE *f, int level)
694 s = lc_get_grp_special(ent);
696 fprintf(f, "/%s\n", ent->name);
698 list_for_each_entry(lc_opt_entry_t, e, &s->grps, list) {
699 lc_opt_print_tree_grp_indent(e, f, level + 2);
702 list_for_each_entry(lc_opt_entry_t, e, &s->opts, list) {
703 lc_opt_print_tree_lc_opt_indent(e, f, level + 2);
708 void lc_opt_print_tree(lc_opt_entry_t *ent, FILE *f)
710 lc_opt_print_tree_grp_indent(ent, f, 0);
714 void lc_opt_from_file(const char *filename, FILE *f, lc_opt_error_handler_t *handler)
717 lc_opt_init_parser(filename, handler);
721 int lc_opt_from_single_arg(const lc_opt_entry_t *root,
722 const char *opt_prefix,
723 const char *arg, lc_opt_error_handler_t *handler)
725 const lc_opt_entry_t *grp = root;
727 int n_prefix = opt_prefix ? strlen(opt_prefix) : 0;
731 lc_opt_err_info_t err;
732 char *end, *buf, *eqsign;
734 if(n >= n_prefix && strncmp(opt_prefix, arg, n_prefix) == 0) {
735 arg = arg + n_prefix;
738 * check, if the next character is a @.
739 * This means, that we want to read options
743 size_t n = strcspn(&arg[1], " \t\n");
744 char *fname = alloca(n + 1);
747 strncpy(fname, &arg[1], n);
748 if((f = fopen(fname, "rt")) != NULL) {
749 lc_opt_from_file(fname, f, handler);
751 set_error(&err, lc_opt_err_none, NULL);
755 set_error(&err, lc_opt_err_file_not_found, arg);
757 return !lc_opt_raise_error(&err, handler, "Unable to open file: %s", fname);
760 /* find the next delimiter (the -) and extract the string up to
762 end = strchr(arg, OPT_DELIM);
763 eqsign = strchr(arg, '=');
764 if (eqsign && eqsign < end)
768 * Copy the part of the option into the buffer and add the
771 buf = obstack_copy0(&obst, arg, end - arg);
773 /* Resolve the group inside the group */
774 grp = lc_opt_find_grp(grp, buf, &err);
775 error = lc_opt_raise_error(&err, handler, ERR_STRING, arg);
779 /* Find the next option part delimiter. */
781 end = strchr(arg, OPT_DELIM);
782 eqsign = strchr(arg, '=');
783 if (eqsign && eqsign < end)
785 obstack_free(&obst, buf);
792 * Now, we are at the last option part:
793 * --grp1-grp2-...-grpn-opt=value
794 * Check, for the = and evaluate the option string. If the = is
795 * missing, we should have a boolean option, but that is checked
798 end = strchr(arg, '=');
799 buf = obstack_copy0(&obst, arg, end ? end - arg : (int) strlen(arg));
800 opt = lc_opt_find_opt(grp, buf, &err);
801 error = lc_opt_raise_error(&err, handler, ERR_STRING, arg);
805 * Now evaluate the parameter of the option (the part after
806 * the =) if it was given.
808 arg = end ? end + 1 : "true";
810 /* Set the value of the option. */
811 lc_opt_occurs(opt, arg, &err);
812 ret = !lc_opt_raise_error(&err, handler, ERR_STRING, arg);
820 int lc_opt_from_argv(const lc_opt_entry_t *root,
821 const char *opt_prefix,
822 int argc, const char *argv[],
823 lc_opt_error_handler_t *handler)
828 for(i = 0; i < argc; ++i) {
829 options_set |= lc_opt_from_single_arg(root, opt_prefix, argv[i], handler);
835 static int opt_arg_type(UNUSED(const lc_arg_occ_t *occ))
837 return lc_arg_type_ptr;
840 static int opt_arg_emit(lc_appendable_t *app, const lc_arg_occ_t *occ, const lc_arg_value_t *arg)
844 lc_opt_entry_t *opt = arg->v_ptr;
848 switch(occ->conversion) {
850 lc_opt_value_to_string(buf, sizeof(buf), opt);
853 s = lc_opt_get_type_name(opt);
866 res = lc_appendable_snadd(app, s, strlen(s));
871 static const lc_arg_handler_t lc_opt_arg_handler = {
877 /* lc_printf facility for options */
879 const lc_arg_env_t *lc_opt_get_arg_env(void)
881 static lc_arg_env_t *env = NULL;
884 env = lc_arg_new_env();
886 lc_arg_register(env, "opt:value", 'V', &lc_opt_arg_handler);
887 lc_arg_register(env, "opt:type", 'T', &lc_opt_arg_handler);
888 lc_arg_register(env, "opt:desc", 'D', &lc_opt_arg_handler);
889 lc_arg_register(env, "opt:name", 'O', &lc_opt_arg_handler);
895 static int lc_opts_default_error_handler(const char *prefix, const lc_opt_err_info_t *err)
897 fprintf(stderr, "%s: %s; %s\n", prefix, err->msg, err->arg);
901 void lc_opts_init(const char *ini_name, lc_opt_entry_t *root, const char *arg_prefix, int argc, const char **argv)
905 char local_ini_file[MAX_PATH];
906 char home_dir_ini_file[MAX_PATH];
909 strncpy(local_ini_file, ini_name, sizeof(local_ini_file));
910 strncat(local_ini_file, ".ini", sizeof(local_ini_file));
911 local_ini_file[sizeof(local_ini_file) - 1] = '\0';
916 /* ARG: need newer SDK to compile this */
917 SHGetFolderPath(NULL, CSIDL_LOCAL_APPDATA, NULL, 0, path);
918 strncat(path, "\\", sizeof(path));
920 strncpy(home_dir_ini_file, local_ini_file, sizeof(home_dir_ini_file));
921 home_dir_ini_file[sizeof(home_dir_ini_file) - 1] = '\0';
924 struct passwd *entry = getpwuid(getuid());
926 strcpy(path, entry->pw_dir);
927 strncat(path, "/", sizeof(path));
929 snprintf(home_dir_ini_file, sizeof(home_dir_ini_file), ".%src", ini_name);
930 home_dir_ini_file[sizeof(home_dir_ini_file) - 1] = '\0';
932 /* FIXME: some error occured */
933 home_dir_ini_file[0] = '\0';
938 strncat(path, home_dir_ini_file, sizeof(path));
939 path[sizeof(path) - 1] = '\0';
941 /* Process ini file in user's home. */
942 f = fopen(path, "rt");
944 lc_opt_from_file(path, f, lc_opts_default_error_handler);
948 /* Process ini file in current directory. */
949 f = fopen(local_ini_file, "rt");
951 lc_opt_from_file(local_ini_file, f, lc_opts_default_error_handler);
955 /* process arguments from the command line */
956 lc_opt_from_argv(root, arg_prefix, argc, argv, lc_opts_default_error_handler);