Blob Blame History Raw
From d1586b86da52810014bd95e46fd5c86a458fca3e Mon Sep 17 00:00:00 2001
From: Robert Marshall <rmarshall@redhat.com>
Date: Tue, 13 Dec 2016 14:36:31 -0500
Subject: [PATCH 62/69] Add index constant definitions instead of open-coded
 values.

Added numeric constants NO_DEFAULT_ENTRY and FIRST_ENTRY_INDEX. This
clarifies the intent of various assignment operations throughout the
source file.

Related: rhbz#1285601
---
 grubby.c | 44 ++++++++++++++++++++++++--------------------
 1 file changed, 24 insertions(+), 20 deletions(-)

diff --git a/grubby.c b/grubby.c
index 344f1da..bece23a 100644
--- a/grubby.c
+++ b/grubby.c
@@ -130,6 +130,10 @@ struct singleEntry {
 #define NEED_END     (1 << 5)
 
 #define MAIN_DEFAULT	    (1 << 0)
+#define FIRST_ENTRY_INDEX    0	/* boot entry index value begin and increment
+				   from this initial value */
+#define NO_DEFAULT_ENTRY    -1	/* indicates that no specific default boot
+				   entry was set or currently exists */
 #define DEFAULT_SAVED       -2
 #define DEFAULT_SAVED_GRUB2 -3
 
@@ -1712,7 +1716,7 @@ static struct grubConfig *readConfig(const char *inName,
 						*end == ' ' || *end == '\t'))
 					end++;
 				if (*end)
-					cfg->defaultImage = -1;
+					cfg->defaultImage = NO_DEFAULT_ENTRY;
 			} else if (defaultLine->numElements == 3) {
 				char *value = defaultLine->elements[2].item;
 				while (*value && (*value == '"' ||
@@ -1725,7 +1729,7 @@ static struct grubConfig *readConfig(const char *inName,
 						*end == ' ' || *end == '\t'))
 					end++;
 				if (*end)
-					cfg->defaultImage = -1;
+					cfg->defaultImage = NO_DEFAULT_ENTRY;
 			}
 		} else if (cfi->defaultSupportSaved &&
 			   !strncmp(defaultLine->elements[1].item, "saved",
@@ -1735,7 +1739,7 @@ static struct grubConfig *readConfig(const char *inName,
 			cfg->defaultImage =
 			    strtol(defaultLine->elements[1].item, &end, 10);
 			if (*end)
-				cfg->defaultImage = -1;
+				cfg->defaultImage = NO_DEFAULT_ENTRY;
 		} else if (defaultLine->numElements >= 2) {
 			int i = 0;
 			while ((entry = findEntryByIndex(cfg, i))) {
@@ -1763,7 +1767,7 @@ static struct grubConfig *readConfig(const char *inName,
 			if (entry) {
 				cfg->defaultImage = i;
 			} else {
-				cfg->defaultImage = -1;
+				cfg->defaultImage = NO_DEFAULT_ENTRY;
 			}
 		}
 	} else if (cfg->cfi->defaultIsSaved && cfg->cfi->getEnv) {
@@ -1780,7 +1784,7 @@ static struct grubConfig *readConfig(const char *inName,
 				cfg->defaultImage = index;
 		}
 	} else {
-		cfg->defaultImage = 0;
+		cfg->defaultImage = FIRST_ENTRY_INDEX;
 	}
 
 	return cfg;
@@ -1800,7 +1804,7 @@ static void writeDefault(FILE * out, char *indent,
 		fprintf(out, "%sdefault%ssaved\n", indent, separator);
 	else if (cfg->cfi->defaultIsSaved) {
 		fprintf(out, "%sset default=\"${saved_entry}\"\n", indent);
-		if (cfg->defaultImage >= 0 && cfg->cfi->setEnv) {
+		if (cfg->defaultImage >= FIRST_ENTRY_INDEX && cfg->cfi->setEnv) {
 			char *title;
 			entry = findEntryByIndex(cfg, cfg->defaultImage);
 			line = getLineByType(LT_MENUENTRY, entry->lines);
@@ -1813,7 +1817,7 @@ static void writeDefault(FILE * out, char *indent,
 							 "saved_entry", title);
 			}
 		}
-	} else if (cfg->defaultImage > -1) {
+	} else if (cfg->defaultImage >= FIRST_ENTRY_INDEX) {
 		if (cfg->cfi->defaultIsIndex) {
 			if (cfg->cfi->defaultIsVariable) {
 				fprintf(out, "%sset default=\"%d\"\n", indent,
@@ -2515,7 +2519,7 @@ struct singleEntry *findTemplate(struct grubConfig *cfg, const char *prefix,
 				}
 			}
 		}
-	} else if (cfg->defaultImage > -1) {
+	} else if (cfg->defaultImage >= FIRST_ENTRY_INDEX) {
 		entry = findEntryByIndex(cfg, cfg->defaultImage);
 		if (entry && suitableImage(entry, prefix, skipRemoved, flags)) {
 			if (indexPtr)
@@ -2597,20 +2601,20 @@ void setDefaultImage(struct grubConfig *config, int isUserSpecifiedKernelPath,
 	int i, j;
 
 	if (newBootEntryIsDefault) {
-		config->defaultImage = 0;
+		config->defaultImage = FIRST_ENTRY_INDEX;
 		return;
 	} else if ((newDefaultBootEntryIndex >= 0) && config->cfi->defaultIsIndex) {
 		if (findEntryByIndex(config, newDefaultBootEntryIndex))
 			config->defaultImage = newDefaultBootEntryIndex;
 		else
-			config->defaultImage = -1;
+			config->defaultImage = NO_DEFAULT_ENTRY;
 		return;
 	} else if (defaultKernelPath) {
 		i = 0;
 		if (findEntryByPath(config, defaultKernelPath, prefix, &i)) {
 			config->defaultImage = i;
 		} else {
-			config->defaultImage = -1;
+			config->defaultImage = NO_DEFAULT_ENTRY;
 			return;
 		}
 	}
@@ -2622,7 +2626,7 @@ void setDefaultImage(struct grubConfig *config, int isUserSpecifiedKernelPath,
 		/* default is set to saved, we don't want to change it */
 		return;
 
-	if (config->defaultImage > -1)
+	if (config->defaultImage >= FIRST_ENTRY_INDEX)
 		entry = findEntryByIndex(config, config->defaultImage);
 	else
 		entry = NULL;
@@ -2639,7 +2643,7 @@ void setDefaultImage(struct grubConfig *config, int isUserSpecifiedKernelPath,
 				config->defaultImage--;
 		}
 	} else if (isUserSpecifiedKernelPath) {
-		config->defaultImage = 0;
+		config->defaultImage = FIRST_ENTRY_INDEX;
 	} else {
 		/* Either we just erased the default (or the default line was bad
 		 * to begin with) and didn't put a new one in. We'll use the first
@@ -2648,7 +2652,7 @@ void setDefaultImage(struct grubConfig *config, int isUserSpecifiedKernelPath,
 		    findTemplate(config, prefix, &config->defaultImage, 1,
 				 flags);
 		if (!newDefault)
-			config->defaultImage = -1;
+			config->defaultImage = NO_DEFAULT_ENTRY;
 	}
 }
 
@@ -5210,11 +5214,11 @@ int main(int argc, const char **argv)
 		struct singleEntry *entry;
 		char *rootspec;
 
-		if (config->defaultImage == -1)
+		if (config->defaultImage == NO_DEFAULT_ENTRY)
 			return 0;
 		if (config->defaultImage == DEFAULT_SAVED_GRUB2 &&
 		    cfi->defaultIsSaved)
-			config->defaultImage = 0;
+			config->defaultImage = FIRST_ENTRY_INDEX;
 		entry = findEntryByIndex(config, config->defaultImage);
 		if (!entry)
 			return 0;
@@ -5237,11 +5241,11 @@ int main(int argc, const char **argv)
 		struct singleLine *line;
 		struct singleEntry *entry;
 
-		if (config->defaultImage == -1)
+		if (config->defaultImage == NO_DEFAULT_ENTRY)
 			return 0;
 		if (config->defaultImage == DEFAULT_SAVED_GRUB2 &&
 		    cfi->defaultIsSaved)
-			config->defaultImage = 0;
+			config->defaultImage = FIRST_ENTRY_INDEX;
 		entry = findEntryByIndex(config, config->defaultImage);
 		if (!entry)
 			return 0;
@@ -5271,11 +5275,11 @@ int main(int argc, const char **argv)
 		return 0;
 
 	} else if (displayDefaultIndex) {
-		if (config->defaultImage == -1)
+		if (config->defaultImage == NO_DEFAULT_ENTRY)
 			return 0;
 		if (config->defaultImage == DEFAULT_SAVED_GRUB2 &&
 		    cfi->defaultIsSaved)
-			config->defaultImage = 0;
+			config->defaultImage = FIRST_ENTRY_INDEX;
 		printf("%i\n", config->defaultImage);
 		return 0;
 
-- 
2.9.3