shithub: libmujs

Download patch

ref: d4655ae8882ef601b06d3f6f0e35bc38ebeea763
parent: 924649a69ecbebbf60339f638e73a0595cfb0732
author: Tor Andersson <tor.andersson@artifex.com>
date: Mon Sep 3 15:41:05 EDT 2018

Show system error message in js_loadfile.

--- a/jsstate.c
+++ b/jsstate.c
@@ -130,36 +130,37 @@
 
 	f = fopen(filename, "rb");
 	if (!f) {
-		js_error(J, "cannot open file: '%s'", filename);
+		js_error(J, "cannot open file '%s': %s", filename, strerror(errno));
 	}
 
 	if (fseek(f, 0, SEEK_END) < 0) {
 		fclose(f);
-		js_error(J, "cannot seek in file: '%s'", filename);
+		js_error(J, "cannot seek in file '%s': %s", filename, strerror(errno));
 	}
 
 	n = ftell(f);
 	if (n < 0) {
 		fclose(f);
-		js_error(J, "cannot tell in file: '%s'", filename);
+		js_error(J, "cannot tell in file '%s': %s", filename, strerror(errno));
 	}
 
 	if (fseek(f, 0, SEEK_SET) < 0) {
 		fclose(f);
-		js_error(J, "cannot seek in file: '%s'", filename);
+		js_error(J, "cannot seek in file '%s': %s", filename, strerror(errno));
 	}
 
-	s = js_malloc(J, n + 1); /* add space for string terminator */
-	if (!s) {
+	if (js_try(J)) {
 		fclose(f);
-		js_error(J, "cannot allocate storage for file contents: '%s'", filename);
+		js_throw(J);
 	}
+	s = js_malloc(J, n + 1); /* add space for string terminator */
+	js_endtry(J);
 
 	t = fread(s, 1, (size_t)n, f);
 	if (t != n) {
 		js_free(J, s);
 		fclose(f);
-		js_error(J, "cannot read data from file: '%s'", filename);
+		js_error(J, "cannot read data from file '%s': %s", filename, strerror(errno));
 	}
 
 	s[n] = 0; /* zero-terminate string containing file data */
--- a/main.c
+++ b/main.c
@@ -2,6 +2,7 @@
 #include <stdlib.h>
 #include <string.h>
 #include <unistd.h>
+#include <errno.h>
 
 #include "mujs.h"
 
@@ -142,29 +143,29 @@
 
 	f = fopen(filename, "rb");
 	if (!f) {
-		js_error(J, "cannot open file: '%s'", filename);
+		js_error(J, "cannot open file '%s': %s", filename, strerror(errno));
 	}
 
 	if (fseek(f, 0, SEEK_END) < 0) {
 		fclose(f);
-		js_error(J, "cannot seek in file: '%s'", filename);
+		js_error(J, "cannot seek in file '%s': %s", filename, strerror(errno));
 	}
 
 	n = ftell(f);
 	if (n < 0) {
 		fclose(f);
-		js_error(J, "cannot tell in file: '%s'", filename);
+		js_error(J, "cannot tell in file '%s': %s", filename, strerror(errno));
 	}
 
 	if (fseek(f, 0, SEEK_SET) < 0) {
 		fclose(f);
-		js_error(J, "cannot seek in file: '%s'", filename);
+		js_error(J, "cannot seek in file '%s': %s", filename, strerror(errno));
 	}
 
 	s = malloc(n + 1);
 	if (!s) {
 		fclose(f);
-		js_error(J, "cannot allocate storage for file contents: '%s'", filename);
+		js_error(J, "out of memory");
 	}
 
 	t = fread(s, 1, n, f);
@@ -171,7 +172,7 @@
 	if (t != n) {
 		free(s);
 		fclose(f);
-		js_error(J, "cannot read data from file: '%s'", filename);
+		js_error(J, "cannot read data from file '%s': %s", filename, strerror(errno));
 	}
 	s[n] = 0;