Skip site navigation (1)Skip section navigation (2)
Date:      Tue, 31 Jul 2018 16:03:30 +0000 (UTC)
From:      Kyle Evans <kevans@FreeBSD.org>
To:        src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org
Subject:   svn commit: r336973 - head/usr.sbin/config
Message-ID:  <201807311603.w6VG3UiA078682@repo.freebsd.org>

next in thread | raw e-mail | index | archive | help
Author: kevans
Date: Tue Jul 31 16:03:30 2018
New Revision: 336973
URL: https://svnweb.freebsd.org/changeset/base/336973

Log:
  config(8): Strip comments from env lines
  
  Consolidates the small bits of logic required for preprocessing a line
  before inclusion into a file or nvlist.

Modified:
  head/usr.sbin/config/mkmakefile.c

Modified: head/usr.sbin/config/mkmakefile.c
==============================================================================
--- head/usr.sbin/config/mkmakefile.c	Tue Jul 31 15:25:03 2018	(r336972)
+++ head/usr.sbin/config/mkmakefile.c	Tue Jul 31 16:03:30 2018	(r336973)
@@ -65,6 +65,7 @@ static void do_before_depend(FILE *);
 static int opteq(const char *, const char *);
 static void read_files(void);
 static void sanitize_envline(char *result, const char *src);
+static bool preprocess(char *line, char *result);
 static void process_into_file(char *line, FILE *ofp);
 static void process_into_nvlist(char *line, nvlist_t *nvl);
 static void dump_nvlist(nvlist_t *nvl, FILE *ofp);
@@ -243,32 +244,44 @@ sanitize_envline(char *result, const char *src)
 	*dst = 0;
 }
 
+/*
+ * Returns true if the caller may use the string.
+ */
+static bool
+preprocess(char *line, char *result)
+{
+
+	char result[BUFSIZ], *s;
+
+	/* Strip any comments */
+	if ((s = strchr(line, '#')) != NULL)
+		*s = '\0';
+	sanitize_envline(result, line);
+	/* Return true if it's non-empty */
+	return (*result != '\0');
+}
+
 static void
 process_into_file(char *line, FILE *ofp)
 {
 	char result[BUFSIZ];
 
-	sanitize_envline(result, line);
-	/* anything left? */
-	if (*result == '\0')
-		return;
-	fprintf(ofp, "\"%s\\0\"\n", result);
+	if (preprocess(line, result))
+		fprintf(ofp, "\"%s\\0\"\n", result);
 }
 
 static void
 process_into_nvlist(char *line, nvlist_t *nvl)
 {
-	char result[BUFSIZ], *s;
+	char result[BUFSIZ];
 
-	sanitize_envline(result, line);
-	/* anything left? */
-	if (*result == '\0')
-		return;
-	s = strchr(result, '=');
-	*s = 0;
-	if (nvlist_exists(nvl, result))
-		nvlist_free(nvl, result);
-	nvlist_add_string(nvl, result, s + 1);
+	if (preprocess(line, result)) {
+		s = strchr(result, '=');
+		*s = '\0';
+		if (nvlist_exists(nvl, result))
+			nvlist_free(nvl, result);
+		nvlist_add_string(nvl, result, s + 1);
+	}
 }
 
 static void



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?201807311603.w6VG3UiA078682>