From 0cbb204d98d8c0425abc7a228f7d951b45b78544 Mon Sep 17 00:00:00 2001 From: midipix Date: Mon, 15 Oct 2018 02:11:00 -0400 Subject: driver: clone, normalize (-l,--library,-L,--library-path) the argument vector. --- src/driver/slbt_driver_ctx.c | 151 ++++++++++++++++++++++++++++++++++++------- 1 file changed, 129 insertions(+), 22 deletions(-) (limited to 'src/driver/slbt_driver_ctx.c') diff --git a/src/driver/slbt_driver_ctx.c b/src/driver/slbt_driver_ctx.c index 401bb43..cac449e 100644 --- a/src/driver/slbt_driver_ctx.c +++ b/src/driver/slbt_driver_ctx.c @@ -5,6 +5,7 @@ /*******************************************************************/ #include +#include #include #include @@ -69,11 +70,6 @@ static const char aclr_blue [] = "\x1b[34m"; static const char aclr_cyan [] = "\x1b[36m"; static const char aclr_white [] = "\x1b[37m"; -struct slbt_split_vector { - char ** targv; - char ** cargv; -}; - struct slbt_driver_ctx_alloc { struct argv_meta * meta; struct slbt_driver_ctx_impl ctx; @@ -130,12 +126,27 @@ static uint32_t slbt_argv_flags(uint32_t flags) return ret; } +static int slbt_free_argv_buffer(struct slbt_split_vector * sargv) +{ + if (sargv->dargs) + free(sargv->dargs); + + if (sargv->dargv) + free(sargv->dargv); + + if (sargv->targv) + free(sargv->targv); + + return -1; +} + static int slbt_driver_usage( int fdout, const char * program, const char * arg, const struct argv_option ** optv, - struct argv_meta * meta) + struct argv_meta * meta, + struct slbt_split_vector * sargv) { char header[512]; @@ -145,6 +156,7 @@ static int slbt_driver_usage( argv_usage(fdout,header,optv,arg); argv_free(meta); + slbt_free_argv_buffer(sargv); return SLBT_USAGE; } @@ -152,7 +164,8 @@ static int slbt_driver_usage( static struct slbt_driver_ctx_impl * slbt_driver_ctx_alloc( struct argv_meta * meta, const struct slbt_fd_ctx * fdctx, - const struct slbt_common_ctx * cctx) + const struct slbt_common_ctx * cctx, + struct slbt_split_vector * sargv) { struct slbt_driver_ctx_alloc * ictx; size_t size; @@ -160,8 +173,15 @@ static struct slbt_driver_ctx_impl * slbt_driver_ctx_alloc( size = sizeof(struct slbt_driver_ctx_alloc); - if (!(ictx = calloc(1,size))) + if (!(ictx = calloc(1,size))) { + slbt_free_argv_buffer(sargv); return 0; + } + + ictx->ctx.dargs = sargv->dargs; + ictx->ctx.dargv = sargv->dargv; + ictx->ctx.targv = sargv->targv; + ictx->ctx.cargv = sargv->cargv; memcpy(&ictx->ctx.fdctx,fdctx,sizeof(*fdctx)); memcpy(&ictx->ctx.cctx,cctx,sizeof(*cctx)); @@ -200,8 +220,13 @@ static int slbt_split_argv( int argc; const char * program; char * compiler; + char ** dargv; char ** targv; char ** cargv; + char * dst; + bool flast; + bool fcopy; + size_t size; struct argv_meta * meta; struct argv_entry * entry; struct argv_entry * mode; @@ -223,7 +248,7 @@ static int slbt_split_argv( if (!argv[1] && (flags & SLBT_DRIVER_VERBOSITY_USAGE)) return slbt_driver_usage( fderr,program, - 0,optv,0); + 0,optv,0,sargv); /* initial argv scan: ... --mode=xxx ... ... */ argv_scan(argv,optv,&ctx,0); @@ -280,10 +305,87 @@ static int slbt_split_argv( return -1; } - /* allocate split vectors */ - for (argc=0, targv=argv; *targv; targv++) - argc++; + /* clone and normalize the argv vector (-l, --library) */ + for (argc=0,size=0,dargv=argv; *dargv; argc++,dargv++) + size += strlen(*dargv) + 1; + + if (!(sargv->dargv = calloc(argc+1,sizeof(char *)))) + return -1; + else if (!(sargv->dargs = calloc(1,size+1))) + return -1; + + for (i=0,flast=false,dargv=sargv->dargv,dst=sargv->dargs; i