Commit 9e6e8e60a533dbed09b9a531eab773b7669a993f

Stefan Sperling 2020-08-02T16:38:51

got parse.y tweaks Rename things to better match their intended purpose. Tweak namespacing. Add a function for freeing config items that are dynamically allocated. Add 'struct gotconfig' to make room for potential global settings we might want to add later, instead of implementing just a list of remote repositories. ok tracey

diff --git a/got/got.h b/got/got.h
index e8f3306..985c532 100644
--- a/got/got.h
+++ b/got/got.h
@@ -14,19 +14,25 @@
  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  */
 
-struct got_config_list_entry {
-	TAILQ_ENTRY(got_config_list_entry) entry;
-	const char	*remote;
-	const char	*repository;
-	const char	*server;
-	const char	*protocol;
-	const char	*user;
+struct gotconfig_remote_repo {
+	TAILQ_ENTRY(gotconfig_remote_repo) entry;
+	char	*name;
+	char	*repository;
+	char	*server;
+	char	*protocol;
+	char	*user;
+};
+TAILQ_HEAD(gotconfig_remote_repo_list, gotconfig_remote_repo);
+
+struct gotconfig {
+	struct gotconfig_remote_repo_list remotes;
+	int nremotes;
 };
-TAILQ_HEAD(got_config_list, got_config_list_entry);
 
 /*
  * Parse individual gotconfig repository files
- * Load got_config_list_entry struct and insert to got_config_list TAILQ
  */
-const struct got_error* parse_got_config(struct got_config_list **,
-    char *filename);
+const struct got_error* gotconfig_parse(struct gotconfig **,
+    const char *filename);
+
+void gotconfig_free(struct gotconfig *);
diff --git a/got/parse.y b/got/parse.y
index 8362222..c89d4f3 100644
--- a/got/parse.y
+++ b/got/parse.y
@@ -84,9 +84,9 @@ int	 symset(const char *, const char *, int);
 char	*symget(const char *);
 
 const struct got_error* gerror = NULL;
-struct got_config_list_entry *gotconfig;
-struct got_config_list got_config_list;
-static const struct got_error*	new_remote(struct got_config_list_entry **);
+struct gotconfig_remote_repo *remote;
+struct gotconfig gotconfig;
+static const struct got_error* new_remote(struct gotconfig_remote_repo **);
 
 typedef struct {
 	union {
@@ -113,8 +113,8 @@ remoteopts2	: remoteopts2 remoteopts1 nl
 	    	| remoteopts1 optnl
 		;
 remoteopts1	: REPOSITORY STRING {
-	    		gotconfig->repository = strdup($2);
-			if (gotconfig->repository == NULL) {
+	    		remote->repository = strdup($2);
+			if (remote->repository == NULL) {
 				free($2);
 				yyerror("strdup");
 				YYERROR;
@@ -122,8 +122,8 @@ remoteopts1	: REPOSITORY STRING {
 			free($2);
 	    	}
 	    	| SERVER STRING {
-	    		gotconfig->server = strdup($2);
-			if (gotconfig->server == NULL) {
+	    		remote->server = strdup($2);
+			if (remote->server == NULL) {
 				free($2);
 				yyerror("strdup");
 				YYERROR;
@@ -131,8 +131,8 @@ remoteopts1	: REPOSITORY STRING {
 			free($2);
 		}
 		| PROTOCOL STRING {
-	    		gotconfig->protocol = strdup($2);
-			if (gotconfig->protocol == NULL) {
+	    		remote->protocol = strdup($2);
+			if (remote->protocol == NULL) {
 				free($2);
 				yyerror("strdup");
 				YYERROR;
@@ -140,8 +140,8 @@ remoteopts1	: REPOSITORY STRING {
 			free($2);
 		}
 		| USER STRING {
-	    		gotconfig->user = strdup($2);
-			if (gotconfig->user == NULL) {
+	    		remote->user = strdup($2);
+			if (remote->user == NULL) {
 				free($2);
 				yyerror("strdup");
 				YYERROR;
@@ -152,21 +152,22 @@ remoteopts1	: REPOSITORY STRING {
 remote		: REMOTE STRING {
 			static const struct got_error* error;
 
-			error = new_remote(&gotconfig);
+			error = new_remote(&remote);
 			if (error) {
 				free($2);
 				yyerror("%s", error->msg);
 				YYERROR;
 			}
-			gotconfig->remote = strdup($2);
-			if (gotconfig->remote == NULL) {
+			remote->name = strdup($2);
+			if (remote->name == NULL) {
 				free($2);
 				yyerror("strdup");
 				YYERROR;
 			}
 			free($2);
 		} '{' optnl remoteopts2 '}' {
-			TAILQ_INSERT_TAIL(&got_config_list, gotconfig, entry);
+			TAILQ_INSERT_TAIL(&gotconfig.remotes, remote, entry);
+			gotconfig.nremotes++;
 		}
 		;
 optnl		: '\n' optnl
@@ -526,11 +527,12 @@ pushfile(struct file **nfile, const char *name)
 }
 
 static const struct got_error*
-new_remote(struct got_config_list_entry **gotconfig) {
-	const struct got_error* error = NULL;
+new_remote(struct gotconfig_remote_repo **remote)
+{
+	const struct got_error *error = NULL;
 
-	if (((*gotconfig) = calloc(1, sizeof(struct got_config_list_entry))) ==
-	    NULL)
+	*remote = calloc(1, sizeof(**remote));
+	if (*remote == NULL)
 	    error = got_error_from_errno("calloc");
 	return error;
 }
@@ -550,11 +552,11 @@ popfile(void)
 }
 
 const struct got_error*
-parse_got_config(struct got_config_list **conf_list, char *filename)
+gotconfig_parse(struct gotconfig **conf, const char *filename)
 {
 	struct sym	*sym, *next;
 
-	*conf_list = NULL;
+	*conf = NULL;
 
 	/*
 	 * We don't require that gotconfig exists
@@ -564,11 +566,10 @@ parse_got_config(struct got_config_list **conf_list, char *filename)
 	if (gerror && gerror->code == GOT_ERR_NO_CONFIG_FILE) {
 		gerror = NULL;
 		goto done;
-	}
-	else if (gerror)
+	} else if (gerror)
 		return gerror;
 
-	TAILQ_INIT(&got_config_list);
+	TAILQ_INIT(&gotconfig.remotes);
 	topfile = file;
 
 	yyparse();
@@ -584,10 +585,27 @@ parse_got_config(struct got_config_list **conf_list, char *filename)
 		}
 	}
 done:
-	*conf_list = &got_config_list;
+	*conf = &gotconfig;
 	return gerror;
 }
 
+void
+gotconfig_free(struct gotconfig *conf)
+{
+	struct gotconfig_remote_repo *remote;
+
+	while (!TAILQ_EMPTY(&conf->remotes)) {
+		remote = TAILQ_FIRST(&conf->remotes);
+		TAILQ_REMOVE(&conf->remotes, remote, entry);
+		free(remote->name);
+		free(remote->repository);
+		free(remote->server);
+		free(remote->protocol);
+		free(remote->user);
+		free(remote);
+	}
+}
+
 int
 symset(const char *nam, const char *val, int persist)
 {