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
27 #include "lc_opts_t.h"
28 #include "lc_opts_enum.h"
30 #include "lc_printf.h"
35 #define ERR_STRING "In argument \"%s\": "
39 #define HELP_TEMPL "%-15s %-10s %-45s"
40 #define HELP_TEMPL_VALS HELP_TEMPL " [%s] (%s)"
42 static struct obstack obst;
44 static void set_name(lc_opt_entry_t *ent, const char *name)
47 ent->hash = hash_str(name);
50 #define entry_matches(ent,hash_val,str) \
51 ((ent)->hash == hash_val && strcmp((ent)->name, (str)) == 0)
53 #define entries_equal(e1,e2) entry_matches(e1, (e2)->hash, (e2)->name)
55 static lc_opt_err_info_t *set_error(lc_opt_err_info_t *err, int error, const char *arg)
66 int lc_opt_raise_error(const lc_opt_err_info_t *err, lc_opt_error_handler_t *handler,
73 if (err && lc_opt_is_error(err)) {
77 vsnprintf(buf, sizeof(buf), fmt, args);
86 static lc_opt_entry_t *init_entry(lc_opt_entry_t *ent, lc_opt_entry_t *parent,
87 const char *name, const char *desc)
89 const char *copied_name;
90 const char *copied_desc;
92 obstack_grow0(&obst, name, strlen(name));
93 copied_name = (char*)obstack_finish(&obst);
94 obstack_grow0(&obst, desc, strlen(desc));
95 copied_desc = (char*)obstack_finish(&obst);
97 memset(ent, 0, sizeof(*ent));
98 set_name(ent, copied_name);
99 ent->desc = copied_desc;
100 ent->parent = parent;
104 static lc_opt_entry_t *init_grp(lc_opt_entry_t *ent, lc_opt_err_info_t *err)
107 INIT_LIST_HEAD(&ent->v.grp.grps);
108 INIT_LIST_HEAD(&ent->v.grp.opts);
110 set_error(err, lc_opt_err_none, "");
112 if (ent->parent->is_grp)
113 list_add_tail(&ent->list, &lc_get_grp_special(ent->parent)->grps);
115 set_error(err, lc_opt_err_grp_expected, ent->parent->name);
121 static lc_opt_entry_t *init_opt(lc_opt_entry_t *ent,
123 void *val, size_t length,
124 lc_opt_callback_t *cb,
126 lc_opt_dump_vals_t *dump_vals,
127 lc_opt_err_info_t *err)
129 lc_opt_special_t *s = lc_get_opt_special(ent);
132 set_error(err, lc_opt_err_none, "");
133 list_add_tail(&ent->list, &lc_get_grp_special(ent->parent)->opts);
139 s->dump_vals = dump_vals;
146 lc_opt_entry_t *lc_opt_root_grp(void)
148 static lc_opt_entry_t root_group;
149 static int inited = 0;
155 init_entry(&root_group, NULL, "root", "The root node");
156 init_grp(&root_group, NULL);
162 int lc_opt_grp_is_root(const lc_opt_entry_t *ent)
164 return ent->parent == NULL;
167 static const char *get_type_name(lc_opt_type_t type)
171 #define XXX(t) case lc_opt_type_ ## t: res = #t; break
179 case lc_opt_type_negbit: res = "bit"; break;
180 case lc_opt_type_negboolean: res = "boolean"; break;
189 const char *lc_opt_get_type_name(const lc_opt_entry_t *ent)
191 return get_type_name(lc_get_opt_special(ent)->type);
194 lc_opt_entry_t *lc_opt_get_grp(lc_opt_entry_t *parent, const char *name)
196 lc_opt_entry_t *ent = lc_opt_find_grp(parent, name, NULL);
199 ent = OALLOC(&obst, lc_opt_entry_t);
200 init_entry(ent, parent, name, "");
207 lc_opt_entry_t *lc_opt_add_opt(lc_opt_entry_t *parent,
208 const char *name, const char *desc,
209 lc_opt_type_t type, void *value, size_t length,
210 lc_opt_callback_t *cb, lc_opt_dump_t *dump,
211 lc_opt_dump_vals_t *dump_vals,
212 lc_opt_err_info_t *err)
214 lc_opt_entry_t *res = NULL;
216 if (parent->is_grp) {
217 lc_opt_entry_t *ent = lc_opt_find_opt(parent, name, NULL);
220 res = OALLOC(&obst, lc_opt_entry_t);
221 init_entry(res, parent, name, desc);
222 init_opt(res, type, value, length, cb, dump, dump_vals, err);
224 set_error(err, lc_opt_err_opt_already_there, name);
226 set_error(err, lc_opt_err_grp_expected, name);
232 static lc_opt_entry_t *lc_opt_find_ent(const struct list_head *head, const char *name,
233 int error_to_use, lc_opt_err_info_t *err)
235 lc_opt_entry_t *found = NULL;
236 int error = error_to_use;
237 unsigned hash = hash_str(name);
239 if (!list_empty(head)) {
240 list_for_each_entry(lc_opt_entry_t, ent, head, list) {
241 if (entry_matches(ent, hash, name)) {
242 error = lc_opt_err_none;
249 set_error(err, error, name);
253 lc_opt_entry_t *lc_opt_find_grp(const lc_opt_entry_t *grp, const char *name, lc_opt_err_info_t *err)
255 return grp ? lc_opt_find_ent(&lc_get_grp_special(grp)->grps,
256 name, lc_opt_err_grp_not_found, err) : NULL;
259 lc_opt_entry_t *lc_opt_find_opt(const lc_opt_entry_t *grp, const char *name, lc_opt_err_info_t *err)
261 return grp ? lc_opt_find_ent(&lc_get_grp_special(grp)->opts,
262 name, lc_opt_err_opt_not_found, err) : NULL;
265 static const lc_opt_entry_t *resolve_up_to_last(const lc_opt_entry_t *root,
266 const char * const *names, int pos, int n, lc_opt_err_info_t *err)
273 ent = lc_opt_find_grp(root, names[pos], err);
274 return ent ? resolve_up_to_last(ent, names, pos + 1, n, err) : NULL;
277 static const char *path_delim = "/.";
279 static lc_opt_entry_t *resolve_up_to_last_str_rec(lc_opt_entry_t *from,
281 const char **last_name)
284 lc_opt_entry_t *res = from;
285 size_t end = strcspn(path, path_delim);
287 if (path[end] != '\0') {
288 /* skip all delimiters */
289 size_t next = strspn(path + end, path_delim);
291 /* copy the part of the path into a buffer */
292 char *buf = (char*)malloc((end+1) * sizeof(buf[0]));
293 strncpy(buf, path, end);
296 /* resolve the group and free */
297 from = lc_opt_get_grp(from, buf);
300 res = resolve_up_to_last_str_rec(from, path + end + next, last_name);
303 else if (last_name != NULL) {
310 static lc_opt_entry_t *resolve_up_to_last_str(lc_opt_entry_t *root, const char *path, const char **last_name)
312 size_t next = strspn(path, path_delim);
314 /* if l != 0 we saw deliminators, so we resolve from the root */
316 root = lc_opt_root_grp();
318 return resolve_up_to_last_str_rec(root, path + next, last_name);
321 lc_opt_entry_t *lc_opt_resolve_grp(const lc_opt_entry_t *root,
322 const char * const *names, int n, lc_opt_err_info_t *err)
324 const lc_opt_entry_t *grp = resolve_up_to_last(root, names, 0, n - 1, err);
325 return lc_opt_find_grp(grp, names[n - 1], err);
328 lc_opt_entry_t *lc_opt_resolve_opt(const lc_opt_entry_t *root,
329 const char * const *names, int n, lc_opt_err_info_t *err)
331 const lc_opt_entry_t *grp = resolve_up_to_last(root, names, 0, n - 1, err);
332 return lc_opt_find_opt(grp, names[n - 1], err);
335 static char *strtolower(char *buf, size_t n, const char *str)
338 for (i = 0; i < n; ++i)
339 buf[i] = tolower((unsigned char)str[i]);
343 int lc_opt_std_cb(const char *name, lc_opt_type_t type, void *data, size_t length, ...)
350 va_start(args, length);
355 case lc_opt_type_bit:
356 integer = va_arg(args, int);
358 *(unsigned*)data |= length;
360 *(unsigned*)data &= ~length;
363 case lc_opt_type_negbit:
364 integer = va_arg(args, int);
366 *(unsigned*)data &= ~length;
368 *(unsigned*)data |= length;
371 case lc_opt_type_boolean:
372 *((int *) data) = va_arg(args, int);
375 case lc_opt_type_negboolean:
376 *((int *) data) = !va_arg(args, int);
379 case lc_opt_type_string:
380 strncpy((char*)data, va_arg(args, const char *), length);
383 case lc_opt_type_int:
384 *((int *) data) = va_arg(args, int);
387 case lc_opt_type_double:
388 *((double *) data) = va_arg(args, double);
399 int lc_opt_std_dump(char *buf, size_t n, const char *name, lc_opt_type_t type, void *data, size_t length)
407 case lc_opt_type_bit:
408 case lc_opt_type_negbit:
409 res = snprintf(buf, n, "%x", *((unsigned *) data));
411 case lc_opt_type_boolean:
412 case lc_opt_type_negboolean:
413 res = snprintf(buf, n, "%s", *((int *) data) ? "true" : "false");
415 case lc_opt_type_string:
416 strncpy(buf, (const char*)data, n);
419 case lc_opt_type_int:
420 res = snprintf(buf, n, "%d", *((int *) data));
422 case lc_opt_type_double:
423 res = snprintf(buf, n, "%g", *((double *) data));
439 int lc_opt_bool_dump_vals(char *buf, size_t n, const char *name, lc_opt_type_t type, void *data, size_t length)
445 strncpy(buf, "true, false", n);
449 int lc_opt_occurs(lc_opt_entry_t *opt, const char *value, lc_opt_err_info_t *err)
451 static const struct {
466 int error = lc_opt_err_illegal_format;
467 lc_opt_special_t *s = lc_get_opt_special(opt);
472 } val_storage, *val = &val_storage;
475 set_error(err, lc_opt_err_opt_not_found, "");
480 set_error(err, lc_opt_err_no_callback, "");
487 case lc_opt_type_int:
488 if (sscanf(value, "%i", (int *) val)) {
489 error = lc_opt_err_unknown_value;
490 if (s->cb(opt->name, s->type, s->value, s->length, val->integer))
491 error = lc_opt_err_none;
495 case lc_opt_type_double:
496 if (sscanf(value, "%lf", (double *) val)) {
497 error = lc_opt_err_unknown_value;
498 if (s->cb(opt->name, s->type, s->value, s->length, val->dbl))
499 error = lc_opt_err_none;
503 case lc_opt_type_boolean:
504 case lc_opt_type_negboolean:
505 case lc_opt_type_bit:
506 case lc_opt_type_negbit:
507 strtolower(buf, sizeof(buf), value);
508 for (i = 0; i < ARRAY_SIZE(bool_strings); ++i) {
509 if (strcmp(buf, bool_strings[i].str) == 0) {
510 val->integer = bool_strings[i].val;
511 error = lc_opt_err_none;
516 if (error == lc_opt_err_none) {
517 error = lc_opt_err_unknown_value;
518 if (s->cb(opt->name, s->type, s->value, s->length, val->integer))
519 error = lc_opt_err_none;
524 case lc_opt_type_string:
525 case lc_opt_type_enum:
526 error = lc_opt_err_unknown_value;
527 if (s->cb(opt->name, s->type, s->value, s->length, value))
528 error = lc_opt_err_none;
530 case lc_opt_type_invalid:
534 set_error(err, error, value);
535 return error == lc_opt_err_none;
538 char *lc_opt_value_to_string(char *buf, size_t len, const lc_opt_entry_t *ent)
540 const lc_opt_special_t *s = lc_get_opt_special(ent);
542 s->dump(buf, len, ent->name, s->type, s->value, s->length);
544 strncpy(buf, "<n/a>", len);
549 static char *lc_opt_values_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_vals(buf, len, ent->name, s->type, s->value, s->length);
558 int lc_opt_add_table(lc_opt_entry_t *root, const lc_opt_table_entry_t *table)
561 lc_opt_err_info_t err;
563 for (i = 0; table[i].name != NULL; ++i) {
565 const lc_opt_table_entry_t *tab = &table[i];
566 lc_opt_entry_t *grp = resolve_up_to_last_str(root, tab->name, &name);
568 lc_opt_add_opt(grp, name, tab->desc, tab->type, tab->value, tab->len, tab->cb, tab->dump, tab->dump_vals, &err);
569 if (err.error != lc_opt_err_none)
576 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)
580 if (!lc_opt_grp_is_root(ent)) {
582 lc_opt_print_grp_path_rec(buf, len, ent->parent, separator, stop_ent);
584 if (l > 0 && l < len-1) {
590 strncat(buf, ent->name, len-1);
593 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)
597 lc_opt_print_grp_path_rec(buf, len, ent, separator, stop_ent);
602 * dump the option tree.
603 * @param ent starting entity
604 * @param separator separator char
605 * @param stop_ent stop at this entity when dumping the name
606 * @param f output file
608 static void lc_opt_print_help_rec(lc_opt_entry_t *ent, char separator, lc_opt_entry_t *stop_ent, FILE *f)
610 lc_grp_special_t *s = lc_get_grp_special(ent);
615 if (!list_empty(&s->opts)) {
616 lc_opt_print_grp_path(grp_name, sizeof(grp_name), ent, separator, stop_ent);
619 fprintf(f, "%s:\n", grp_name);
621 list_for_each_entry(lc_opt_entry_t, e, &s->opts, list) {
624 lc_opt_value_to_string(value, sizeof(value), e);
625 lc_opt_values_to_string(values, sizeof(values), e);
626 fprintf(f, HELP_TEMPL_VALS "\n", e->name, lc_opt_get_type_name(e), e->desc, value, values);
630 list_for_each_entry(lc_opt_entry_t, e, &s->grps, list) {
631 lc_opt_print_help_rec(e, separator, stop_ent, f);
636 void lc_opt_print_help(lc_opt_entry_t *ent, FILE *f)
638 fprintf(f, HELP_TEMPL_VALS "\n", "option", "type", "description", "default", "possible options");
639 lc_opt_print_help_rec(ent, '.', NULL, f);
642 void lc_opt_print_help_for_entry(lc_opt_entry_t *ent, char separator, FILE *f)
644 fprintf(f, HELP_TEMPL_VALS "\n", "option", "type", "description", "default", "possible options");
645 lc_opt_print_help_rec(ent, separator, ent, f);
649 static void indent(FILE *f, int n)
652 for (i = 0; i < n; ++i)
656 static void lc_opt_print_tree_lc_opt_indent(lc_opt_entry_t *ent, FILE *f, int level)
659 lc_opt_special_t *s = lc_get_opt_special(ent);
662 fprintf(f, "%c%s(\"%s\"):%s = %s\n", s->is_set ? '+' : '-', ent->name,
663 ent->desc, lc_opt_get_type_name(ent), lc_opt_value_to_string(buf, sizeof(buf), ent));
666 static void lc_opt_print_tree_grp_indent(lc_opt_entry_t *ent, FILE *f, int level)
671 s = lc_get_grp_special(ent);
673 fprintf(f, "/%s\n", ent->name);
675 list_for_each_entry(lc_opt_entry_t, e, &s->grps, list) {
676 lc_opt_print_tree_grp_indent(e, f, level + 2);
679 list_for_each_entry(lc_opt_entry_t, e, &s->opts, list) {
680 lc_opt_print_tree_lc_opt_indent(e, f, level + 2);
685 void lc_opt_print_tree(lc_opt_entry_t *ent, FILE *f)
687 lc_opt_print_tree_grp_indent(ent, f, 0);
690 static int lc_opts_default_error_handler(const char *prefix, const lc_opt_err_info_t *err)
692 fprintf(stderr, "%s: %s; %s\n", prefix, err->msg, err->arg);
696 int lc_opt_from_single_arg(const lc_opt_entry_t *root,
697 const char *opt_prefix,
698 const char *arg, lc_opt_error_handler_t *handler)
700 const lc_opt_entry_t *grp = root;
701 size_t n = strlen(arg);
702 size_t n_prefix = opt_prefix != NULL ? strlen(opt_prefix) : 0;
706 lc_opt_err_info_t err;
707 const char *end, *eqsign;
709 if (n >= n_prefix && (n_prefix == 0 || strncmp(opt_prefix, arg, n_prefix) == 0)) {
710 arg = arg + n_prefix;
712 /* find the next delimiter (the -) and extract the string up to
714 end = strchr(arg, OPT_DELIM);
715 eqsign = strchr(arg, '=');
716 if (eqsign && eqsign < end)
718 while (end != NULL) {
720 * Copy the part of the option into the buffer and add the
723 char *buf = (char*)obstack_copy0(&obst, arg, end - arg);
725 /* Resolve the group inside the group */
726 grp = lc_opt_find_grp(grp, buf, &err);
727 error = lc_opt_raise_error(&err, handler, ERR_STRING, arg);
731 /* Find the next option part delimiter. */
733 end = strchr(arg, OPT_DELIM);
734 eqsign = strchr(arg, '=');
735 if (eqsign && eqsign < end)
737 obstack_free(&obst, buf);
745 * Now, we are at the last option part:
746 * --grp1-grp2-...-grpn-opt=value
747 * Check, for the = and evaluate the option string. If the = is
748 * missing, we should have a boolean option, but that is checked
751 end = strchr(arg, '=');
752 buf = (char*)obstack_copy0(&obst, arg, end ? end - arg : (int) strlen(arg));
753 opt = lc_opt_find_opt(grp, buf, &err);
754 error = lc_opt_raise_error(&err, handler, ERR_STRING, arg);
758 * Now evaluate the parameter of the option (the part after
759 * the =) if it was given.
761 arg = end ? end + 1 : "true";
763 /* Set the value of the option. */
764 lc_opt_occurs(opt, arg, &err);
765 ret = !lc_opt_raise_error(&err, handler, ERR_STRING, arg);
773 int lc_opt_from_argv(const lc_opt_entry_t *root,
774 const char *opt_prefix,
775 int argc, const char *argv[],
776 lc_opt_error_handler_t *handler)
782 handler = lc_opts_default_error_handler;
784 for (i = 0; i < argc; ++i) {
785 options_set |= lc_opt_from_single_arg(root, opt_prefix, argv[i], handler);
791 static int opt_arg_type(const lc_arg_occ_t *occ)
794 return lc_arg_type_ptr;
797 static int opt_arg_emit(lc_appendable_t *app, const lc_arg_occ_t *occ, const lc_arg_value_t *arg)
801 lc_opt_entry_t *opt = (lc_opt_entry_t*)arg->v_ptr;
805 switch (occ->conversion) {
807 lc_opt_value_to_string(buf, sizeof(buf), opt);
810 s = lc_opt_get_type_name(opt);
823 res = lc_appendable_snadd(app, s, strlen(s));
828 static const lc_arg_handler_t lc_opt_arg_handler = {
834 /* lc_printf facility for options */
836 const lc_arg_env_t *lc_opt_get_arg_env(void)
838 static lc_arg_env_t *env = NULL;
841 env = lc_arg_new_env();
843 lc_arg_register(env, "opt:value", 'V', &lc_opt_arg_handler);
844 lc_arg_register(env, "opt:type", 'T', &lc_opt_arg_handler);
845 lc_arg_register(env, "opt:desc", 'D', &lc_opt_arg_handler);
846 lc_arg_register(env, "opt:name", 'O', &lc_opt_arg_handler);