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)
lc_opt_print_help_for_entry(be_grp, '-', stdout);
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. */
......
......@@ -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.
* Beware: return value of 0 means error.
*/
static int set_opt_module(const char *name, lc_opt_type_t type, void *data,
size_t length, ...)
static bool set_opt_module(const char *name, lc_opt_type_t type, void *data,
size_t length, ...)
{
module_opt_data_t *moddata = (module_opt_data_t*)data;
int res = 0;
bool res = false;
va_list args;
const char *opt;
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,
for (module = *(moddata->list_head); module != NULL; module = module->next) {
if (strcmp(module->name, opt) == 0) {
*(moddata->var) = module->data;
res = 1;
res = true;
break;
}
}
......@@ -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,
moddata, sizeof(moddata[0]),
set_opt_module, dump_opt_module, dump_opt_module_vals,
NULL);
set_opt_module, dump_opt_module, dump_opt_module_vals);
}
This diff is collapsed.
......@@ -13,6 +13,7 @@
#define _LC_OPTS_H
#include <stdio.h>
#include <stdbool.h>
#include "lc_printf.h"
......@@ -29,40 +30,14 @@ typedef enum {
lc_opt_type_double
} 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 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_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 {
const char *name; /**< The name of 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,
void *value, size_t length,
lc_opt_callback_t *cb,
lc_opt_dump_t *dump,
lc_opt_dump_vals_t *dump_vals,
lc_opt_err_info_t *err);
lc_opt_dump_vals_t *dump_vals);
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);
......@@ -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.
* @param grp The group to search inside.
* @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.
*/
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.
* @param grp The group to search inside.
* @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.
*/
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.
* @param root The group to start resolving from.
* @param names A string array containing the path to the group.
* @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.
*/
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.
* @param root The group to start resolving from.
* @param names A string array containing the path to the option.
* @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.
*/
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.
* @param opt The option to set.
* @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.
*/
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.
......@@ -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);
int 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);
bool lc_opt_add_table(lc_opt_entry_t *grp, const lc_opt_table_entry_t *table);
/**
* Set options from a single (command line) argument.
* @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 handler An error handler.
* @return 1, if the argument was set, 0 if not.
*/
int lc_opt_from_single_arg(const lc_opt_entry_t *grp,
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);
int lc_opt_from_single_arg(const lc_opt_entry_t *grp, const char *arg);
#endif
......@@ -16,7 +16,7 @@
static const char *delim = " \t|,";
#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; \
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
char *s, *tmp; \
size_t begin, end; \
const char *arg; \
int res = 0; \
bool res = false; \
\
(void) name; \
(void) type; \
......@@ -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) { \
if (strcmp(s, items[i].name) == 0) { \
*var->value op items[i].value; \
res = 1; \
res = true; \
} \
} \
} \
......
......@@ -19,7 +19,7 @@ typedef struct { \
const lc_opt_enum_ ## N ## _items_t *items; \
} 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_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 {
#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_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 {
} lc_grp_special_t;
typedef struct {
lc_opt_type_t type;
lc_opt_callback_t *cb;
lc_opt_dump_t *dump;
lc_opt_type_t type;
lc_opt_callback_t *cb;
lc_opt_dump_t *dump;
lc_opt_dump_vals_t *dump_vals;
void *value;
size_t length;
unsigned is_set : 1;
void *value;
size_t length;
bool is_set : 1;
} lc_opt_special_t;
struct lc_opt_entry_t {
unsigned hash;
const char *name;
const char *desc;
unsigned hash;
const char *name;
const char *desc;
struct lc_opt_entry_t *parent;
unsigned is_grp : 1;
bool is_grp : 1;
struct list_head list;
union {
lc_grp_special_t grp;
lc_opt_special_t opt;
......@@ -47,8 +44,4 @@ struct lc_opt_entry_t {
#define lc_get_opt_special(ent) (&(ent)->v.opt)
#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
......@@ -28,15 +28,15 @@
/* printf implementation */
typedef struct lc_arg_t {
struct lc_arg_t *next;
const char *name;
char letter;
int lc_arg_type;
struct lc_arg_t *next;
const char *name;
char letter;
int lc_arg_type;
const lc_arg_handler_t *handler;
} lc_arg_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 *upper[26]; /**< Map for upper conversion specifiers. */
};
......@@ -79,27 +79,25 @@ void lc_arg_free_env(lc_arg_env_t *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 *ent;
int base = 0;
lc_arg_t **map = NULL;
arg.name = name;
arg.letter = letter;
arg.handler = handler;
lc_arg_t **map = NULL;
int base = 0;
if (isupper((unsigned char)letter)) {
map = env->upper;
base = 'A';
}
else if (islower((unsigned char)letter)) {
} else if (islower((unsigned char)letter)) {
map = env->lower;
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)
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_
make_fmt(fmt, sizeof(fmt), occ);
switch (occ->conversion) {
/* Store the number of written characters in the given
* int pointer location */
case 'n':
{
int *num = (int*)val->v_ptr;
*num = (int)app->written;
}
case 'n': {
int *num = (int*)val->v_ptr;
*num = (int)app->written;
break;
}
/* strings are dumped directly, since they can get really big. A
* buffer of 128 letters for all other types should be enough. */
case 's':
{
const char *str = (const char*)val->v_ptr;
res = lc_arg_append(app, occ, str, strlen(str));
}
case 's': {
const char *str = (const char*)val->v_ptr;
res = lc_arg_append(app, occ, str, strlen(str));
break;
}
default:
{
int len = MAX(128, occ->width + 1);
char *buf = XMALLOCN(char, len);
res = dispatch_snprintf(buf, len, fmt, occ->lc_arg_type, val);
res = lc_appendable_snadd(app, buf, res);
free(buf);
}
default: {
int len = MAX(128, occ->width + 1);
char *buf = XMALLOCN(char, len);
res = dispatch_snprintf(buf, len, fmt, occ->lc_arg_type, val);
res = lc_appendable_snadd(app, buf, res);
free(buf);
}
}
return res;
......@@ -333,29 +327,26 @@ static char *read_int(const char *s, int *value)
/* 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;
const char *s;
int res = 0;
const char *last = fmt + strlen(fmt);
/* Find the first % */
s = strchr(fmt, '%');
const char *s = strchr(fmt, '%');
/* Emit the text before the first % was found */
lc_appendable_snadd(app, fmt, (s ? s : last) - fmt);
while (s != NULL) {
lc_arg_occ_t occ;
lc_arg_value_t val;
const lc_arg_t *arg = NULL;
const char *old;
char ch;
/* We must be at a '%' */
assert(*s == '%');
/* Reset the occurrence structure */
lc_arg_occ_t occ;
memset(&occ, 0, sizeof(occ));
/* 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,
* - some modifiers followed by a conversion specifier
* - or some other character, which ends this format invalidly
*/
ch = *s;
char ch = *s;
const lc_arg_t *arg = NULL;
switch (ch) {
case '%':
s++;
res += lc_appendable_chadd(app, '%');
break;
case '{':
{
const char *named = ++s;
case '{': {
const char *named = ++s;
/* Read until the closing brace or end of the string. */
for (ch = *s; ch != '}' && ch != '\0'; ch = *++s) {
}
/* Read until the closing brace or end of the string. */
for (ch = *s; ch != '}' && ch != '\0'; ch = *++s) {
}
if (s - named) {
size_t n = s - named;
char *name;
lc_arg_t tmp;
if (s - named) {
size_t n = s - named;
char *name;
lc_arg_t tmp;
name = (char*) malloc(sizeof(char) * (n + 1));
memcpy(name, named, sizeof(char) * n);
name[n] = '\0';
tmp.name = name;
name = (char*) malloc(sizeof(char) * (n + 1));
memcpy(name, named, sizeof(char) * n);
name[n] = '\0';
tmp.name = name;
arg = set_find(lc_arg_t, env->args, &tmp, sizeof(tmp), hash_str(named));
occ.modifier = "";
occ.modifier_length = 0;
arg = set_find(lc_arg_t, env->args, &tmp, sizeof(tmp), hash_str(named));
occ.modifier = "";
occ.modifier_length = 0;
/* Set the conversion specifier of the occurrence to the
* letter specified in the argument description. */
if (arg)
occ.conversion = arg->letter;
/* Set the conversion specifier of the occurrence to the
* letter specified in the argument description. */
if (arg)
occ.conversion = arg->letter;
free(name);
free(name);
/* If we ended with a closing brace, move the current
* pointer after it, since it is not to be dumped. */
if (ch == '}')
s++;
}
/* If we ended with a closing brace, move the current
* pointer after it, since it is not to be dumped. */
if (ch == '}')
s++;
}
break;
}
default:
{
const char *mod = s;
/* Read, as long there are letters */
while (isalpha((unsigned char)ch) && !arg) {
int base = 'a';
lc_arg_t * const *map = env->lower;
/* If uppercase, select the uppercase map from the environment */
if (isupper((unsigned char)ch)) {
base = 'A';
map = env->upper;
}
if (map[ch - base] != NULL) {
occ.modifier = mod;
occ.modifier_length = s - mod;
occ.conversion = ch;
arg = map[ch - base];
}
ch = *++s;
default: {
const char *mod = s;
/* Read, as long there are letters */
while (isalpha((unsigned char)ch) && !arg) {
int base = 'a';
lc_arg_t *const *map = env->lower;
/* If uppercase, select the uppercase map from the
* environment */
if (isupper((unsigned char)ch)) {
base = 'A';
map = env->upper;
}
if (map[ch - base] != NULL) {
occ.modifier = mod;
occ.modifier_length = s - mod;
occ.conversion = ch;
arg = map[ch - base];
}
ch = *++s;
}
}
}
/* Call the handler if an argument was determined */
......@@ -490,7 +481,7 @@ int lc_evpprintf(const lc_arg_env_t *env, lc_appendable_t *app, const char *fmt,
res += handler->emit(app, &occ, &val);
}
old = s;
const char *old = s;
s = strchr(s, '%');
res += lc_appendable_snadd(app, old, (s ? s : last) - old);
}
......@@ -502,20 +493,18 @@ int lc_evpprintf(const lc_arg_env_t *env, lc_appendable_t *app, const char *fmt,
int lc_epprintf(const lc_arg_env_t *env, lc_appendable_t *app, const char *fmt, ...)
{
int res;
va_list args;
va_start(args, fmt);
res = lc_evpprintf(env, app, fmt, args);
int res = lc_evpprintf(env, app, fmt, args);
va_end(args);
return res;
}
int lc_pprintf(lc_appendable_t *app, const char *fmt, ...)
{
int res;
va_list args;
va_start(args, fmt);
res = lc_vpprintf(app, fmt, args);
int res = lc_vpprintf(app, fmt, args);
va_end(args);
return res;
}
......@@ -527,40 +516,36 @@ int lc_vpprintf(lc_appendable_t *app, const char *fmt, va_list args)
int lc_eprintf(const lc_arg_env_t *env, const char *fmt, ...)
{
int res;
va_list args;
va_start(args, fmt);
res = lc_efprintf(env, stdout, fmt, args);
int res = lc_efprintf(env, stdout, fmt, args);
va_end(args);
return res;
}
int lc_esnprintf(const lc_arg_env_t *env, char *buf, size_t len, const char *fmt, ...)
{
int res;
va_list args;
va_start(args, fmt);
res = lc_evsnprintf(env, buf, len, fmt, args);
int res = lc_evsnprintf(env, buf, len, fmt, args);
va_end(args);
return res;
}
int lc_efprintf(const lc_arg_env_t *env, FILE *file, const char *fmt, ...)
{
int res;
va_list args;
va_start(args, fmt);
res = lc_evfprintf(env, file, fmt, args);
int res = lc_evfprintf(env, file, fmt, args);
va_end(args);
return res;
}