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"
56 #define ERR_STRING "In argument \"%s\": "
60 #define HELP_TEMPL "%-15s %-10s %-45s"
61 #define HELP_TEMPL_VALS HELP_TEMPL " [%s] (%s)"
63 static struct obstack obst;
65 static void set_name(lc_opt_entry_t *ent, const char *name)
68 ent->hash = HASH_STR(name, strlen(name));
71 #define entry_matches(ent,hash_val,str) \
72 ((ent)->hash == hash_val && strcmp((ent)->name, (str)) == 0)
74 #define entries_equal(e1,e2) entry_matches(e1, (e2)->hash, (e2)->name)
76 static lc_opt_err_info_t *set_error(lc_opt_err_info_t *err, int error, const char *arg)
87 int lc_opt_raise_error(const lc_opt_err_info_t *err, lc_opt_error_handler_t *handler,
94 if(err && lc_opt_is_error(err)) {
98 vsnprintf(buf, sizeof(buf), fmt, args);
107 static lc_opt_entry_t *init_entry(lc_opt_entry_t *ent, lc_opt_entry_t *parent,
108 const char *name, const char *desc)
110 const char *copied_name;
111 const char *copied_desc;
113 obstack_grow0(&obst, name, strlen(name));
114 copied_name = obstack_finish(&obst);
115 obstack_grow0(&obst, desc, strlen(desc));
116 copied_desc = obstack_finish(&obst);
118 memset(ent, 0, sizeof(*ent));
119 set_name(ent, copied_name);
120 ent->desc = copied_desc;
121 ent->parent = parent;
125 static lc_opt_entry_t *init_grp(lc_opt_entry_t *ent, lc_opt_err_info_t *err)
128 INIT_LIST_HEAD(&ent->v.grp.grps);
129 INIT_LIST_HEAD(&ent->v.grp.opts);
131 set_error(err, lc_opt_err_none, "");
133 if(ent->parent->is_grp)
134 list_add_tail(&ent->list, &lc_get_grp_special(ent->parent)->grps);
136 set_error(err, lc_opt_err_grp_expected, ent->parent->name);
142 static lc_opt_entry_t *init_opt(lc_opt_entry_t *ent,
144 void *val, size_t length,
145 lc_opt_callback_t *cb,
147 lc_opt_dump_vals_t *dump_vals,
148 lc_opt_err_info_t *err)
150 lc_opt_special_t *s = lc_get_opt_special(ent);
153 set_error(err, lc_opt_err_none, "");
154 list_add_tail(&ent->list, &lc_get_grp_special(ent->parent)->opts);
160 s->dump_vals = dump_vals;
167 lc_opt_entry_t *lc_opt_root_grp(void)
169 static lc_opt_entry_t root_group;
170 static int inited = 0;
176 init_entry(&root_group, NULL, "root", "The root node");
177 init_grp(&root_group, NULL);
183 int lc_opt_grp_is_root(const lc_opt_entry_t *ent)
185 return ent->parent == NULL;
188 static const char *get_type_name(lc_opt_type_t type)
192 #define XXX(t) case lc_opt_type_ ## t: res = #t; break
200 case lc_opt_type_negbit: res = "bit"; break;
201 case lc_opt_type_negboolean: res = "boolean"; break;
210 const char *lc_opt_get_type_name(const lc_opt_entry_t *ent)
212 return get_type_name(lc_get_opt_special(ent)->type);
216 lc_opt_entry_t *lc_opt_find_grp(const lc_opt_entry_t *grp, const char *name, lc_opt_err_info_t *err);
217 lc_opt_entry_t *lc_opt_find_opt(const lc_opt_entry_t *grp, const char *name, lc_opt_err_info_t *err);
219 lc_opt_entry_t *lc_opt_get_grp(lc_opt_entry_t *parent, const char *name)
221 lc_opt_entry_t *ent = lc_opt_find_grp(parent, name, NULL);
224 ent = obstack_alloc(&obst, sizeof(*ent));
225 init_entry(ent, parent, name, "");
232 lc_opt_entry_t *lc_opt_add_opt(lc_opt_entry_t *parent,
233 const char *name, const char *desc,
234 lc_opt_type_t type, void *value, size_t length,
235 lc_opt_callback_t *cb, lc_opt_dump_t *dump,
236 lc_opt_dump_vals_t *dump_vals,
237 lc_opt_err_info_t *err)
239 lc_opt_entry_t *res = NULL;
242 lc_opt_entry_t *ent = lc_opt_find_opt(parent, name, NULL);
245 res = obstack_alloc(&obst, sizeof(*ent));
246 init_entry(res, parent, name, desc);
247 init_opt(res, type, value, length, cb, dump, dump_vals, err);
249 set_error(err, lc_opt_err_opt_already_there, name);
251 set_error(err, lc_opt_err_grp_expected, name);
257 static lc_opt_entry_t *lc_opt_find_ent(const struct list_head *head, const char *name,
258 int error_to_use, lc_opt_err_info_t *err)
260 lc_opt_entry_t *ent, *found = NULL;
261 int error = error_to_use;
262 unsigned hash = HASH_STR(name, strlen(name));
264 if(!list_empty(head)) {
265 list_for_each_entry(lc_opt_entry_t, ent, head, list) {
266 if(entry_matches(ent, hash, name)) {
267 error = lc_opt_err_none;
274 set_error(err, error, name);
278 lc_opt_entry_t *lc_opt_find_grp(const lc_opt_entry_t *grp, const char *name, lc_opt_err_info_t *err)
280 return grp ? lc_opt_find_ent(&lc_get_grp_special(grp)->grps,
281 name, lc_opt_err_grp_not_found, err) : NULL;
284 lc_opt_entry_t *lc_opt_find_opt(const lc_opt_entry_t *grp, const char *name, lc_opt_err_info_t *err)
286 return grp ? lc_opt_find_ent(&lc_get_grp_special(grp)->opts,
287 name, lc_opt_err_opt_not_found, err) : NULL;
290 static const lc_opt_entry_t *resolve_up_to_last(const lc_opt_entry_t *root,
291 const char * const *names, int pos, int n, lc_opt_err_info_t *err)
298 ent = lc_opt_find_grp(root, names[pos], err);
299 return ent ? resolve_up_to_last(ent, names, pos + 1, n, err) : NULL;
302 static const char *path_delim = "/.";
304 static lc_opt_entry_t *resolve_up_to_last_str_rec(lc_opt_entry_t *from,
306 const char **last_name)
309 lc_opt_entry_t *res = from;
310 size_t end = strcspn(path, path_delim);
312 if(path[end] != '\0') {
313 /* skip all delimiters */
314 size_t next = strspn(path + end, path_delim);
316 /* copy the part of the path into a buffer */
317 char *buf = malloc((end+1) * sizeof(buf[0]));
318 strncpy(buf, path, end);
321 /* resolve the group and free */
322 from = lc_opt_get_grp(from, buf);
325 res = resolve_up_to_last_str_rec(from, path + end + next, last_name);
328 else if(last_name != NULL) {
335 static lc_opt_entry_t *resolve_up_to_last_str(lc_opt_entry_t *root, const char *path, const char **last_name)
337 size_t next = strspn(path, path_delim);
339 /* if l != 0 we saw deliminators, so we resolve from the root */
341 root = lc_opt_root_grp();
343 return resolve_up_to_last_str_rec(root, path + next, last_name);
346 lc_opt_entry_t *lc_opt_resolve_grp(const lc_opt_entry_t *root,
347 const char * const *names, int n, lc_opt_err_info_t *err)
349 const lc_opt_entry_t *grp = resolve_up_to_last(root, names, 0, n - 1, err);
350 return lc_opt_find_grp(grp, names[n - 1], err);
353 lc_opt_entry_t *lc_opt_resolve_opt(const lc_opt_entry_t *root,
354 const char * const *names, int n, lc_opt_err_info_t *err)
356 const lc_opt_entry_t *grp = resolve_up_to_last(root, names, 0, n - 1, err);
357 return lc_opt_find_opt(grp, names[n - 1], err);
360 static char *strtolower(char *buf, size_t n, const char *str)
363 for(i = 0; i < n; ++i)
364 buf[i] = tolower(str[i]);
368 int lc_opt_std_cb(UNUSED(const char *name), lc_opt_type_t type, void *data, size_t length, ...)
374 va_start(args, length);
379 case lc_opt_type_bit:
380 integer = va_arg(args, int);
382 *((int *) data) |= length;
384 *((int *) data) &= ~length;
387 case lc_opt_type_negbit:
388 integer = va_arg(args, int);
390 *((int *) data) &= ~length;
392 *((int *) data) |= length;
395 case lc_opt_type_boolean:
396 *((int *) data) = va_arg(args, int);
399 case lc_opt_type_negboolean:
400 *((int *) data) = !va_arg(args, int);
403 case lc_opt_type_string:
404 strncpy(data, va_arg(args, const char *), length);
407 case lc_opt_type_int:
408 *((int *) data) = va_arg(args, int);
411 case lc_opt_type_double:
412 *((double *) data) = va_arg(args, double);
423 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))
429 case lc_opt_type_bit:
430 case lc_opt_type_negbit:
431 res = snprintf(buf, n, "%x", *((int *) data));
433 case lc_opt_type_boolean:
434 case lc_opt_type_negboolean:
435 res = snprintf(buf, n, "%s", *((int *) data) ? "true" : "false");
437 case lc_opt_type_string:
438 strncpy(buf, data, n);
441 case lc_opt_type_int:
442 res = snprintf(buf, n, "%d", *((int *) data));
444 case lc_opt_type_double:
445 res = snprintf(buf, n, "%g", *((double *) data));
461 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))
463 strncpy(buf, "true, false", n);
467 int lc_opt_occurs(lc_opt_entry_t *opt, const char *value, lc_opt_err_info_t *err)
469 static const struct {
484 int error = lc_opt_err_illegal_format;
485 lc_opt_special_t *s = lc_get_opt_special(opt);
490 } val_storage, *val = &val_storage;
493 set_error(err, lc_opt_err_opt_not_found, "");
498 set_error(err, lc_opt_err_no_callback, "");
505 case lc_opt_type_int:
506 if(sscanf(value, "%i", (int *) val)) {
507 error = lc_opt_err_unknown_value;
508 if (s->cb(opt->name, s->type, s->value, s->length, val->integer))
509 error = lc_opt_err_none;
513 case lc_opt_type_double:
514 if(sscanf(value, "%lf", (double *) val)) {
515 error = lc_opt_err_unknown_value;
516 if (s->cb(opt->name, s->type, s->value, s->length, val->dbl))
517 error = lc_opt_err_none;
521 case lc_opt_type_boolean:
522 case lc_opt_type_negboolean:
523 case lc_opt_type_bit:
524 case lc_opt_type_negbit:
525 strtolower(buf, sizeof(buf), value);
526 for(i = 0; i < LC_ARRSIZE(bool_strings); ++i) {
527 if(strcmp(buf, bool_strings[i].str) == 0) {
528 val->integer = bool_strings[i].val;
529 error = lc_opt_err_none;
534 if (error == lc_opt_err_none) {
535 error = lc_opt_err_unknown_value;
536 if (s->cb(opt->name, s->type, s->value, s->length, val->integer))
537 error = lc_opt_err_none;
542 case lc_opt_type_string:
543 case lc_opt_type_enum:
544 error = lc_opt_err_unknown_value;
545 if (s->cb(opt->name, s->type, s->value, s->length, value))
546 error = lc_opt_err_none;
550 set_error(err, error, value);
551 return error == lc_opt_err_none;
554 char *lc_opt_value_to_string(char *buf, size_t len, const lc_opt_entry_t *ent)
556 const lc_opt_special_t *s = lc_get_opt_special(ent);
558 s->dump(buf, len, ent->name, s->type, s->value, s->length);
560 strncpy(buf, "<n/a>", len);
565 char *lc_opt_values_to_string(char *buf, size_t len, const lc_opt_entry_t *ent)
567 const lc_opt_special_t *s = lc_get_opt_special(ent);
569 s->dump_vals(buf, len, ent->name, s->type, s->value, s->length);
574 static lc_opt_entry_t *resolve_up_to_last_str(lc_opt_entry_t *root, const char *path, const char **last_name);
576 int lc_opt_add_table(lc_opt_entry_t *root, const lc_opt_table_entry_t *table)
579 lc_opt_err_info_t err;
581 for(i = 0; table[i].name != NULL; ++i) {
583 const lc_opt_table_entry_t *tab = &table[i];
584 lc_opt_entry_t *grp = resolve_up_to_last_str(root, tab->name, &name);
586 lc_opt_add_opt(grp, name, tab->desc, tab->type, tab->value, tab->len, tab->cb, tab->dump, tab->dump_vals, &err);
587 if(err.error != lc_opt_err_none)
594 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)
598 if (!lc_opt_grp_is_root(ent)) {
600 lc_opt_print_grp_path_rec(buf, len, ent->parent, separator, stop_ent);
602 if (l > 0 && l < len-1) {
608 strncat(buf, ent->name, len);
611 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)
615 lc_opt_print_grp_path_rec(buf, len, ent, separator, stop_ent);
620 * dump the option tree.
621 * @param ent starting entity
622 * @param separator separator char
623 * @param stop_ent stop at this entity when dumping the name
624 * @param f output file
626 static void lc_opt_print_help_rec(lc_opt_entry_t *ent, char separator, lc_opt_entry_t *stop_ent, FILE *f)
628 lc_grp_special_t *s = lc_get_grp_special(ent);
634 if(!list_empty(&s->opts)) {
635 lc_opt_print_grp_path(grp_name, sizeof(grp_name), ent, separator, stop_ent);
638 fprintf(f, "%s:\n", grp_name);
640 list_for_each_entry(lc_opt_entry_t, e, &s->opts, list) {
643 lc_opt_value_to_string(value, sizeof(value), e);
644 lc_opt_values_to_string(values, sizeof(values), e);
645 fprintf(f, HELP_TEMPL_VALS "\n", e->name, lc_opt_get_type_name(e), e->desc, value, values);
649 list_for_each_entry(lc_opt_entry_t, e, &s->grps, list) {
650 lc_opt_print_help_rec(e, separator, stop_ent, f);
655 void lc_opt_print_help(lc_opt_entry_t *ent, FILE *f)
657 fprintf(f, HELP_TEMPL_VALS "\n", "option", "type", "description", "default", "possible options");
658 lc_opt_print_help_rec(ent, '.', NULL, f);
661 void lc_opt_print_help_for_entry(lc_opt_entry_t *ent, char separator, FILE *f)
663 fprintf(f, HELP_TEMPL_VALS "\n", "option", "type", "description", "default", "possible options");
664 lc_opt_print_help_rec(ent, separator, ent, f);
668 static void indent(FILE *f, int n)
671 for(i = 0; i < n; ++i)
675 static void lc_opt_print_tree_lc_opt_indent(lc_opt_entry_t *ent, FILE *f, int level)
678 lc_opt_special_t *s = lc_get_opt_special(ent);
681 fprintf(f, "%c%s(\"%s\"):%s = %s\n", s->is_set ? '+' : '-', ent->name,
682 ent->desc, lc_opt_get_type_name(ent), lc_opt_value_to_string(buf, sizeof(buf), ent));
685 static void lc_opt_print_tree_grp_indent(lc_opt_entry_t *ent, FILE *f, int level)
692 s = lc_get_grp_special(ent);
694 fprintf(f, "/%s\n", ent->name);
696 list_for_each_entry(lc_opt_entry_t, e, &s->grps, list) {
697 lc_opt_print_tree_grp_indent(e, f, level + 2);
700 list_for_each_entry(lc_opt_entry_t, e, &s->opts, list) {
701 lc_opt_print_tree_lc_opt_indent(e, f, level + 2);
706 void lc_opt_print_tree(lc_opt_entry_t *ent, FILE *f)
708 lc_opt_print_tree_grp_indent(ent, f, 0);
711 static int lc_opts_default_error_handler(const char *prefix, const lc_opt_err_info_t *err)
713 fprintf(stderr, "%s: %s; %s\n", prefix, err->msg, err->arg);
717 void lc_opt_from_file(const char *filename, FILE *f, lc_opt_error_handler_t *handler)
720 handler = lc_opts_default_error_handler;
722 lc_opt_init_parser(filename, handler);
726 int lc_opt_from_single_arg(const lc_opt_entry_t *root,
727 const char *opt_prefix,
728 const char *arg, lc_opt_error_handler_t *handler)
730 const lc_opt_entry_t *grp = root;
732 int n_prefix = opt_prefix ? strlen(opt_prefix) : 0;
736 lc_opt_err_info_t err;
737 char *end, *buf, *eqsign;
739 if(n >= n_prefix && strncmp(opt_prefix, arg, n_prefix) == 0) {
740 arg = arg + n_prefix;
743 * check, if the next character is a @.
744 * This means, that we want to read options
748 size_t n = strcspn(&arg[1], " \t\n");
749 char *fname = alloca(n + 1);
752 strncpy(fname, &arg[1], n);
753 if((f = fopen(fname, "rt")) != NULL) {
754 lc_opt_from_file(fname, f, handler);
756 set_error(&err, lc_opt_err_none, NULL);
760 set_error(&err, lc_opt_err_file_not_found, arg);
762 return !lc_opt_raise_error(&err, handler, "Unable to open file: %s", fname);
765 /* find the next delimiter (the -) and extract the string up to
767 end = strchr(arg, OPT_DELIM);
768 eqsign = strchr(arg, '=');
769 if (eqsign && eqsign < end)
773 * Copy the part of the option into the buffer and add the
776 buf = obstack_copy0(&obst, arg, end - arg);
778 /* Resolve the group inside the group */
779 grp = lc_opt_find_grp(grp, buf, &err);
780 error = lc_opt_raise_error(&err, handler, ERR_STRING, arg);
784 /* Find the next option part delimiter. */
786 end = strchr(arg, OPT_DELIM);
787 eqsign = strchr(arg, '=');
788 if (eqsign && eqsign < end)
790 obstack_free(&obst, buf);
797 * Now, we are at the last option part:
798 * --grp1-grp2-...-grpn-opt=value
799 * Check, for the = and evaluate the option string. If the = is
800 * missing, we should have a boolean option, but that is checked
803 end = strchr(arg, '=');
804 buf = obstack_copy0(&obst, arg, end ? end - arg : (int) strlen(arg));
805 opt = lc_opt_find_opt(grp, buf, &err);
806 error = lc_opt_raise_error(&err, handler, ERR_STRING, arg);
810 * Now evaluate the parameter of the option (the part after
811 * the =) if it was given.
813 arg = end ? end + 1 : "true";
815 /* Set the value of the option. */
816 lc_opt_occurs(opt, arg, &err);
817 ret = !lc_opt_raise_error(&err, handler, ERR_STRING, arg);
825 int lc_opt_from_argv(const lc_opt_entry_t *root,
826 const char *opt_prefix,
827 int argc, const char *argv[],
828 lc_opt_error_handler_t *handler)
834 handler = lc_opts_default_error_handler;
836 for(i = 0; i < argc; ++i) {
837 options_set |= lc_opt_from_single_arg(root, opt_prefix, argv[i], handler);
843 static int opt_arg_type(UNUSED(const lc_arg_occ_t *occ))
845 return lc_arg_type_ptr;
848 static int opt_arg_emit(lc_appendable_t *app, const lc_arg_occ_t *occ, const lc_arg_value_t *arg)
852 lc_opt_entry_t *opt = arg->v_ptr;
856 switch(occ->conversion) {
858 lc_opt_value_to_string(buf, sizeof(buf), opt);
861 s = lc_opt_get_type_name(opt);
874 res = lc_appendable_snadd(app, s, strlen(s));
879 static const lc_arg_handler_t lc_opt_arg_handler = {
885 /* lc_printf facility for options */
887 const lc_arg_env_t *lc_opt_get_arg_env(void)
889 static lc_arg_env_t *env = NULL;
892 env = lc_arg_new_env();
894 lc_arg_register(env, "opt:value", 'V', &lc_opt_arg_handler);
895 lc_arg_register(env, "opt:type", 'T', &lc_opt_arg_handler);
896 lc_arg_register(env, "opt:desc", 'D', &lc_opt_arg_handler);
897 lc_arg_register(env, "opt:name", 'O', &lc_opt_arg_handler);
903 void lc_opt_default_configs(const char *ini_name)
907 char local_ini_file[MAX_PATH];
908 char home_dir_ini_file[MAX_PATH];
911 strncpy(local_ini_file, ini_name, sizeof(local_ini_file));
912 strncat(local_ini_file, ".ini", sizeof(local_ini_file));
913 local_ini_file[sizeof(local_ini_file) - 1] = '\0';
918 /* ARG: need newer SDK to compile this */
919 SHGetFolderPath(NULL, CSIDL_LOCAL_APPDATA, NULL, 0, path);
920 strncat(path, "\\", sizeof(path));
922 strncpy(home_dir_ini_file, local_ini_file, sizeof(home_dir_ini_file));
923 home_dir_ini_file[sizeof(home_dir_ini_file) - 1] = '\0';
926 struct passwd *entry = getpwuid(getuid());
928 strcpy(path, entry->pw_dir);
929 strncat(path, "/", sizeof(path));
931 snprintf(home_dir_ini_file, sizeof(home_dir_ini_file), ".%src", ini_name);
932 home_dir_ini_file[sizeof(home_dir_ini_file) - 1] = '\0';
934 /* FIXME: some error occurred */
935 home_dir_ini_file[0] = '\0';
940 strncat(path, home_dir_ini_file, sizeof(path));
941 path[sizeof(path) - 1] = '\0';
943 /* Process ini file in user's home. */
944 f = fopen(path, "rt");
946 lc_opt_from_file(path, f, lc_opts_default_error_handler);
950 /* Process ini file in current directory. */
951 f = fopen(local_ini_file, "rt");
953 lc_opt_from_file(local_ini_file, f, lc_opts_default_error_handler);