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

use parser from oblibs

parent 20c30065
No related branches found
No related tags found
No related merge requests found
......@@ -19,21 +19,21 @@
#include <errno.h>
#include <sys/stat.h>
#include <sys/mount.h>
#include <sys/reboot.h>
#include <oblibs/error2.h>
#include <oblibs/files.h>
#include <oblibs/string.h>
#include <oblibs/obgetopt.h>
#include <oblibs/environ.h>
#include <oblibs/sastr.h>
#include <skalibs/buffer.h>
#include <skalibs/djbunix.h>
#include <skalibs/diuint32.h>
#include <skalibs/genalloc.h>
#include <skalibs/stralloc.h>
#include <skalibs/types.h>
#include <66/config.h>
#include <66/environ.h>
#include <66/constants.h>
unsigned int VERBOSITY = 1 ;
......@@ -68,7 +68,7 @@ static void sulogin(char const *msg,char const *arg)
fd_close(0) ;
if (dup2(fdin,0) == -1) strerr_diefu1x(111,"duplicate stdin -- you are on your own") ;
fd_close(fdin) ;
if (*msg) strerr_warnwu2sys(msg,arg) ;
if (*msg) strerr_warnwu2x(msg,arg) ;
pid = child_spawn0(newarg[0],newarg,genv) ;
if (waitpid_nointr(pid,&wstat, 0) < 0)
strerr_diefu1sys(111,"wait for sulogin -- you are on your own") ;
......@@ -101,10 +101,9 @@ static void parse_conf(void)
static char const *valid[] =
{ "VERBOSITY", "PATH", "LIVE", "TREE", "RCINIT", "UMASK", "RESCAN", 0 } ;
int r ;
unsigned int i = 0, j = 0 ;
unsigned int j = 0 ;
stralloc src = STRALLOC_ZERO ;
stralloc saconf = STRALLOC_ZERO ;
genalloc gaconf = GENALLOC_ZERO ;
stralloc val = STRALLOC_ZERO ;
if (skel[0] != '/') sulogin("skeleton directory must be an aboslute path: ",skel) ;
size_t skelen = strlen(skel) ;
memcpy(confile,skel,skelen) ;
......@@ -114,51 +113,44 @@ static void parse_conf(void)
size_t filesize=file_get_size(confile) ;
r = openreadfileclose(confile,&src,filesize) ;
if(!r) sulogin("open configuration file: ",confile) ;
if (!stralloc_0(&src)) sulogin("append stralloc configuration file","") ;
if (!stralloc_0(&src)) sulogin("append stralloc of file: ",confile) ;
r = env_split(&gaconf,&saconf,&src) ;
if (!r) sulogin("parse configuration file: ",confile) ;
for (;i < genalloc_len(diuint32,&gaconf) ; i++)
for (char const *const *p = valid;*p;p++,j++)
{
char *key = saconf.s + genalloc_s(diuint32,&gaconf)[i].left ;
char *val = saconf.s + genalloc_s(diuint32,&gaconf)[i].right ;
j = 0 ;
for (char const *const *p = valid;*p;p++,j++)
if (!stralloc_copy(&val,&src)) sulogin("copy stralloc of file: ",confile) ;
if (!environ_get_val_of_key(&val,*p)) continue ;
sastr_clean_element(&val) ;
if (!val.len) strerr_warnwu3x("get value of: ",*p," -- keeps the default") ;
switch (j)
{
if (!strcmp(*p,key))
{
switch (j)
{
case 0: if (!uint0_scan(val, &VERBOSITY)) sulogin("invalid VERBOSITY value: ","") ;
break ;
case 1: memcpy(tpath,val,strlen(val)) ;
tpath[strlen(val)] = 0 ;
path = tpath ;
break ;
case 2: memcpy(tlive,val,strlen(val)) ;
tlive[strlen(val)] = 0 ;
live = tlive ;
if (live[0] != '/') sulogin ("LIVE must be an absolute path","") ;
break ;
case 3: memcpy(ttree,val,strlen(val)) ;
ttree[strlen(val)] = 0 ;
tree = ttree ;
break ;
case 4: memcpy(trcinit,val,strlen(val)) ;
trcinit[strlen(val)] = 0 ;
rcinit = trcinit ;
if (rcinit[0] != '/') sulogin ("RCINIT must be an absolute path","") ;
break ;
case 5: if (!uint0_oscan(val, &mask)) sulogin("invalid MASK value","") ; break ;
case 6: if (!uint0_scan(val, &rescan)) sulogin("invalid RESCAN value","") ; break ;
default: break ;
}
}
case 0: if (!uint0_scan(val.s, &VERBOSITY)) sulogin("parse VERBOSITY value: ",val.s) ;
break ;
case 1: memcpy(tpath,val.s,val.len) ;
tpath[val.len] = 0 ;
path = tpath ;
break ;
case 2: memcpy(tlive,val.s,val.len) ;
tlive[val.len] = 0 ;
live = tlive ;
if (live[0] != '/') sulogin ("LIVE must be an absolute path: ",val.s) ;
break ;
case 3: memcpy(ttree,val.s,val.len) ;
ttree[val.len] = 0 ;
tree = ttree ;
break ;
case 4: memcpy(trcinit,val.s,val.len) ;
trcinit[val.len] = 0 ;
rcinit = trcinit ;
if (rcinit[0] != '/') sulogin ("RCINIT must be an absolute path: ",val.s) ;
break ;
case 5: if (!uint0_oscan(val.s, &mask)) sulogin("invalid MASK value: ",val.s) ; break ;
case 6: if (!uint0_scan(val.s, &rescan)) sulogin("invalid RESCAN value: ",val.s) ; break ;
default: break ;
}
}
genalloc_free(diuint32,&gaconf) ;
stralloc_free(&saconf) ;
stralloc_free(&val) ;
stralloc_free(&src) ;
}
......@@ -330,8 +322,10 @@ int main(int argc, char const *const *argv,char const *const *envp)
make_cmdline(SS_EXTBINPREFIX "66-init",t,3,"initiate earlier service of tree: ",tree,envp) ;
}
if (envdir)
if (!env_get_from_src(&envmodifs,envdir)) sulogin("get environment from: ",envdir) ;
if (envdir) {
int e = environ_get_envfile(&envmodifs,envdir) ;
if (e <= 0){ environ_get_envfile_error(e,envdir) ; sulogin("","") ; }
}
{
strerr_warni3x("Starts boot logger at: ",live,"/log/0") ;
......@@ -360,6 +354,7 @@ int main(int argc, char const *const *argv,char const *const *envp)
pid = fork() ;
if (pid == -1) sulogin("fork: ",rcinit) ;
if (!pid) run_stage2(newenvp, 2, envmodifs.s,envmodifs.len) ;
if (reboot(RB_DISABLE_CAD) == -1) sulogin("trap ctrl-alt-del","") ;
if (fd_copy(2, 1) == -1) sulogin("copy stderr to stdout","") ;
fd_close(fdin) ;
xpathexec_r(newargv, newenvp, 2, envmodifs.s, envmodifs.len) ;
......
......@@ -25,6 +25,7 @@
#include <oblibs/types.h>
#include <oblibs/files.h>
#include <oblibs/string.h>
#include <oblibs/environ.h>
#include <skalibs/buffer.h>
#include <skalibs/stralloc.h>
......@@ -549,7 +550,7 @@ int main(int argc, char const *const *argv, char const *const *envp)
if (envdir.s[0] != '/')
strerr_dief3x(110,"environment: ",envdir.s," must be an absolute path") ;
if (!build_env(envdir.s,envp,newenv,TMPENV)) strerr_diefu2x(111,"build environment with: ",envdir.s) ;
if (!environ_get_envfile_n_merge(envdir.s,envp,newenv,TMPENV)) strerr_diefu2x(111,"build environment with: ",envdir.s) ;
genv = newenv ;
}
else genv = envp ;
......
......@@ -26,6 +26,8 @@
#include <stdio.h>
#include <sys/wait.h>
#include <oblibs/environ.h>
#include <skalibs/posixplz.h>
#include <skalibs/uint32.h>
#include <skalibs/types.h>
......@@ -40,7 +42,6 @@
#include <skalibs/djbunix.h>
#include <skalibs/iopause.h>
#include <skalibs/skamisc.h>
#include <skalibs/diuint32.h>
#include <execline/config.h>
......@@ -48,7 +49,6 @@
#include <66/config.h>
#include <66/constants.h>
#include <66/environ.h>
#define STAGE4_FILE "stage4"
#define DOTPREFIX ".66-shutdownd:"
......@@ -107,29 +107,16 @@ ssize_t file_get_size(const char* filename)
static void parse_conf(char const *confile,char *rcshut,size_t filesize)
{
int r ;
unsigned int i = 0 ;
stralloc src = STRALLOC_ZERO ;
stralloc saconf = STRALLOC_ZERO ;
genalloc gaconf = GENALLOC_ZERO ;
r = openreadfileclose(confile,&src,filesize) ;
if(!r) strerr_diefu2sys(111,"open configuration file: ",confile) ;
if (!stralloc_0(&src)) strerr_diefu1sys(111,"append stralloc configuration file") ;
r = env_split(&gaconf,&saconf,&src) ;
if (!r) strerr_diefu2sys(111,"parse configuration file: ",confile) ;
for (;i < genalloc_len(diuint32,&gaconf) ; i++)
if (environ_get_val_of_key(&src,"RCSHUTDOWN"))
{
char *key = saconf.s + genalloc_s(diuint32,&gaconf)[i].left ;
char *val = saconf.s + genalloc_s(diuint32,&gaconf)[i].right ;
if (!strcmp(key,"RCSHUTDOWN"))
{
memcpy(rcshut,val,strlen(val)) ;
rcshut[strlen(val)] = 0 ;
}
memcpy(rcshut,src.s,src.len) ;
rcshut[src.len] = 0 ;
}
genalloc_free(diuint32,&gaconf) ;
stralloc_free(&saconf) ;
stralloc_free(&src) ;
}
......
${LIB66}
-lexecline
${LIBEXECLINE}
-loblibs
-lexecline
-lskarnet
${LIBEXECLINE}
${LIB66}
-lexecline
-loblibs
-lexecline
-lskarnet
${LIBEXECLINE}
......@@ -21,28 +21,20 @@
#include <oblibs/stralist.h>
#include <oblibs/error2.h>
#include <oblibs/types.h>
#include <oblibs/obgetopt.h>
#include <oblibs/directory.h>
#include <oblibs/files.h>
#include <oblibs/environ.h>
#include <oblibs/sastr.h>
#include <skalibs/bytestr.h>
#include <skalibs/stralloc.h>
#include <skalibs/genalloc.h>
#include <skalibs/env.h>
#include <skalibs/djbunix.h>
#include <skalibs/diuint32.h>
#include <skalibs/env.h>
#include <skalibs/sgetopt.h>
#include <execline/execline.h>
#include <66/parser.h>
#include <66/environ.h>
#include <66/constants.h>
static stralloc SAENV = STRALLOC_ZERO ;
static genalloc GAENV = GENALLOC_ZERO ; //diuint32, pos in senv
#define USAGE "execl-envfile [ -h help ] [ -l ] src prog"
static inline void info_help (void)
......@@ -60,43 +52,36 @@ static inline void info_help (void)
strerr_diefu1sys(111, "write to stdout") ;
}
int loop_stra(stralloc *sa,char const *search)
void clean_n_unexport(stralloc *modifs, stralloc *dst, stralloc *src)
{
size_t i = 0, len = sa->len ;
for (;i < len; i += strlen(sa->s + i) + 1)
if (!strcmp(sa->s+i,search)) return 1 ;
return 0 ;
if (!environ_clean_envfile(modifs,src)) strerr_diefu2sys(111,"prepare modified environment of: ",src->s) ;
if (!sastr_split_string_in_nline(modifs)) strerr_diefu2sys(111,"build environment line of: ",src->s) ; ;
if (!stralloc_cats(dst,src->s)) exitstralloc("clean_n_unexport") ;
}
int main (int argc, char const *const *argv, char const *const *envp)
{
int r, i, unexport ;
int insist = 1 ;
size_t pathlen ;
char const *path = 0 ;
char const *file = 0 ;
char tpath[MAXENV + 1] ;
char tfile[MAXENV+1] ;
int r = 0, unexport = 0, insist = 1, nfile = 0, nvar = 0 ;
size_t pos = 0, pathlen = 0 ;
char const *path = 0, *file = 0 ;
char tpath[MAXENV + 1], tfile[MAXENV+1] ;
stralloc src = STRALLOC_ZERO ;
stralloc modifs = STRALLOC_ZERO ;
stralloc dst = STRALLOC_ZERO ;
genalloc toparse = GENALLOC_ZERO ; //stralist
stralloc toparse = STRALLOC_ZERO ;
stralloc key = STRALLOC_ZERO ;
stralloc val = STRALLOC_ZERO ;
exlsn_t info = EXLSN_ZERO ;
r = i = unexport = 0 ;
insist = 1 ;
PROG = "execl-envfile" ;
{
subgetopt_t l = SUBGETOPT_ZERO ;
for (;;)
{
int opt = subgetopt_r(argc,argv, ">hlf:", &l) ;
int opt = subgetopt_r(argc,argv, "hlf:", &l) ;
if (opt == -1) break ;
if (opt == -2) strerr_dief1x(110,"options must be set first") ;
switch (opt)
{
case 'h' : info_help(); return 0 ;
......@@ -107,7 +92,6 @@ int main (int argc, char const *const *argv, char const *const *envp)
}
argc -= l.ind ; argv += l.ind ;
}
if (argc < 2) exitusage(USAGE) ;
path = *argv ;
......@@ -123,11 +107,11 @@ int main (int argc, char const *const *argv, char const *const *envp)
else
{
r = scan_mode(path,S_IFREG) ;
if (!r) strerr_diefu2sys(111,"find: ",path) ;
if (!r && insist) strerr_diefu2sys(111,"find: ",path) ;
if (r < 0)
{
r = scan_mode(path,S_IFDIR) ;
if (!r) strerr_diefu2sys(111,"find: ",path) ;
if (!r && insist) strerr_diefu2sys(111,"find: ",path) ;
if (r < 0) strerr_dief2x(111,"invalid format of: ", path) ;
if (path[0] == '.')
{
......@@ -152,16 +136,15 @@ int main (int argc, char const *const *argv, char const *const *envp)
}
}
r = dir_get(&toparse,path,"",S_IFREG) ;
r = sastr_dir_get(&toparse,path,"",S_IFREG) ;
if (!r && insist) strerr_diefu2sys(111,"get file from: ",path) ;
else if ((!r && !insist) || !genalloc_len(stralist,&toparse))
else if ((!r && !insist) || !toparse.len)
{
xpathexec_run(argv[0],argv,envp) ;
}
if (file)
{
r = stra_findidx(&toparse,file) ;
ssize_t r = sastr_cmp(&toparse,file) ;
if (r < 0)
{
if (insist) strerr_diefu2x(111,"find: ",file) ;
......@@ -170,27 +153,23 @@ int main (int argc, char const *const *argv, char const *const *envp)
xpathexec_run(argv[0],argv,envp) ;
}
}
if (!file_readputsa(&src,path,file)) strerr_diefu4sys(111,"read file: ",path,"/",file) ;
if (!env_parsenclean(&modifs,&src)) strerr_diefu4x(111,"parse and clean environment of: ",path,"/",file) ;
if (!env_split(&GAENV,&SAENV,&src)) strerr_diefu4x(111,"split environment of: ",path,"/",file) ;
if (genalloc_len(diuint32,&GAENV) > MAXVAR) strerr_dief4x(111,"to many variables in file: ",path,"/",file) ;
clean_n_unexport(&modifs,&dst,&src) ;
}
else
{
for (i = 0 ; i < genalloc_len(stralist,&toparse) ; i++)
for (;pos < toparse.len; pos += strlen(toparse.s + pos) + 1)
{
nfile++;
src.len = 0 ;
size_t n = genalloc_len(diuint32,&GAENV) + MAXVAR ;
if (i > MAXFILE) strerr_dief2x(111,"to many file to parse in: ",path) ;
if (!file_readputsa(&src,path,gaistr(&toparse,i))) strerr_diefu4sys(111,"read file: ",path,"/",gaistr(&toparse,i)) ;
if (!env_parsenclean(&modifs,&src)) strerr_diefu4x(111,"parse and clean environment of: ",path,"/",gaistr(&toparse,i)) ;
if (!env_split(&GAENV,&SAENV,&src)) strerr_diefu4x(111,"split environment of: ",path,"/",gaistr(&toparse,i)) ;
if (genalloc_len(diuint32,&GAENV) > n) strerr_dief4x(111,"to many variables in file: ",path,"/",gaistr(&toparse,i)) ;
if (nfile > MAXFILE) strerr_dief2x(111,"to many file to parse in: ",path) ;
if (!file_readputsa(&src,path,toparse.s+pos)) strerr_diefu4sys(111,"read file: ",path,"/",toparse.s+pos) ;
clean_n_unexport(&modifs,&dst,&src) ;
nvar = environ_get_num_of_line(&src) ;
if (nvar == -1) strerr_diefu4sys(111,"get number of line of:",path,"/",toparse.s+pos) ;
if (nvar > MAXVAR) strerr_dief4x(111,"to many variables in file: ",path,"/",toparse.s+pos) ;
}
}
genalloc_deepfree(stralist,&toparse,stra_free) ;
stralloc_free(&src) ;
/** be able to freed the stralloc before existing */
......@@ -203,35 +182,45 @@ int main (int argc, char const *const *argv, char const *const *envp)
char const *newenv[n + 1] ;
if (!env_merge (newenv, n ,envp,env_len(envp),tmp, modifs.len)) strerr_diefu1sys(111,"build environment") ;
for (i = 0 ; i < genalloc_len(diuint32,&GAENV) ; i++)
{
unexport = 0 ;
int key = genalloc_s(diuint32,&GAENV)[i].left ;
int val = genalloc_s(diuint32,&GAENV)[i].right ;
if ((SAENV.s+val)[0] == SS_VAR_UNEXPORT)
{
val++ ;
unexport = 1 ;
}
if(!loop_stra(&info.vars,SAENV.s + key))
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) ;
if (!sastr_split_string_in_nline(&dst)) strerr_diefu1x(111,"split line") ;
pos = 0 ;
while (pos < dst.len)
{
unexport = 0 ;
key.len = val.len = 0 ;
if (!stralloc_copy(&key,&dst) ||
!stralloc_copy(&val,&dst)) retstralloc(111,"main") ;
if (!environ_get_key_nclean(&key,&pos)) strerr_diefu2x(111,"get key from line: ",key.s) ;
pos-- ;// retrieve the '=' character
if (!environ_get_val(&val,&pos)) strerr_diefu2x(111,"get value from line: ",val.s) ;
char *uval = val.s ;
if (val.s[0] == VAR_UNEXPORT)
{
uval = val.s+1 ;
unexport = 1 ;
}
if(sastr_cmp(&info.vars,key.s) == -1)
if (!environ_substitute(key.s,uval,&info,newenv,unexport))
strerr_diefu4x(111,"substitute value of: ",key.s," by: ",uval) ;
}
genalloc_free(diuint32,&GAENV) ;
stralloc_free(&SAENV) ;
stralloc_free(&key) ;
stralloc_free(&val) ;
stralloc_free(&dst) ;
stralloc_free(&modifs) ;
modifs.len = 0 ;
if (!env_string (&modifs, argv, (unsigned int) argc)) strerr_diefu1x(111,"make environment string") ;
r = el_substitute (&dst, modifs.s, modifs.len, info.vars.s, info.values.s,
genalloc_s (elsubst_t const, &info.data),genalloc_len (elsubst_t const, &info.data)) ;
if (r < 0) strerr_diefu1sys(111,"el_substitute") ;
else if (!r) _exit(0) ;
stralloc_free(&modifs) ;
char const *v[r + 1] ;
if (!env_make (v, r ,dst.s, dst.len)) strerr_diefu1sys(111,"make environment") ;
v[r] = 0 ;
pathexec_r (v, newenv, env_len(newenv),info.modifs.s,info.modifs.len) ;
}
......@@ -15,7 +15,9 @@
#include <unistd.h>
#include <sys/types.h>
#include <stdio.h>
#include <oblibs/error2.h>
#include <oblibs/environ.h>
#include <skalibs/types.h>
#include <skalibs/buffer.h>
......@@ -83,8 +85,8 @@ int main (int argc, char const **argv, char const *const *envp)
cuid[uid_fmt(cuid,uid)] = 0 ;
cgid[gid_fmt(cgid,gid)] = 0 ;
if (!env_addkv("UID",cuid,&info)) strerr_diefu1sys(111,"set UID") ;
if (!env_addkv("GID",cgid,&info)) strerr_diefu1sys(111,"set GID") ;
if (!environ_add_key_val("UID",cuid,&info)) strerr_diefu1sys(111,"set UID") ;
if (!environ_add_key_val("GID",cgid,&info)) strerr_diefu1sys(111,"set GID") ;
if (!env_string(&sa,argv,(unsigned int) argc)) strerr_diefu1sys(111,"environment string") ;
......
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