Blame SOURCES/0062-Add-index-constant-definitions-instead-of-open-coded.patch

903092
From d1586b86da52810014bd95e46fd5c86a458fca3e Mon Sep 17 00:00:00 2001
903092
From: Robert Marshall <rmarshall@redhat.com>
903092
Date: Tue, 13 Dec 2016 14:36:31 -0500
903092
Subject: [PATCH 62/69] Add index constant definitions instead of open-coded
903092
 values.
903092
903092
Added numeric constants NO_DEFAULT_ENTRY and FIRST_ENTRY_INDEX. This
903092
clarifies the intent of various assignment operations throughout the
903092
source file.
903092
903092
Related: rhbz#1285601
903092
---
903092
 grubby.c | 44 ++++++++++++++++++++++++--------------------
903092
 1 file changed, 24 insertions(+), 20 deletions(-)
903092
903092
diff --git a/grubby.c b/grubby.c
903092
index 344f1da..bece23a 100644
903092
--- a/grubby.c
903092
+++ b/grubby.c
903092
@@ -130,6 +130,10 @@ struct singleEntry {
903092
 #define NEED_END     (1 << 5)
903092
 
903092
 #define MAIN_DEFAULT	    (1 << 0)
903092
+#define FIRST_ENTRY_INDEX    0	/* boot entry index value begin and increment
903092
+				   from this initial value */
903092
+#define NO_DEFAULT_ENTRY    -1	/* indicates that no specific default boot
903092
+				   entry was set or currently exists */
903092
 #define DEFAULT_SAVED       -2
903092
 #define DEFAULT_SAVED_GRUB2 -3
903092
 
903092
@@ -1712,7 +1716,7 @@ static struct grubConfig *readConfig(const char *inName,
903092
 						*end == ' ' || *end == '\t'))
903092
 					end++;
903092
 				if (*end)
903092
-					cfg->defaultImage = -1;
903092
+					cfg->defaultImage = NO_DEFAULT_ENTRY;
903092
 			} else if (defaultLine->numElements == 3) {
903092
 				char *value = defaultLine->elements[2].item;
903092
 				while (*value && (*value == '"' ||
903092
@@ -1725,7 +1729,7 @@ static struct grubConfig *readConfig(const char *inName,
903092
 						*end == ' ' || *end == '\t'))
903092
 					end++;
903092
 				if (*end)
903092
-					cfg->defaultImage = -1;
903092
+					cfg->defaultImage = NO_DEFAULT_ENTRY;
903092
 			}
903092
 		} else if (cfi->defaultSupportSaved &&
903092
 			   !strncmp(defaultLine->elements[1].item, "saved",
903092
@@ -1735,7 +1739,7 @@ static struct grubConfig *readConfig(const char *inName,
903092
 			cfg->defaultImage =
903092
 			    strtol(defaultLine->elements[1].item, &end, 10);
903092
 			if (*end)
903092
-				cfg->defaultImage = -1;
903092
+				cfg->defaultImage = NO_DEFAULT_ENTRY;
903092
 		} else if (defaultLine->numElements >= 2) {
903092
 			int i = 0;
903092
 			while ((entry = findEntryByIndex(cfg, i))) {
903092
@@ -1763,7 +1767,7 @@ static struct grubConfig *readConfig(const char *inName,
903092
 			if (entry) {
903092
 				cfg->defaultImage = i;
903092
 			} else {
903092
-				cfg->defaultImage = -1;
903092
+				cfg->defaultImage = NO_DEFAULT_ENTRY;
903092
 			}
903092
 		}
903092
 	} else if (cfg->cfi->defaultIsSaved && cfg->cfi->getEnv) {
903092
@@ -1780,7 +1784,7 @@ static struct grubConfig *readConfig(const char *inName,
903092
 				cfg->defaultImage = index;
903092
 		}
903092
 	} else {
903092
-		cfg->defaultImage = 0;
903092
+		cfg->defaultImage = FIRST_ENTRY_INDEX;
903092
 	}
903092
 
903092
 	return cfg;
903092
@@ -1800,7 +1804,7 @@ static void writeDefault(FILE * out, char *indent,
903092
 		fprintf(out, "%sdefault%ssaved\n", indent, separator);
903092
 	else if (cfg->cfi->defaultIsSaved) {
903092
 		fprintf(out, "%sset default=\"${saved_entry}\"\n", indent);
903092
-		if (cfg->defaultImage >= 0 && cfg->cfi->setEnv) {
903092
+		if (cfg->defaultImage >= FIRST_ENTRY_INDEX && cfg->cfi->setEnv) {
903092
 			char *title;
903092
 			entry = findEntryByIndex(cfg, cfg->defaultImage);
903092
 			line = getLineByType(LT_MENUENTRY, entry->lines);
903092
@@ -1813,7 +1817,7 @@ static void writeDefault(FILE * out, char *indent,
903092
 							 "saved_entry", title);
903092
 			}
903092
 		}
903092
-	} else if (cfg->defaultImage > -1) {
903092
+	} else if (cfg->defaultImage >= FIRST_ENTRY_INDEX) {
903092
 		if (cfg->cfi->defaultIsIndex) {
903092
 			if (cfg->cfi->defaultIsVariable) {
903092
 				fprintf(out, "%sset default=\"%d\"\n", indent,
903092
@@ -2515,7 +2519,7 @@ struct singleEntry *findTemplate(struct grubConfig *cfg, const char *prefix,
903092
 				}
903092
 			}
903092
 		}
903092
-	} else if (cfg->defaultImage > -1) {
903092
+	} else if (cfg->defaultImage >= FIRST_ENTRY_INDEX) {
903092
 		entry = findEntryByIndex(cfg, cfg->defaultImage);
903092
 		if (entry && suitableImage(entry, prefix, skipRemoved, flags)) {
903092
 			if (indexPtr)
903092
@@ -2597,20 +2601,20 @@ void setDefaultImage(struct grubConfig *config, int isUserSpecifiedKernelPath,
903092
 	int i, j;
903092
 
903092
 	if (newBootEntryIsDefault) {
903092
-		config->defaultImage = 0;
903092
+		config->defaultImage = FIRST_ENTRY_INDEX;
903092
 		return;
903092
 	} else if ((newDefaultBootEntryIndex >= 0) && config->cfi->defaultIsIndex) {
903092
 		if (findEntryByIndex(config, newDefaultBootEntryIndex))
903092
 			config->defaultImage = newDefaultBootEntryIndex;
903092
 		else
903092
-			config->defaultImage = -1;
903092
+			config->defaultImage = NO_DEFAULT_ENTRY;
903092
 		return;
903092
 	} else if (defaultKernelPath) {
903092
 		i = 0;
903092
 		if (findEntryByPath(config, defaultKernelPath, prefix, &i)) {
903092
 			config->defaultImage = i;
903092
 		} else {
903092
-			config->defaultImage = -1;
903092
+			config->defaultImage = NO_DEFAULT_ENTRY;
903092
 			return;
903092
 		}
903092
 	}
903092
@@ -2622,7 +2626,7 @@ void setDefaultImage(struct grubConfig *config, int isUserSpecifiedKernelPath,
903092
 		/* default is set to saved, we don't want to change it */
903092
 		return;
903092
 
903092
-	if (config->defaultImage > -1)
903092
+	if (config->defaultImage >= FIRST_ENTRY_INDEX)
903092
 		entry = findEntryByIndex(config, config->defaultImage);
903092
 	else
903092
 		entry = NULL;
903092
@@ -2639,7 +2643,7 @@ void setDefaultImage(struct grubConfig *config, int isUserSpecifiedKernelPath,
903092
 				config->defaultImage--;
903092
 		}
903092
 	} else if (isUserSpecifiedKernelPath) {
903092
-		config->defaultImage = 0;
903092
+		config->defaultImage = FIRST_ENTRY_INDEX;
903092
 	} else {
903092
 		/* Either we just erased the default (or the default line was bad
903092
 		 * to begin with) and didn't put a new one in. We'll use the first
903092
@@ -2648,7 +2652,7 @@ void setDefaultImage(struct grubConfig *config, int isUserSpecifiedKernelPath,
903092
 		    findTemplate(config, prefix, &config->defaultImage, 1,
903092
 				 flags);
903092
 		if (!newDefault)
903092
-			config->defaultImage = -1;
903092
+			config->defaultImage = NO_DEFAULT_ENTRY;
903092
 	}
903092
 }
903092
 
903092
@@ -5210,11 +5214,11 @@ int main(int argc, const char **argv)
903092
 		struct singleEntry *entry;
903092
 		char *rootspec;
903092
 
903092
-		if (config->defaultImage == -1)
903092
+		if (config->defaultImage == NO_DEFAULT_ENTRY)
903092
 			return 0;
903092
 		if (config->defaultImage == DEFAULT_SAVED_GRUB2 &&
903092
 		    cfi->defaultIsSaved)
903092
-			config->defaultImage = 0;
903092
+			config->defaultImage = FIRST_ENTRY_INDEX;
903092
 		entry = findEntryByIndex(config, config->defaultImage);
903092
 		if (!entry)
903092
 			return 0;
903092
@@ -5237,11 +5241,11 @@ int main(int argc, const char **argv)
903092
 		struct singleLine *line;
903092
 		struct singleEntry *entry;
903092
 
903092
-		if (config->defaultImage == -1)
903092
+		if (config->defaultImage == NO_DEFAULT_ENTRY)
903092
 			return 0;
903092
 		if (config->defaultImage == DEFAULT_SAVED_GRUB2 &&
903092
 		    cfi->defaultIsSaved)
903092
-			config->defaultImage = 0;
903092
+			config->defaultImage = FIRST_ENTRY_INDEX;
903092
 		entry = findEntryByIndex(config, config->defaultImage);
903092
 		if (!entry)
903092
 			return 0;
903092
@@ -5271,11 +5275,11 @@ int main(int argc, const char **argv)
903092
 		return 0;
903092
 
903092
 	} else if (displayDefaultIndex) {
903092
-		if (config->defaultImage == -1)
903092
+		if (config->defaultImage == NO_DEFAULT_ENTRY)
903092
 			return 0;
903092
 		if (config->defaultImage == DEFAULT_SAVED_GRUB2 &&
903092
 		    cfi->defaultIsSaved)
903092
-			config->defaultImage = 0;
903092
+			config->defaultImage = FIRST_ENTRY_INDEX;
903092
 		printf("%i\n", config->defaultImage);
903092
 		return 0;
903092
 
903092
-- 
903092
2.9.3
903092