aboutsummaryrefslogtreecommitdiff
path: root/soin/soin.c
blob: b86b505bf7ad732511d64ab18774a17db84a1edd (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
/* soin: inline troff .so requests */
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>

#define LNLEN		2048

static int soin(char *path);

static int soin_cmd(char *s)
{
	char path[LNLEN];
	char *d = path;
	if (s[0] != '.' || s[1] != 's' || s[2] != 'o' || s[3] != ' ')
		return 1;
	s += 3;
	while (isspace((unsigned char) *s))
		s++;
	if (s[0] == '"') {
		s++;
		while (*s && *s != '\n' && *s != '"')
			*d++ = *s++;
	} else {
		while (*s && *s != ' ' && *s != '\n')
			*d++ = *s++;
	}
	*d = '\0';
	return soin(path);
}

static int soin(char *path)
{
	FILE *fp = path ? fopen(path, "r") : stdin;
	int lineno = 1;
	char ln[LNLEN];
	if (!fp) {
		fprintf(stderr, "soin: cannot open <%s>\n", path);
		return 1;
	}
	printf(".lf %d %s\n", lineno, path ? path : "stdin");
	while (fgets(ln, sizeof(ln), fp)) {
		lineno++;
		if (!soin_cmd(ln))
			printf(".lf %d %s\n", lineno, path ? path : "stdin");
		else
			fputs(ln, stdout);
		if (ln[0] == '.' && ln[1] == 'l' && ln[2] == 'f')
			sscanf(ln, ".lf %d", &lineno);
	}
	if (path)
		fclose(fp);
	return 0;
}

int main(void)
{
	soin(NULL);
	return 0;
}