git: 2ffdc21324bd - main - config: make changes to allow some parts to build as C++

From: Kyle Evans <kevans_at_FreeBSD.org>
Date: Wed, 08 Feb 2023 06:10:23 UTC
The branch main has been updated by kevans:

URL: https://cgit.FreeBSD.org/src/commit/?id=2ffdc21324bd1772fa2f40efed3987afecabf5cb

commit 2ffdc21324bd1772fa2f40efed3987afecabf5cb
Author:     Kyle Evans <kevans@FreeBSD.org>
AuthorDate: 2023-02-08 06:02:56 +0000
Commit:     Kyle Evans <kevans@FreeBSD.org>
CommitDate: 2023-02-08 06:09:10 +0000

    config: make changes to allow some parts to build as C++
    
    Highlights:
    - Avoid keywords (this, not) as variable names
    - Move yyparse into config.h with other declarations
    - All declarations in config.h are assumed guilty until proven innocent
    - Some const-correctness
    - Casting malloc/calloc returns
    
    Note that we're not building any C++ here yet, this will be introduced
    in other commits to replace some of the lib dependencies.  Reducing the
    number of FreeBSD-specific dependencies we have reduces some friction
    for building our bootstrap tools independently in other environments.
    
    Reviewed by:    imp
    Sponsored by:   Klara, Inc.
    Sponsored by:   NetApp, Inc.
    Differential Revision:  https://reviews.freebsd.org/D38274
---
 usr.sbin/config/config.h     |  6 ++++++
 usr.sbin/config/main.c       | 19 ++++++++++---------
 usr.sbin/config/mkmakefile.c | 44 ++++++++++++++++++++++----------------------
 usr.sbin/config/mkoptions.c  | 28 ++++++++++++++--------------
 4 files changed, 52 insertions(+), 45 deletions(-)

diff --git a/usr.sbin/config/config.h b/usr.sbin/config/config.h
index 79d0a788bae7..7d97d66979e2 100644
--- a/usr.sbin/config/config.h
+++ b/usr.sbin/config/config.h
@@ -35,12 +35,15 @@
 /*
  * Config.
  */
+#include <sys/cdefs.h>	/* __BEGIN_DECLS/__END_DECLS */
 #include <sys/types.h>
 #include <sys/queue.h>
 #include <stdbool.h>
 #include <stdlib.h>
 #include <string.h>
 
+__BEGIN_DECLS
+
 struct cfgfile {
 	STAILQ_ENTRY(cfgfile)	cfg_next;
 	char	*cfg_path;
@@ -190,6 +193,7 @@ char	*raisestr(char *);
 void	remember(const char *);
 void	moveifchanged(const char *, const char *);
 int	yylex(void);
+int	yyparse(void);
 void	options(void);
 void	makefile(void);
 void	makeenv(void);
@@ -218,5 +222,7 @@ extern int	versreq;
 extern char *PREFIX;		/* Config file name - for error messages */
 extern char srcdir[];		/* root of the kernel source tree */
 
+__END_DECLS;
+
 #define eq(a,b)	(!strcmp(a,b))
 #define ns(s)	strdup(s)
diff --git a/usr.sbin/config/main.c b/usr.sbin/config/main.c
index 60bafb0b46ba..988c296d97ce 100644
--- a/usr.sbin/config/main.c
+++ b/usr.sbin/config/main.c
@@ -105,10 +105,9 @@ static void cleanheaders(char *);
 static void kernconfdump(const char *);
 static void badversion(void);
 static void checkversion(void);
-extern int yyparse(void);
 
 struct hdr_list {
-	char *h_name;
+	const char *h_name;
 	struct hdr_list *h_next;
 } *htab;
 
@@ -620,10 +619,12 @@ moveifchanged(const char *from_name, const char *to_name)
 	tsize = (size_t)from_sb.st_size;
 
 	if (!changed) {
-		p = mmap(NULL, tsize, PROT_READ, MAP_SHARED, from_fd, (off_t)0);
+		p = (char *)mmap(NULL, tsize, PROT_READ, MAP_SHARED, from_fd,
+		    (off_t)0);
 		if (p == MAP_FAILED)
 			err(EX_OSERR, "mmap %s", from_name);
-		q = mmap(NULL, tsize, PROT_READ, MAP_SHARED, to_fd, (off_t)0);
+		q = (char *)mmap(NULL, tsize, PROT_READ, MAP_SHARED, to_fd,
+		    (off_t)0);
 		if (q == MAP_FAILED)
 			err(EX_OSERR, "mmap %s", to_name);
 
@@ -688,7 +689,7 @@ cleanheaders(char *p)
 void
 remember(const char *file)
 {
-	char *s;
+	const char *s;
 	struct hdr_list *hl;
 
 	if ((s = strrchr(file, '/')) != NULL)
@@ -697,16 +698,16 @@ remember(const char *file)
 		s = ns(file);
 
 	if (strchr(s, '_') && strncmp(s, "opt_", 4) != 0) {
-		free(s);
+		free(__DECONST(char *, s));
 		return;
 	}
 	for (hl = htab; hl != NULL; hl = hl->h_next) {
 		if (eq(s, hl->h_name)) {
-			free(s);
+			free(__DECONST(char *, s));
 			return;
 		}
 	}
-	hl = calloc(1, sizeof(*hl));
+	hl = (struct hdr_list *)calloc(1, sizeof(*hl));
 	if (hl == NULL)
 		err(EXIT_FAILURE, "calloc");
 	hl->h_name = s;
@@ -740,7 +741,7 @@ kernconfdump(const char *file)
 	if (fp == NULL)
 		err(EXIT_FAILURE, "fdopen() failed");
 	osz = 1024;
-	o = calloc(1, osz);
+	o = (char *)calloc(1, osz);
 	if (o == NULL)
 		err(EXIT_FAILURE, "Couldn't allocate memory");
 	/* ELF note section header. */
diff --git a/usr.sbin/config/mkmakefile.c b/usr.sbin/config/mkmakefile.c
index 4f3bd13ffca3..9f103c0e5214 100644
--- a/usr.sbin/config/mkmakefile.c
+++ b/usr.sbin/config/mkmakefile.c
@@ -392,9 +392,9 @@ read_file(char *fname)
 	struct file_list *tp;
 	struct device *dp;
 	struct opt *op;
-	char *wd, *this, *compilewith, *depends, *clean, *warning;
+	char *wd, *rfile, *compilewith, *depends, *clean, *warning;
 	const char *objprefix;
-	int compile, match, nreqs, std, filetype, not,
+	int compile, match, nreqs, std, filetype, negate,
 	    imp_rule, no_ctfconvert, no_obj, before_depend, nowerror;
 
 	fp = fopen(fname, "r");
@@ -434,13 +434,13 @@ next:
 			;
 		goto next;
 	}
-	this = ns(wd);
+	rfile = ns(wd);
 	wd = get_word(fp);
 	if (wd == (char *)EOF)
 		return;
 	if (wd == NULL)
-		errout("%s: No type for %s.\n", fname, this);
-	tp = fl_lookup(this);
+		errout("%s: No type for %s.\n", fname, rfile);
+	tp = fl_lookup(rfile);
 	compile = 0;
 	match = 1;
 	nreqs = 0;
@@ -454,25 +454,25 @@ next:
 	no_obj = 0;
 	before_depend = 0;
 	nowerror = 0;
-	not = 0;
+	negate = 0;
 	filetype = NORMAL;
 	objprefix = "";
 	if (eq(wd, "standard"))
 		std = 1;
 	else if (!eq(wd, "optional"))
 		errout("%s: \"%s\" %s must be optional or standard\n",
-		    fname, wd, this);
+		    fname, wd, rfile);
 	for (wd = get_word(fp); wd; wd = get_word(fp)) {
 		if (wd == (char *)EOF)
 			return;
 		if (eq(wd, "!")) {
-			not = 1;
+			negate = 1;
 			continue;
 		}
 		if (eq(wd, "|")) {
 			if (nreqs == 0)
 				errout("%s: syntax error describing %s\n",
-				       fname, this);
+				       fname, rfile);
 			compile += match;
 			match = 1;
 			nreqs = 0;
@@ -491,7 +491,7 @@ next:
 				errout("%s: alternate rule required when "
 				       "\"no-implicit-rule\" is specified for"
 				       " %s.\n",
-				       fname, this);
+				       fname, rfile);
 			imp_rule++;
 			continue;
 		}
@@ -503,7 +503,7 @@ next:
 			wd = get_quoted_word(fp);
 			if (wd == (char *)EOF || wd == NULL)
 				errout("%s: %s missing dependency string.\n",
-				       fname, this);
+				       fname, rfile);
 			depends = ns(wd);
 			continue;
 		}
@@ -511,7 +511,7 @@ next:
 			wd = get_quoted_word(fp);
 			if (wd == (char *)EOF || wd == NULL)
 				errout("%s: %s missing clean file list.\n",
-				       fname, this);
+				       fname, rfile);
 			clean = ns(wd);
 			continue;
 		}
@@ -519,7 +519,7 @@ next:
 			wd = get_quoted_word(fp);
 			if (wd == (char *)EOF || wd == NULL)
 				errout("%s: %s missing compile command string.\n",
-				       fname, this);
+				       fname, rfile);
 			compilewith = ns(wd);
 			continue;
 		}
@@ -527,7 +527,7 @@ next:
 			wd = get_quoted_word(fp);
 			if (wd == (char *)EOF || wd == NULL)
 				errout("%s: %s missing warning text string.\n",
-				       fname, this);
+				       fname, rfile);
 			warning = ns(wd);
 			continue;
 		}
@@ -535,7 +535,7 @@ next:
 			wd = get_quoted_word(fp);
 			if (wd == (char *)EOF || wd == NULL)
 				errout("%s: %s missing object prefix string.\n",
-				       fname, this);
+				       fname, rfile);
 			objprefix = ns(wd);
 			continue;
 		}
@@ -554,10 +554,10 @@ next:
 		nreqs++;
 		if (std)
 			errout("standard entry %s has optional inclusion specifier %s!\n",
-			       this, wd);
+			       rfile, wd);
 		STAILQ_FOREACH(dp, &dtab, d_next)
 			if (eq(dp->d_name, wd)) {
-				if (not)
+				if (negate)
 					match = 0;
 				else
 					dp->d_done |= DEVDONE;
@@ -566,21 +566,21 @@ next:
 		SLIST_FOREACH(op, &opt, op_next)
 			if (op->op_value == 0 &&
 			    strcasecmp(op->op_name, wd) == 0) {
-				if (not)
+				if (negate)
 					match = 0;
 				goto nextparam;
 			}
-		match &= not;
+		match &= negate;
 nextparam:;
-		not = 0;
+		negate = 0;
 	}
 	compile += match;
 	if (compile && tp == NULL) {
 		if (std == 0 && nreqs == 0)
 			errout("%s: what is %s optional on?\n",
-			       fname, this);
+			       fname, rfile);
 		tp = new_fent();
-		tp->f_fn = this;
+		tp->f_fn = rfile;
 		tp->f_type = filetype;
 		if (filetype == LOCAL)
 			tp->f_srcprefix = "";
diff --git a/usr.sbin/config/mkoptions.c b/usr.sbin/config/mkoptions.c
index bf611cdb12c3..b93e56e3a491 100644
--- a/usr.sbin/config/mkoptions.c
+++ b/usr.sbin/config/mkoptions.c
@@ -312,41 +312,41 @@ tooption(char *name)
 
 	
 static void
-check_duplicate(const char *fname, const char *this)
+check_duplicate(const char *fname, const char *chkopt)
 {
 	struct opt_list *po;
 
 	SLIST_FOREACH(po, &otab, o_next) {
-		if (eq(po->o_name, this)) {
+		if (eq(po->o_name, chkopt)) {
 			fprintf(stderr, "%s: Duplicate option %s.\n",
-			    fname, this);
+			    fname, chkopt);
 			exit(1);
 		}
 	}
 }
 
 static void
-insert_option(const char *fname, char *this, char *val)
+insert_option(const char *fname, char *optname, char *val)
 {
 	struct opt_list *po;
 
-	check_duplicate(fname, this);
+	check_duplicate(fname, optname);
 	po = (struct opt_list *) calloc(1, sizeof *po);
 	if (po == NULL)
 		err(EXIT_FAILURE, "calloc");
-	po->o_name = this;
+	po->o_name = optname;
 	po->o_file = val;
 	po->o_flags = 0;
 	SLIST_INSERT_HEAD(&otab, po, o_next);
 }
 
 static void
-update_option(const char *this, char *val, int flags)
+update_option(const char *optname, char *val, int flags)
 {
 	struct opt_list *po;
 
 	SLIST_FOREACH(po, &otab, o_next) {
-		if (eq(po->o_name, this)) {
+		if (eq(po->o_name, optname)) {
 			free(po->o_file);
 			po->o_file = val;
 			po->o_flags = flags;
@@ -364,7 +364,7 @@ static int
 read_option_file(const char *fname, int flags)
 {
 	FILE *fp;
-	char *wd, *this, *val;
+	char *wd, *optname, *val;
 	char genopt[MAXPATHLEN];
 
 	fp = fopen(fname, "r");
@@ -378,17 +378,17 @@ read_option_file(const char *fname, int flags)
 				continue;
 			continue;
 		}
-		this = ns(wd);
+		optname = ns(wd);
 		val = get_word(fp);
 		if (val == (char *)EOF)
 			return (1);
 		if (val == NULL) {
 			if (flags) {
 				fprintf(stderr, "%s: compat file requires two"
-				    " words per line at %s\n", fname, this);
+				    " words per line at %s\n", fname, optname);
 				exit(1);
 			}
-			char *s = ns(this);
+			char *s = ns(optname);
 			(void)snprintf(genopt, sizeof(genopt), "opt_%s.h",
 			    lower(s));
 			val = genopt;
@@ -396,9 +396,9 @@ read_option_file(const char *fname, int flags)
 		}
 		val = ns(val);
 		if (flags == 0)
-			insert_option(fname, this, val);
+			insert_option(fname, optname, val);
 		else
-			update_option(this, val, flags);
+			update_option(optname, val, flags);
 	}
 	(void)fclose(fp);
 	return (1);