diff --git a/src/include/66/66.h b/src/include/66/66.h index 2cde05764924c9db84ac0b58bed9a46cd38d484f..fcbee9ac0d1a2e58386a04813a1cea3b280ba608 100644 --- a/src/include/66/66.h +++ b/src/include/66/66.h @@ -20,12 +20,13 @@ #include <66/constants.h> #include <66/db.h> #include <66/enum.h> +#include <66/environ.h> #include <66/parser.h> +#include <66/rc.h> #include <66/resolve.h> #include <66/ssexec.h> #include <66/state.h> #include <66/svc.h> -#include <66/rc.h> #include <66/tree.h> #include <66/utils.h> diff --git a/src/include/66/environ.h b/src/include/66/environ.h new file mode 100644 index 0000000000000000000000000000000000000000..6103377c585a5e8a00b0afee6465f1ae7741c3c8 --- /dev/null +++ b/src/include/66/environ.h @@ -0,0 +1,26 @@ +/* + * environ.h + * + * Copyright (c) 2018-2019 Eric Vidal <eric@obarun.org> + * + * All rights reserved. + * + * This file is part of Obarun. It is subject to the license terms in + * the LICENSE file found in the top-level directory of this + * distribution. + * This file may not be copied, modified, propagated, or distributed + * except according to the terms contained in the LICENSE file./ + */ + +#ifndef ENVIRON_H +#define ENVIRON_H + +#include <skalibs/stralloc.h> +#include <skalibs/genalloc.h> + +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) ; + +#endif diff --git a/src/lib66/deps-lib/66 b/src/lib66/deps-lib/66 index 45c507394421119011c9a0ba68dae6f63bbd0372..8427aa85241968397fa07d14ab516dbcb7de0d36 100644 --- a/src/lib66/deps-lib/66 +++ b/src/lib66/deps-lib/66 @@ -8,6 +8,7 @@ db_get_permissions.o db_ok.o db_switch_to.o db_update.o +environ.o get_enum.o get_uidgid.o get_userhome.o diff --git a/src/lib66/environ.c b/src/lib66/environ.c new file mode 100644 index 0000000000000000000000000000000000000000..e01b49f53fe32e24cb76eab70e5183defbe74a81 --- /dev/null +++ b/src/lib66/environ.c @@ -0,0 +1,158 @@ +/* + * environ.c + * + * Copyright (c) 2018-2019 Eric Vidal <eric@obarun.org> + * + * All rights reserved. + * + * This file is part of Obarun. It is subject to the license terms in + * the LICENSE file found in the top-level directory of this + * distribution. + * This file may not be copied, modified, propagated, or distributed + * except according to the terms contained in the LICENSE file./ + */ + +#include <stddef.h> +#include <stdio.h> + +#include <oblibs/string.h> +#include <oblibs/stralist.h> + +#include <skalibs/stralloc.h> +#include <skalibs/genalloc.h> +#include <skalibs/diuint32.h> + +#include <66/parser.h> + +/* @Return 1 on success + * @Return 0 on fail + * @Return -1 for empty line */ +int env_clean(stralloc *src) +{ + int r, e = 1 ; + unsigned int i ; + genalloc gatmp =GENALLOC_ZERO ;//stralist + stralloc kp = STRALLOC_ZERO ; + stralloc tmp = STRALLOC_ZERO ; + + size_t pos = 0 ; + char const *file = "env_clean" ; + parse_mill_t line = { .open = '@', .close = '=', \ + .skip = " \t\r", .skiplen = 3, \ + .end = "\n", .endlen = 1, \ + .jump = "#", .jumplen = 1,\ + .check = 0, .flush = 0, \ + .forceskip = 0, .force = 1, \ + .inner = PARSE_MILL_INNER_ZERO } ; + + size_t blen = src->len, n = 0 ; + if (!stralloc_inserts(src,0,"@")) goto err ; + while(pos < (blen+n) && n < 2) + { + kp.len = 0 ; + genalloc_deepfree(stralist,&gatmp,stra_free) ; + line.inner.nopen = line.inner.nclose = 0 ; + r = parse_config(&line,file,src,&kp,&pos) ; + if (!r) goto err ; + if (r < 0 && !n){ e = -1 ; goto freed ; } + if (!stralloc_0(&kp)) goto err ; + if (!clean_val(&gatmp,kp.s)) goto err ; + for (i = 0 ; i < genalloc_len(stralist,&gatmp) ; i++) + { + if ((i+1) < genalloc_len(stralist,&gatmp)) + { + if (!stralloc_cats(&tmp,gaistr(&gatmp,i))) goto err ; + if (!stralloc_cats(&tmp," ")) goto err ; + } + else if (!stralloc_cats(&tmp,gaistr(&gatmp,i))) goto err ; + } + if (!n) if (!stralloc_cats(&tmp,"=")) goto err ; + if (!stralloc_inserts(src,pos,"@")) goto err ; + n++; + } + + if (!stralloc_0(&tmp)) goto err ; + if (!stralloc_copy(src,&tmp)) goto err ; + + freed: + stralloc_free(&kp) ; + stralloc_free(&tmp) ; + genalloc_deepfree(stralist,&gatmp,stra_free) ; + return e ; + + err: + stralloc_free(&kp) ; + stralloc_free(&tmp) ; + genalloc_deepfree(stralist,&gatmp,stra_free) ; + return 0 ; +} + +int env_split_one(char *line,genalloc *ga,stralloc *sa) +{ + char *k = 0 ; + char *v = 0 ; + diuint32 tmp = DIUINT32_ZERO ; + k = line ; + v = line ; + + obstr_sep(&v,"=") ; + if (v == NULL) return 0 ; + + tmp.left = sa->len ; + if(!stralloc_catb(sa,k,strlen(k)+1)) return 0 ; + + if (!obstr_trim(v,'\n')) return 0 ; + tmp.right = sa->len ; + if(!stralloc_catb(sa,v,strlen(v)+1)) return 0 ; + + if (!genalloc_append(diuint32,ga,&tmp)) return 0 ; + + return 1 ; +} + +int env_split(genalloc *gaenv,stralloc *saenv,stralloc *src) +{ + int nbline = 0, i = 0 ; + printf("src::%s\n",src->s) ; + genalloc gatmp = GENALLOC_ZERO ;//stralist + nbline = get_nbline_ga(src->s,src->len,&gatmp) ; + printf("nbline::%i\n",nbline) ; + for (; i < nbline ; i++) + { + char *line = gaistr(&gatmp,i) ; + printf("line::%s\n",line) ; + if (!env_split_one(line,gaenv,saenv)) goto err ; + } + genalloc_deepfree(stralist,&gatmp,stra_free) ; + return 1 ; + err: + genalloc_deepfree(stralist,&gatmp,stra_free) ; + return 0 ; +} + +int env_parsenclean(stralloc *modifs,stralloc *src) +{ + int nbline = 0, i = 0 ; + genalloc gatmp = GENALLOC_ZERO ;//stralist + + if (!parse_env(src)) goto err ; + if (!env_clean(src)) goto err ; + nbline = get_nbline_ga(src->s,src->len,&gatmp) ; +printf("src + u::%s\n",src->s ) ; + for (; i < nbline ; i++) + { + char *line = gaistr(&gatmp,i) ; + int u = 0 ; + if (line[0] == '!') u++ ; + printf("line + u::%s\n",gaistr(&gatmp,i) ) ; + if (!stralloc_catb(modifs,line + u ,strlen(line) - u) || + !stralloc_0(modifs)) goto err ; + } + + genalloc_deepfree(stralist,&gatmp,stra_free) ; + + return 1 ; + err: + genalloc_deepfree(stralist,&gatmp,stra_free) ; + return 0 ; +}