[mw-devel] MW3 r920 - branches/jscript/src
arthur at sucs.org
arthur at sucs.org
Tue Mar 20 20:04:11 GMT 2007
Author: arthur
Date: 2007-03-20 20:04:11 +0000 (Tue, 20 Mar 2007)
New Revision: 920
Modified:
branches/jscript/src/Makefile
branches/jscript/src/init.c
branches/jscript/src/js.c
branches/jscript/src/js.h
Log:
Load scripts (mw & js) from a url, closes #11
Modified: branches/jscript/src/Makefile
===================================================================
--- branches/jscript/src/Makefile 2007-03-20 09:35:02 UTC (rev 919)
+++ branches/jscript/src/Makefile 2007-03-20 20:04:11 UTC (rev 920)
@@ -17,7 +17,7 @@
# cflags for standard 'cc' compiler
CFLAGS+= -Wall -pedantic -fpie -std=gnu99 -D_GNU_SOURCE
LDFLAGS+= -pie
-LDLIBS+= -lreadline -lhistory -ltermcap -lcrypt -ljs -lsqlite3
+LDLIBS+= -lreadline -lhistory -ltermcap -lcrypt -ljs -lsqlite3 -lcurl
# info strings, do not edit.
DEFS:= -DBUILD_DATE=\"$(shell date +%Y%m%d)\"
Modified: branches/jscript/src/init.c
===================================================================
--- branches/jscript/src/init.c 2007-03-20 09:35:02 UTC (rev 919)
+++ branches/jscript/src/init.c 2007-03-20 20:04:11 UTC (rev 920)
@@ -14,6 +14,7 @@
#include "bb.h"
#include "alias.h"
#include "js.h"
+#include <curl/curl.h>
extern Alias alias_list;
extern Alias bind_list;
@@ -58,29 +59,55 @@
int lineno;
struct stat stats;
- if (filename[0] == '/' ||
- !strncmp(filename, "../", 3) ||
- strstr(filename, "/../"))
- {
- printf(_("Cannot load \"%s\": Illegal path\n"), filename);
- return;
+ if (strncasecmp(filename, "http://", 7)==0
+ || strncasecmp(filename, "https://", 8)==0) {
+ CURL *cl;
+ char cerr[CURL_ERROR_SIZE];
+ /* use libcurl to grab the file */
+ file = tmpfile();
+ if (file == NULL) {
+ fprintf(stderr, "Error opening temporary file\n");
+ }
+ cl = curl_easy_init();
+/* curl_easy_setopt(cl, CURLOPT_NOPROGRESS, 0); */
+ curl_easy_setopt(cl, CURLOPT_WRITEDATA, file);
+ curl_easy_setopt(cl, CURLOPT_URL, filename);
+ curl_easy_setopt(cl, CURLOPT_ERRORBUFFER, cerr);
+ if (curl_easy_perform(cl))
+ fprintf(stderr, "Error loading %s: %s\n", filename, cerr);
+ curl_easy_cleanup(cl);
+ fseek(file, 0, SEEK_SET);
+ } else {
+ if (filename[0] == '/' ||
+ !strncmp(filename, "../", 3) ||
+ strstr(filename, "/../"))
+ {
+ printf(_("Cannot load \"%s\": Illegal path\n"), filename);
+ return;
+ }
+ snprintf(path, 1023, "%s/%s", base, filename);
+ if (stat(path, &stats))
+ {
+ if (strcmp(".mwrc", filename))
+ printf(_("Error reading %s: %s\n"), path, strerror(errno));
+ return;
+ }
+ if (!S_ISREG(stats.st_mode))
+ {
+ printf(_("Error reading %s: Not a regular file\n"), path);
+ return;
+ }
+
+ if ((file=fopen(path,"r"))==NULL)
+ {
+ if (strcmp(".mwrc", filename)) printf(_("Error reading %s: %s\n"), path, strerror(errno));
+ return;
+ }
}
- snprintf(path, 1023, "%s/%s", base, filename);
- if (stat(path, &stats))
- {
- if (strcmp(".mwrc", filename))
- printf(_("Error reading %s: %s\n"), path, strerror(errno));
- return;
- }
- if (!S_ISREG(stats.st_mode))
- {
- printf(_("Error reading %s: Not a regular file\n"), path);
- return;
- }
- if ((file=fopen(path,"r"))==NULL)
- {
- if (strcmp(".mwrc", filename)) printf(_("Error reading %s: %s\n"), path, strerror(errno));
+ if ((a=strrchr(filename, '.'))!=NULL && strncasecmp(a, ".js", 3)==0) {
+ load_jsfile(file, filename);
+ fclose(file);
return;
}
Modified: branches/jscript/src/js.c
===================================================================
--- branches/jscript/src/js.c 2007-03-20 09:35:02 UTC (rev 919)
+++ branches/jscript/src/js.c 2007-03-20 20:04:11 UTC (rev 920)
@@ -461,6 +461,44 @@
free(line);
}
+int load_jsfile(FILE *f, char *filename)
+{
+ char *body;
+ int where, len;
+ JSBool success;
+ JSScript *script = NULL;
+ jsval retval;
+ uintN lineno=0;
+
+ where = ftell(f);
+ fseek(f, 0, SEEK_END);
+ len = ftell(f);
+ fseek(f, where, SEEK_SET);
+
+ printf("Loading %d bytes from %s\n", len, filename);
+
+ body = malloc(len+1);
+ fread(body, 1, len, f);
+ body[len]=0;
+
+ /* Compile the js file specified */
+ script = JS_CompileScript(jscx, jsroot, body, len, filename, lineno);
+ free(body);
+ if (script == NULL) {
+ printf("Failed to compile js script: %s\n", filename);
+ return 0;
+ }
+
+ /* Execute the compiled script */
+ success = JS_ExecuteScript(jscx, jsroot, script, &retval);
+ if (success == JS_FALSE) {
+ printf("Failed to execute js script: %s\n", filename);
+ return 0;
+ }
+
+ return 1;
+}
+
/* Load and execute a js file */
int
load_js(char *filename) {
Modified: branches/jscript/src/js.h
===================================================================
--- branches/jscript/src/js.h 2007-03-20 09:35:02 UTC (rev 919)
+++ branches/jscript/src/js.h 2007-03-20 20:04:11 UTC (rev 920)
@@ -1,5 +1,6 @@
/* js.c */
int js_exec(char *name, int argc, char **argvc);
+int load_jsfile(FILE *f, char *filename);
int load_js(char *filename);
int is_js(char *name);
int setup_js(void);
More information about the mw-devel
mailing list