From 35023fab0f3fb6496f220d990da990e8582bc649 Mon Sep 17 00:00:00 2001 From: fennecdjay Date: Mon, 23 Sep 2019 23:54:14 +0200 Subject: [PATCH] :art: Improve arg handling --- include/arg.h | 2 +- src/arg.c | 82 ++++++++++++++++++++++++++++++++++++++------------- src/compile.c | 13 ++++---- src/gwion.c | 22 +++++++------- 4 files changed, 81 insertions(+), 38 deletions(-) diff --git a/include/arg.h b/include/arg.h index f80ac7e4..e3e4f1a9 100644 --- a/include/arg.h +++ b/include/arg.h @@ -14,5 +14,5 @@ typedef struct Arg_ { } Arg; ANN void arg_release(Arg*); -ANN void arg_parse(const Gwion, Arg*); +ANN m_bool arg_parse(const Gwion, Arg*); #endif diff --git a/src/arg.c b/src/arg.c index e2e3b331..3975dafd 100644 --- a/src/arg.c +++ b/src/arg.c @@ -66,13 +66,9 @@ static const char usage[] = ANN static void config_parse(const Gwion, Arg*, const m_str); -#define CASE(a,b) case a: (b) ; break; -#define get_arg(a) (a[i][2] == '\0' ? arg->argv[++i] : a[i] + 2) -#define ARG2INT(a) strtol(get_arg(a), NULL, 10) +#define ARG2INT(a) strtol(a, NULL, 10) ANN2(1) static inline void arg_set_pass(const Gwion gwion, const m_str str) { - if(!str) - gw_err(_("needs arguments")); const Vector v = split_args(gwion->mp, str); pass_set(gwion, v); for(m_uint i = 0; i < vector_size(v); ++i) @@ -80,26 +76,72 @@ ANN2(1) static inline void arg_set_pass(const Gwion gwion, const m_str str) { free_vector(gwion->mp, v); } -ANN void _arg_parse(const Gwion gwion, Arg* arg) { +ANN static inline m_str _get_arg(Arg* arg, int *i) { + const char key = arg->argv[*i][1]; + const m_str str = (arg->argv[*i][2] == '\0' ? arg->argv[++(*i)] : arg->argv[*i] + 2); + if(!str) + gw_err(_("option '-%c' needs arguments\n"), key); + return str; +} +ANN m_bool _arg_parse(const Gwion gwion, Arg* arg) { for(int i = 1; i < arg->argc; ++i) { if(arg->argv[i][0] == '-') { + m_str tmp; switch(arg->argv[i][1]) { - CASE('h', gw_err(usage)) - CASE('k', gw_err("CFLAGS: %s\nLDFLAGS: %s\n", CFLAGS, LDFLAGS)) - CASE('c', config_parse(gwion, arg, get_arg(arg->argv))) - CASE('p', vector_add(&arg->lib, (vtype)get_arg(arg->argv))) - CASE('m', vector_add(&arg->mod, (vtype)get_arg(arg->argv))) - CASE('l', arg->loop = (m_bool)ARG2INT(arg->argv) > 0 ? 1 : -1) - CASE('z', arg->memoize = (uint32_t)ARG2INT(arg->argv)) - CASE('i', arg->si->in = (uint8_t)ARG2INT(arg->argv)) - CASE('o', arg->si->out = (uint8_t)ARG2INT(arg->argv)) - CASE('s', arg->si->sr = (uint32_t)ARG2INT(arg->argv)) - CASE('d', arg->si->arg = get_arg(arg->argv)) - CASE('g', arg_set_pass(gwion, get_arg(arg->argv))) + case 'h': + gw_err(usage); + break; + case 'k': + gw_err("CFLAGS: %s\nLDFLAGS: %s\n", CFLAGS, LDFLAGS); + break; + case 'c': + CHECK_OB((tmp = _get_arg(arg, &i))) + config_parse(gwion, arg, tmp); + break; + case 'p': + CHECK_OB((tmp = _get_arg(arg, &i))) + vector_add(&arg->lib, (vtype)tmp); + break; + case 'm': + CHECK_OB((tmp = _get_arg(arg, &i))) + vector_add(&arg->mod, (vtype)tmp); + break; + case 'l': + CHECK_OB((tmp = _get_arg(arg, &i))) + arg->loop = (m_bool)ARG2INT(tmp) > 0 ? 1 : -1; + break; + case 'z': + CHECK_OB((tmp = _get_arg(arg, &i))) + arg->memoize = (uint32_t)ARG2INT(tmp); + break; + case 'i': + CHECK_OB((tmp = _get_arg(arg, &i))) + arg->si->in = (uint8_t)ARG2INT(tmp); + break; + case 'o': + CHECK_OB((tmp = _get_arg(arg, &i))) + arg->si->out = (uint8_t)ARG2INT(tmp); + break; + case 's': + CHECK_OB((tmp = _get_arg(arg, &i))) + arg->si->sr = (uint32_t)ARG2INT(tmp); + break; + case 'd': + CHECK_OB((tmp = _get_arg(arg, &i))) + arg->si->arg = tmp; + break; + case 'g': + CHECK_OB((tmp = _get_arg(arg, &i))) + arg_set_pass(gwion, tmp); + break; + default: + gw_err(_("invalid arguments")); + return GW_ERROR; } } else vector_add(&arg->add, (vtype)arg->argv[i]); } + return GW_OK; } ANN static void split_line(const m_str line, const Vector v) { @@ -153,8 +195,8 @@ ANN static void config_default(const Gwion gwion , Arg* arg) { config_parse(gwion, arg, c); } -ANN void arg_parse(const Gwion gwion, Arg* a) { +ANN m_bool arg_parse(const Gwion gwion, Arg* a) { arg_init(a); config_default(gwion, a); - _arg_parse(gwion, a); + return _arg_parse(gwion, a); } diff --git a/src/compile.c b/src/compile.c index a91d8c0f..68a3d649 100644 --- a/src/compile.c +++ b/src/compile.c @@ -98,11 +98,14 @@ static m_uint _compile(struct Gwion_* gwion, struct Compiler* c) { gw_err(_("while compiling file '%s'\n"), c->base); return 0; } - const VM_Shred shred = new_vm_shred(gwion->mp, gwion->emit->info->code); - shred->info->args = c->args; - vm_add_shred(gwion->vm, shred); - gwion->emit->info->code = NULL; - return shred->tick->xid; + if(gwion->emit->info->code) { + const VM_Shred shred = new_vm_shred(gwion->mp, gwion->emit->info->code); + shred->info->args = c->args; + vm_add_shred(gwion->vm, shred); + gwion->emit->info->code = NULL; + return shred->tick->xid; + } + return GW_OK; } static m_uint compile(struct Gwion_* gwion, struct Compiler* c) { diff --git a/src/gwion.c b/src/gwion.c index f80bb4dc..e26a9202 100644 --- a/src/gwion.c +++ b/src/gwion.c @@ -74,18 +74,16 @@ ANN m_bool gwion_ini(const Gwion gwion, Arg* arg) { gwion->data = new_gwiondata(gwion->mp); pass_default(gwion); arg->si = gwion->vm->bbq->si; - arg_parse(gwion, arg); - gwion->emit->info->memoize = arg->memoize; - gwion->plug = new_plug(gwion->mp, &arg->lib); -// map_set(&gwion->data->pass_map, (vtype)"check", (vtype)type_engine_check_prog); -// map_set(&gwion->data->pass_map, (vtype)"emit", (vtype)emit_ast); -// vector_add(&gwion->data->pass, (vtype)type_engine_check_prog); -// vector_add(&gwion->data->pass, (vtype)emit_ast); - shreduler_set_loop(gwion->vm->shreduler, arg->loop); - if(gwion_audio(gwion) > 0 && gwion_engine(gwion)) { - gwion_compile(gwion, &arg->add); - plug_run(gwion, &arg->mod); - return GW_OK; + const m_bool ret = arg_parse(gwion, arg); + if(ret) { + gwion->emit->info->memoize = arg->memoize; + gwion->plug = new_plug(gwion->mp, &arg->lib); + shreduler_set_loop(gwion->vm->shreduler, arg->loop); + if(gwion_audio(gwion) > 0 && gwion_engine(gwion)) { + gwion_compile(gwion, &arg->add); + plug_run(gwion, &arg->mod); + return GW_OK; + } } return GW_ERROR; } -- 2.43.0