Commit f371a95a authored by rswindell's avatar rswindell
Browse files

Will now read script from stdin if no module name is specified on the

command-line and stdin is not a TTY/console. This should allow Unix (e.g. CGI)
scripts to be self-contained by starting with #!/path/to/jsexec.
parent f7eceec0
......@@ -490,6 +490,9 @@ long js_exec(const char *fname, char** args)
{
int argc=0;
char path[MAX_PATH+1];
char line[1024];
char* js_buf=NULL;
size_t js_buflen;
JSObject* js_scope=NULL;
JSScript* js_script=NULL;
JSString* arg;
......@@ -498,18 +501,19 @@ long js_exec(const char *fname, char** args)
jsval rval=JSVAL_VOID;
int32 result=0;
if(strcspn(fname,"/\\")==strlen(fname)) {
sprintf(path,"%s%s%s",scfg.mods_dir,fname,js_ext(fname));
if(scfg.mods_dir[0]==0 || !fexistcase(path))
sprintf(path,"%s%s%s",scfg.exec_dir,fname,js_ext(fname));
} else
sprintf(path,"%s%s",fname,js_ext(fname));
if(!fexistcase(path)) {
fprintf(errfp,"!Module file (%s) doesn't exist\n",path);
return(-1);
if(fname!=NULL) {
if(strcspn(fname,"/\\")==strlen(fname)) {
sprintf(path,"%s%s%s",scfg.mods_dir,fname,js_ext(fname));
if(scfg.mods_dir[0]==0 || !fexistcase(path))
sprintf(path,"%s%s%s",scfg.exec_dir,fname,js_ext(fname));
} else
sprintf(path,"%s%s",fname,js_ext(fname));
if(!fexistcase(path)) {
fprintf(errfp,"!Module file (%s) doesn't exist\n",path);
return(-1);
}
}
JS_ClearPendingException(js_cx);
js_scope=JS_NewObject(js_cx, NULL, NULL, js_glob);
......@@ -534,15 +538,35 @@ long js_exec(const char *fname, char** args)
JS_DefineProperty(js_cx, js_scope, "argc", INT_TO_JSVAL(argc)
,NULL,NULL,JSPROP_READONLY|JSPROP_ENUMERATE);
if((js_script=JS_CompileFile(js_cx, js_scope, path))==NULL) {
fprintf(errfp,"!Error compiling %s\n",path);
return(-1);
}
JS_SetBranchCallback(js_cx, js_BranchCallback);
if(fname==NULL) { /* Use stdin for script source */
js_buflen=0;
while(!feof(stdin)) {
if(!fgets(line,sizeof(line),stdin))
break;
if((js_buf=realloc(js_buf,js_buflen+strlen(line)))==NULL) {
fprintf(errfp,"!Error allocating %lu bytes of memory\n"
,js_buflen+strlen(line));
return(-1);
}
strcpy(js_buf+js_buflen,line);
js_buflen+=strlen(line);
}
if((js_script=JS_CompileScript(js_cx, js_scope, js_buf, js_buflen, NULL, 1))==NULL) {
fprintf(errfp,"!Error compiling script from stdin\n");
return(-1);
}
} else {
if((js_script=JS_CompileFile(js_cx, js_scope, path))==NULL) {
fprintf(errfp,"!Error compiling %s\n",path);
return(-1);
}
}
JS_ExecuteScript(js_cx, js_scope, js_script, &rval);
JS_GetProperty(js_cx, js_glob, "exit_code", &rval);
if(rval!=JSVAL_VOID) {
......@@ -645,7 +669,7 @@ int main(int argc, char **argv, char** environ)
SAFECOPY(scfg.ctrl_dir,p);
}
if(module==NULL) {
if(module==NULL && isatty(fileno(stdin))) {
fprintf(errfp,"\n!Module name not specified\n");
usage(errfp);
bail(1);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment