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
31 /* Includes to determine user's home directory */
35 #include <sys/types.h>
40 /* maximum length of a path. */
46 #include "lc_common_t.h"
47 #include "lc_opts_t.h"
48 #include "lc_opts_enum.h"
50 #include "lc_printf.h"
53 #define ERR_STRING "In argument \"%s\": "
57 #define HELP_TEMPL "%-15s %-10s %-45s"
58 #define HELP_TEMPL_VALS HELP_TEMPL " [%s] (%s)"
60 static struct obstack obst;
62 static void set_name(lc_opt_entry_t *ent, const char *name)
65 ent->hash = HASH_STR(name, strlen(name));
68 #define entry_matches(ent,hash_val,str) \
69 ((ent)->hash == hash_val && strcmp((ent)->name, (str)) == 0)
71 #define entries_equal(e1,e2) entry_matches(e1, (e2)->hash, (e2)->name)
73 static lc_opt_err_info_t *set_error(lc_opt_err_info_t *err, int error, const char *arg)
84 int lc_opt_raise_error(const lc_opt_err_info_t *err, lc_opt_error_handler_t *handler,
91 if (err && lc_opt_is_error(err)) {
95 vsnprintf(buf, sizeof(buf), fmt, args);
104 static lc_opt_entry_t *init_entry(lc_opt_entry_t *ent, lc_opt_entry_t *parent,
105 const char *name, const char *desc)
107 const char *copied_name;
108 const char *copied_desc;
110 obstack_grow0(&obst, name, strlen(name));
111 copied_name = (char*)obstack_finish(&obst);
112 obstack_grow0(&obst, desc, strlen(desc));
113 copied_desc = (char*)obstack_finish(&obst);
115 memset(ent, 0, sizeof(*ent));
116 set_name(ent, copied_name);
117 ent->desc = copied_desc;
118 ent->parent = parent;
122 static lc_opt_entry_t *init_grp(lc_opt_entry_t *ent, lc_opt_err_info_t *err)
125 INIT_LIST_HEAD(&ent->v.grp.grps);
126 INIT_LIST_HEAD(&ent->v.grp.opts);
128 set_error(err, lc_opt_err_none, "");
130 if (ent->parent->is_grp)
131 list_add_tail(&ent->list, &lc_get_grp_special(ent->parent)->grps);
133 set_error(err, lc_opt_err_grp_expected, ent->parent->name);
139 static lc_opt_entry_t *init_opt(lc_opt_entry_t *ent,
141 void *val, size_t length,
142 lc_opt_callback_t *cb,
144 lc_opt_dump_vals_t *dump_vals,
145 lc_opt_err_info_t *err)
147 lc_opt_special_t *s = lc_get_opt_special(ent);
150 set_error(err, lc_opt_err_none, "");
151 list_add_tail(&ent->list, &lc_get_grp_special(ent->parent)->opts);
157 s->dump_vals = dump_vals;
164 lc_opt_entry_t *lc_opt_root_grp(void)
166 static lc_opt_entry_t root_group;
167 static int inited = 0;
173 init_entry(&root_group, NULL, "root", "The root node");
174 init_grp(&root_group, NULL);
180 int lc_opt_grp_is_root(const lc_opt_entry_t *ent)
182 return ent->parent == NULL;
185 static const char *get_type_name(lc_opt_type_t type)
189 #define XXX(t) case lc_opt_type_ ## t: res = #t; break
197 case lc_opt_type_negbit: res = "bit"; break;
198 case lc_opt_type_negboolean: res = "boolean"; break;
207 const char *lc_opt_get_type_name(const lc_opt_entry_t *ent)
209 return get_type_name(lc_get_opt_special(ent)->type);
212 lc_opt_entry_t *lc_opt_get_grp(lc_opt_entry_t *parent, const char *name)
214 lc_opt_entry_t *ent = lc_opt_find_grp(parent, name, NULL);
217 ent = OALLOC(&obst, lc_opt_entry_t);
218 init_entry(ent, parent, name, "");
225 lc_opt_entry_t *lc_opt_add_opt(lc_opt_entry_t *parent,
226 const char *name, const char *desc,
227 lc_opt_type_t type, void *value, size_t length,
228 lc_opt_callback_t *cb, lc_opt_dump_t *dump,
229 lc_opt_dump_vals_t *dump_vals,
230 lc_opt_err_info_t *err)
232 lc_opt_entry_t *res = NULL;
234 if (parent->is_grp) {
235 lc_opt_entry_t *ent = lc_opt_find_opt(parent, name, NULL);
238 res = OALLOC(&obst, lc_opt_entry_t);
239 init_entry(res, parent, name, desc);
240 init_opt(res, type, value, length, cb, dump, dump_vals, err);
242 set_error(err, lc_opt_err_opt_already_there, name);
244 set_error(err, lc_opt_err_grp_expected, name);
250 static lc_opt_entry_t *lc_opt_find_ent(const struct list_head *head, const char *name,
251 int error_to_use, lc_opt_err_info_t *err)
253 lc_opt_entry_t *ent, *found = NULL;
254 int error = error_to_use;
255 unsigned hash = HASH_STR(name, strlen(name));
257 if (!list_empty(head)) {
258 list_for_each_entry(lc_opt_entry_t, ent, head, list) {
259 if (entry_matches(ent, hash, name)) {
260 error = lc_opt_err_none;
267 set_error(err, error, name);
271 lc_opt_entry_t *lc_opt_find_grp(const lc_opt_entry_t *grp, const char *name, lc_opt_err_info_t *err)
273 return grp ? lc_opt_find_ent(&lc_get_grp_special(grp)->grps,
274 name, lc_opt_err_grp_not_found, err) : NULL;
277 lc_opt_entry_t *lc_opt_find_opt(const lc_opt_entry_t *grp, const char *name, lc_opt_err_info_t *err)
279 return grp ? lc_opt_find_ent(&lc_get_grp_special(grp)->opts,
280 name, lc_opt_err_opt_not_found, err) : NULL;
283 static const lc_opt_entry_t *resolve_up_to_last(const lc_opt_entry_t *root,
284 const char * const *names, int pos, int n, lc_opt_err_info_t *err)
291 ent = lc_opt_find_grp(root, names[pos], err);
292 return ent ? resolve_up_to_last(ent, names, pos + 1, n, err) : NULL;
295 static const char *path_delim = "/.";
297 static lc_opt_entry_t *resolve_up_to_last_str_rec(lc_opt_entry_t *from,
299 const char **last_name)
302 lc_opt_entry_t *res = from;
303 size_t end = strcspn(path, path_delim);
305 if (path[end] != '\0') {
306 /* skip all delimiters */
307 size_t next = strspn(path + end, path_delim);
309 /* copy the part of the path into a buffer */
310 char *buf = (char*)malloc((end+1) * sizeof(buf[0]));
311 strncpy(buf, path, end);
314 /* resolve the group and free */
315 from = lc_opt_get_grp(from, buf);
318 res = resolve_up_to_last_str_rec(from, path + end + next, last_name);
321 else if (last_name != NULL) {
328 static lc_opt_entry_t *resolve_up_to_last_str(lc_opt_entry_t *root, const char *path, const char **last_name)
330 size_t next = strspn(path, path_delim);
332 /* if l != 0 we saw deliminators, so we resolve from the root */
334 root = lc_opt_root_grp();
336 return resolve_up_to_last_str_rec(root, path + next, last_name);
339 lc_opt_entry_t *lc_opt_resolve_grp(const lc_opt_entry_t *root,
340 const char * const *names, int n, lc_opt_err_info_t *err)
342 const lc_opt_entry_t *grp = resolve_up_to_last(root, names, 0, n - 1, err);
343 return lc_opt_find_grp(grp, names[n - 1], err);
346 lc_opt_entry_t *lc_opt_resolve_opt(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_opt(grp, names[n - 1], err);
353 static char *strtolower(char *buf, size_t n, const char *str)
356 for (i = 0; i < n; ++i)
357 buf[i] = tolower(str[i]);
361 int lc_opt_std_cb(UNUSED(const char *name), lc_opt_type_t type, void *data, size_t length, ...)
367 va_start(args, length);
372 case lc_opt_type_bit:
373 integer = va_arg(args, int);
375 *((int *) data) |= length;
377 *((int *) data) &= ~length;
380 case lc_opt_type_negbit:
381 integer = va_arg(args, int);
383 *((int *) data) &= ~length;
385 *((int *) data) |= length;
388 case lc_opt_type_boolean:
389 *((int *) data) = va_arg(args, int);
392 case lc_opt_type_negboolean:
393 *((int *) data) = !va_arg(args, int);
396 case lc_opt_type_string:
397 strncpy((char*)data, va_arg(args, const char *), length);
400 case lc_opt_type_int:
401 *((int *) data) = va_arg(args, int);
404 case lc_opt_type_double:
405 *((double *) data) = va_arg(args, double);
416 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))
422 case lc_opt_type_bit:
423 case lc_opt_type_negbit:
424 res = snprintf(buf, n, "%x", *((unsigned *) data));
426 case lc_opt_type_boolean:
427 case lc_opt_type_negboolean:
428 res = snprintf(buf, n, "%s", *((int *) data) ? "true" : "false");
430 case lc_opt_type_string:
431 strncpy(buf, (const char*)data, n);
434 case lc_opt_type_int:
435 res = snprintf(buf, n, "%d", *((int *) data));
437 case lc_opt_type_double:
438 res = snprintf(buf, n, "%g", *((double *) data));
454 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))
456 strncpy(buf, "true, false", n);
460 int lc_opt_occurs(lc_opt_entry_t *opt, const char *value, lc_opt_err_info_t *err)
462 static const struct {
477 int error = lc_opt_err_illegal_format;
478 lc_opt_special_t *s = lc_get_opt_special(opt);
483 } val_storage, *val = &val_storage;
486 set_error(err, lc_opt_err_opt_not_found, "");
491 set_error(err, lc_opt_err_no_callback, "");
498 case lc_opt_type_int:
499 if (sscanf(value, "%i", (int *) val)) {
500 error = lc_opt_err_unknown_value;
501 if (s->cb(opt->name, s->type, s->value, s->length, val->integer))
502 error = lc_opt_err_none;
506 case lc_opt_type_double:
507 if (sscanf(value, "%lf", (double *) val)) {
508 error = lc_opt_err_unknown_value;
509 if (s->cb(opt->name, s->type, s->value, s->length, val->dbl))
510 error = lc_opt_err_none;
514 case lc_opt_type_boolean:
515 case lc_opt_type_negboolean:
516 case lc_opt_type_bit:
517 case lc_opt_type_negbit:
518 strtolower(buf, sizeof(buf), value);
519 for (i = 0; i < LC_ARRSIZE(bool_strings); ++i) {
520 if (strcmp(buf, bool_strings[i].str) == 0) {
521 val->integer = bool_strings[i].val;
522 error = lc_opt_err_none;
527 if (error == lc_opt_err_none) {
528 error = lc_opt_err_unknown_value;
529 if (s->cb(opt->name, s->type, s->value, s->length, val->integer))
530 error = lc_opt_err_none;
535 case lc_opt_type_string:
536 case lc_opt_type_enum:
537 error = lc_opt_err_unknown_value;
538 if (s->cb(opt->name, s->type, s->value, s->length, value))
539 error = lc_opt_err_none;
541 case lc_opt_type_invalid:
545 set_error(err, error, value);
546 return error == lc_opt_err_none;
549 char *lc_opt_value_to_string(char *buf, size_t len, const lc_opt_entry_t *ent)
551 const lc_opt_special_t *s = lc_get_opt_special(ent);
553 s->dump(buf, len, ent->name, s->type, s->value, s->length);
555 strncpy(buf, "<n/a>", len);
560 static char *lc_opt_values_to_string(char *buf, size_t len, const lc_opt_entry_t *ent)
562 const lc_opt_special_t *s = lc_get_opt_special(ent);
564 s->dump_vals(buf, len, ent->name, s->type, s->value, s->length);
569 int lc_opt_add_table(lc_opt_entry_t *root, const lc_opt_table_entry_t *table)
572 lc_opt_err_info_t err;
574 for (i = 0; table[i].name != NULL; ++i) {
576 const lc_opt_table_entry_t *tab = &table[i];
577 lc_opt_entry_t *grp = resolve_up_to_last_str(root, tab->name, &name);
579 lc_opt_add_opt(grp, name, tab->desc, tab->type, tab->value, tab->len, tab->cb, tab->dump, tab->dump_vals, &err);
580 if (err.error != lc_opt_err_none)
587 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)
591 if (!lc_opt_grp_is_root(ent)) {
593 lc_opt_print_grp_path_rec(buf, len, ent->parent, separator, stop_ent);
595 if (l > 0 && l < len-1) {
601 strncat(buf, ent->name, len-1);
604 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)
608 lc_opt_print_grp_path_rec(buf, len, ent, separator, stop_ent);
613 * dump the option tree.
614 * @param ent starting entity
615 * @param separator separator char
616 * @param stop_ent stop at this entity when dumping the name
617 * @param f output file
619 static void lc_opt_print_help_rec(lc_opt_entry_t *ent, char separator, lc_opt_entry_t *stop_ent, FILE *f)
621 lc_grp_special_t *s = lc_get_grp_special(ent);
627 if (!list_empty(&s->opts)) {
628 lc_opt_print_grp_path(grp_name, sizeof(grp_name), ent, separator, stop_ent);
631 fprintf(f, "%s:\n", grp_name);
633 list_for_each_entry(lc_opt_entry_t, e, &s->opts, list) {
636 lc_opt_value_to_string(value, sizeof(value), e);
637 lc_opt_values_to_string(values, sizeof(values), e);
638 fprintf(f, HELP_TEMPL_VALS "\n", e->name, lc_opt_get_type_name(e), e->desc, value, values);
642 list_for_each_entry(lc_opt_entry_t, e, &s->grps, list) {
643 lc_opt_print_help_rec(e, separator, stop_ent, f);
648 void lc_opt_print_help(lc_opt_entry_t *ent, FILE *f)
650 fprintf(f, HELP_TEMPL_VALS "\n", "option", "type", "description", "default", "possible options");
651 lc_opt_print_help_rec(ent, '.', NULL, f);
654 void lc_opt_print_help_for_entry(lc_opt_entry_t *ent, char separator, FILE *f)
656 fprintf(f, HELP_TEMPL_VALS "\n", "option", "type", "description", "default", "possible options");
657 lc_opt_print_help_rec(ent, separator, ent, f);
661 static void indent(FILE *f, int n)
664 for (i = 0; i < n; ++i)
668 static void lc_opt_print_tree_lc_opt_indent(lc_opt_entry_t *ent, FILE *f, int level)
671 lc_opt_special_t *s = lc_get_opt_special(ent);
674 fprintf(f, "%c%s(\"%s\"):%s = %s\n", s->is_set ? '+' : '-', ent->name,
675 ent->desc, lc_opt_get_type_name(ent), lc_opt_value_to_string(buf, sizeof(buf), ent));
678 static void lc_opt_print_tree_grp_indent(lc_opt_entry_t *ent, FILE *f, int level)
685 s = lc_get_grp_special(ent);
687 fprintf(f, "/%s\n", ent->name);
689 list_for_each_entry(lc_opt_entry_t, e, &s->grps, list) {
690 lc_opt_print_tree_grp_indent(e, f, level + 2);
693 list_for_each_entry(lc_opt_entry_t, e, &s->opts, list) {
694 lc_opt_print_tree_lc_opt_indent(e, f, level + 2);
699 void lc_opt_print_tree(lc_opt_entry_t *ent, FILE *f)
701 lc_opt_print_tree_grp_indent(ent, f, 0);
704 static int lc_opts_default_error_handler(const char *prefix, const lc_opt_err_info_t *err)
706 fprintf(stderr, "%s: %s; %s\n", prefix, err->msg, err->arg);
710 int lc_opt_from_single_arg(const lc_opt_entry_t *root,
711 const char *opt_prefix,
712 const char *arg, lc_opt_error_handler_t *handler)
714 const lc_opt_entry_t *grp = root;
715 size_t n = strlen(arg);
716 size_t n_prefix = opt_prefix ? strlen(opt_prefix) : 0;
720 lc_opt_err_info_t err;
721 const char *end, *eqsign;
723 if (n >= n_prefix && strncmp(opt_prefix, arg, n_prefix) == 0) {
724 arg = arg + n_prefix;
726 /* find the next delimiter (the -) and extract the string up to
728 end = strchr(arg, OPT_DELIM);
729 eqsign = strchr(arg, '=');
730 if (eqsign && eqsign < end)
732 while (end != NULL) {
734 * Copy the part of the option into the buffer and add the
737 char *buf = (char*)obstack_copy0(&obst, arg, end - arg);
739 /* Resolve the group inside the group */
740 grp = lc_opt_find_grp(grp, buf, &err);
741 error = lc_opt_raise_error(&err, handler, ERR_STRING, arg);
745 /* Find the next option part delimiter. */
747 end = strchr(arg, OPT_DELIM);
748 eqsign = strchr(arg, '=');
749 if (eqsign && eqsign < end)
751 obstack_free(&obst, buf);
759 * Now, we are at the last option part:
760 * --grp1-grp2-...-grpn-opt=value
761 * Check, for the = and evaluate the option string. If the = is
762 * missing, we should have a boolean option, but that is checked
765 end = strchr(arg, '=');
766 buf = (char*)obstack_copy0(&obst, arg, end ? end - arg : (int) strlen(arg));
767 opt = lc_opt_find_opt(grp, buf, &err);
768 error = lc_opt_raise_error(&err, handler, ERR_STRING, arg);
772 * Now evaluate the parameter of the option (the part after
773 * the =) if it was given.
775 arg = end ? end + 1 : "true";
777 /* Set the value of the option. */
778 lc_opt_occurs(opt, arg, &err);
779 ret = !lc_opt_raise_error(&err, handler, ERR_STRING, arg);
787 int lc_opt_from_argv(const lc_opt_entry_t *root,
788 const char *opt_prefix,
789 int argc, const char *argv[],
790 lc_opt_error_handler_t *handler)
796 handler = lc_opts_default_error_handler;
798 for (i = 0; i < argc; ++i) {
799 options_set |= lc_opt_from_single_arg(root, opt_prefix, argv[i], handler);
805 static int opt_arg_type(UNUSED(const lc_arg_occ_t *occ))
807 return lc_arg_type_ptr;
810 static int opt_arg_emit(lc_appendable_t *app, const lc_arg_occ_t *occ, const lc_arg_value_t *arg)
814 lc_opt_entry_t *opt = (lc_opt_entry_t*)arg->v_ptr;
818 switch (occ->conversion) {
820 lc_opt_value_to_string(buf, sizeof(buf), opt);
823 s = lc_opt_get_type_name(opt);
836 res = lc_appendable_snadd(app, s, strlen(s));
841 static const lc_arg_handler_t lc_opt_arg_handler = {
847 /* lc_printf facility for options */
849 const lc_arg_env_t *lc_opt_get_arg_env(void)
851 static lc_arg_env_t *env = NULL;
854 env = lc_arg_new_env();
856 lc_arg_register(env, "opt:value", 'V', &lc_opt_arg_handler);
857 lc_arg_register(env, "opt:type", 'T', &lc_opt_arg_handler);
858 lc_arg_register(env, "opt:desc", 'D', &lc_opt_arg_handler);
859 lc_arg_register(env, "opt:name", 'O', &lc_opt_arg_handler);