Changeset View
Changeset View
Standalone View
Standalone View
ports-mgmt/dialog4ports/files/patch-dialog4ports.c
- This file was added.
Property | Old Value | New Value |
---|---|---|
fbsd:nokeywords | null | yes \ No newline at end of property |
svn:eol-style | null | native \ No newline at end of property |
svn:mime-type | null | text/plain \ No newline at end of property |
--- dialog4ports.c.orig 2016-07-08 14:49:08 UTC | |||||
+++ dialog4ports.c | |||||
@@ -43,6 +43,7 @@ | |||||
static int list_no = 0; | |||||
static int group = 0; | |||||
+static char const *env_delimiter = " \t"; | |||||
/* The initial items size */ | |||||
static int items_sz = 5; | |||||
@@ -50,6 +51,12 @@ static int items_sz = 5; | |||||
static StringList *enable_items = NULL; | |||||
/* New items */ | |||||
static StringList *new_items = NULL; | |||||
+/* One options section */ | |||||
+struct section { | |||||
+ char env_name[32]; | |||||
+ char section_name[256]; | |||||
+ int type; | |||||
+}; | |||||
/* add item to items */ | |||||
static void | |||||
@@ -126,74 +133,161 @@ parse_env_sl(char const *env_name) | |||||
/* parsing part */ | |||||
static int | |||||
-parsing_env(dialog_mixedlist **items, char const *env_name, int type) | |||||
+parse_env_all(dialog_mixedlist **items, char const *env_name, int type) | |||||
{ | |||||
- char *env, buf[256]; | |||||
- char const *delimiter = " \t"; | |||||
- char *token, *token2; | |||||
+ char *env; | |||||
+ char *token; | |||||
char *temp, *tofree; | |||||
- char *temp2, *tofree2; | |||||
env = getenv(env_name); | |||||
if (env == NULL) | |||||
return (0); | |||||
- if (strcmp(env_name, "ALL_OPTIONS") == 0) { | |||||
- tofree = temp = strdup(env); | |||||
+ tofree = temp = strdup(env); | |||||
+ while ((token = strsep(&temp, env_delimiter)) != NULL) { | |||||
+ if (token[0] == '\0') | |||||
+ continue; | |||||
+ add_item(items, token, get_desc(token, ""), is_enable(token), | |||||
+ is_new(token), type, group); | |||||
+ } | |||||
+ free(tofree); | |||||
- while ((token = strsep(&temp, delimiter)) != NULL) { | |||||
- if (token[0] == '\0') | |||||
- continue; | |||||
- add_item(items, token, get_desc(token, ""), is_enable(token), | |||||
- is_new(token), type, group); | |||||
+ group++; | |||||
+ | |||||
+ return (0); | |||||
+} | |||||
+ | |||||
+static void | |||||
+order_sections(struct section **sections, int sections_size) | |||||
+{ | |||||
+ char *env; | |||||
+ char *token, *temp, *tofree; | |||||
+ struct section *ordered_sections, *sec; | |||||
+ int i, found; | |||||
+ | |||||
+ env = getenv("OPTIONS_SECTION_ORDER"); | |||||
+ if (env == NULL || env[0] == '\0') | |||||
+ return; | |||||
+ | |||||
+ if (sections_size == 0) | |||||
+ errx(EXIT_FAILURE, "OPTIONS_SECTION_ORDER is defined, but there are no option sections"); | |||||
+ | |||||
+ ordered_sections = malloc(sizeof(struct section)*sections_size); | |||||
+ sec = ordered_sections; | |||||
+ found = 0; | |||||
+ | |||||
+ tofree = temp = strdup(env); | |||||
+ while ((token = strsep(&temp, env_delimiter)) != NULL) { | |||||
+ if (token[0] == '\0') | |||||
+ continue; | |||||
+ | |||||
+ for (i = 0; i < sections_size; i++) { | |||||
+ if (strcmp((*sections)[i].section_name, token) == 0) { | |||||
+ *sec++ = (*sections)[i]; | |||||
+ found++; | |||||
+ break; | |||||
+ } | |||||
} | |||||
- free(tofree); | |||||
- } else { | |||||
+ if (i >= sections_size) | |||||
+ errx(EXIT_FAILURE, "can't find section %s from OPTIONS_SECTION_ORDER in any option group", token); | |||||
+ } | |||||
+ free(tofree); | |||||
+ | |||||
+ if (found != sections_size) | |||||
+ errx(EXIT_FAILURE, "Items in OPTIONS_SECTION_ORDER should match the declared option sections"); | |||||
+ | |||||
+ free(*sections); | |||||
+ *sections = ordered_sections; | |||||
+} | |||||
+ | |||||
+static void | |||||
+read_env_section(struct section **sections, int *sections_size, char const *env_name, int type) | |||||
+{ | |||||
+ char *env; | |||||
+ char *token; | |||||
+ char *temp, *tofree; | |||||
+ struct section *new_section; | |||||
+ | |||||
+ env = getenv(env_name); | |||||
+ if (env == NULL) | |||||
+ return; | |||||
+ | |||||
+ tofree = temp = strdup(env); | |||||
+ while ((token = strsep(&temp, env_delimiter)) != NULL) { | |||||
+ if (token[0] == '\0') | |||||
+ continue; | |||||
+ | |||||
+ if (*sections_size == 0) | |||||
+ *sections = malloc(sizeof(struct section)); | |||||
+ else | |||||
+ *sections = realloc(*sections, sizeof(struct section) * (*sections_size + 1)); | |||||
+ | |||||
+ new_section = *sections + *sections_size; | |||||
+ | |||||
+ strcpy(new_section->env_name, env_name); | |||||
+ strncpy(new_section->section_name, token, sizeof(new_section->section_name)); | |||||
+ new_section->type = type; | |||||
+ | |||||
+ ++*sections_size; | |||||
+ } | |||||
+ free(tofree); | |||||
+} | |||||
+ | |||||
+static int | |||||
+add_sections(dialog_mixedlist **items, struct section *sections, int sections_size) | |||||
+{ | |||||
+ int i; | |||||
+ char *env, buf[256]; | |||||
+ char *token; | |||||
+ char *temp, *tofree; | |||||
+ struct section *sec = sections; | |||||
+ | |||||
+ for (i = 0; i < sections_size; i++, sec++) { | |||||
+ add_item(items, get_desc(sec->section_name, sec->section_name), "", false, false, | |||||
+ ITEM_SEPARATOR, group); | |||||
+ | |||||
+ snprintf(buf, sizeof(buf), "%s_%s", sec->env_name, sec->section_name); | |||||
+ env = getenv(buf); | |||||
+ if (env == NULL) | |||||
+ errx(EXIT_FAILURE, "%s does not exists", buf); | |||||
+ | |||||
tofree = temp = strdup(env); | |||||
- while ((token = strsep(&temp, delimiter)) != NULL) { | |||||
+ while ((token = strsep(&temp, env_delimiter)) != NULL) { | |||||
if (token[0] == '\0') | |||||
continue; | |||||
- add_item(items, get_desc(token, token), "", false, false, | |||||
- ITEM_SEPARATOR, group); | |||||
- | |||||
- snprintf(buf, sizeof(buf), "%s_%s", env_name, token); | |||||
- env = getenv(buf); | |||||
- if (env == NULL) | |||||
- errx(EXIT_FAILURE, "%s does not exists", buf); | |||||
- tofree2 = temp2 = strdup(env); | |||||
- while ((token2 = strsep(&temp2, delimiter)) != NULL) { | |||||
- if (token2[0] == '\0') | |||||
- continue; | |||||
- add_item(items, token2, get_desc(token2, ""), | |||||
- is_enable(token2), is_new(token2), type, group); | |||||
- } | |||||
- free(tofree2); | |||||
- group++; | |||||
+ add_item(items, token, get_desc(token, ""), | |||||
+ is_enable(token), is_new(token), sec->type, group); | |||||
} | |||||
- | |||||
free(tofree); | |||||
+ group++; | |||||
} | |||||
+ | |||||
if (group == 0) | |||||
group++; | |||||
return (0); | |||||
} | |||||
- | |||||
/* prepare items for next drawing*/ | |||||
static dialog_mixedlist * | |||||
prepare_items(void) | |||||
{ | |||||
dialog_mixedlist *items = NULL; | |||||
+ struct section *sections = NULL; | |||||
+ int sections_size = 0; | |||||
enable_items = parse_env_sl("PORT_OPTIONS"); | |||||
new_items = parse_env_sl("NEW_OPTIONS"); | |||||
- parsing_env(&items, "ALL_OPTIONS", ITEM_CHECK); | |||||
- parsing_env(&items, "OPTIONS_GROUP", ITEM_CHECK); | |||||
- parsing_env(&items, "OPTIONS_MULTI", ITEM_CHECK); | |||||
- parsing_env(&items, "OPTIONS_SINGLE", ITEM_RADIO); | |||||
- parsing_env(&items, "OPTIONS_RADIO", ITEM_RADIO); | |||||
+ parse_env_all(&items, "ALL_OPTIONS", ITEM_CHECK); | |||||
+ read_env_section(§ions, §ions_size, "OPTIONS_GROUP", ITEM_CHECK); | |||||
+ read_env_section(§ions, §ions_size, "OPTIONS_MULTI", ITEM_CHECK); | |||||
+ read_env_section(§ions, §ions_size, "OPTIONS_SINGLE", ITEM_RADIO); | |||||
+ read_env_section(§ions, §ions_size, "OPTIONS_RADIO", ITEM_RADIO); | |||||
+ | |||||
+ order_sections(§ions, sections_size); | |||||
+ | |||||
+ add_sections(&items, sections, sections_size); | |||||
return (items); | |||||
} |