preprocessor for man pages

This commit is contained in:
startx2017 2020-09-01 13:16:24 -04:00
parent 0ce2595ffe
commit f624e289c5
7 changed files with 175 additions and 7 deletions

4
.gitignore vendored
View file

@ -8,6 +8,7 @@
*.gcno
*.DS_Store
.directory
*.man
Makefile
autom4te.cache/
config.log
@ -35,7 +36,8 @@ src/fsec-optimize/fsec-optimize
src/fcopy/fcopy
src/fldd/fldd
src/fbuilder/fbuilder
etc/profstats
src/profstats/profstats
src/man/preproc
uids.h
seccomp
seccomp.debug

View file

@ -16,7 +16,7 @@ BUSYBOX_WORKAROUND=@BUSYBOX_WORKAROUND@
HAVE_SUID=@HAVE_SUID@
all: all_items man filters
APPS = src/firecfg/firecfg src/firejail/firejail src/firemon/firemon src/profstats/profstats
APPS = src/firecfg/firecfg src/firejail/firejail src/firemon/firemon src/profstats/profstats src/man/preproc
SBOX_APPS = src/faudit/faudit src/fbuilder/fbuilder src/ftee/ftee
SBOX_APPS_NON_DUMPABLE = src/fcopy/fcopy src/fldd/fldd src/fnet/fnet src/fnetfilter/fnetfilter
MYDIRS = src/lib
@ -36,8 +36,9 @@ mydirs: mydirs $(MYDIRS)
$(MYDIRS):
$(MAKE) -C $@
$(MANPAGES): $(wildcard src/man/*.txt)
./mkman.sh $(VERSION) src/man/$(basename $@).txt $@
$(MANPAGES): $(wildcard src/man/*.man)
./mkman.sh $(VERSION) src/man/$(basename $@).man $@
man: $(MANPAGES)

3
configure vendored
View file

@ -4170,7 +4170,7 @@ fi
ac_config_files="$ac_config_files mkdeb.sh"
ac_config_files="$ac_config_files Makefile src/common.mk src/lib/Makefile src/fcopy/Makefile src/fnet/Makefile src/firejail/Makefile src/fnetfilter/Makefile src/firemon/Makefile src/libtrace/Makefile src/libtracelog/Makefile src/firecfg/Makefile src/fbuilder/Makefile src/fsec-print/Makefile src/ftee/Makefile src/faudit/Makefile src/fseccomp/Makefile src/fldd/Makefile src/libpostexecseccomp/Makefile src/fsec-optimize/Makefile src/profstats/Makefile"
ac_config_files="$ac_config_files Makefile src/common.mk src/lib/Makefile src/fcopy/Makefile src/fnet/Makefile src/firejail/Makefile src/fnetfilter/Makefile src/firemon/Makefile src/libtrace/Makefile src/libtracelog/Makefile src/firecfg/Makefile src/fbuilder/Makefile src/fsec-print/Makefile src/ftee/Makefile src/faudit/Makefile src/fseccomp/Makefile src/fldd/Makefile src/libpostexecseccomp/Makefile src/fsec-optimize/Makefile src/profstats/Makefile src/man/Makefile"
cat >confcache <<\_ACEOF
# This file is a shell script that caches the results of configure
@ -4900,6 +4900,7 @@ do
"src/libpostexecseccomp/Makefile") CONFIG_FILES="$CONFIG_FILES src/libpostexecseccomp/Makefile" ;;
"src/fsec-optimize/Makefile") CONFIG_FILES="$CONFIG_FILES src/fsec-optimize/Makefile" ;;
"src/profstats/Makefile") CONFIG_FILES="$CONFIG_FILES src/profstats/Makefile" ;;
"src/man/Makefile") CONFIG_FILES="$CONFIG_FILES src/man/Makefile" ;;
*) as_fn_error $? "invalid argument: \`$ac_config_target'" "$LINENO" 5;;
esac

View file

@ -199,7 +199,7 @@ AC_CONFIG_FILES([mkdeb.sh], [chmod +x mkdeb.sh])
AC_OUTPUT(Makefile src/common.mk src/lib/Makefile src/fcopy/Makefile src/fnet/Makefile src/firejail/Makefile src/fnetfilter/Makefile \
src/firemon/Makefile src/libtrace/Makefile src/libtracelog/Makefile src/firecfg/Makefile src/fbuilder/Makefile src/fsec-print/Makefile \
src/ftee/Makefile src/faudit/Makefile src/fseccomp/Makefile src/fldd/Makefile src/libpostexecseccomp/Makefile src/fsec-optimize/Makefile \
src/profstats/Makefile)
src/profstats/Makefile src/man/Makefile)
echo
echo "Configuration options:"

View file

@ -32,7 +32,8 @@ BINOBJS = $(foreach file, $(OBJS), $file)
CFLAGS = @CFLAGS@
CFLAGS += -ggdb $(HAVE_FATAL_WARNINGS) -O2 -DVERSION='"$(VERSION)"' $(HAVE_GCOV)
CFLAGS += -DPREFIX='"$(prefix)"' -DSYSCONFDIR='"$(sysconfdir)/firejail"' -DLIBDIR='"$(libdir)"' -DBINDIR='"$(bindir)"'
CFLAGS += $(HAVE_X11) $(HAVE_PRIVATE_HOME) $(HAVE_APPARMOR) $(HAVE_OVERLAYFS) $(HAVE_FIRETUNNEL) $(HAVE_GLOBALCFG) $(HAVE_CHROOT) $(HAVE_NETWORK) $(HAVE_USERNS) $(HAVE_FILE_TRANSFER) $(HAVE_WHITELIST) $(HAVE_SELINUX)
MANFLAGS = $(HAVE_X11) $(HAVE_PRIVATE_HOME) $(HAVE_APPARMOR) $(HAVE_OVERLAYFS) $(HAVE_FIRETUNNEL) $(HAVE_GLOBALCFG) $(HAVE_CHROOT) $(HAVE_NETWORK) $(HAVE_USERNS) $(HAVE_FILE_TRANSFER) $(HAVE_WHITELIST) $(HAVE_SELINUX)
CFLAGS += $(MANFLAGS)
CFLAGS += -fstack-protector-all -D_FORTIFY_SOURCE=2 -fPIE -pie -Wformat -Wformat-security
LDFLAGS += -pie -Wl,-z,relro -Wl,-z,now -lpthread
EXTRA_LDFLAGS +=@EXTRA_LDFLAGS@

17
src/man/Makefile.in Normal file
View file

@ -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

146
src/man/preproc.c Normal file
View file

@ -0,0 +1,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);
}