Skip to content
Snippets Groups Projects
Commit 7767d952 authored by Eric Vidal's avatar Eric Vidal :speech_balloon:
Browse files

split exlsn_s struct,env_substitute function from execl-envfile to...

split exlsn_s struct,env_substitute function from execl-envfile to environ.h,add make_env_from_line, env_addkv function
parent a1e34fda
No related branches found
No related tags found
No related merge requests found
......@@ -40,23 +40,9 @@
unsigned int VERBOSITY = 1 ;
static stralloc SAENV = STRALLOC_ZERO ;
static genalloc GAENV = GENALLOC_ZERO ; //diuint32, pos in senv
#define MAXVAR 50
#define MAXFILE 500
#define MAXENV 4096
#define USAGE "execl-envfile [ -h help ] [ -f file ] [ -l ] dir prog"
typedef struct exlsn_s exlsn_t, *exlsn_t_ref ;
struct exlsn_s
{
stralloc vars ;
stralloc values ;
genalloc data ; // array of elsubst
stralloc modifs ;
} ;
#define EXLSN_ZERO { .vars = STRALLOC_ZERO, .values = STRALLOC_ZERO, .data = GENALLOC_ZERO, .modifs = STRALLOC_ZERO }
static inline void info_help (void)
{
static char const *help =
......@@ -72,51 +58,6 @@ static inline void info_help (void)
strerr_diefu1sys(111, "write to stdout") ;
}
static int env_substitute(char const *key, char const *val,exlsn_t *info, char const *const *envp,int unexport)
{
char const *defaultval = "" ;
char const *x ;
int insist = 0 ;
eltransforminfo_t si = ELTRANSFORMINFO_ZERO ;
elsubst_t blah ;
blah.var = info->vars.len ;
blah.value = info->values.len ;
if (el_vardupl(key, info->vars.s, info->vars.len)) strerr_dief1x(111, "bad substitution key") ;
if (!stralloc_catb(&info->vars,key, strlen(key) + 1)) retstralloc(111,"env_substitute") ;
x = env_get2(envp, key) ;
if (!x)
{
if (insist) strerr_dief2x(111, val,": is not set") ;
x = defaultval ;
}
else if (unexport)
{
if (!stralloc_catb(&info->modifs, key, strlen(key) + 1)) goto err ;
}
if (!x) blah.n = 0 ;
else
{
int r ;
if (!stralloc_cats(&info->values, x)) goto err ;
r = el_transform(&info->values, blah.value, &si) ;
if (r < 0) goto err ;
blah.n = r ;
}
if (!genalloc_append(elsubst_t, &info->data, &blah)) goto err ;
return 1 ;
err:
info->vars.len = blah.var ;
info->values.len = blah.value ;
return 0 ;
}
int main (int argc, char const *const *argv, char const *const *envp)
{
int r, i, one, unexport ;
......@@ -219,7 +160,7 @@ int main (int argc, char const *const *argv, char const *const *envp)
key++ ;
unexport = 1 ;
}
env_substitute(SAENV.s + key,SAENV.s + val,&info,newenv,unexport) ;
if (!env_substitute(SAENV.s + key,SAENV.s + val,&info,newenv,unexport)) strerr_diefu4x(111,"substitute value of: ",SAENV.s + key," by: ",SAENV.s + val) ;
}
genalloc_free(diuint32,&GAENV) ;
stralloc_free(&SAENV) ;
......
......@@ -18,9 +18,27 @@
#include <skalibs/stralloc.h>
#include <skalibs/genalloc.h>
#define MAXVAR 50
#define MAXFILE 500
#define MAXENV 4096
typedef struct exlsn_s exlsn_t, *exlsn_t_ref ;
struct exlsn_s
{
stralloc vars ;
stralloc values ;
genalloc data ; // array of elsubst
stralloc modifs ;
} ;
#define EXLSN_ZERO { .vars = STRALLOC_ZERO, .values = STRALLOC_ZERO, .data = GENALLOC_ZERO, .modifs = STRALLOC_ZERO }
extern int env_clean(stralloc *src) ;
extern int env_split_one(char *line,genalloc *ga,stralloc *sa) ;
extern int env_split(genalloc *gaenv,stralloc *saenv,stralloc *src) ;
extern int env_parsenclean(stralloc *modifs,stralloc *src) ;
extern int make_env_from_line(char const **v,stralloc *sa) ;
extern int env_substitute(char const *key, char const *val,exlsn_t *info, char const *const *envp,int unexport) ;
extern int env_addkv (const char *key, const char *val, exlsn_t *info) ;
#endif
......@@ -21,9 +21,13 @@
#include <skalibs/stralloc.h>
#include <skalibs/genalloc.h>
#include <skalibs/diuint32.h>
#include <skalibs/env.h>
#include <skalibs/strerr2.h>
#include <66/parser.h>
#include <66/environ.h>
#include <execline/execline.h>
/* @Return 1 on success
* @Return 0 on fail
* @Return -1 for empty line */
......@@ -172,3 +176,97 @@ int env_parsenclean(stralloc *modifs,stralloc *src)
stralloc_free(&tmp) ;
return 0 ;
}
int make_env_from_line(char const **v,stralloc *sa)
{
genalloc gatmp = GENALLOC_ZERO ;
stralloc copy = STRALLOC_ZERO ;
unsigned int i = 0 ;
if (!sa->len) goto err ;
if (!clean_val(&gatmp,sa->s)) goto err ;
for (;i < genalloc_len(stralist,&gatmp) ; i++)
{
char *line = gaistr(&gatmp,i) ;
if (!stralloc_catb(&copy,line,gaistrlen(&gatmp,i) + 1)) goto err ;
}
stralloc_copy(sa,&copy) ;
stralloc_free(&copy) ;
if (!env_make(v,i,sa->s,sa->len)) goto err ;
genalloc_deepfree(stralist,&gatmp,stra_free) ;
return i ;
err:
stralloc_free(&copy) ;
genalloc_deepfree(stralist,&gatmp,stra_free) ;
return 0 ;
}
int env_substitute(char const *key, char const *val,exlsn_t *info, char const *const *envp,int unexport)
{
char const *defaultval = "" ;
char const *x ;
int insist = 0 ;
eltransforminfo_t si = ELTRANSFORMINFO_ZERO ;
elsubst_t blah ;
blah.var = info->vars.len ;
blah.value = info->values.len ;
if (el_vardupl(key, info->vars.s, info->vars.len)) { strerr_warnw1x("bad substitution key") ; goto err ; }
if (!stralloc_catb(&info->vars,key, strlen(key) + 1)) { strerr_warnw1x("env_substitute") ; goto err ; }
x = env_get2(envp, key) ;
if (!x)
{
if (insist) { strerr_warnw2x(val,": is not set") ; goto err ; }
x = defaultval ;
}
else if (unexport)
{
if (!stralloc_catb(&info->modifs, key, strlen(key) + 1)) goto err ;
}
if (!x) blah.n = 0 ;
else
{
int r ;
if (!stralloc_cats(&info->values, x)) goto err ;
r = el_transform(&info->values, blah.value, &si) ;
if (r < 0) goto err ;
blah.n = r ;
}
if (!genalloc_append(elsubst_t, &info->data, &blah)) goto err ;
return 1 ;
err:
info->vars.len = blah.var ;
info->values.len = blah.value ;
return 0 ;
}
int env_addkv (const char *key, const char *val, exlsn_t *info)
{
int r ;
eltransforminfo_t si = ELTRANSFORMINFO_ZERO ;
elsubst_t blah ;
blah.var = info->vars.len ;
blah.value = info->values.len ;
if (el_vardupl (key, info->vars.s, info->vars.len)) goto err ;
if (!stralloc_catb (&info->vars, key, strlen(key) + 1)) goto err ;
if (!stralloc_cats (&info->values, val)) goto err ;
r = el_transform (&info->values, blah.value, &si) ;
if (r < 0) goto err;
blah.n = r ;
if (!genalloc_append (elsubst_t, &info->data, &blah)) goto err ;
return 1 ;
err:
info->vars.len = blah.var ;
info->values.len = blah.value ;
return 0 ;
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment