Commit 41519271 authored by Matthias Braun's avatar Matthias Braun
Browse files

libcore: remove unused parts, cleanup, use C99

This especially removes unused complicated error reporting mechanisms
parent c1106f5b
...@@ -278,7 +278,7 @@ int be_parse_arg(const char *arg) ...@@ -278,7 +278,7 @@ int be_parse_arg(const char *arg)
lc_opt_print_help_for_entry(be_grp, '-', stdout); lc_opt_print_help_for_entry(be_grp, '-', stdout);
return -1; return -1;
} }
return lc_opt_from_single_arg(be_grp, NULL, arg, NULL); return lc_opt_from_single_arg(be_grp, arg);
} }
/* Perform schedule verification if requested. */ /* Perform schedule verification if requested. */
......
...@@ -143,11 +143,11 @@ typedef struct module_opt_data_t { ...@@ -143,11 +143,11 @@ typedef struct module_opt_data_t {
* Searches in list for module option. If found, set option to given value and return true. * Searches in list for module option. If found, set option to given value and return true.
* Beware: return value of 0 means error. * Beware: return value of 0 means error.
*/ */
static int set_opt_module(const char *name, lc_opt_type_t type, void *data, static bool set_opt_module(const char *name, lc_opt_type_t type, void *data,
size_t length, ...) size_t length, ...)
{ {
module_opt_data_t *moddata = (module_opt_data_t*)data; module_opt_data_t *moddata = (module_opt_data_t*)data;
int res = 0; bool res = false;
va_list args; va_list args;
const char *opt; const char *opt;
const be_module_list_entry_t *module; const be_module_list_entry_t *module;
...@@ -160,7 +160,7 @@ static int set_opt_module(const char *name, lc_opt_type_t type, void *data, ...@@ -160,7 +160,7 @@ static int set_opt_module(const char *name, lc_opt_type_t type, void *data,
for (module = *(moddata->list_head); module != NULL; module = module->next) { for (module = *(moddata->list_head); module != NULL; module = module->next) {
if (strcmp(module->name, opt) == 0) { if (strcmp(module->name, opt) == 0) {
*(moddata->var) = module->data; *(moddata->var) = module->data;
res = 1; res = true;
break; break;
} }
} }
...@@ -251,6 +251,5 @@ void be_add_module_list_opt(lc_opt_entry_t *grp, const char *name, ...@@ -251,6 +251,5 @@ void be_add_module_list_opt(lc_opt_entry_t *grp, const char *name,
lc_opt_add_opt(grp, name, description, lc_opt_type_enum, lc_opt_add_opt(grp, name, description, lc_opt_type_enum,
moddata, sizeof(moddata[0]), moddata, sizeof(moddata[0]),
set_opt_module, dump_opt_module, dump_opt_module_vals, set_opt_module, dump_opt_module, dump_opt_module_vals);
NULL);
} }
This diff is collapsed.
...@@ -13,6 +13,7 @@ ...@@ -13,6 +13,7 @@
#define _LC_OPTS_H #define _LC_OPTS_H
#include <stdio.h> #include <stdio.h>
#include <stdbool.h>
#include "lc_printf.h" #include "lc_printf.h"
...@@ -29,40 +30,14 @@ typedef enum { ...@@ -29,40 +30,14 @@ typedef enum {
lc_opt_type_double lc_opt_type_double
} lc_opt_type_t; } lc_opt_type_t;
/**
* Error codes.
*/
typedef enum {
lc_opt_err_none = 0,
lc_opt_err_no_callback,
lc_opt_err_illegal_option_type,
lc_opt_err_illegal_format,
lc_opt_err_grp_not_found,
lc_opt_err_opt_not_found,
lc_opt_err_grp_expected,
lc_opt_err_opt_already_there,
lc_opt_err_file_not_found,
lc_opt_err_unknown_value
} lc_opt_err_t;
typedef struct {
int error;
const char *msg;
const char *arg;
} lc_opt_err_info_t;
#define lc_opt_is_error(err) ((err)->error != lc_opt_err_none)
typedef struct lc_opt_entry_t lc_opt_entry_t; typedef struct lc_opt_entry_t lc_opt_entry_t;
typedef int (lc_opt_callback_t)(const char *name, lc_opt_type_t type, void *data, size_t length, ...); typedef bool (lc_opt_callback_t)(const char *name, lc_opt_type_t type, void *data, size_t length, ...);
typedef int (lc_opt_dump_t)(char *buf, size_t n, const char *name, lc_opt_type_t type, void *data, size_t length); typedef int (lc_opt_dump_t)(char *buf, size_t n, const char *name, lc_opt_type_t type, void *data, size_t length);
typedef int (lc_opt_dump_vals_t)(char *buf, size_t n, const char *name, lc_opt_type_t type, void *data, size_t length); typedef int (lc_opt_dump_vals_t)(char *buf, size_t n, const char *name, lc_opt_type_t type, void *data, size_t length);
typedef int (lc_opt_error_handler_t)(const char *prefix, const lc_opt_err_info_t *err);
typedef struct { typedef struct {
const char *name; /**< The name of the option. */ const char *name; /**< The name of the option. */
const char *desc; /**< A description for the option. */ const char *desc; /**< A description for the option. */
...@@ -152,10 +127,9 @@ lc_opt_entry_t *lc_opt_add_opt(lc_opt_entry_t *grp, ...@@ -152,10 +127,9 @@ lc_opt_entry_t *lc_opt_add_opt(lc_opt_entry_t *grp,
void *value, size_t length, void *value, size_t length,
lc_opt_callback_t *cb, lc_opt_callback_t *cb,
lc_opt_dump_t *dump, lc_opt_dump_t *dump,
lc_opt_dump_vals_t *dump_vals, lc_opt_dump_vals_t *dump_vals);
lc_opt_err_info_t *err);
int lc_opt_std_cb(const char *name, lc_opt_type_t type, void *data, size_t length, ...); bool lc_opt_std_cb(const char *name, lc_opt_type_t type, void *data, size_t length, ...);
int lc_opt_std_dump(char *buf, size_t n, const char *name, lc_opt_type_t type, void *data, size_t length); int lc_opt_std_dump(char *buf, size_t n, const char *name, lc_opt_type_t type, void *data, size_t length);
...@@ -178,50 +152,45 @@ int lc_opt_bool_dump_vals(char *buf, size_t n, const char *name, lc_opt_type_t t ...@@ -178,50 +152,45 @@ int lc_opt_bool_dump_vals(char *buf, size_t n, const char *name, lc_opt_type_t t
* Find a group inside another group. * Find a group inside another group.
* @param grp The group to search inside. * @param grp The group to search inside.
* @param name The name of the group you are looking for. * @param name The name of the group you are looking for.
* @param err Error info (may be NULL).
* @return The group or NULL, if no such group can be found. * @return The group or NULL, if no such group can be found.
*/ */
lc_opt_entry_t *lc_opt_find_grp(const lc_opt_entry_t *grp, const char *name, lc_opt_err_info_t *err); lc_opt_entry_t *lc_opt_find_grp(const lc_opt_entry_t *grp, const char *name);
/** /**
* Find an option inside another group. * Find an option inside another group.
* @param grp The group to search inside. * @param grp The group to search inside.
* @param name The name of the option you are looking for. * @param name The name of the option you are looking for.
* @param err Error info (may be NULL).
* @return The group or NULL, if no such option can be found. * @return The group or NULL, if no such option can be found.
*/ */
lc_opt_entry_t *lc_opt_find_opt(const lc_opt_entry_t *grp, const char *name, lc_opt_err_info_t *err); lc_opt_entry_t *lc_opt_find_opt(const lc_opt_entry_t *grp, const char *name);
/** /**
* Resolve a group. * Resolve a group.
* @param root The group to start resolving from. * @param root The group to start resolving from.
* @param names A string array containing the path to the group. * @param names A string array containing the path to the group.
* @param n Number of entries in @p names to consider. * @param n Number of entries in @p names to consider.
* @param err Error information (may be NULL).
* @return The group or NULL, if none is found. * @return The group or NULL, if none is found.
*/ */
lc_opt_entry_t *lc_opt_resolve_grp(const lc_opt_entry_t *root, lc_opt_entry_t *lc_opt_resolve_grp(const lc_opt_entry_t *root,
const char * const *names, int n, lc_opt_err_info_t *err); const char * const *names, int n);
/** /**
* Resolve an option. * Resolve an option.
* @param root The group to start resolving from. * @param root The group to start resolving from.
* @param names A string array containing the path to the option. * @param names A string array containing the path to the option.
* @param n Number of entries in @p names to consider. * @param n Number of entries in @p names to consider.
* @param err Error information (may be NULL).
* @return The option or NULL, if none is found. * @return The option or NULL, if none is found.
*/ */
lc_opt_entry_t *lc_opt_resolve_opt(const lc_opt_entry_t *root, lc_opt_entry_t *lc_opt_resolve_opt(const lc_opt_entry_t *root,
const char * const *names, int n, lc_opt_err_info_t *err); const char * const *names, int n);
/** /**
* Set the value of an option. * Set the value of an option.
* @param opt The option to set. * @param opt The option to set.
* @param value The value of the option in a string representation. * @param value The value of the option in a string representation.
* @param err Error information (may be NULL).
* @return 0, if an error occurred, 1 else. * @return 0, if an error occurred, 1 else.
*/ */
int lc_opt_occurs(lc_opt_entry_t *opt, const char *value, lc_opt_err_info_t *err); bool lc_opt_occurs(lc_opt_entry_t *opt, const char *value);
/** /**
* Convert the option to a string representation. * Convert the option to a string representation.
...@@ -254,38 +223,14 @@ void lc_opt_print_help_for_entry(lc_opt_entry_t *ent, char separator, FILE *f); ...@@ -254,38 +223,14 @@ void lc_opt_print_help_for_entry(lc_opt_entry_t *ent, char separator, FILE *f);
void lc_opt_print_tree(lc_opt_entry_t *ent, FILE *f); void lc_opt_print_tree(lc_opt_entry_t *ent, FILE *f);
int lc_opt_add_table(lc_opt_entry_t *grp, const lc_opt_table_entry_t *table); bool lc_opt_add_table(lc_opt_entry_t *grp, const lc_opt_table_entry_t *table);
/**
* The same as lc_opt_from_single_arg() only for an array of arguments.
*/
int lc_opt_from_argv(const lc_opt_entry_t *root,
const char *opt_prefix,
int argc, const char *argv[],
lc_opt_error_handler_t *handler);
/** /**
* Set options from a single (command line) argument. * Set options from a single (command line) argument.
* @param root The root group we start resolving from. * @param root The root group we start resolving from.
* @param opt_prefix The option prefix which shall be stripped of (mostly --).
* @param arg The command line argument itself. * @param arg The command line argument itself.
* @param handler An error handler.
* @return 1, if the argument was set, 0 if not. * @return 1, if the argument was set, 0 if not.
*/ */
int lc_opt_from_single_arg(const lc_opt_entry_t *grp, int lc_opt_from_single_arg(const lc_opt_entry_t *grp, const char *arg);
const char *opt_prefix,
const char *arg,
lc_opt_error_handler_t *handler);
/**
* Get printf environment for the option module.
* Currently implemented options are:
* %{opt:value} (%V) Value of an option.
* %{opt:type} (%T) Type of an option.
* %{opt:name} (%O) Name of an option.
* %{opt:desc} (%D) Description of an option.
* @return The option printf environment.
*/
const lc_arg_env_t *lc_opt_get_arg_env(void);
#endif #endif
...@@ -16,7 +16,7 @@ ...@@ -16,7 +16,7 @@
static const char *delim = " \t|,"; static const char *delim = " \t|,";
#define DECL_CB(N, op) \ #define DECL_CB(N, op) \
int lc_opt_enum_ ## N ## _cb(const char *name, lc_opt_type_t type, void *data, size_t len, ...) \ bool lc_opt_enum_ ## N ## _cb(const char *name, lc_opt_type_t type, void *data, size_t len, ...) \
{ \ { \
lc_opt_enum_ ## N ## _var_t *var = (lc_opt_enum_ ## N ## _var_t*)data; \ lc_opt_enum_ ## N ## _var_t *var = (lc_opt_enum_ ## N ## _var_t*)data; \
const lc_opt_enum_ ## N ## _items_t *items = var->items; \ const lc_opt_enum_ ## N ## _items_t *items = var->items; \
...@@ -25,7 +25,7 @@ int lc_opt_enum_ ## N ## _cb(const char *name, lc_opt_type_t type, void *data, s ...@@ -25,7 +25,7 @@ int lc_opt_enum_ ## N ## _cb(const char *name, lc_opt_type_t type, void *data, s
char *s, *tmp; \ char *s, *tmp; \
size_t begin, end; \ size_t begin, end; \
const char *arg; \ const char *arg; \
int res = 0; \ bool res = false; \
\ \
(void) name; \ (void) name; \
(void) type; \ (void) type; \
...@@ -50,7 +50,7 @@ int lc_opt_enum_ ## N ## _cb(const char *name, lc_opt_type_t type, void *data, s ...@@ -50,7 +50,7 @@ int lc_opt_enum_ ## N ## _cb(const char *name, lc_opt_type_t type, void *data, s
for (i = 0; items[i].name != NULL; ++i) { \ for (i = 0; items[i].name != NULL; ++i) { \
if (strcmp(s, items[i].name) == 0) { \ if (strcmp(s, items[i].name) == 0) { \
*var->value op items[i].value; \ *var->value op items[i].value; \
res = 1; \ res = true; \
} \ } \
} \ } \
} \ } \
......
...@@ -19,7 +19,7 @@ typedef struct { \ ...@@ -19,7 +19,7 @@ typedef struct { \
const lc_opt_enum_ ## N ## _items_t *items; \ const lc_opt_enum_ ## N ## _items_t *items; \
} lc_opt_enum_ ## N ## _var_t; \ } lc_opt_enum_ ## N ## _var_t; \
\ \
int lc_opt_enum_ ## N ## _cb(const char *name, lc_opt_type_t type, void *data, size_t len, ...); \ bool lc_opt_enum_ ## N ## _cb(const char *name, lc_opt_type_t type, void *data, size_t len, ...); \
int lc_opt_enum_ ## N ## _dump(char *buf, size_t n, const char *name, lc_opt_type_t type, void *data, size_t len); \ int lc_opt_enum_ ## N ## _dump(char *buf, size_t n, const char *name, lc_opt_type_t type, void *data, size_t len); \
int lc_opt_enum_ ## N ## _dump_vals(char *buf, size_t n, const char *name, lc_opt_type_t type, void *data, size_t len); \ int lc_opt_enum_ ## N ## _dump_vals(char *buf, size_t n, const char *name, lc_opt_type_t type, void *data, size_t len); \
...@@ -48,7 +48,7 @@ typedef struct { ...@@ -48,7 +48,7 @@ typedef struct {
#define LC_OPT_ENT_ENUM_FUNC_PTR(name, desc, var) _LC_OPT_ENT_ENUM(func_ptr, name, desc, var) #define LC_OPT_ENT_ENUM_FUNC_PTR(name, desc, var) _LC_OPT_ENT_ENUM(func_ptr, name, desc, var)
int lc_opt_enum_func_ptr_cb(const char *name, lc_opt_type_t type, void *data, size_t len, ...); bool lc_opt_enum_func_ptr_cb(const char *name, lc_opt_type_t type, void *data, size_t len, ...);
int lc_opt_enum_func_ptr_dump(char *buf, size_t n, const char *name, lc_opt_type_t type, void *data, size_t len); int lc_opt_enum_func_ptr_dump(char *buf, size_t n, const char *name, lc_opt_type_t type, void *data, size_t len);
int lc_opt_enum_func_ptr_dump_vals(char *buf, size_t n, const char *name, lc_opt_type_t type, void *data, size_t len); int lc_opt_enum_func_ptr_dump_vals(char *buf, size_t n, const char *name, lc_opt_type_t type, void *data, size_t len);
......
...@@ -19,25 +19,22 @@ typedef struct { ...@@ -19,25 +19,22 @@ typedef struct {
} lc_grp_special_t; } lc_grp_special_t;
typedef struct { typedef struct {
lc_opt_type_t type; lc_opt_type_t type;
lc_opt_callback_t *cb; lc_opt_callback_t *cb;
lc_opt_dump_t *dump; lc_opt_dump_t *dump;
lc_opt_dump_vals_t *dump_vals; lc_opt_dump_vals_t *dump_vals;
void *value; void *value;
size_t length; size_t length;
unsigned is_set : 1; bool is_set : 1;
} lc_opt_special_t; } lc_opt_special_t;
struct lc_opt_entry_t { struct lc_opt_entry_t {
unsigned hash; unsigned hash;
const char *name; const char *name;
const char *desc; const char *desc;
struct lc_opt_entry_t *parent; struct lc_opt_entry_t *parent;
bool is_grp : 1;
unsigned is_grp : 1;
struct list_head list; struct list_head list;
union { union {
lc_grp_special_t grp; lc_grp_special_t grp;
lc_opt_special_t opt; lc_opt_special_t opt;
...@@ -47,8 +44,4 @@ struct lc_opt_entry_t { ...@@ -47,8 +44,4 @@ struct lc_opt_entry_t {
#define lc_get_opt_special(ent) (&(ent)->v.opt) #define lc_get_opt_special(ent) (&(ent)->v.opt)
#define lc_get_grp_special(ent) (&(ent)->v.grp) #define lc_get_grp_special(ent) (&(ent)->v.grp)
int lc_opt_raise_error(const lc_opt_err_info_t *err,
lc_opt_error_handler_t *handler,
const char *fmt, ...);
#endif #endif
...@@ -28,15 +28,15 @@ ...@@ -28,15 +28,15 @@
/* printf implementation */ /* printf implementation */
typedef struct lc_arg_t { typedef struct lc_arg_t {
struct lc_arg_t *next; struct lc_arg_t *next;
const char *name; const char *name;
char letter; char letter;
int lc_arg_type; int lc_arg_type;
const lc_arg_handler_t *handler; const lc_arg_handler_t *handler;
} lc_arg_t; } lc_arg_t;
struct lc_arg_env_t { struct lc_arg_env_t {
set *args; /**< Map for named arguments. */ set *args; /**< Map for named arguments. */
lc_arg_t *lower[26]; /**< Map for lower conversion specifiers. */ lc_arg_t *lower[26]; /**< Map for lower conversion specifiers. */
lc_arg_t *upper[26]; /**< Map for upper conversion specifiers. */ lc_arg_t *upper[26]; /**< Map for upper conversion specifiers. */
}; };
...@@ -79,27 +79,25 @@ void lc_arg_free_env(lc_arg_env_t *env) ...@@ -79,27 +79,25 @@ void lc_arg_free_env(lc_arg_env_t *env)
free(env); free(env);
} }
int lc_arg_register(lc_arg_env_t *env, const char *name, char letter, const lc_arg_handler_t *handler) int lc_arg_register(lc_arg_env_t *env, const char *name, char letter,
const lc_arg_handler_t *handler)
{ {
lc_arg_t arg; lc_arg_t arg;
lc_arg_t *ent;
int base = 0;
lc_arg_t **map = NULL;
arg.name = name; arg.name = name;
arg.letter = letter; arg.letter = letter;
arg.handler = handler; arg.handler = handler;
lc_arg_t **map = NULL;
int base = 0;
if (isupper((unsigned char)letter)) { if (isupper((unsigned char)letter)) {
map = env->upper; map = env->upper;
base = 'A'; base = 'A';
} } else if (islower((unsigned char)letter)) {
else if (islower((unsigned char)letter)) {
map = env->lower; map = env->lower;
base = 'a'; base = 'a';
} }
ent = set_insert(lc_arg_t, env->args, &arg, sizeof(arg), hash_str(name)); lc_arg_t *ent = set_insert(lc_arg_t, env->args, &arg, sizeof(arg), hash_str(name));
if (ent && base != 0) if (ent && base != 0)
map[letter - base] = ent; map[letter - base] = ent;
...@@ -262,33 +260,29 @@ static int std_emit(lc_appendable_t *app, const lc_arg_occ_t *occ, const lc_arg_ ...@@ -262,33 +260,29 @@ static int std_emit(lc_appendable_t *app, const lc_arg_occ_t *occ, const lc_arg_
make_fmt(fmt, sizeof(fmt), occ); make_fmt(fmt, sizeof(fmt), occ);
switch (occ->conversion) { switch (occ->conversion) {
/* Store the number of written characters in the given /* Store the number of written characters in the given
* int pointer location */ * int pointer location */
case 'n': case 'n': {
{ int *num = (int*)val->v_ptr;
int *num = (int*)val->v_ptr; *num = (int)app->written;
*num = (int)app->written;
}
break; break;
}
/* strings are dumped directly, since they can get really big. A /* strings are dumped directly, since they can get really big. A
* buffer of 128 letters for all other types should be enough. */ * buffer of 128 letters for all other types should be enough. */
case 's': case 's': {
{ const char *str = (const char*)val->v_ptr;
const char *str = (const char*)val->v_ptr; res = lc_arg_append(app, occ, str, strlen(str));
res = lc_arg_append(app, occ, str, strlen(str));
}
break; break;
}
default: default: {
{ int len = MAX(128, occ->width + 1);
int len = MAX(128, occ->width + 1); char *buf = XMALLOCN(char, len);
char *buf = XMALLOCN(char, len); res = dispatch_snprintf(buf, len, fmt, occ->lc_arg_type, val);
res = dispatch_snprintf(buf, len, fmt, occ->lc_arg_type, val); res = lc_appendable_snadd(app, buf, res);
res = lc_appendable_snadd(app, buf, res); free(buf);
free(buf); }
}
} }
return res; return res;
...@@ -333,29 +327,26 @@ static char *read_int(const char *s, int *value) ...@@ -333,29 +327,26 @@ static char *read_int(const char *s, int *value)
/* Generic printf() function. */ /* Generic printf() function. */
int lc_evpprintf(const lc_arg_env_t *env, lc_appendable_t *app, const char *fmt, va_list args) int lc_evpprintf(const lc_arg_env_t *env, lc_appendable_t *app, const char *fmt,
va_list args)
{ {
int res = 0; int res = 0;
const char *s;
const char *last = fmt + strlen(fmt); const char *last = fmt + strlen(fmt);
/* Find the first % */ /* Find the first % */
s = strchr(fmt, '%'); const char *s = strchr(fmt, '%');
/* Emit the text before the first % was found */ /* Emit the text before the first % was found */
lc_appendable_snadd(app, fmt, (s ? s : last) - fmt); lc_appendable_snadd(app, fmt, (s ? s : last) - fmt);
while (s != NULL) { while (s != NULL) {
lc_arg_occ_t occ;
lc_arg_value_t val; lc_arg_value_t val;
const lc_arg_t *arg = NULL;
const char *old;
char ch;
/* We must be at a '%' */ /* We must be at a '%' */
assert(*s == '%'); assert(*s == '%');
/* Reset the occurrence structure */ /* Reset the occurrence structure */
lc_arg_occ_t occ;
memset(&occ, 0, sizeof(occ)); memset(&occ, 0, sizeof(occ));
/* Eat all flags and set the corresponding flags in the occ struct */ /* Eat all flags and set the corresponding flags in the occ struct */
...@@ -400,74 +391,74 @@ int lc_evpprintf(const lc_arg_env_t *env, lc_appendable_t *app, const char *fmt, ...@@ -400,74 +391,74 @@ int lc_evpprintf(const lc_arg_env_t *env, lc_appendable_t *app, const char *fmt,
* - some modifiers followed by a conversion specifier * - some modifiers followed by a conversion specifier
* - or some other character, which ends this format invalidly * - or some other character, which ends this format invalidly
*/ */
ch = *s; char ch = *s;
const lc_arg_t *arg = NULL;
switch (ch) { switch (ch) {
case '%': case '%':
s++; s++;
res += lc_appendable_chadd(app, '%'); res += lc_appendable_chadd(app, '%');
break; break;
case '{': case '{': {
{ const char *named = ++s;
const char *named = ++s;
/* Read until the closing brace or end of the string. */ /* Read until the closing brace or end of the string. */
for (ch = *s; ch != '}' && ch != '\0'; ch = *++s) { for (ch = *s; ch != '}' && ch != '\0'; ch = *++s) {
} }