From 4b41d8bf5c459f986b23050793107507127e1be1 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=A9mie=20Astor?= Date: Mon, 25 Nov 2019 22:15:47 +0100 Subject: [PATCH] :art: Carg utils --- include/arg.h | 3 +-- include/plug.h | 1 - src/arg.c | 49 ++++++++++++++++++++----------------------------- src/gwion.c | 5 ++++- src/main.c | 2 +- src/plug.c | 37 ------------------------------------- util | 2 +- 7 files changed, 27 insertions(+), 72 deletions(-) diff --git a/include/arg.h b/include/arg.h index 86c08db6..7efc4f97 100644 --- a/include/arg.h +++ b/include/arg.h @@ -2,13 +2,12 @@ #define __ARG typedef struct Arg_ { - char** argv; + struct CArg arg; struct Vector_ add; struct Vector_ lib; struct Vector_ mod; struct Vector_ config; struct SoundInfo_ *si; - int argc; m_bool loop; } Arg; diff --git a/include/plug.h b/include/plug.h index 6a81aa66..2065ae5d 100644 --- a/include/plug.h +++ b/include/plug.h @@ -27,5 +27,4 @@ ANN void* get_module(const struct Gwion_*, const m_str); #define GWMODEND(a) ANN void GWMODEND_NAME(const struct Gwion_ *gwion NUSED, void* self NUSED) #define GWDRIVER(a) ANN void GWDRIVER_NAME(DriverData* d) -ANN Vector split_args(MemPool, const m_str str); #endif diff --git a/src/arg.c b/src/arg.c index 39652278..eee2bc16 100644 --- a/src/arg.c +++ b/src/arg.c @@ -72,56 +72,49 @@ ANN2(1) static inline void arg_set_pass(const Gwion gwion, const m_str str) { free_vector(gwion->mp, v); } -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] == '-') { + struct CArg *ca = &arg->arg; + for(ca->idx = 1; ca->idx < ca->argc; ++ca->idx) { + if(ca->argv[ca->idx][0] == '-') { m_str tmp; - switch(arg->argv[i][1]) { + switch(ca->argv[ca->idx][1]) { case 'h': gw_err(usage); break; case 'c': - CHECK_OB((tmp = _get_arg(arg, &i))) + CHECK_OB((tmp = option_argument(ca))) config_parse(gwion, arg, tmp); break; case 'p': - CHECK_OB((tmp = _get_arg(arg, &i))) + CHECK_OB((tmp = option_argument(ca))) vector_add(&arg->lib, (vtype)tmp); break; case 'm': - CHECK_OB((tmp = _get_arg(arg, &i))) + CHECK_OB((tmp = option_argument(ca))) vector_add(&arg->mod, (vtype)tmp); break; case 'l': - CHECK_OB((tmp = _get_arg(arg, &i))) + CHECK_OB((tmp = option_argument(ca))) arg->loop = (m_bool)ARG2INT(tmp) > 0 ? 1 : -1; break; case 'i': - CHECK_OB((tmp = _get_arg(arg, &i))) + CHECK_OB((tmp = option_argument(ca))) arg->si->in = (uint8_t)ARG2INT(tmp); break; case 'o': - CHECK_OB((tmp = _get_arg(arg, &i))) + CHECK_OB((tmp = option_argument(ca))) arg->si->out = (uint8_t)ARG2INT(tmp); break; case 's': - CHECK_OB((tmp = _get_arg(arg, &i))) + CHECK_OB((tmp = option_argument(ca))) arg->si->sr = (uint32_t)ARG2INT(tmp); break; case 'd': - CHECK_OB((tmp = _get_arg(arg, &i))) + CHECK_OB((tmp = option_argument(ca))) arg->si->arg = tmp; break; case 'g': - CHECK_OB((tmp = _get_arg(arg, &i))) + CHECK_OB((tmp = option_argument(ca))) arg_set_pass(gwion, tmp); break; default: @@ -129,7 +122,7 @@ ANN m_bool _arg_parse(const Gwion gwion, Arg* arg) { return GW_ERROR; } } else - vector_add(&arg->add, (vtype)arg->argv[i]); + vector_add(&arg->add, (vtype)ca->argv[ca->idx]); } return GW_OK; } @@ -142,8 +135,8 @@ ANN static void split_line(const m_str line, const Vector v) { const m_bool arg = (str[sz-1] == '\n'); vector_add(v, (vtype)strndup(str, arg ? sz - 1 : sz)); } - free(d); - free(c); + xfree(d); + xfree(c); } ANN static Vector get_config(const m_str name) { @@ -167,13 +160,11 @@ ANN static Vector get_config(const m_str name) { ANN static void config_parse(const Gwion gwion, Arg* arg, const m_str name) { const Vector v = get_config(name); if(v) { - int argc = arg->argc; - char** argv = arg->argv; - arg->argc = vector_size(v); - arg->argv = (m_str*)(v->ptr + OFFSET); + struct CArg ca = arg->arg; + arg->arg.argc = vector_size(v); + arg->arg.argv = (m_str*)(v->ptr + OFFSET); _arg_parse(gwion, arg); - arg->argc = argc; - arg->argv = argv; + arg->arg = ca; vector_add(&arg->config, (vtype)v); } } diff --git a/src/gwion.c b/src/gwion.c index 1f5d5d23..bad13366 100644 --- a/src/gwion.c +++ b/src/gwion.c @@ -115,6 +115,8 @@ ANN static void gwion_end_child(const Gwion gwion) { } ANN static void gwion_cleaner(const Gwion gwion) { + if(!gwion->type[et_shred]) + return; const VM_Code code = new_vm_code(gwion->mp, NULL, 0, ae_flag_builtin, "in code dtor"); gwion->vm->cleaner_shred = new_vm_shred(gwion->mp, code); vm_add_shred(gwion->vm, gwion->vm->cleaner_shred); @@ -124,7 +126,8 @@ ANN void gwion_end(const Gwion gwion) { gwion_cleaner(gwion); gwion_end_child(gwion); free_env(gwion->env); - free_vm_shred(gwion->vm->cleaner_shred); + if(gwion->vm->cleaner_shred) + free_vm_shred(gwion->vm->cleaner_shred); free_emitter(gwion->mp, gwion->emit); free_vm(gwion->vm); pparg_end(gwion->ppa); diff --git a/src/main.c b/src/main.c index 7aaebfea..1d7f6dd5 100644 --- a/src/main.c +++ b/src/main.c @@ -15,7 +15,7 @@ static void sig(int unused NUSED) { } int main(int argc, char** argv) { - Arg arg = { .argc=argc, .argv=argv, .loop=-1 }; + Arg arg = { .arg={.argc=argc, .argv=argv}, .loop=-1 }; signal(SIGINT, sig); signal(SIGTERM, sig); struct Gwion_ gwion = {}; diff --git a/src/plug.c b/src/plug.c index eee7f245..dbfd0452 100644 --- a/src/plug.c +++ b/src/plug.c @@ -166,43 +166,6 @@ void free_plug(const struct Gwion_ *gwion) { mp_free(gwion->mp, PlugInfo, p); } -struct ArgSplitter { - m_str str; - Vector v; - MemPool mp; -}; - -ANN static void _split_args(struct ArgSplitter *as) { - const size_t sz = strlen(as->str); - char buf[sz + 1], prev = '\0'; - m_uint i = 0, j = 0; - char c; - while((c = as->str[i]) != '\0') { - const m_bool skip = prev == '\\'; - const m_bool comma = c == ','; - if(comma) { - if(!skip) - break; - --j; - } - buf[j++] = (prev = c); - ++i; - } - buf[i] = '\0'; - vector_add(as->v, (vtype)mstrdup(as->mp, buf)); - if(i == sz) - return; - as->str += i + 1; - _split_args(as); -} - -ANN Vector split_args(MemPool p, const m_str str) { - struct ArgSplitter as = { .str=str, .v=new_vector(p), .mp=p }; - _split_args(&as); - return as.v; -} - - ANN static Vector get_arg(MemPool p, const m_str name, const Vector v) { const size_t len = strlen(name); for(m_uint i = vector_size(v) + 1; --i;) { diff --git a/util b/util index 4c79bd70..10ddfce3 160000 --- a/util +++ b/util @@ -1 +1 @@ -Subproject commit 4c79bd70a6e728bd3850364abac403be74979db2 +Subproject commit 10ddfce37e99cae2871f87989f122dcecc81b9ee -- 2.43.0