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
34 /* Includes to determine user's home directory */
38 #include <sys/types.h>
43 /* maximum length of a path. */
49 #include "lc_common_t.h"
50 #include "lc_opts_t.h"
51 #include "lc_opts_enum.h"
52 #include "lc_parser_t.h"
54 #include "lc_printf.h"
57 #define ERR_STRING "In argument \"%s\": "
61 #define HELP_TEMPL "%-15s %-10s %-45s"
62 #define HELP_TEMPL_VALS HELP_TEMPL " [%s] (%s)"
64 static struct obstack obst;
66 static void set_name(lc_opt_entry_t *ent, const char *name)
69 ent->hash = HASH_STR(name, strlen(name));
72 #define entry_matches(ent,hash_val,str) \
73 ((ent)->hash == hash_val && strcmp((ent)->name, (str)) == 0)
75 #define entries_equal(e1,e2) entry_matches(e1, (e2)->hash, (e2)->name)
77 static lc_opt_err_info_t *set_error(lc_opt_err_info_t *err, int error, const char *arg)
88 int lc_opt_raise_error(const lc_opt_err_info_t *err, lc_opt_error_handler_t *handler,
95 if (err && lc_opt_is_error(err)) {
99 vsnprintf(buf, sizeof(buf), fmt, args);
108 static lc_opt_entry_t *init_entry(lc_opt_entry_t *ent, lc_opt_entry_t *parent,
109 const char *name, const char *desc)
111 const char *copied_name;
112 const char *copied_desc;
114 obstack_grow0(&obst, name, strlen(name));
115 copied_name = obstack_finish(&obst);
116 obstack_grow0(&obst, desc, strlen(desc));
117 copied_desc = obstack_finish(&obst);
119 memset(ent, 0, sizeof(*ent));
120 set_name(ent, copied_name);
121 ent->desc = copied_desc;
122 ent->parent = parent;
126 static lc_opt_entry_t *init_grp(lc_opt_entry_t *ent, lc_opt_err_info_t *err)
129 INIT_LIST_HEAD(&ent->v.grp.grps);
130 INIT_LIST_HEAD(&ent->v.grp.opts);
132 set_error(err, lc_opt_err_none, "");
134 if (ent->parent->is_grp)
135 list_add_tail(&ent->list, &lc_get_grp_special(ent->parent)->grps);
137 set_error(err, lc_opt_err_grp_expected, ent->parent->name);
143 static lc_opt_entry_t *init_opt(lc_opt_entry_t *ent,
145 void *val, size_t length,
146 lc_opt_callback_t *cb,
148 lc_opt_dump_vals_t *dump_vals,
149 lc_opt_err_info_t *err)
151 lc_opt_special_t *s = lc_get_opt_special(ent);
154 set_error(err, lc_opt_err_none, "");
155 list_add_tail(&ent->list, &lc_get_grp_special(ent->parent)->opts);
161 s->dump_vals = dump_vals;
168 lc_opt_entry_t *lc_opt_root_grp(void)
170 static lc_opt_entry_t root_group;
171 static int inited = 0;
177 init_entry(&root_group, NULL, "root", "The root node");
178 init_grp(&root_group, NULL);
184 int lc_opt_grp_is_root(const lc_opt_entry_t *ent)
186 return ent->parent == NULL;
189 static const char *get_type_name(lc_opt_type_t type)
193 #define XXX(t) case lc_opt_type_ ## t: res = #t; break
201 case lc_opt_type_negbit: res = "bit"; break;
202 case lc_opt_type_negboolean: res = "boolean"; break;
211 const char *lc_opt_get_type_name(const lc_opt_entry_t *ent)
213 return get_type_name(lc_get_opt_special(ent)->type);
216 lc_opt_entry_t *lc_opt_get_grp(lc_opt_entry_t *parent, const char *name)
218 lc_opt_entry_t *ent = lc_opt_find_grp(parent, name, NULL);
221 ent = OALLOC(&obst, lc_opt_entry_t);
222 init_entry(ent, parent, name, "");
229 lc_opt_entry_t *lc_opt_add_opt(lc_opt_entry_t *parent,
230 const char *name, const char *desc,
231 lc_opt_type_t type, void *value, size_t length,
232 lc_opt_callback_t *cb, lc_opt_dump_t *dump,
233 lc_opt_dump_vals_t *dump_vals,
234 lc_opt_err_info_t *err)
236 lc_opt_entry_t *res = NULL;
238 if (parent->is_grp) {
239 lc_opt_entry_t *ent = lc_opt_find_opt(parent, name, NULL);
242 res = OALLOC(&obst, lc_opt_entry_t);
243 init_entry(res, parent, name, desc);
244 init_opt(res, type, value, length, cb, dump, dump_vals, err);
246 set_error(err, lc_opt_err_opt_already_there, name);
248 set_error(err, lc_opt_err_grp_expected, name);
254 static lc_opt_entry_t *lc_opt_find_ent(const struct list_head *head, const char *name,
255 int error_to_use, lc_opt_err_info_t *err)
257 lc_opt_entry_t *ent, *found = NULL;
258 int error = error_to_use;
259 unsigned hash = HASH_STR(name, strlen(name));
261 if (!list_empty(head)) {
262 list_for_each_entry(lc_opt_entry_t, ent, head, list) {
263 if (entry_matches(ent, hash, name)) {
264 error = lc_opt_err_none;
271 set_error(err, error, name);
275 lc_opt_entry_t *lc_opt_find_grp(const lc_opt_entry_t *grp, const char *name, lc_opt_err_info_t *err)
277 return grp ? lc_opt_find_ent(&lc_get_grp_special(grp)->grps,
278 name, lc_opt_err_grp_not_found, err) : NULL;
281 lc_opt_entry_t *lc_opt_find_opt(const lc_opt_entry_t *grp, const char *name, lc_opt_err_info_t *err)
283 return grp ? lc_opt_find_ent(&lc_get_grp_special(grp)->opts,
284 name, lc_opt_err_opt_not_found, err) : NULL;
287 static const lc_opt_entry_t *resolve_up_to_last(const lc_opt_entry_t *root,
288 const char * const *names, int pos, int n, lc_opt_err_info_t *err)
295 ent = lc_opt_find_grp(root, names[pos], err);
296 return ent ? resolve_up_to_last(ent, names, pos + 1, n, err) : NULL;
299 static const char *path_delim = "/.";
301 static lc_opt_entry_t *resolve_up_to_last_str_rec(lc_opt_entry_t *from,
303 const char **last_name)
306 lc_opt_entry_t *res = from;
307 size_t end = strcspn(path, path_delim);
309 if (path[end] != '\0') {
310 /* skip all delimiters */
311 size_t next = strspn(path + end, path_delim);
313 /* copy the part of the path into a buffer */
314 char *buf = malloc((end+1) * sizeof(buf[0]));
315 strncpy(buf, path, end);
318 /* resolve the group and free */
319 from = lc_opt_get_grp(from, buf);
322 res = resolve_up_to_last_str_rec(from, path + end + next, last_name);
325 else if (last_name != NULL) {
332 static lc_opt_entry_t *resolve_up_to_last_str(lc_opt_entry_t *root, const char *path, const char **last_name)
334 size_t next = strspn(path, path_delim);
336 /* if l != 0 we saw deliminators, so we resolve from the root */
338 root = lc_opt_root_grp();
340 return resolve_up_to_last_str_rec(root, path + next, last_name);
343 lc_opt_entry_t *lc_opt_resolve_grp(const lc_opt_entry_t *root,
344 const char * const *names, int n, lc_opt_err_info_t *err)
346 const lc_opt_entry_t *grp = resolve_up_to_last(root, names, 0, n - 1, err);
347 return lc_opt_find_grp(grp, names[n - 1], err);
350 lc_opt_entry_t *lc_opt_resolve_opt(const lc_opt_entry_t *root,
351 const char * const *names, int n, lc_opt_err_info_t *err)
353 const lc_opt_entry_t *grp = resolve_up_to_last(root, names, 0, n - 1, err);
354 return lc_opt_find_opt(grp, names[n - 1], err);
357 static char *strtolower(char *buf, size_t n, const char *str)
360 for (i = 0; i < n; ++i)
361 buf[i] = tolower(str[i]);
365 int lc_opt_std_cb(UNUSED(const char *name), lc_opt_type_t type, void *data, size_t length, ...)
371 va_start(args, length);
376 case lc_opt_type_bit:
377 integer = va_arg(args, int);
379 *((int *) data) |= length;
381 *((int *) data) &= ~length;
384 case lc_opt_type_negbit:
385 integer = va_arg(args, int);
387 *((int *) data) &= ~length;
389 *((int *) data) |= length;
392 case lc_opt_type_boolean:
393 *((int *) data) = va_arg(args, int);
396 case lc_opt_type_negboolean:
397 *((int *) data) = !va_arg(args, int);
400 case lc_opt_type_string:
401 strncpy(data, va_arg(args, const char *), length);
404 case lc_opt_type_int:
405 *((int *) data) = va_arg(args, int);
408 case lc_opt_type_double:
409 *((double *) data) = va_arg(args, double);
420 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))
426 case lc_opt_type_bit:
427 case lc_opt_type_negbit:
428 res = snprintf(buf, n, "%x", *((unsigned *) data));
430 case lc_opt_type_boolean:
431 case lc_opt_type_negboolean:
432 res = snprintf(buf, n, "%s", *((int *) data) ? "true" : "false");
434 case lc_opt_type_string:
435 strncpy(buf, data, n);
438 case lc_opt_type_int:
439 res = snprintf(buf, n, "%d", *((int *) data));
441 case lc_opt_type_double:
442 res = snprintf(buf, n, "%g", *((double *) data));
458 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))
460 strncpy(buf, "true, false", n);
464 int lc_opt_occurs(lc_opt_entry_t *opt, const char *value, lc_opt_err_info_t *err)
466 static const struct {
481 int error = lc_opt_err_illegal_format;
482 lc_opt_special_t *s = lc_get_opt_special(opt);
487 } val_storage, *val = &val_storage;
490 set_error(err, lc_opt_err_opt_not_found, "");
495 set_error(err, lc_opt_err_no_callback, "");
502 case lc_opt_type_int:
503 if (sscanf(value, "%i", (int *) val)) {
504 error = lc_opt_err_unknown_value;
505 if (s->cb(opt->name, s->type, s->value, s->length, val->integer))
506 error = lc_opt_err_none;
510 case lc_opt_type_double:
511 if (sscanf(value, "%lf", (double *) val)) {
512 error = lc_opt_err_unknown_value;
513 if (s->cb(opt->name, s->type, s->value, s->length, val->dbl))
514 error = lc_opt_err_none;
518 case lc_opt_type_boolean:
519 case lc_opt_type_negboolean:
520 case lc_opt_type_bit:
521 case lc_opt_type_negbit:
522 strtolower(buf, sizeof(buf), value);
523 for (i = 0; i < LC_ARRSIZE(bool_strings); ++i) {
524 if (strcmp(buf, bool_strings[i].str) == 0) {
525 val->integer = bool_strings[i].val;
526 error = lc_opt_err_none;
531 if (error == lc_opt_err_none) {
532 error = lc_opt_err_unknown_value;
533 if (s->cb(opt->name, s->type, s->value, s->length, val->integer))
534 error = lc_opt_err_none;
539 case lc_opt_type_string:
540 case lc_opt_type_enum:
541 error = lc_opt_err_unknown_value;
542 if (s->cb(opt->name, s->type, s->value, s->length, value))
543 error = lc_opt_err_none;
547 set_error(err, error, value);
548 return error == lc_opt_err_none;
551 char *lc_opt_value_to_string(char *buf, size_t len, const lc_opt_entry_t *ent)
553 const lc_opt_special_t *s = lc_get_opt_special(ent);
555 s->dump(buf, len, ent->name, s->type, s->value, s->length);
557 strncpy(buf, "<n/a>", len);
562 static char *lc_opt_values_to_string(char *buf, size_t len, const lc_opt_entry_t *ent)
564 const lc_opt_special_t *s = lc_get_opt_special(ent);
566 s->dump_vals(buf, len, ent->name, s->type, s->value, s->length);
571 int lc_opt_add_table(lc_opt_entry_t *root, const lc_opt_table_entry_t *table)
574 lc_opt_err_info_t err;
576 for (i = 0; table[i].name != NULL; ++i) {
578 const lc_opt_table_entry_t *tab = &table[i];
579 lc_opt_entry_t *grp = resolve_up_to_last_str(root, tab->name, &name);
581 lc_opt_add_opt(grp, name, tab->desc, tab->type, tab->value, tab->len, tab->cb, tab->dump, tab->dump_vals, &err);
582 if (err.error != lc_opt_err_none)
589 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)
593 if (!lc_opt_grp_is_root(ent)) {
595 lc_opt_print_grp_path_rec(buf, len, ent->parent, separator, stop_ent);
597 if (l > 0 && l < len-1) {
603 strncat(buf, ent->name, len-1);
606 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)
610 lc_opt_print_grp_path_rec(buf, len, ent, separator, stop_ent);
615 * dump the option tree.
616 * @param ent starting entity
617 * @param separator separator char
618 * @param stop_ent stop at this entity when dumping the name
619 * @param f output file
621 static void lc_opt_print_help_rec(lc_opt_entry_t *ent, char separator, lc_opt_entry_t *stop_ent, FILE *f)
623 lc_grp_special_t *s = lc_get_grp_special(ent);
629 if (!list_empty(&s->opts)) {
630 lc_opt_print_grp_path(grp_name, sizeof(grp_name), ent, separator, stop_ent);
633 fprintf(f, "%s:\n", grp_name);
635 list_for_each_entry(lc_opt_entry_t, e, &s->opts, list) {
638 lc_opt_value_to_string(value, sizeof(value), e);
639 lc_opt_values_to_string(values, sizeof(values), e);
640 fprintf(f, HELP_TEMPL_VALS "\n", e->name, lc_opt_get_type_name(e), e->desc, value, values);
644 list_for_each_entry(lc_opt_entry_t, e, &s->grps, list) {
645 lc_opt_print_help_rec(e, separator, stop_ent, f);
650 void lc_opt_print_help(lc_opt_entry_t *ent, FILE *f)
652 fprintf(f, HELP_TEMPL_VALS "\n", "option", "type", "description", "default", "possible options");
653 lc_opt_print_help_rec(ent, '.', NULL, f);
656 void lc_opt_print_help_for_entry(lc_opt_entry_t *ent, char separator, FILE *f)
658 fprintf(f, HELP_TEMPL_VALS "\n", "option", "type", "description", "default", "possible options");
659 lc_opt_print_help_rec(ent, separator, ent, f);
663 static void indent(FILE *f, int n)
666 for (i = 0; i < n; ++i)
670 static void lc_opt_print_tree_lc_opt_indent(lc_opt_entry_t *ent, FILE *f, int level)
673 lc_opt_special_t *s = lc_get_opt_special(ent);
676 fprintf(f, "%c%s(\"%s\"):%s = %s\n", s->is_set ? '+' : '-', ent->name,
677 ent->desc, lc_opt_get_type_name(ent), lc_opt_value_to_string(buf, sizeof(buf), ent));
680 static void lc_opt_print_tree_grp_indent(lc_opt_entry_t *ent, FILE *f, int level)
687 s = lc_get_grp_special(ent);
689 fprintf(f, "/%s\n", ent->name);
691 list_for_each_entry(lc_opt_entry_t, e, &s->grps, list) {
692 lc_opt_print_tree_grp_indent(e, f, level + 2);
695 list_for_each_entry(lc_opt_entry_t, e, &s->opts, list) {
696 lc_opt_print_tree_lc_opt_indent(e, f, level + 2);
701 void lc_opt_print_tree(lc_opt_entry_t *ent, FILE *f)
703 lc_opt_print_tree_grp_indent(ent, f, 0);
706 static int lc_opts_default_error_handler(const char *prefix, const lc_opt_err_info_t *err)
708 fprintf(stderr, "%s: %s; %s\n", prefix, err->msg, err->arg);
712 void lc_opt_from_file(const char *filename, FILE *f, lc_opt_error_handler_t *handler)
715 handler = lc_opts_default_error_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)
766 while (end != NULL) {
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)
829 handler = lc_opts_default_error_handler;
831 for (i = 0; i < argc; ++i) {
832 options_set |= lc_opt_from_single_arg(root, opt_prefix, argv[i], handler);
838 static int opt_arg_type(UNUSED(const lc_arg_occ_t *occ))
840 return lc_arg_type_ptr;
843 static int opt_arg_emit(lc_appendable_t *app, const lc_arg_occ_t *occ, const lc_arg_value_t *arg)
847 lc_opt_entry_t *opt = arg->v_ptr;
851 switch (occ->conversion) {
853 lc_opt_value_to_string(buf, sizeof(buf), opt);
856 s = lc_opt_get_type_name(opt);
869 res = lc_appendable_snadd(app, s, strlen(s));
874 static const lc_arg_handler_t lc_opt_arg_handler = {
880 /* lc_printf facility for options */
882 const lc_arg_env_t *lc_opt_get_arg_env(void)
884 static lc_arg_env_t *env = NULL;
887 env = lc_arg_new_env();
889 lc_arg_register(env, "opt:value", 'V', &lc_opt_arg_handler);
890 lc_arg_register(env, "opt:type", 'T', &lc_opt_arg_handler);
891 lc_arg_register(env, "opt:desc", 'D', &lc_opt_arg_handler);
892 lc_arg_register(env, "opt:name", 'O', &lc_opt_arg_handler);
898 void lc_opt_default_configs(const char *ini_name)
902 char local_ini_file[MAX_PATH];
903 char home_dir_ini_file[MAX_PATH];
906 strncpy(local_ini_file, ini_name, sizeof(local_ini_file));
907 strncat(local_ini_file, ".ini", sizeof(local_ini_file)-1);
908 local_ini_file[sizeof(local_ini_file) - 1] = '\0';
913 /* ARG: need newer SDK to compile this */
914 SHGetFolderPath(NULL, CSIDL_LOCAL_APPDATA, NULL, 0, path);
915 strncat(path, "\\", sizeof(path)-1);
917 strncpy(home_dir_ini_file, local_ini_file, sizeof(home_dir_ini_file));
918 home_dir_ini_file[sizeof(home_dir_ini_file) - 1] = '\0';
921 struct passwd *entry = getpwuid(getuid());
923 strcpy(path, entry->pw_dir);
924 strncat(path, "/", sizeof(path)-1);
926 snprintf(home_dir_ini_file, sizeof(home_dir_ini_file), ".%src", ini_name);
927 home_dir_ini_file[sizeof(home_dir_ini_file) - 1] = '\0';
929 /* FIXME: some error occurred */
930 home_dir_ini_file[0] = '\0';
935 strncat(path, home_dir_ini_file, sizeof(path)-1);
936 path[sizeof(path) - 1] = '\0';
938 /* Process ini file in user's home. */
939 f = fopen(path, "rt");
941 lc_opt_from_file(path, f, lc_opts_default_error_handler);
945 /* Process ini file in current directory. */
946 f = fopen(local_ini_file, "rt");
948 lc_opt_from_file(local_ini_file, f, lc_opts_default_error_handler);