aboutsummaryrefslogtreecommitdiffstats
path: root/src/man/preproc.c
blob: 34a49d335954bb00aeeb18d8376c117c5e075821 (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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>

#define MAXBUF 4096
#define MAXMACROS 64
static char *macro[MAXMACROS] = {NULL};

static void add_macro(char *m) {
	assert(m);
	int i;
	for (i = 0; i < MAXMACROS && macro[i]; i++);
	if (i == MAXMACROS) {
		fprintf(stderr, "Error: maximum number of marcros (%d) exceeded\n", MAXMACROS);
		exit(1);
	}

	macro[i] = m;
}

static char *find_macro(char *m) {
	assert(m);
	int i = 0;
	while (i < MAXMACROS && macro[i]) {
		if (strcmp(macro[i], m) == 0)
			return m;
		i++;
	}

	return NULL;
}

static void usage(void) {
	printf("Simple preprocessor for man pages. It supports:\n");
	printf("\t#if 0 ... #endif\n");
	printf("\t#ifdef macro ... #endif\n");
	printf("Usage: preproc [--help] [-Dmacro] manpage.txt\n");
	return;
}


int main(int argc, char **argv) {
	if (argc == 1) {
		fprintf(stderr, "Error: no files/arguments provided\n");
		usage();
		exit(1);
	}

	int i;
	for (i = 1; i < argc; i++) {
		if (strncmp(argv[i], "-D", 2) == 0)
			add_macro(argv[i] + 2);
		else if (strcmp(argv[i], "--help") == 0) {
			usage();
			return 0;
		}
		else if (*argv[i] == '-') {
			fprintf(stderr, "Error: invalid argument %s\n", argv[i]);
			exit(1);
		}
		else
			break;
	}

	char *ptr = strstr(argv[i], ".txt");
	if (!ptr || strlen(ptr) != 4) {
		fprintf(stderr, "Error: input file needs to have a .txt extension\n"),
		exit(1);
	}

	FILE *fp = fopen(argv[i], "r");
	if (!fp) {
		fprintf(stderr, "Error: cannot open %s\n", argv[i]);
		exit(1);
	}
	char *outfile = strdup(argv[i]);
	if (!outfile)
		goto errout;
	ptr = strstr(outfile, ".txt");
	assert(ptr);
	strcpy(ptr, ".man");
	FILE *fpout = fopen(outfile, "w");
	if (!fpout)
		goto errout;

	char buf[MAXBUF];
	int disabled = 0;
	int enabled = 0;
	int line = 0;;
	while (fgets(buf, MAXBUF, fp)) {
		line++;
		if (disabled && strncmp(buf, "#if", 3) == 0) {
			fprintf(stderr, "Error %d: already in a #if block on line %d\n", __LINE__, line);
			exit(1);
		}
		if ((!disabled && !enabled) && strncmp(buf, "#endif", 6) == 0) {
			fprintf(stderr, "Error %d: unmatched #endif on line %d\n", __LINE__, line);
			exit(1);
		}

		char *ptr = strchr(buf, '\n');
		if (ptr)
			*ptr = '\0';

		if (strncmp(buf, "#if 0", 5) == 0) {
			disabled = 1;
			continue;
		}
		if (strncmp(buf, "#ifdef", 6) == 0) {
			char *ptr = buf + 6;
			if (*ptr != ' ' && *ptr != '\t') {
				fprintf(stderr, "Error %d: invalid macro on line %d\n", __LINE__, line);
				exit(1);
			}

			while (*ptr == ' ' || *ptr == '\t')
				ptr++;

			if (!find_macro(ptr))
				disabled = 1;
			else
				enabled = 1;
			continue;
		}

		if (strncmp(buf, "#endif", 6) == 0) {
			disabled = 0;
			enabled = 1;
			continue;
		}

		if (!disabled) {
//			printf("%s\n", buf);
			fprintf(fpout, "%s\n", buf);
		}
	}
	fclose(fp);

	return 0;

errout:
	fclose(fp);
	fprintf(stderr, "Error: cannot open output file\n");
	exit(1);
}