svn commit: r336973 - head/usr.sbin/config

Kyle Evans kevans at FreeBSD.org
Tue Jul 31 16:03:31 UTC 2018


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


More information about the svn-src-all mailing list