Commit 02209233 authored by rswindell's avatar rswindell
Browse files

Added support for Unix shell scripts by ignoring the first line if it starts with #!.

parent 93f453e1
...@@ -489,6 +489,7 @@ static const char* js_ext(const char* fname) ...@@ -489,6 +489,7 @@ static const char* js_ext(const char* fname)
long js_exec(const char *fname, char** args) long js_exec(const char *fname, char** args)
{ {
int argc=0; int argc=0;
uint line_no;
char path[MAX_PATH+1]; char path[MAX_PATH+1];
char line[1024]; char line[1024];
char* js_buf=NULL; char* js_buf=NULL;
...@@ -540,34 +541,33 @@ long js_exec(const char *fname, char** args) ...@@ -540,34 +541,33 @@ long js_exec(const char *fname, char** args)
JS_SetBranchCallback(js_cx, js_BranchCallback); JS_SetBranchCallback(js_cx, js_BranchCallback);
if(fname==NULL) { /* Use stdin for script source */ if(fname==NULL) /* Use stdin for script source */
fprintf(statfp,"Reading script from stdin\n"); SAFECOPY(path,"stdin");
js_buflen=0; fprintf(statfp,"Reading script from %s\n",path);
while(!feof(stdin)) { line_no=0;
if(!fgets(line,sizeof(line),stdin)) js_buflen=0;
break; while(!feof(stdin)) {
if(!fgets(line,sizeof(line),stdin))
if((js_buf=realloc(js_buf,js_buflen+strlen(line)))==NULL) { break;
fprintf(errfp,"!Error allocating %u bytes of memory\n" line_no++;
,js_buflen+strlen(line)); #ifdef __unix__ /* Support Unix Shell Scripts that start with #!/path/to/jsexec */
return(-1); if(line_no==1 && strncmp(line,"#!",2))
} continue;
strcpy(js_buf+js_buflen,line); #endif
js_buflen+=strlen(line); if((js_buf=realloc(js_buf,js_buflen+strlen(line)))==NULL) {
} fprintf(errfp,"!Error allocating %u bytes of memory\n"
if((js_script=JS_CompileScript(js_cx, js_scope, js_buf, js_buflen, NULL, 1))==NULL) { ,js_buflen+strlen(line));
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); 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, fname==NULL ? NULL : path, 1))==NULL) {
fprintf(errfp,"!Error compiling script from stdin\n");
return(-1);
} }
JS_ExecuteScript(js_cx, js_scope, js_script, &rval); JS_ExecuteScript(js_cx, js_scope, js_script, &rval);
JS_GetProperty(js_cx, js_glob, "exit_code", &rval); JS_GetProperty(js_cx, js_glob, "exit_code", &rval);
if(rval!=JSVAL_VOID) { if(rval!=JSVAL_VOID) {
......
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