shithub: fnt

Download patch

ref: 1a1a1f4fa68b29192b85422426756ac78c5acb79
parent: 103afe43213f6cb1e6969ffa72fe0dab1db1c45e
author: Sigrid Solveig Haflínudóttir <sigrid@ftrv.se>
date: Sat Jul 13 13:44:27 EDT 2024

better error reporting on POSIX

--- a/otf.h.in
+++ b/otf.h.in
@@ -10,6 +10,7 @@
 typedef uint16_t u16int;
 typedef uint32_t u32int;
 typedef uint64_t u64int;
+char *otferrstr(void);
 #else
 #pragma incomplete Otf
 #endif
--- a/plan9/otf.h
+++ b/plan9/otf.h
@@ -10,6 +10,7 @@
 typedef uint16_t u16int;
 typedef uint32_t u32int;
 typedef uint64_t u64int;
+char *otferrstr(void);
 #else
 #pragma incomplete Otf
 #endif
--- a/plan9/test.c
+++ b/plan9/test.c
@@ -106,12 +106,12 @@
 
 	out.aux = Bfdopen(1, OWRITE);
 	for(i = 0; i < argc; i++){
-		if(h <= 0)
-			Bprint(out.aux, "%s\n", argv[i]);
 		if((in.aux = Bopen(argv[i], OREAD)) == nil || (o = otfopen(&in)) == nil){
 			fprint(2, "%r\n");
 			continue;
 		}
+		if(h <= 0)
+			Bprint(out.aux, "%s\n", argv[i]);
 		if(G && gi < 0){
 			int i, n = otfglyfnum(o);
 			Image *im = h > 0 ? calloc(n, sizeof(*im)) : nil;
@@ -145,7 +145,7 @@
 		}else{
 			int n = otfglyfnum(o);
 			if(gi >= n)
-				sysfatal("glyph %d out of range, max %d\n", gi, n-1);
+				sysfatal("glyph %d out of range, max %d", gi, n-1);
 			Glyf *g = otfglyf(o, gi);
 			if(g == nil){
 				fprint(2, "%d: %r\n", gi);
--- a/unix/otfsys.c
+++ b/unix/otfsys.c
@@ -2,21 +2,16 @@
 #include <stdarg.h>
 #include <time.h>
 
-char *
-fmttime(long long v)
-{
-	static char buf[32];
-	time_t t = v;
-	struct tm *tm;
-	tm = gmtime(&t);
-	strftime(buf, sizeof(buf), "%c", tm);
-	return buf;
-}
-
 #define ERRMAX 512
 
 static char errstr[ERRMAX];
 
+char *
+otferrstr(void)
+{
+	return errstr;
+}
+
 void
 werrstr(char *fmt, ...)
 {
@@ -29,5 +24,16 @@
 	va_end(a);
 	if(n >= 2 && buf[n-2] == '%' && buf[n-1] == 'r')
 		snprintf(buf+n-2, sizeof(buf)-n+2, "%s", errstr);
-	strcpy(errstr, buf);
+	snprintf(errstr, sizeof(errstr), "%s", buf);
+}
+
+char *
+fmttime(long long v)
+{
+	static char buf[32];
+	time_t t = v;
+	struct tm *tm;
+	tm = gmtime(&t);
+	strftime(buf, sizeof(buf), "%c", tm);
+	return buf;
 }
--- a/unix/test.c
+++ b/unix/test.c
@@ -1,7 +1,8 @@
+#include <err.h>
+#include <math.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
-#include <math.h>
 #include "otf.h"
 
 typedef struct Image Image;
@@ -44,10 +45,9 @@
 	bh *= 1.5;
 	npix *= 1.5;
 	npix += bw*gap;
-	if((b = malloc(npix)) == NULL){
-		fprintf(stderr, "no memory\n");
-		exit(1);
-	}
+	if((b = malloc(npix)) == NULL)
+		err(1, NULL);
+
 	memset(b, 0xff, npix);
 	x = y = gap;
 	for(i = 0; i < n; i++)
@@ -133,28 +133,24 @@
 
 	out.aux = fdopen(1, "wb");
 	for(i = 0; i < argc; i++){
+		if((in.aux = fopen(argv[i], "rb")) == NULL)
+			err(1, "%s", argv[i]);
+		if((o = otfopen(&in)) == NULL)
+			errx(1, "%s: %s", argv[i], otferrstr());
 		if(h <= 0)
 			fprintf(out.aux, "%s\n", argv[i]);
-		if((in.aux = fopen(argv[i], "rb")) == NULL || (o = otfopen(&in)) == NULL){
-			fprintf(stderr, "%s: failed\n", argv[i]);
-			continue;
-		}
 		if(G && gi < 0){
 			int i, n = otfglyfnum(o);
 			Image *im = h > 0 ? calloc(n, sizeof(*im)) : NULL;
 			for(i = 0; i < n; i++){
 				Glyf *g = otfglyf(o, i);
-				if(g == NULL){
-					fprintf(stderr, "glyph %d: failed to parse\n", i);
-					exit(1);
-				}
+				if(g == NULL)
+					errx(1, "glyph %d: %s", i, otferrstr());
 				if(h > 0 && g->simple != NULL && g->numberOfContours > 0){
 					int w;
 					u8int *b = otfdrawglyf(g, h, &w);
-					if(b == NULL){
-						fprintf(stderr, "glyph %d: failed to draw\n", i);
-						exit(1);
-					}
+					if(b == NULL)
+						errx(1, "glyph %d: %s", i, otferrstr());
 					im[i].w = w;
 					im[i].b = b;
 				}else if(h <= 0){
@@ -173,23 +169,19 @@
 			otfprint(o, &out, indentΔ);
 		}else{
 			int n = otfglyfnum(o);
-			if(gi >= n){
-				fprintf(stderr, "glyph %d out of range, max %d\n", gi, n-1);
-				exit(1);
-			}
+			if(gi >= n)
+				errx(1, "glyph %d out of range, max %d", gi, n-1);
 			Glyf *g = otfglyf(o, gi);
 			if(g == NULL){
-				fprintf(stderr, "%d: failed\n", gi);
+				errx(1, "glyph %d: %s", gi, otferrstr());
 			}else if(h > 0){
 				if(g->component != NULL)
-					fprintf(stderr, "%d: component\n", gi);
+					errx(1, "glyph %d: component", gi);
 				else{
 					int w;
 					u8int *b = otfdrawglyf(g, h, &w);
-					if(b == NULL){
-						fprintf(stderr, "failed\n");
-						exit(1);
-					}
+					if(b == NULL)
+						errx(1, "glyph %d: %s", gi, otferrstr());
 					fprintf(out.aux, "%11s %11d %11d %11d %11d ", "k8", 0, 0, w, h);
 					fwrite(b, 1, w*h, out.aux);
 					free(b);