Commit Diff


commit - /dev/null
commit + 78832cdd90c45d36ed5d4133e151adc6688038f3
blob - /dev/null
blob + 35909084bb9ca70cf3f4c45139d2565f5a7cd72f (mode 644)
--- /dev/null
+++ Makefile
@@ -0,0 +1,44 @@
+include version.mk
+
+BIN = comment
+OBJ = $(BIN:=.o)
+SRC = $(BIN:=.c)
+MAN = $(BIN:=.1)
+
+PREFIX ?= $(DESTDIR)/usr/local
+MANPREFIX ?= $(PREFIX)/man
+
+bindir = $(PREFIX)/bin
+man1dir = $(MANPREFIX)/man1
+
+all: $(BIN)
+
+$(BIN): $(OBJ)
+	$(CC) -o $@ $(OBJ)
+
+.c.o:
+	$(CC) -std=c99 -pedantic $(CFLAGS) $(CPPFLAGS) -c $<
+
+install: all
+	mkdir -p $(bindir)
+	install -m 755 $(BIN) $(bindir)
+	mkdir -p $(man1dir)
+	install -m 644 $(MAN) $(man1dir)
+
+uninstall:
+	cd $(bindir) && rm -f $(BIN)
+	cd $(man1dir) && rm -f $(MAN)
+
+clean:
+	-rm -rf $(BIN) $(OBJ) comment$(V) *.tar.gz *.core
+
+dist: clean
+	mkdir comment$(V)
+	cp $(SRC) $(MAN) README Makefile version.mk comment$(V)
+	tar cf - comment$(V) | gzip >comment$(V).tar.gz
+	rm -rf comment$(V)
+
+tags: $(SRC)
+	ctags $(SRC)
+
+.PHONY: all install uninstall clean dist
blob - /dev/null
blob + de1d697658bfc7619dbac1d825f4b3cb03bb8584 (mode 644)
--- /dev/null
+++ README
@@ -0,0 +1,11 @@
+comment is a small preprocessor for stripping comments from input. It
+is useful for languages that don't support comments on their own. comment
+supports both block comments and single-line comment. Block comments do not
+nest. By default C-like block comment delimiters (/* comment */) and
+C++-like single-line comments (// comment) are used, but any other
+string may be specified instead.
+
+To install, run `make', followed by `make install' (possibly as root):
+
+	$ make
+	# make install
blob - /dev/null
blob + e9fe3a327f89743d7c8ab25c3db8913219e4acfd (mode 644)
--- /dev/null
+++ comment.1
@@ -0,0 +1,60 @@
+.Dd August 4, 2024
+.Dt COMMENT 1
+.Os
+.Sh NAME
+.Nm comment
+.Nd strip comments from input
+.Sh SYNOPSIS
+.Nm
+.Op Fl b Ar beginstring
+.Op Fl e Ar endstring
+.Op Fl s Ar singlestring
+.Op Ar
+.Sh DESCRIPTION
+The utility
+.Nm
+reads files sequentially, writing them to standard output with comments
+stripped.
+If
+.Ar file
+is a single dash
+.Pq Sq -
+or absent,
+.Nm
+reads from standard input.
+.Pp
+Both block comments and single-line comments are supported.
+The strings
+.Dq /*
+starts a block comment,
+.Dq */
+ends a block comment and
+.Dq //
+starts a single-line comment,
+unless specified otherwise.
+Block comments do not nest.
+.Pp
+The options are as follows:
+.Bl -tag -width Ds
+.It Fl b Ar beginstring
+Use
+.Ar beginstring
+to start block comments.
+If empty,
+no block comments are stripped.
+.It Fl e Ar endstring
+Use
+.Ar endstring
+to end block comments.
+If empty,
+block comments go until the end of the input.
+.It Fl s Ar singlestring
+Use
+.Ar singlestring
+to begin single-line comments.
+If empty, no single-line comments are stripped.
+.El
+.Sh EXIT STATUS
+.Ex -std
+.Sh AUTHORS
+.An Alexander Arkhipov Aq Mt aa@manpager.org .
blob - /dev/null
blob + 1d65a42b4bbe0cfb942749f89033efe2ee9e398e (mode 644)
--- /dev/null
+++ comment.c
@@ -0,0 +1,135 @@
+/*
+ * Copyright (c) 2024 Alexander Arkhipov <aa@manpager.org>
+ *
+ * Permission to use, copy, modify, and distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+#include <errno.h>
+#include <stdbool.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+/* default values */
+#define BEGIN	"/*"
+#define END	"*/"
+#define SINGLE	"//"
+
+/* strings initialising/ending comments and ... */
+static char	*begin = BEGIN;
+static char	*end = END;
+static char	*single = SINGLE;
+/* ... their lengths */
+static int	 beginlen;
+static int	 endlen;
+static int	 singlelen;
+
+static char 	*progname; /* for warnings/errors */
+
+/* program exit status */
+static int	status = 0;
+
+/* commentfile - strip comments from the file filename (or stdin, if "-") */
+static void
+commentfile(char *filename)
+{
+	FILE	*fp;
+	char	*line = NULL;
+	size_t	 linesz = 0;
+	ssize_t	 linelen;
+	bool	 inblockc = false;
+
+	if (strcmp(filename, "-") == 0)
+		fp = stdin;
+	else if (!(fp = fopen(filename, "r"))) {
+		status++;
+		fprintf(stderr, "%s: couldn't open %s: %s\n", progname,
+		    filename, strerror(errno));
+		return;
+	}
+
+	while ((linelen = getline(&line, &linesz, fp)) != -1)
+		for (int i = 0; line[i] != '\0'; i++) {
+			if (inblockc) {
+				if (endlen > 0 &&
+				    strncmp(line+i, end, endlen) == 0) {
+					inblockc = false;
+					i += endlen-1;
+				}
+				continue;
+			}
+
+			if (beginlen > 0 &&
+			    strncmp(line+i, begin, beginlen) == 0) {
+				inblockc = true;
+				i += beginlen-1;
+			} else if (singlelen > 0 &&
+			    strncmp(line+i, single, singlelen) == 0) {
+				putchar('\n');
+				break;
+			} else
+				putchar(line[i]);
+		}
+
+	free(line);
+	if (ferror(fp)) {
+		fprintf(stderr, "an error occured reading %s\n", filename);
+		status++;
+	}
+}
+
+/* comment - strip comments from files or stdin */
+int
+main(int argc, char *argv[])
+{
+	int ch;
+
+	progname = argv[0];
+
+	while ((ch = getopt(argc, argv, "b:e:s:")) != -1)
+		switch (ch) {
+		case 'b':
+			begin = optarg;
+			break;
+		case 'e':
+			end = optarg;
+			break;
+		case 's':
+			single = optarg;
+			break;
+		default:
+			fprintf(stderr,
+"usage: comment [-b begin] [-e end] [-s single] [file ...]\n");
+			return 1;
+		}
+	argc -= optind;
+	argv += optind;
+
+	if (strchr(begin, '\n') || strchr(end, '\n') || strchr(single, '\n')) {
+		fprintf(stderr, "the -bes arguments cannot contain newlines\n");
+		return 1;
+	}
+
+	singlelen = strlen(single);
+	beginlen = strlen(begin);
+	endlen = strlen(end);
+
+	if (argc > 0)
+		for (int i = 0; i < argc; i++)
+			commentfile(argv[i]);
+	else
+		commentfile("-");
+
+	return status;
+}
blob - /dev/null
blob + f623384ddf344ff6e3d37f96317242f142b00d31 (mode 644)
--- /dev/null
+++ version.mk
@@ -0,0 +1 @@
+V = 0.1