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"
53 #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 = (char*)obstack_finish(&obst);
115 obstack_grow0(&obst, desc, strlen(desc));
116 copied_desc = (char*)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);
215 lc_opt_entry_t *lc_opt_get_grp(lc_opt_entry_t *parent, const char *name)
217 lc_opt_entry_t *ent = lc_opt_find_grp(parent, name, NULL);
220 ent = OALLOC(&obst, lc_opt_entry_t);
221 init_entry(ent, parent, name, "");
228 lc_opt_entry_t *lc_opt_add_opt(lc_opt_entry_t *parent,
229 const char *name, const char *desc,
230 lc_opt_type_t type, void *value, size_t length,
231 lc_opt_callback_t *cb, lc_opt_dump_t *dump,
232 lc_opt_dump_vals_t *dump_vals,
233 lc_opt_err_info_t *err)
235 lc_opt_entry_t *res = NULL;
237 if (parent->is_grp) {
238 lc_opt_entry_t *ent = lc_opt_find_opt(parent, name, NULL);
241 res = OALLOC(&obst, lc_opt_entry_t);
242 init_entry(res, parent, name, desc);
243 init_opt(res, type, value, length, cb, dump, dump_vals, err);
245 set_error(err, lc_opt_err_opt_already_there, name);
247 set_error(err, lc_opt_err_grp_expected, name);
253 static lc_opt_entry_t *lc_opt_find_ent(const struct list_head *head, const char *name,
254 int error_to_use, lc_opt_err_info_t *err)
256 lc_opt_entry_t *ent, *found = NULL;
257 int error = error_to_use;
258 unsigned hash = HASH_STR(name, strlen(name));
260 if (!list_empty(head)) {
261 list_for_each_entry(lc_opt_entry_t, ent, head, list) {
262 if (entry_matches(ent, hash, name)) {
263 error = lc_opt_err_none;
270 set_error(err, error, name);
274 lc_opt_entry_t *lc_opt_find_grp(const lc_opt_entry_t *grp, const char *name, lc_opt_err_info_t *err)
276 return grp ? lc_opt_find_ent(&lc_get_grp_special(grp)->grps,
277 name, lc_opt_err_grp_not_found, err) : NULL;
280 lc_opt_entry_t *lc_opt_find_opt(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)->opts,
283 name, lc_opt_err_opt_not_found, err) : NULL;
286 static const lc_opt_entry_t *resolve_up_to_last(const lc_opt_entry_t *root,
287 const char * const *names, int pos, int n, lc_opt_err_info_t *err)
294 ent = lc_opt_find_grp(root, names[pos], err);
295 return ent ? resolve_up_to_last(ent, names, pos + 1, n, err) : NULL;
298 static const char *path_delim = "/.";
300 static lc_opt_entry_t *resolve_up_to_last_str_rec(lc_opt_entry_t *from,
302 const char **last_name)
305 lc_opt_entry_t *res = from;
306 size_t end = strcspn(path, path_delim);
308 if (path[end] != '\0') {
309 /* skip all delimiters */
310 size_t next = strspn(path + end, path_delim);
312 /* copy the part of the path into a buffer */
313 char *buf = (char*)malloc((end+1) * sizeof(buf[0]));
314 strncpy(buf, path, end);
317 /* resolve the group and free */
318 from = lc_opt_get_grp(from, buf);
321 res = resolve_up_to_last_str_rec(from, path + end + next, last_name);
324 else if (last_name != NULL) {
331 static lc_opt_entry_t *resolve_up_to_last_str(lc_opt_entry_t *root, const char *path, const char **last_name)
333 size_t next = strspn(path, path_delim);
335 /* if l != 0 we saw deliminators, so we resolve from the root */
337 root = lc_opt_root_grp();
339 return resolve_up_to_last_str_rec(root, path + next, last_name);
342 lc_opt_entry_t *lc_opt_resolve_grp(const lc_opt_entry_t *root,
343 const char * const *names, int n, lc_opt_err_info_t *err)
345 const lc_opt_entry_t *grp = resolve_up_to_last(root, names, 0, n - 1, err);
346 return lc_opt_find_grp(grp, names[n - 1], err);
349 lc_opt_entry_t *lc_opt_resolve_opt(const lc_opt_entry_t *root,
350 const char * const *names, int n, lc_opt_err_info_t *err)
352 const lc_opt_entry_t *grp = resolve_up_to_last(root, names, 0, n - 1, err);
353 return lc_opt_find_opt(grp, names[n - 1], err);
356 static char *strtolower(char *buf, size_t n, const char *str)
359 for (i = 0; i < n; ++i)
360 buf[i] = tolower(str[i]);
364 int lc_opt_std_cb(UNUSED(const char *name), lc_opt_type_t type, void *data, size_t length, ...)
370 va_start(args, length);
375 case lc_opt_type_bit:
376 integer = va_arg(args, int);
378 *((int *) data) |= length;
380 *((int *) data) &= ~length;
383 case lc_opt_type_negbit:
384 integer = va_arg(args, int);
386 *((int *) data) &= ~length;
388 *((int *) data) |= length;
391 case lc_opt_type_boolean:
392 *((int *) data) = va_arg(args, int);
395 case lc_opt_type_negboolean:
396 *((int *) data) = !va_arg(args, int);
399 case lc_opt_type_string:
400 strncpy((char*)data, va_arg(args, const char *), length);
403 case lc_opt_type_int:
404 *((int *) data) = va_arg(args, int);
407 case lc_opt_type_double:
408 *((double *) data) = va_arg(args, double);
419 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))
425 case lc_opt_type_bit:
426 case lc_opt_type_negbit:
427 res = snprintf(buf, n, "%x", *((unsigned *) data));
429 case lc_opt_type_boolean:
430 case lc_opt_type_negboolean:
431 res = snprintf(buf, n, "%s", *((int *) data) ? "true" : "false");
433 case lc_opt_type_string:
434 strncpy(buf, (const char*)data, n);
437 case lc_opt_type_int:
438 res = snprintf(buf, n, "%d", *((int *) data));
440 case lc_opt_type_double:
441 res = snprintf(buf, n, "%g", *((double *) data));
457 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))
459 strncpy(buf, "true, false", n);
463 int lc_opt_occurs(lc_opt_entry_t *opt, const char *value, lc_opt_err_info_t *err)
465 static const struct {
480 int error = lc_opt_err_illegal_format;
481 lc_opt_special_t *s = lc_get_opt_special(opt);
486 } val_storage, *val = &val_storage;
489 set_error(err, lc_opt_err_opt_not_found, "");
494 set_error(err, lc_opt_err_no_callback, "");
501 case lc_opt_type_int:
502 if (sscanf(value, "%i", (int *) val)) {
503 error = lc_opt_err_unknown_value;
504 if (s->cb(opt->name, s->type, s->value, s->length, val->integer))
505 error = lc_opt_err_none;
509 case lc_opt_type_double:
510 if (sscanf(value, "%lf", (double *) val)) {
511 error = lc_opt_err_unknown_value;
512 if (s->cb(opt->name, s->type, s->value, s->length, val->dbl))
513 error = lc_opt_err_none;
517 case lc_opt_type_boolean:
518 case lc_opt_type_negboolean:
519 case lc_opt_type_bit:
520 case lc_opt_type_negbit:
521 strtolower(buf, sizeof(buf), value);
522 for (i = 0; i < LC_ARRSIZE(bool_strings); ++i) {
523 if (strcmp(buf, bool_strings[i].str) == 0) {
524 val->integer = bool_strings[i].val;
525 error = lc_opt_err_none;
530 if (error == lc_opt_err_none) {
531 error = lc_opt_err_unknown_value;
532 if (s->cb(opt->name, s->type, s->value, s->length, val->integer))
533 error = lc_opt_err_none;
538 case lc_opt_type_string:
539 case lc_opt_type_enum:
540 error = lc_opt_err_unknown_value;
541 if (s->cb(opt->name, s->type, s->value, s->length, value))
542 error = lc_opt_err_none;
544 case lc_opt_type_invalid:
548 set_error(err, error, value);
549 return error == lc_opt_err_none;
552 char *lc_opt_value_to_string(char *buf, size_t len, const lc_opt_entry_t *ent)
554 const lc_opt_special_t *s = lc_get_opt_special(ent);
556 s->dump(buf, len, ent->name, s->type, s->value, s->length);
558 strncpy(buf, "<n/a>", len);
563 static char *lc_opt_values_to_string(char *buf, size_t len, const lc_opt_entry_t *ent)
565 const lc_opt_special_t *s = lc_get_opt_special(ent);
567 s->dump_vals(buf, len, ent->name, s->type, s->value, s->length);
572 int lc_opt_add_table(lc_opt_entry_t *root, const lc_opt_table_entry_t *table)
575 lc_opt_err_info_t err;
577 for (i = 0; table[i].name != NULL; ++i) {
579 const lc_opt_table_entry_t *tab = &table[i];
580 lc_opt_entry_t *grp = resolve_up_to_last_str(root, tab->name, &name);
582 lc_opt_add_opt(grp, name, tab->desc, tab->type, tab->value, tab->len, tab->cb, tab->dump, tab->dump_vals, &err);
583 if (err.error != lc_opt_err_none)
590 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)
594 if (!lc_opt_grp_is_root(ent)) {
596 lc_opt_print_grp_path_rec(buf, len, ent->parent, separator, stop_ent);
598 if (l > 0 && l < len-1) {
604 strncat(buf, ent->name, len-1);
607 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)
611 lc_opt_print_grp_path_rec(buf, len, ent, separator, stop_ent);
616 * dump the option tree.
617 * @param ent starting entity
618 * @param separator separator char
619 * @param stop_ent stop at this entity when dumping the name
620 * @param f output file
622 static void lc_opt_print_help_rec(lc_opt_entry_t *ent, char separator, lc_opt_entry_t *stop_ent, FILE *f)
624 lc_grp_special_t *s = lc_get_grp_special(ent);
630 if (!list_empty(&s->opts)) {
631 lc_opt_print_grp_path(grp_name, sizeof(grp_name), ent, separator, stop_ent);
634 fprintf(f, "%s:\n", grp_name);
636 list_for_each_entry(lc_opt_entry_t, e, &s->opts, list) {
639 lc_opt_value_to_string(value, sizeof(value), e);
640 lc_opt_values_to_string(values, sizeof(values), e);
641 fprintf(f, HELP_TEMPL_VALS "\n", e->name, lc_opt_get_type_name(e), e->desc, value, values);
645 list_for_each_entry(lc_opt_entry_t, e, &s->grps, list) {
646 lc_opt_print_help_rec(e, separator, stop_ent, f);
651 void lc_opt_print_help(lc_opt_entry_t *ent, FILE *f)
653 fprintf(f, HELP_TEMPL_VALS "\n", "option", "type", "description", "default", "possible options");
654 lc_opt_print_help_rec(ent, '.', NULL, f);
657 void lc_opt_print_help_for_entry(lc_opt_entry_t *ent, char separator, FILE *f)
659 fprintf(f, HELP_TEMPL_VALS "\n", "option", "type", "description", "default", "possible options");
660 lc_opt_print_help_rec(ent, separator, ent, f);
664 static void indent(FILE *f, int n)
667 for (i = 0; i < n; ++i)
671 static void lc_opt_print_tree_lc_opt_indent(lc_opt_entry_t *ent, FILE *f, int level)
674 lc_opt_special_t *s = lc_get_opt_special(ent);
677 fprintf(f, "%c%s(\"%s\"):%s = %s\n", s->is_set ? '+' : '-', ent->name,
678 ent->desc, lc_opt_get_type_name(ent), lc_opt_value_to_string(buf, sizeof(buf), ent));
681 static void lc_opt_print_tree_grp_indent(lc_opt_entry_t *ent, FILE *f, int level)
688 s = lc_get_grp_special(ent);
690 fprintf(f, "/%s\n", ent->name);
692 list_for_each_entry(lc_opt_entry_t, e, &s->grps, list) {
693 lc_opt_print_tree_grp_indent(e, f, level + 2);
696 list_for_each_entry(lc_opt_entry_t, e, &s->opts, list) {
697 lc_opt_print_tree_lc_opt_indent(e, f, level + 2);
702 void lc_opt_print_tree(lc_opt_entry_t *ent, FILE *f)
704 lc_opt_print_tree_grp_indent(ent, f, 0);
707 static int lc_opts_default_error_handler(const char *prefix, const lc_opt_err_info_t *err)
709 fprintf(stderr, "%s: %s; %s\n", prefix, err->msg, err->arg);
713 int lc_opt_from_single_arg(const lc_opt_entry_t *root,
714 const char *opt_prefix,
715 const char *arg, lc_opt_error_handler_t *handler)
717 const lc_opt_entry_t *grp = root;
718 size_t n = strlen(arg);
719 size_t n_prefix = opt_prefix ? strlen(opt_prefix) : 0;
723 lc_opt_err_info_t err;
724 const char *end, *eqsign;
726 if (n >= n_prefix && strncmp(opt_prefix, arg, n_prefix) == 0) {
727 arg = arg + n_prefix;
729 /* find the next delimiter (the -) and extract the string up to
731 end = strchr(arg, OPT_DELIM);
732 eqsign = strchr(arg, '=');
733 if (eqsign && eqsign < end)
735 while (end != NULL) {
737 * Copy the part of the option into the buffer and add the
740 char *buf = (char*)obstack_copy0(&obst, arg, end - arg);
742 /* Resolve the group inside the group */
743 grp = lc_opt_find_grp(grp, buf, &err);
744 error = lc_opt_raise_error(&err, handler, ERR_STRING, arg);
748 /* Find the next option part delimiter. */
750 end = strchr(arg, OPT_DELIM);
751 eqsign = strchr(arg, '=');
752 if (eqsign && eqsign < end)
754 obstack_free(&obst, buf);
762 * Now, we are at the last option part:
763 * --grp1-grp2-...-grpn-opt=value
764 * Check, for the = and evaluate the option string. If the = is
765 * missing, we should have a boolean option, but that is checked
768 end = strchr(arg, '=');
769 buf = (char*)obstack_copy0(&obst, arg, end ? end - arg : (int) strlen(arg));
770 opt = lc_opt_find_opt(grp, buf, &err);
771 error = lc_opt_raise_error(&err, handler, ERR_STRING, arg);
775 * Now evaluate the parameter of the option (the part after
776 * the =) if it was given.
778 arg = end ? end + 1 : "true";
780 /* Set the value of the option. */
781 lc_opt_occurs(opt, arg, &err);
782 ret = !lc_opt_raise_error(&err, handler, ERR_STRING, arg);
790 int lc_opt_from_argv(const lc_opt_entry_t *root,
791 const char *opt_prefix,
792 int argc, const char *argv[],
793 lc_opt_error_handler_t *handler)
799 handler = lc_opts_default_error_handler;
801 for (i = 0; i < argc; ++i) {
802 options_set |= lc_opt_from_single_arg(root, opt_prefix, argv[i], handler);
808 static int opt_arg_type(UNUSED(const lc_arg_occ_t *occ))
810 return lc_arg_type_ptr;
813 static int opt_arg_emit(lc_appendable_t *app, const lc_arg_occ_t *occ, const lc_arg_value_t *arg)
817 lc_opt_entry_t *opt = (lc_opt_entry_t*)arg->v_ptr;
821 switch (occ->conversion) {
823 lc_opt_value_to_string(buf, sizeof(buf), opt);
826 s = lc_opt_get_type_name(opt);
839 res = lc_appendable_snadd(app, s, strlen(s));
844 static const lc_arg_handler_t lc_opt_arg_handler = {
850 /* lc_printf facility for options */
852 const lc_arg_env_t *lc_opt_get_arg_env(void)
854 static lc_arg_env_t *env = NULL;
857 env = lc_arg_new_env();
859 lc_arg_register(env, "opt:value", 'V', &lc_opt_arg_handler);
860 lc_arg_register(env, "opt:type", 'T', &lc_opt_arg_handler);
861 lc_arg_register(env, "opt:desc", 'D', &lc_opt_arg_handler);
862 lc_arg_register(env, "opt:name", 'O', &lc_opt_arg_handler);