From f624e289c5d4465d09219305f907f1f39ca23f5e Mon Sep 17 00:00:00 2001 From: startx2017 Date: Tue, 1 Sep 2020 13:16:24 -0400 Subject: preprocessor for man pages --- src/man/Makefile.in | 17 ++++++ src/man/preproc.c | 146 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 163 insertions(+) create mode 100644 src/man/Makefile.in create mode 100644 src/man/preproc.c (limited to 'src/man') diff --git a/src/man/Makefile.in b/src/man/Makefile.in new file mode 100644 index 000000000..0180baee5 --- /dev/null +++ b/src/man/Makefile.in @@ -0,0 +1,17 @@ +all: firecfg.man firejail.man firejail-login.man firejail-users.man firejail-profile.man firemon.man + +include ../common.mk + +%.o : %.c $(H_FILE_LIST) + $(CC) $(CFLAGS) $(EXTRA_CFLAGS) $(INCLUDE) -c $< -o $@ + +preproc: $(OBJS) + $(CC) $(LDFLAGS) -o $@ $(OBJS) $(LIBS) $(EXTRA_LDFLAGS) + +%.man: %.txt preproc + ./preproc $(MANFLAGS) $< + +clean:; rm -fr *.o preproc *.gcov *.gcda *.gcno *.plist *.man alldone + +distclean: clean + rm -fr Makefile diff --git a/src/man/preproc.c b/src/man/preproc.c new file mode 100644 index 000000000..34a49d335 --- /dev/null +++ b/src/man/preproc.c @@ -0,0 +1,146 @@ +#include +#include +#include +#include + +#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); +} -- cgit v1.2.3-70-g09d2