Index | Thread | Search

From:
Mikolaj Kucharski <mikolaj@kucharski.name>
Subject:
Skip empty lines and comments in a disklabel template file
To:
tech@openbsd.org
Date:
Tue, 3 Nov 2026 03:18:02 +0000

Download raw body.

Thread
  • Mikolaj Kucharski:

    Skip empty lines and comments in a disklabel template file

Hi.

I have few disklabel template files, which I would like to comment, but
disklabel -T file doesn't support comments, hence the below diff.


Index: sbin/disklabel/editor.c
===================================================================
RCS file: /cvs/src/sbin/disklabel/editor.c,v
diff -u -p -r1.428 editor.c
--- sbin/disklabel/editor.c	15 Mar 2026 15:16:12 -0000	1.428
+++ sbin/disklabel/editor.c	3 Nov 2026 03:11:50 -0000
@@ -1863,7 +1863,7 @@ parse_autotable(char *filename)
 	FILE	*cfile;
 	size_t	 linesize = 0;
 	char	*line = NULL, *buf, *t;
-	uint	 idx = 0, pctsum = 0;
+	uint	 idx = 0, lineno = 0, pctsum = 0;
 	struct space_allocation *sa;
 
 	if (strcmp(filename, "-") == 0)
@@ -1875,6 +1875,15 @@ parse_autotable(char *filename)
 	alloc_table_nitems = 1;
 
 	while (getline(&line, &linesize, cfile) != -1) {
+		lineno++;
+
+		/* Skip empty lines and comments. */
+		buf = line;
+		while (isspace((unsigned char)*buf))
+			buf++;
+		if (*buf == '\0' || *buf == '#')
+			continue;
+
 		if ((alloc_table[0].table = reallocarray(alloc_table[0].table,
 		    idx + 1, sizeof(*sa))) == NULL)
 			err(1, NULL);
@@ -1882,20 +1891,19 @@ parse_autotable(char *filename)
 		memset(sa, 0, sizeof(*sa));
 		idx++;
 
-		buf = line;
 		if ((sa->mp = get_token(&buf)) == NULL ||
 		    (sa->mp[0] != '/' && strcasecmp(sa->mp, "swap") &&
 		    strcasecmp(sa->mp, "raid")))
-			errx(1, "%s: parse error on line %u", filename, idx);
+			errx(1, "%s: parse error on line %u", filename, lineno);
 		if ((t = get_token(&buf)) == NULL ||
 		    parse_sizerange(t, &sa->minsz, &sa->maxsz) == -1)
-			errx(1, "%s: parse error on line %u", filename, idx);
+			errx(1, "%s: parse error on line %u", filename, lineno);
 		if ((t = get_token(&buf)) != NULL &&
 		    parse_pct(t, &sa->rate) == -1)
-			errx(1, "%s: parse error on line %u", filename, idx);
+			errx(1, "%s: parse error on line %u", filename, lineno);
 		if (sa->minsz > sa->maxsz)
 			errx(1, "%s: min size > max size on line %u", filename,
-			    idx);
+			    lineno);
 		pctsum += sa->rate;
 	}
 	if (pctsum > 100)
Index: regress/sbin/disklabel/Makefile
===================================================================
RCS file: /cvs/src/regress/sbin/disklabel/Makefile,v
diff -u -p -r1.8 Makefile
--- regress/sbin/disklabel/Makefile	12 Oct 2023 16:59:23 -0000	1.8
+++ regress/sbin/disklabel/Makefile	3 Nov 2026 03:11:50 -0000
@@ -106,6 +106,27 @@ update: $s-update
 
 .endfor
 
+# Test that -T templates correctly ignore empty lines, whitespace-only
+# lines and comments (including those with leading whitespace).
+REGRESS_TARGETS +=	run-template-comments
+
+run-template-comments: ${DISKLABEL}
+	rm -f diskimage template-comments.out
+	dd if=/dev/zero of=diskimage bs=1m count=1 seek=4096 status=none
+	${SUDO} vnconfig diskimage >vnd
+	${SUDO} fdisk ${FDISKOPT} $$(<vnd) >/dev/null
+	# Must succeed (no parse error)
+	${SUDO} ${DISKLABEL} -n -A -T ${.CURDIR}/template-comments $$(<vnd) \
+		>template-comments.out
+	# Sanity checks
+	grep -q '4.2BSD.*# /$$' template-comments.out
+	grep -q '[[:space:]]swap[[:space:]]' template-comments.out
+	grep -q '4.2BSD.*# /home$$' template-comments.out
+	${SUDO} vnconfig -u $$(<vnd) || true
+	@echo "template-comments: ok"
+
+CLEANFILES +=	template-comments.out
+
 REGRESS_CLEANUP +=	cleanup
 cleanup:
 	-${SUDO} vnconfig -u $$(<vnd)
Index: regress/sbin/disklabel/template-comments
===================================================================
RCS file: regress/sbin/disklabel/template-comments
diff -N regress/sbin/disklabel/template-comments
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ regress/sbin/disklabel/template-comments	3 Nov 2026 03:11:50 -0000
@@ -0,0 +1,25 @@
+# This is a comment at the beginning of the file
+
+# Another comment
+
+	# comment with leading tab
+   # comment with leading spaces
+
+# empty lines and pure whitespace lines follow
+
+
+   	   
+
+# now the real content
+
+/		1G
+swap		512M-1G 10%
+
+# comment between entries
+	# another indented comment
+
+/home		*
+
+# trailing comment
+# and empty lines at the end
+


-- 
Regards,
 Mikolaj