[mw-devel] MW3 r977 - trunk/src

arthur at sucs.org arthur at sucs.org
Fri Oct 26 11:27:02 BST 2007


Author: arthur
Date: 2007-10-26 11:27:02 +0100 (Fri, 26 Oct 2007)
New Revision: 977

Modified:
   trunk/src/init.c
   trunk/src/js.c
Log:
load script files from a central repository if not found locally


Modified: trunk/src/init.c
===================================================================
--- trunk/src/init.c	2007-10-26 09:17:30 UTC (rev 976)
+++ trunk/src/init.c	2007-10-26 10:27:02 UTC (rev 977)
@@ -29,7 +29,7 @@
 #include "frl.h"
 
 /* Prototypes */
-void ReadInitFile(char *base, char *filename);
+int ReadInitFile(char *base, char *filename);
 void LoadFunction(char *, char *, FILE *, int *, const char *, int);
 void DestroyFunction(char *);
 
@@ -43,13 +43,20 @@
 		myid=geteuid();
 		seteuid(getuid());
 
-		ReadInitFile(pw->pw_dir, name);
+		/* try to load the personal copy*/
+		if (ReadInitFile(pw->pw_dir, name)) {
+			/* try the system wide one instead */
+			if (ReadInitFile(HOMEPATH"/scripts", name)) {
+				if (strcmp(".mwrc", name)!=0)
+					fprintf(stderr, "Could not find file %s to load it.\n", name);
+			}
+		}
 
 		seteuid(myid);
 	}
 }
 
-void ReadInitFile(char *base, char *filename)
+int ReadInitFile(char *base, char *filename)
 {
 	FILE *file;	
 	char *buff, *backup, *header;
@@ -90,32 +97,31 @@
 		    strstr(filename, "/../"))
 		{
 			printf(_("Cannot load \"%s\": Illegal path\n"), filename);
-			return;
+			return 1;
 		}
 		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;
+			/* be quiet about its not there, handle higher up */
+			return 1;
 		}
 		if (!S_ISREG(stats.st_mode))
 		{
 			printf(_("Error reading %s: Not a regular file\n"), path);
-			return;
+			return 1;
 		}
 
 		if ((file=fopen(path,"r"))==NULL)
 		{
 			if (strcmp(".mwrc", filename))	printf(_("Error reading %s: %s\n"), path, strerror(errno));
-			return;
+			return 1;
 		}
 	}
 
 	if ((a=strrchr(filename, '.'))!=NULL && strncasecmp(a, ".js", 3)==0) {
-		load_jsfile(file, filename);
+		int ret = load_jsfile(file, filename);
 		fclose(file);
-		return;
+		return ret;
 	}
 
 	lineno=0;	
@@ -394,4 +400,5 @@
 		free(header);
 	}
 	fclose(file);
+	return 0;
 }

Modified: trunk/src/js.c
===================================================================
--- trunk/src/js.c	2007-10-26 09:17:30 UTC (rev 976)
+++ trunk/src/js.c	2007-10-26 10:27:02 UTC (rev 977)
@@ -1170,12 +1170,12 @@
 	len = ftell(f);
 	fseek(f, where, SEEK_SET);
 
-	printf("Loading %d bytes from %s\n", len, filename);
+/*	printf("Loading %d bytes from %s\n", len, filename); */
 
 	body = malloc(len+1);
 	if(body==NULL) {
 		fprintf(stderr, "load_jsfile: could not allocate memory for javascript file\n");
-		return 0;
+		return 1;
 	}
 	
 	fread(body, 1, len, f);
@@ -1188,13 +1188,13 @@
 	
 	if(unicode_body==NULL) {
 		fprintf(stderr, "load_jsfile: failed to allocate memory for javascript file\n");
-		return 0;
+		return 1;
 	}
 	utferror = convert_string_charset( body, "UTF-8", len, (char *)unicode_body, "UCS-2", sizeof(jschar) * (len+1), &length, NULL, NULL, NULL, NULL);
 	if(utferror<0)
 	{
 		fprintf(stderr, "load_jsfile: failed to convert script to jschar string. Error %d\n", utferror);
-		return 0;
+		return 1;
 	}
 	
 	if(utferror & WINVALIDCHARS) {
@@ -1213,7 +1213,7 @@
 	
 	if (script == NULL) {
 		printf("Failed to compile js script: %s\n", filename);
-		return 0;
+		return 1;
 	}
 	
 	/* Execute the compiled script */
@@ -1222,13 +1222,13 @@
 	success = JS_ExecuteScript(jscx, jsroot, script, &retval);
 	if (success == JS_FALSE) {
 		printf("Failed to execute js script: %s\n", filename);
-		return 0;
+		return 1;
 	}
 	js_clear_timeout();
 	// now the script has been run we can destroy it (the context retains the functions/objects it created)
 	
 	JS_DestroyScript(jscx, script);
-	return 1;
+	return 0;
 }
 
 /* does the named function exist in javascript */





More information about the mw-devel mailing list