2005-05-29 04:13:10 +04:00
/*
Unix SMB / CIFS implementation .
2005-06-04 07:51:38 +04:00
Standalone client for ejs scripting .
2005-05-29 04:13:10 +04:00
Copyright ( C ) Tim Potter < tpot @ samba . org > 2005
This program is free software ; you can redistribute it and / or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation ; either version 2 of the License , or
( at your option ) any later version .
This program is distributed in the hope that it will be useful ,
but WITHOUT ANY WARRANTY ; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
GNU General Public License for more details .
You should have received a copy of the GNU General Public License
along with this program ; if not , write to the Free Software
Foundation , Inc . , 675 Mass Ave , Cambridge , MA 0213 9 , USA .
*/
# include "includes.h"
2005-05-29 15:35:56 +04:00
# include "dynconfig.h"
2005-05-29 07:53:36 +04:00
# include "lib/ejs/ejs.h"
2005-05-29 04:13:10 +04:00
2005-05-29 08:10:22 +04:00
void ejs_exception ( const char * reason )
2005-05-29 04:13:10 +04:00
{
fprintf ( stderr , " smbscript exception: %s " , reason ) ;
2005-06-04 07:51:38 +04:00
exit ( 127 ) ;
2005-05-29 04:13:10 +04:00
}
int main ( int argc , const char * argv [ ] )
{
EjsId eid ;
2005-05-29 16:41:59 +04:00
EjsHandle handle = 0 ;
2005-05-29 04:13:10 +04:00
MprVar result ;
2005-06-03 12:00:42 +04:00
char * emsg , * script ;
size_t script_size ;
2005-05-29 15:35:56 +04:00
TALLOC_CTX * mem_ctx = talloc_new ( NULL ) ;
2005-05-29 15:58:14 +04:00
const char * * argv_list = NULL ;
2005-06-04 07:51:38 +04:00
struct MprVar v , * return_var ;
int exit_status , i ;
2005-05-29 04:13:10 +04:00
2005-05-29 15:58:14 +04:00
if ( argc < 2 ) {
2005-05-29 12:12:16 +04:00
fprintf ( stderr , " Usage: %s <scriptfile> \n " , argv [ 0 ] ) ;
2005-06-04 07:51:38 +04:00
exit ( 127 ) ;
2005-05-29 12:12:16 +04:00
}
2005-05-29 15:35:56 +04:00
setup_logging ( argv [ 0 ] , DEBUG_STDOUT ) ;
2005-05-29 15:43:52 +04:00
if ( ! lp_load ( dyn_CONFIGFILE , False , False , False ) ) {
2005-05-29 15:35:56 +04:00
fprintf ( stderr , " %s: Can't load %s - run testparm to debug it \n " ,
argv [ 0 ] , dyn_CONFIGFILE ) ;
2005-06-04 07:51:38 +04:00
exit ( 127 ) ;
2005-05-29 15:35:56 +04:00
}
2005-05-30 02:11:32 +04:00
load_interfaces ( ) ;
2005-06-12 10:37:25 +04:00
smbscript_init_subsystems ;
2005-05-29 15:35:56 +04:00
mprSetCtx ( mem_ctx ) ;
2005-05-29 07:29:10 +04:00
if ( ejsOpen ( NULL , NULL , NULL ) ! = 0 ) {
2005-05-29 07:25:21 +04:00
fprintf ( stderr , " smbscript: ejsOpen(): unable to initialise "
" EJ subsystem \n " ) ;
2005-06-04 07:51:38 +04:00
exit ( 127 ) ;
2005-05-29 07:25:21 +04:00
}
2005-05-29 04:13:10 +04:00
2005-05-29 15:35:56 +04:00
smb_setup_ejs_functions ( ) ;
2005-05-29 07:25:21 +04:00
2005-05-29 07:29:10 +04:00
if ( ( eid = ejsOpenEngine ( handle , 0 ) ) = = ( EjsId ) - 1 ) {
2005-05-29 07:25:21 +04:00
fprintf ( stderr , " smbscript: ejsOpenEngine(): unable to "
" initialise an EJS engine \n " ) ;
2005-06-04 07:51:38 +04:00
exit ( 127 ) ;
2005-05-29 07:25:21 +04:00
}
2005-05-29 15:58:14 +04:00
/* setup ARGV[] in the ejs environment */
for ( i = 2 ; i < argc ; i + + ) {
argv_list = str_list_add ( argv_list , argv [ i ] ) ;
}
v = mprList ( " ARGV " , argv_list ) ;
2005-06-04 07:35:38 +04:00
mprSetPropertyValue ( & v , " length " , mprCreateIntegerVar ( argc - 2 ) ) ;
2005-05-29 15:58:14 +04:00
mprCreateProperty ( ejsGetGlobalObject ( eid ) , " ARGV " , & v ) ;
2005-06-03 12:00:42 +04:00
/* load the script and advance past interpreter line*/
script = file_load ( argv [ 1 ] , & script_size ) ;
2005-06-03 16:04:26 +04:00
if ( ( script_size > 2 ) & & script [ 0 ] = = ' # ' & & script [ 1 ] = = ' ! ' ) {
script + = 2 ;
script_size - = 2 ;
while ( script_size ) {
if ( * script = = ' \r ' | | * script = = ' \n ' )
break ;
script + + ;
script_size - - ;
}
}
2005-05-29 15:58:14 +04:00
/* run the script */
2005-06-03 12:00:42 +04:00
if ( ejsEvalScript ( eid , script , & result , & emsg ) = = - 1 ) {
fprintf ( stderr , " smbscript: ejsEvalScript(): %s \n " , emsg ) ;
2005-06-04 07:51:38 +04:00
exit ( 127 ) ;
2005-05-29 07:25:21 +04:00
}
2005-06-04 07:51:38 +04:00
return_var = ejsGetReturnValue ( eid ) ;
exit_status = return_var - > integer ;
2005-05-29 07:25:21 +04:00
ejsClose ( ) ;
2005-05-29 04:13:10 +04:00
2005-05-29 15:35:56 +04:00
talloc_free ( mem_ctx ) ;
2005-06-04 07:51:38 +04:00
return exit_status ;
2005-05-29 04:13:10 +04:00
}