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

add environ file and function

parent f655692c
No related branches found
No related tags found
No related merge requests found
......@@ -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>
......
/*
* 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
......@@ -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
......
/*
* 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 ;
}
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