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

fix instance name behavior, implement module, add -z for color

parent 32f57b0c
No related branches found
No related tags found
No related merge requests found
...@@ -34,7 +34,7 @@ ...@@ -34,7 +34,7 @@
#include <66/parser.h> #include <66/parser.h>
#include <66/constants.h> #include <66/constants.h>
#define USAGE "66-parser [ -h ] [ -v verbosity ] [ -f ] [ -c|C ] service destination" #define USAGE "66-parser [ -h ] [ -z ] [ -v verbosity ] [ -f ] [ -c|C ] service destination"
static inline void info_help (void) static inline void info_help (void)
{ {
...@@ -42,7 +42,8 @@ static inline void info_help (void) ...@@ -42,7 +42,8 @@ static inline void info_help (void)
"66-parser <options> service destination\n" "66-parser <options> service destination\n"
"\n" "\n"
"options :\n" "options :\n"
" -h: print this help\n" " -h: print this help\n"
" -z: use color\n"
" -v: increase/decrease verbosity\n" " -v: increase/decrease verbosity\n"
" -f: force to overwrite existing destination\n" " -f: force to overwrite existing destination\n"
" -c: merge it environment configuration file from frontend file\n" " -c: merge it environment configuration file from frontend file\n"
...@@ -82,15 +83,18 @@ int main(int argc, char const *const *argv,char const *const *envp) ...@@ -82,15 +83,18 @@ int main(int argc, char const *const *argv,char const *const *envp)
char const *sv ; char const *sv ;
char name[4095+1] ; char name[4095+1] ;
char srcdir[4095+1] ; char srcdir[4095+1] ;
int type ; unsigned int type ;
uint8_t force = 0 , conf = 0 ; uint8_t force = 0 , conf = 0 ;
log_color = &log_color_disable ;
PROG = "66-parser" ; PROG = "66-parser" ;
{ {
subgetopt_t l = SUBGETOPT_ZERO ; subgetopt_t l = SUBGETOPT_ZERO ;
for (;;) for (;;)
{ {
int opt = getopt_args(argc,argv, ">hv:fcC", &l) ; int opt = getopt_args(argc,argv, ">hv:fcCz", &l) ;
if (opt == -1) break ; if (opt == -1) break ;
if (opt == -2) log_die(LOG_EXIT_USER,"options must be set first") ; if (opt == -2) log_die(LOG_EXIT_USER,"options must be set first") ;
switch (opt) switch (opt)
...@@ -100,6 +104,7 @@ int main(int argc, char const *const *argv,char const *const *envp) ...@@ -100,6 +104,7 @@ int main(int argc, char const *const *argv,char const *const *envp)
case 'f' : force = 1 ; break ; case 'f' : force = 1 ; break ;
case 'c' : if (conf) log_usage(USAGE) ; conf = 1 ; break ; case 'c' : if (conf) log_usage(USAGE) ; conf = 1 ; break ;
case 'C' : if (conf) log_usage(USAGE) ; conf = 2 ; break ; case 'C' : if (conf) log_usage(USAGE) ; conf = 2 ; break ;
case 'z' : log_color = !isatty(1) ? &log_color_disable : &log_color_enable ; break ;
default : log_usage(USAGE) ; default : log_usage(USAGE) ;
} }
} }
...@@ -110,11 +115,15 @@ int main(int argc, char const *const *argv,char const *const *envp) ...@@ -110,11 +115,15 @@ int main(int argc, char const *const *argv,char const *const *envp)
sv = argv[0] ; sv = argv[0] ;
dir = argv[1] ; dir = argv[1] ;
if (dir[0] != '/') log_die(LOG_EXIT_USER, "directory: ",dir," must be an absolute path") ; if (dir[0] != '/') log_die(LOG_EXIT_USER, "directory: ",dir," must be an absolute path") ;
if (sv[0] != '/') log_die(LOG_EXIT_USER, "service: ",sv," must be an absolute path") ; if (sv[0] != '/') log_die(LOG_EXIT_USER, "service: ",sv," must be an absolute path") ;
if (!basename(name,sv)) log_dieu(LOG_EXIT_SYS,"set name");
if (!ob_basename(name,sv)) log_dieu(LOG_EXIT_SYS,"set name");
size_t svlen = strlen(sv) ; size_t svlen = strlen(sv) ;
size_t namelen = strlen(name) ; size_t namelen = strlen(name) ;
char tmp[svlen + 1 + namelen + 1] ; char tmp[svlen + 1 + namelen + 1] ;
r = scan_mode(sv,S_IFDIR) ; r = scan_mode(sv,S_IFDIR) ;
if (r > 0) if (r > 0)
...@@ -125,33 +134,43 @@ int main(int argc, char const *const *argv,char const *const *envp) ...@@ -125,33 +134,43 @@ int main(int argc, char const *const *argv,char const *const *envp)
tmp[svlen + 1 + namelen] = 0 ; tmp[svlen + 1 + namelen] = 0 ;
sv = tmp ; sv = tmp ;
} }
if (!dirname(srcdir,sv)) log_dieu(LOG_EXIT_SYS,"set directory name") ;
if (!ob_dirname(srcdir,sv)) log_dieu(LOG_EXIT_SYS,"set directory name") ;
check_dir(dir,force,0) ; check_dir(dir,force,0) ;
if (!read_svfile(&src,name,srcdir)) log_dieusys(LOG_EXIT_SYS,"open: ",sv) ; if (!auto_stra(&insta,name)) log_die_nomem("stralloc") ;
if (!get_svtype(&service,src.s)) log_dieu(LOG_EXIT_SYS,"enable to get the type of: ",sv) ;
if (!stralloc_cats(&insta,name) ||
!stralloc_0(&insta)) log_die_nomem("stralloc") ;
ista = instance_check(insta.s) ; ista = instance_check(insta.s) ;
if (!ista) log_die(LOG_EXIT_SYS,"invalid instance name: ",insta.s) ; if (!ista) log_die(LOG_EXIT_SYS,"invalid instance name: ",insta.s) ;
if (ista > 0) if (ista > 0)
{ {
if (!instance_create(&src,insta.s,SS_INSTANCE,ista)) if (!instance_splitname(&insta,name,ista,SS_INSTANCE_TEMPLATE)) log_dieu(LOG_EXIT_SYS,"split instance name of: ",name) ;
}
if (!read_svfile(&src,insta.s,srcdir)) log_dieusys(LOG_EXIT_SYS,"open: ",sv) ;
if (!get_svtype(&service,src.s)) log_dieu(LOG_EXIT_SYS,"get service type of: ",sv) ;
if (ista > 0)
{
if (!instance_create(&src,name,SS_INSTANCE,ista))
log_dieu(LOG_EXIT_SYS,"create instance service: ",name) ; log_dieu(LOG_EXIT_SYS,"create instance service: ",name) ;
memcpy(name,insta.s,insta.len) ; memcpy(name,insta.s,insta.len) ;
name[insta.len] = 0 ; name[insta.len] = 0 ;
} }
if (!parser(&service,&src,sv,service.cname.itype)) log_dieu(LOG_EXIT_SYS,"parse service file: ",sv) ; if (!parser(&service,&src,sv,service.cname.itype)) log_dieu(LOG_EXIT_SYS,"parse service file: ",sv) ;
if (!stralloc_cats(&dst,dir) ||
!stralloc_cats(&dst,"/") || if (!auto_stra(&dst,dir,"/",name)) log_die_nomem("stralloc") ;
!stralloc_cats(&dst,name) ||
!stralloc_0(&dst)) log_die_nomem("stralloc") ;
check_dir(dst.s,force,1) ; check_dir(dst.s,force,1) ;
type = service.cname.itype ; type = service.cname.itype ;
srcdirlen = strlen(srcdir) ; srcdirlen = strlen(srcdir) ;
service.src = keep.len ; service.src = keep.len ;
if (!stralloc_catb(&keep,srcdir,srcdirlen + 1)) log_die_nomem("stralloc") ; if (!stralloc_catb(&keep,srcdir,srcdirlen + 1)) log_die_nomem("stralloc") ;
/**quick fix /**quick fix
* WIP on parser this will change soon*/ * WIP on parser this will change soon*/
...@@ -161,7 +180,7 @@ int main(int argc, char const *const *argv,char const *const *envp) ...@@ -161,7 +180,7 @@ int main(int argc, char const *const *argv,char const *const *envp)
stralloc saname = STRALLOC_ZERO ; stralloc saname = STRALLOC_ZERO ;
if (!stralloc_cats(&saname,keep.s + service.cname.name)) log_die_nomem("stralloc") ; if (!stralloc_cats(&saname,keep.s + service.cname.name)) log_die_nomem("stralloc") ;
if (!instance_splitname(&sainsta,name,ista,0)) log_dieu(LOG_EXIT_SYS,"split instance name: ",name) ; if (!instance_splitname(&sainsta,name,ista,SS_INSTANCE_TEMPLATE)) log_dieu(LOG_EXIT_SYS,"split instance name: ",name) ;
if (sastr_find(&saname,sainsta.s) == -1) if (sastr_find(&saname,sainsta.s) == -1)
log_die(LOG_EXIT_USER,"invalid instantiated service name: ", keep.s + service.cname.name) ; log_die(LOG_EXIT_USER,"invalid instantiated service name: ", keep.s + service.cname.name) ;
...@@ -173,6 +192,7 @@ int main(int argc, char const *const *argv,char const *const *envp) ...@@ -173,6 +192,7 @@ int main(int argc, char const *const *argv,char const *const *envp)
service.cname.name = keep.len ; service.cname.name = keep.len ;
if (!stralloc_catb(&keep,name,namelen + 1)) log_die_nomem("stralloc") ; if (!stralloc_catb(&keep,name,namelen + 1)) log_die_nomem("stralloc") ;
} }
/* save and prepare environment file */ /* save and prepare environment file */
if (service.opts[2]) if (service.opts[2])
{ {
...@@ -189,33 +209,7 @@ int main(int argc, char const *const *argv,char const *const *envp) ...@@ -189,33 +209,7 @@ int main(int argc, char const *const *argv,char const *const *envp)
if (!stralloc_catb(&keep,conf.s,conf.len + 1)) log_die_nomem("stralloc") ; if (!stralloc_catb(&keep,conf.s,conf.len + 1)) log_die_nomem("stralloc") ;
stralloc_free(&conf) ; stralloc_free(&conf) ;
} }
if (ista > 0 && service.cname.name >= 0 )
{
stralloc sainsta = STRALLOC_ZERO ;
stralloc saname = STRALLOC_ZERO ;
if (!stralloc_cats(&saname,keep.s + service.cname.name)) goto err ;
if (!instance_splitname(&sainsta,name,ista,0)) goto err ;
if (sastr_find(&saname,sainsta.s) == -1)
{
log_warn("invalid instantiated service name: ", keep.s + service.cname.name) ;
goto err ;
}
stralloc_free(&sainsta) ;
stralloc_free(&saname) ;
goto swtch ;
err:
stralloc_free(&sainsta) ;
stralloc_free(&saname) ;
return 0 ;
}
else
{
service.cname.name = keep.len ;
if (!stralloc_catb(&keep,name,namelen + 1)) return 0 ;
}
swtch:
switch(type) switch(type)
{ {
case TYPE_CLASSIC: case TYPE_CLASSIC:
...@@ -230,17 +224,22 @@ int main(int argc, char const *const *argv,char const *const *envp) ...@@ -230,17 +224,22 @@ int main(int argc, char const *const *argv,char const *const *envp)
if (!write_oneshot(&service, dst.s, conf)) if (!write_oneshot(&service, dst.s, conf))
log_dieu(LOG_EXIT_SYS,"write: ",name) ; log_dieu(LOG_EXIT_SYS,"write: ",name) ;
break ; break ;
case TYPE_MODULE:
case TYPE_BUNDLE: case TYPE_BUNDLE:
if (!write_bundle(&service, dst.s)) if (!write_bundle(&service, dst.s))
log_dieu(LOG_EXIT_SYS,"write: ",name) ; log_dieu(LOG_EXIT_SYS,"write: ",name) ;
break ; break ;
default: break ; default: break ;
} }
log_info("Written successfully: ",name, " at: ",dir) ;
sv_alltype_free(&service) ; sv_alltype_free(&service) ;
stralloc_free(&keep) ; stralloc_free(&keep) ;
stralloc_free(&deps) ; stralloc_free(&deps) ;
stralloc_free(&src) ; stralloc_free(&src) ;
stralloc_free(&dst) ; stralloc_free(&dst) ;
return 0 ; return 0 ;
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment