shithub: neatroff

Download patch

ref: 46d34678fbd5bdc6ebf9c797ddc4267f34c22519
parent: 83e0919c862f62422e065f6ce1a684e202457c37
author: Ali Gholami Rudi <ali@rudi.ir>
date: Sun Apr 14 10:07:00 EDT 2013

xroff: read files passed as arguments

--- a/Makefile
+++ b/Makefile
@@ -1,5 +1,5 @@
 CC = cc
-CFLAGS = -Wall -O2
+CFLAGS = -Wall -O2 -DTROFFROOT=\"/root/troff/home\"
 LDFLAGS =
 
 all: xroff
--- a/in.c
+++ b/in.c
@@ -15,6 +15,9 @@
 };
 
 static struct inbuf *buf;
+static char files[NFILES][PATHLEN];
+static int nfiles;
+static int cfile;
 
 static char **args_init(char **args);
 static void args_free(char **args);
@@ -40,7 +43,7 @@
 
 void in_source(char *path)
 {
-	FILE *fin = path ? fopen(path, "r") : stdin;
+	FILE *fin = path && path[0] ? fopen(path, "r") : stdin;
 	if (fin) {
 		in_new();
 		buf->fin = fin;
@@ -49,6 +52,12 @@
 	}
 }
 
+void in_queue(char *path)
+{
+	if (nfiles < NFILES)
+		snprintf(files[nfiles++], PATHLEN - 1, "%s", path ? path : "");
+}
+
 static void in_pop(void)
 {
 	struct inbuf *old = buf;
@@ -61,10 +70,17 @@
 	free(old);
 }
 
+static int in_nextfile(void)
+{
+	while (!buf && cfile < nfiles)
+		in_source(files[cfile++]);
+	return !buf;
+}
+
 int in_next(void)
 {
 	int c;
-	if (!buf)
+	if (!buf && in_nextfile())
 		return -1;
 	if (buf->backed >= 0) {
 		c = buf->backed;
@@ -71,7 +87,7 @@
 		buf->backed = -1;
 		return c;
 	}
-	while (buf) {
+	while (buf || !in_nextfile()) {
 		if (buf->buf && buf->pos < buf->len)
 			break;
 		if (!buf->buf && (c = getc(buf->fin)) >= 0)
--- a/xroff.c
+++ b/xroff.c
@@ -7,6 +7,7 @@
  */
 #include <stdarg.h>
 #include <stdio.h>
+#include <string.h>
 #include "xroff.h"
 
 static void g_init(void)
@@ -32,13 +33,26 @@
 	va_end(ap);
 }
 
-int main(void)
+int main(int argc, char **argv)
 {
-	dev_open("/root/troff/home/font/devutf");
+	int i;
+	char path[PATHLEN];
+	dev_open(TROFFROOT "/font/devutf");
 	env_init();
 	tr_init();
 	g_init();
-	in_source(NULL);	/* reading from standard input */
+	for (i = 1; i < argc; i++) {
+		if (argv[i][0] != '-' || !argv[i][0])
+			break;
+		if (argv[i][1] == 'm') {
+			sprintf(path, TROFFROOT "/tmac/tmac.%s", argv[i] + 2);
+			in_queue(path);
+		}
+	}
+	if (i == argc)
+		in_queue(NULL);	/* reading from standard input */
+	for (; i < argc; i++)
+		in_queue(!strcmp("-", argv[i]) ? NULL : argv[i]);
 	compile();
 	env_free();
 	dev_close();
--- a/xroff.h
+++ b/xroff.h
@@ -4,6 +4,7 @@
 
 /* predefined array limits */
 #define PATHLEN		1024	/* path length */
+#define NFILES		16	/* number of input files */
 #define NFONTS		32	/* number of fonts */
 #define FNLEN		32	/* font name length */
 #define NGLYPHS		512	/* glyphs in fonts */
@@ -92,16 +93,17 @@
 int dev_spacewid(void);
 
 /* different layers of neatroff */
-int in_next(void);	/* input layer */
-int cp_next(void);	/* copy-mode layer */
-int tr_next(void);	/* troff layer */
+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);
-void in_source(char *path);
-char *in_arg(int i);
-void in_back(int c);
-char *in_filename(void);
-void cp_back(int c);
-void cp_skip(void);	/* skip current input line or block */
+void in_source(char *path);	/* .so request */
+void in_queue(char *path);	/* next input file */
+char *in_arg(int i);		/* look up argument */
+void in_back(int c);		/* push back input character */
+char *in_filename(void);	/* current filename */
+void cp_back(int c);		/* push back copy-mode character */
+void cp_skip(void);		/* skip current input line or block */
 
 /* rendering */
 void render(void);	/* read from in.c and print the output */