shithub: neatroff

Download patch

ref: 9e1124cb460b1bdfc52c18b299fe0a7d56c5270a
parent: 5a353cb70b578a6dfb39d93a73df43fe194aaa02
author: Ali Gholami Rudi <[email protected]>
date: Sun Nov 25 14:37:04 EST 2012

unify array limits

--- a/dev.c
+++ b/dev.c
@@ -4,9 +4,6 @@
 #include <string.h>
 #include "xroff.h"
 
-#define PATHLEN		1024
-#define MAXFONTS	32
-
 char dev_dir[PATHLEN];	/* device directory */
 int dev_res;		/* device resolution */
 int dev_uwid;		/* device unitwidth */
@@ -14,8 +11,8 @@
 int dev_ver;		/* minimum vertical movement */
 
 /* mounted fonts */
-static char fn_name[MAXFONTS][FNLEN];	/* font names */
-static struct font *fn_font[MAXFONTS];	/* font structs */
+static char fn_name[NFONTS][FNLEN];	/* font names */
+static struct font *fn_font[NFONTS];	/* font structs */
 static int fn_n;			/* number of mounted fonts */
 
 static void skipline(FILE* filp)
@@ -55,7 +52,7 @@
 int dev_open(char *dir)
 {
 	char path[PATHLEN];
-	char tok[LLEN];
+	char tok[ILNLEN];
 	int i;
 	FILE *desc;
 	strcpy(dev_dir, dir);
--- a/font.c
+++ b/font.c
@@ -41,9 +41,9 @@
 
 static void font_charset(struct font *fn, FILE *fin)
 {
-	char tok[LLEN];
-	char name[LLEN];
-	char id[LLEN];
+	char tok[ILNLEN];
+	char name[ILNLEN];
+	char id[ILNLEN];
 	struct glyph *glyph = NULL;
 	struct glyph *prev = NULL;
 	int wid, type;
@@ -72,7 +72,7 @@
 struct font *font_open(char *path)
 {
 	struct font *fn = malloc(sizeof(*fn));
-	char tok[LLEN];
+	char tok[ILNLEN];
 	FILE *fin;
 	fin = fopen(path, "r");
 	memset(fn, 0, sizeof(*fn));
--- a/out.c
+++ b/out.c
@@ -76,8 +76,8 @@
 void output(char *s)
 {
 	struct glyph *g;
-	char c[LLEN];
-	char arg[LINELEN];
+	char c[GNLEN * 2];
+	char arg[ILNLEN];
 	printf("v%d\n", n_v);
 	printf("H%d\n", n_o + n_i);
 	while (*s) {
--- a/ren.c
+++ b/ren.c
@@ -11,9 +11,9 @@
 	int blanks;	/* blanks before word */
 };
 
-static char buf[LINELEN];		/* output buffer */
+static char buf[LNLEN];			/* output buffer */
 static int buflen;
-static struct word words[LINELEN];	/* words in the buffer */
+static struct word words[NWORDS];	/* words in the buffer */
 static int nwords;
 static int wid;				/* total width of the buffer */
 static int ren_backed = -1;		/* pushed back character */
@@ -176,7 +176,7 @@
 
 static void ren_br(int adj)
 {
-	char out[LINELEN];
+	char out[LNLEN];
 	buf[buflen] = '\0';
 	if (buflen) {
 		adjust(out, adj);
@@ -190,8 +190,8 @@
 
 void render(void)
 {
-	char c[LLEN];
-	char arg[LINELEN];
+	char c[GNLEN * 2];
+	char arg[ILNLEN];
 	struct glyph *g;
 	int g_wid;
 	struct word *word = NULL;
--- a/tr.c
+++ b/tr.c
@@ -4,9 +4,6 @@
 #include <string.h>
 #include "xroff.h"
 
-#define LINEL		1024
-#define LEN(a)		(sizeof(a) / sizeof((a)[0]))
-
 static int tr_nl = 1;
 
 static int unit_scale(int c, int n, int mag)
@@ -283,8 +280,8 @@
 	int c = cp_next();
 	int nl = c == '\n';
 	char *args[NARGS + 1] = {NULL};
-	char buf[LINEL];
-	char cmd[LINEL];
+	char cmd[RLEN];
+	char buf[LNLEN];
 	struct cmd *req = NULL;
 	int argc;
 	int i;
@@ -292,18 +289,18 @@
 		nl = 1;
 		args[0] = cmd;
 		cmd[0] = c;
-		arg_regname(cmd + 1, LINEL - 2);
+		arg_regname(cmd + 1, sizeof(cmd) - 1);
 		for (i = 0; i < LEN(cmds); i++)
 			if (!strcmp(cmd + 1, cmds[i].id))
 				req = &cmds[i];
 		if (req) {
 			if (req->args)
-				argc = req->args(args + 1, NARGS, buf, LINEL);
+				argc = req->args(args + 1, NARGS, buf, sizeof(buf));
 			else
-				argc = mkargs(args + 1, NARGS, buf, LINEL);
+				argc = mkargs(args + 1, NARGS, buf, sizeof(buf));
 			req->f(argc + 1, args);
 		} else {
-			argc = mkargs(args + 1, NARGS, buf, LINEL);
+			argc = mkargs(args + 1, NARGS, buf, sizeof(buf));
 			if (str_get(REG(cmd[1], cmd[2])))
 				in_push(str_get(REG(cmd[1], cmd[2])), args + 1);
 		}
--- a/xroff.h
+++ b/xroff.h
@@ -1,13 +1,21 @@
-#define SC_IN		(dev_res)
-#define SC_PT		(SC_IN / 72)
+/* converting scales */
+#define SC_IN		(dev_res)	/* inch in units */
+#define SC_PT		(SC_IN / 72)	/* point in units */
 
-#define FNLEN		32
-#define FNGLYPHS	512
-#define FNNAME		32
-#define LLEN		128
-#define LINELEN		1024
-#define NARGS		9
+/* predefined array limits */
+#define PATHLEN		1024	/* path length */
+#define NFONTS		32	/* number of fonts */
+#define FNLEN		32	/* font name length */
+#define NGLYPHS		512	/* glyphs in fonts */
+#define GNLEN		32	/* glyph name length */
+#define ILNLEN		256	/* line limit of input files */
+#define LNLEN		4000	/* line buffer length (ren.c/out.c) */
+#define NWORDS		1000	/* number of words in line buffer */
+#define NARGS		9	/* number of macro arguments */
+#define RLEN		4	/* register/macro name */
 
+#define LEN(a)		(sizeof(a) / sizeof((a)[0]))
+
 /* number registers */
 extern int nreg[];
 int num_get(int id);
@@ -38,8 +46,8 @@
 extern int dev_ver;
 
 struct glyph {
-	char name[FNNAME];	/* name of the glyph */
-	char id[FNNAME];	/* device-dependent glyph identifier */
+	char name[FNLEN];	/* name of the glyph */
+	char id[FNLEN];		/* device-dependent glyph identifier */
 	struct font *font;	/* glyph font */
 	int wid;		/* character width */
 	int type;		/* character type; ascender/descender */
@@ -47,12 +55,12 @@
 
 struct font {
 	char name[FNLEN];
-	struct glyph glyphs[FNGLYPHS];
+	struct glyph glyphs[NGLYPHS];
 	int nglyphs;
 	int spacewid;
 	int special;
-	char c[FNGLYPHS][FNNAME];	/* character names in charset */
-	struct glyph *g[FNGLYPHS];	/* character glyphs in charset */
+	char c[NGLYPHS][FNLEN];	/* character names in charset */
+	struct glyph *g[NGLYPHS];	/* character glyphs in charset */
 	int n;				/* number of characters in charset */
 };
 
@@ -61,6 +69,7 @@
 void dev_close(void);
 int dev_mnt(int pos, char *id, char *name);
 int dev_font(char *id);
+int charwid(int wid, int sz);
 
 /* font-related functions */
 struct font *font_open(char *path);
@@ -74,9 +83,9 @@
 int dev_spacewid(void);
 
 /* different layers of neatroff */
-int in_next(void);
-int cp_next(void);
-int tr_next(void);
+int in_next(void);	/* input layer */
+int cp_next(void);	/* copy-mode layer */
+int tr_next(void);	/* troff layer */
 void in_push(char *s, char **args);
 char *in_arg(int i);
 void in_back(int c);
@@ -83,8 +92,8 @@
 void cp_back(int c);
 
 /* rendering */
-void render(void);
-void output(char *s);
+void render(void);	/* read from in.c and print the output */
+void output(char *s);	/* output the given rendered line */
 
 /* troff commands */
 void tr_br(int argc, char **args);
@@ -97,7 +106,6 @@
 /* helpers */
 void errmsg(char *msg, ...);
 int utf8len(int c);
-int charwid(int wid, int sz);
 
 /* variable length string buffer */
 struct sbuf {