aboutsummaryrefslogtreecommitdiffstats
path: root/src/man/preproc.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/man/preproc.c')
-rw-r--r--src/man/preproc.c166
1 files changed, 0 insertions, 166 deletions
diff --git a/src/man/preproc.c b/src/man/preproc.c
deleted file mode 100644
index eefa45278..000000000
--- a/src/man/preproc.c
+++ /dev/null
@@ -1,166 +0,0 @@
1/*
2 * Copyright (C) 2014-2020 Firejail Authors
3 *
4 * This file is part of firejail project
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19*/
20
21#include <stdio.h>
22#include <stdlib.h>
23#include <string.h>
24#include <assert.h>
25
26#define MAXBUF 4096
27#define MAXMACROS 64
28static char *macro[MAXMACROS] = {NULL};
29
30static void add_macro(char *m) {
31 assert(m);
32 int i;
33 for (i = 0; i < MAXMACROS && macro[i]; i++);
34 if (i == MAXMACROS) {
35 fprintf(stderr, "Error: maximum number of marcros (%d) exceeded\n", MAXMACROS);
36 exit(1);
37 }
38
39 macro[i] = m;
40}
41
42static char *find_macro(char *m) {
43 assert(m);
44 int i = 0;
45 while (i < MAXMACROS && macro[i]) {
46 if (strcmp(macro[i], m) == 0)
47 return m;
48 i++;
49 }
50
51 return NULL;
52}
53
54static void usage(void) {
55 printf("Simple preprocessor for man pages. It supports:\n");
56 printf("\t#if 0 ... #endif\n");
57 printf("\t#ifdef macro ... #endif\n");
58 printf("Usage: preproc [--help] [-Dmacro] manpage.txt\n");
59 return;
60}
61
62
63int main(int argc, char **argv) {
64 if (argc == 1) {
65 fprintf(stderr, "Error: no files/arguments provided\n");
66 usage();
67 exit(1);
68 }
69
70 int i;
71 for (i = 1; i < argc; i++) {
72 if (strncmp(argv[i], "-D", 2) == 0)
73 add_macro(argv[i] + 2);
74 else if (strcmp(argv[i], "--help") == 0) {
75 usage();
76 return 0;
77 }
78 else if (*argv[i] == '-') {
79 fprintf(stderr, "Error: invalid argument %s\n", argv[i]);
80 exit(1);
81 }
82 else
83 break;
84 }
85
86 char *ptr = strstr(argv[i], ".txt");
87 if (!ptr || strlen(ptr) != 4) {
88 fprintf(stderr, "Error: input file needs to have a .txt extension\n"),
89 exit(1);
90 }
91
92 FILE *fp = fopen(argv[i], "r");
93 if (!fp) {
94 fprintf(stderr, "Error: cannot open %s\n", argv[i]);
95 exit(1);
96 }
97 char *outfile = strdup(argv[i]);
98 if (!outfile)
99 goto errout;
100 ptr = strstr(outfile, ".txt");
101 assert(ptr);
102 strcpy(ptr, ".man");
103 FILE *fpout = fopen(outfile, "w");
104 if (!fpout)
105 goto errout;
106
107 char buf[MAXBUF];
108 int disabled = 0;
109 int enabled = 0;
110 int line = 0;;
111 while (fgets(buf, MAXBUF, fp)) {
112 line++;
113 if (disabled && strncmp(buf, "#if", 3) == 0) {
114 fprintf(stderr, "Error %d: already in a #if block on line %d\n", __LINE__, line);
115 exit(1);
116 }
117 if ((!disabled && !enabled) && strncmp(buf, "#endif", 6) == 0) {
118 fprintf(stderr, "Error %d: unmatched #endif on line %d\n", __LINE__, line);
119 exit(1);
120 }
121
122 char *ptr = strchr(buf, '\n');
123 if (ptr)
124 *ptr = '\0';
125
126 if (strncmp(buf, "#if 0", 5) == 0) {
127 disabled = 1;
128 continue;
129 }
130 if (strncmp(buf, "#ifdef", 6) == 0) {
131 char *ptr = buf + 6;
132 if (*ptr != ' ' && *ptr != '\t') {
133 fprintf(stderr, "Error %d: invalid macro on line %d\n", __LINE__, line);
134 exit(1);
135 }
136
137 while (*ptr == ' ' || *ptr == '\t')
138 ptr++;
139
140 if (!find_macro(ptr))
141 disabled = 1;
142 else
143 enabled = 1;
144 continue;
145 }
146
147 if (strncmp(buf, "#endif", 6) == 0) {
148 disabled = 0;
149 enabled = 1;
150 continue;
151 }
152
153 if (!disabled) {
154// printf("%s\n", buf);
155 fprintf(fpout, "%s\n", buf);
156 }
157 }
158 fclose(fp);
159
160 return 0;
161
162errout:
163 fclose(fp);
164 fprintf(stderr, "Error: cannot open output file\n");
165 exit(1);
166}