@@ -507,19 +507,42 @@ int _check_zenroom_result(zenroom_t *zz) {
507507 return (exitcode );
508508}
509509
510+ static void _resolve_context_mode (const char * context , const char * * runtime_context , const char * * sideload_lua ) {
511+ if (context ) {
512+ const char * nl = strchr (context , '\n' );
513+ if (nl ) {
514+ size_t first_line_len = (size_t )(nl - context );
515+ if ((first_line_len == 3 && strncmp (context , "lua" , 3 ) == 0 ) ||
516+ (first_line_len == 4 && strncmp (context , "lua\r" , 4 ) == 0 )) {
517+ * runtime_context = NULL ;
518+ * sideload_lua = nl + 1 ;
519+ return ;
520+ }
521+ }
522+ }
523+ * runtime_context = context ;
524+ * sideload_lua = NULL ;
525+ }
526+
510527int zencode_exec (const char * script , const char * conf , const char * keys , const char * data ,
511528 const char * extra , const char * context ) {
512529
513- const char * c , * k , * d , * e , * x ;
530+ const char * c , * k , * d , * e , * x , * side ;
514531 c = conf ? (conf [0 ] == '\0' ) ? NULL : conf : NULL ;
515532 k = keys ? (keys [0 ] == '\0' ) ? NULL : keys : NULL ;
516533 d = data ? (data [0 ] == '\0' ) ? NULL : data : NULL ;
517534 e = extra ? (extra [0 ] == '\0' ) ? NULL : extra : NULL ;
518535 x = context ? (context [0 ] == '\0' ) ? NULL : context : NULL ;
536+ _resolve_context_mode (x , & x , & side );
519537
520538 zenroom_t * Z = zen_init_extra (c , k , d , e , x );
521539 if (_check_zenroom_init (Z ) != SUCCESS ) return ERR_INIT ;
522540 if (_check_script_arg (Z , script ) != SUCCESS ) return ERR_INIT ;
541+ if (side ) {
542+ if (_check_script_arg (Z , side ) != SUCCESS ) return ERR_INIT ;
543+ zen_exec_lua (Z , side );
544+ if (Z -> exitcode != SUCCESS ) return _check_zenroom_result (Z );
545+ }
523546
524547 zen_exec_zencode (Z , script );
525548 return ( _check_zenroom_result (Z ) );
@@ -548,12 +571,13 @@ int zencode_exec_tobuf(const char *script, const char *conf, const char *keys, c
548571 char * stdout_buf , size_t stdout_len ,
549572 char * stderr_buf , size_t stderr_len ) {
550573
551- const char * c , * k , * d , * e , * x ;
574+ const char * c , * k , * d , * e , * x , * side ;
552575 c = conf ? (conf [0 ] == '\0' ) ? NULL : conf : NULL ;
553576 k = keys ? (keys [0 ] == '\0' ) ? NULL : keys : NULL ;
554577 d = data ? (data [0 ] == '\0' ) ? NULL : data : NULL ;
555578 e = extra ? (extra [0 ] == '\0' ) ? NULL : extra : NULL ;
556579 x = context ? (context [0 ] == '\0' ) ? NULL : context : NULL ;
580+ _resolve_context_mode (x , & x , & side );
557581
558582 zenroom_t * Z = zen_init_extra (c , k , d , e , x );
559583 if (_check_zenroom_init (Z ) != SUCCESS ) return ERR_INIT ;
@@ -564,6 +588,11 @@ int zencode_exec_tobuf(const char *script, const char *conf, const char *keys, c
564588 Z -> stdout_len = stdout_len ;
565589 Z -> stderr_buf = stderr_buf ;
566590 Z -> stderr_len = stderr_len ;
591+ if (side ) {
592+ if (_check_script_arg (Z , side ) != SUCCESS ) return ERR_INIT ;
593+ zen_exec_lua (Z , side );
594+ if (Z -> exitcode != SUCCESS ) return _check_zenroom_result (Z );
595+ }
567596 zen_exec_zencode (Z , script );
568597 return ( _check_zenroom_result (Z ));
569598}
0 commit comments