Line 0
Link Here
|
|
|
1 |
--- dialog4ports.c.orig 2016-07-08 14:49:08 UTC |
2 |
+++ dialog4ports.c |
3 |
@@ -43,6 +43,7 @@ |
4 |
|
5 |
static int list_no = 0; |
6 |
static int group = 0; |
7 |
+static char const *env_delimiter = " \t"; |
8 |
|
9 |
/* The initial items size */ |
10 |
static int items_sz = 5; |
11 |
@@ -50,16 +51,22 @@ static int items_sz = 5; |
12 |
static StringList *enable_items = NULL; |
13 |
/* New items */ |
14 |
static StringList *new_items = NULL; |
15 |
+/* One options section */ |
16 |
+typedef struct { |
17 |
+ char env_name[32]; |
18 |
+ char section_name[256]; |
19 |
+ int type; |
20 |
+} dialog_section; |
21 |
|
22 |
/* add item to items */ |
23 |
static void |
24 |
-add_item(dialog_mixedlist **items, char const *name, char const *text, |
25 |
+add_item(dialog_option_item **items, char const *name, char const *text, |
26 |
bool state, bool new, int type, int grp) |
27 |
{ |
28 |
|
29 |
if ((list_no + 1 > items_sz) || *items == NULL) { |
30 |
items_sz *= 2; |
31 |
- *items = realloc(*items, items_sz * sizeof(dialog_mixedlist)); |
32 |
+ *items = realloc(*items, items_sz * sizeof(dialog_option_item)); |
33 |
if (*items == NULL) |
34 |
err(EXIT_FAILURE, "Need more memory!"); |
35 |
} |
36 |
@@ -126,81 +133,171 @@ parse_env_sl(char const *env_name) |
37 |
|
38 |
/* parsing part */ |
39 |
static int |
40 |
-parsing_env(dialog_mixedlist **items, char const *env_name, int type) |
41 |
+parse_env_all(dialog_option_item **items, char const *env_name, int type) |
42 |
{ |
43 |
- char *env, buf[256]; |
44 |
- char const *delimiter = " \t"; |
45 |
- char *token, *token2; |
46 |
+ char *env; |
47 |
+ char *token; |
48 |
char *temp, *tofree; |
49 |
- char *temp2, *tofree2; |
50 |
|
51 |
env = getenv(env_name); |
52 |
if (env == NULL) |
53 |
return (0); |
54 |
|
55 |
- if (strcmp(env_name, "ALL_OPTIONS") == 0) { |
56 |
- tofree = temp = strdup(env); |
57 |
+ tofree = temp = strdup(env); |
58 |
+ while ((token = strsep(&temp, env_delimiter)) != NULL) { |
59 |
+ if (token[0] == '\0') |
60 |
+ continue; |
61 |
+ add_item(items, token, get_desc(token, ""), is_enable(token), |
62 |
+ is_new(token), type, group); |
63 |
+ } |
64 |
+ free(tofree); |
65 |
|
66 |
- while ((token = strsep(&temp, delimiter)) != NULL) { |
67 |
- if (token[0] == '\0') |
68 |
- continue; |
69 |
- add_item(items, token, get_desc(token, ""), is_enable(token), |
70 |
- is_new(token), type, group); |
71 |
+ group++; |
72 |
+ |
73 |
+ return (0); |
74 |
+} |
75 |
+ |
76 |
+static void |
77 |
+order_sections(dialog_section **sections, int sections_size) |
78 |
+{ |
79 |
+ char *env; |
80 |
+ char *token, *temp, *tofree; |
81 |
+ dialog_section *ordered_sections, *sec; |
82 |
+ int i, found; |
83 |
+ |
84 |
+ env = getenv("OPTIONS_SECTION_ORDER"); |
85 |
+ if (env == NULL || env[0] == '\0') |
86 |
+ return; |
87 |
+ |
88 |
+ if (sections_size == 0) |
89 |
+ errx(EXIT_FAILURE, "OPTIONS_SECTION_ORDER is defined, but there are no option sections"); |
90 |
+ |
91 |
+ ordered_sections = malloc(sizeof(dialog_section)*sections_size); |
92 |
+ sec = ordered_sections; |
93 |
+ found = 0; |
94 |
+ |
95 |
+ tofree = temp = strdup(env); |
96 |
+ while ((token = strsep(&temp, env_delimiter)) != NULL) { |
97 |
+ if (token[0] == '\0') |
98 |
+ continue; |
99 |
+ |
100 |
+ for (i = 0; i < sections_size; i++) { |
101 |
+ if (strcmp((*sections)[i].section_name, token) == 0) { |
102 |
+ *sec++ = (*sections)[i]; |
103 |
+ found++; |
104 |
+ break; |
105 |
+ } |
106 |
} |
107 |
- free(tofree); |
108 |
- } else { |
109 |
+ if (i >= sections_size) |
110 |
+ errx(EXIT_FAILURE, "can't find section %s from OPTIONS_SECTION_ORDER in any option group", token); |
111 |
+ } |
112 |
+ free(tofree); |
113 |
+ |
114 |
+ if (found != sections_size) |
115 |
+ errx(EXIT_FAILURE, "Items in OPTIONS_SECTION_ORDER should match the declared option sections"); |
116 |
+ |
117 |
+ free(*sections); |
118 |
+ *sections = ordered_sections; |
119 |
+} |
120 |
+ |
121 |
+static void |
122 |
+read_env_section(dialog_section **sections, int *sections_size, char const *env_name, int type) |
123 |
+{ |
124 |
+ char *env; |
125 |
+ char *token; |
126 |
+ char *temp, *tofree; |
127 |
+ dialog_section *new_section; |
128 |
+ |
129 |
+ env = getenv(env_name); |
130 |
+ if (env == NULL) |
131 |
+ return; |
132 |
+ |
133 |
+ tofree = temp = strdup(env); |
134 |
+ while ((token = strsep(&temp, env_delimiter)) != NULL) { |
135 |
+ if (token[0] == '\0') |
136 |
+ continue; |
137 |
+ |
138 |
+ if (*sections_size == 0) |
139 |
+ *sections = malloc(sizeof(dialog_section)); |
140 |
+ else |
141 |
+ *sections = realloc(*sections, sizeof(dialog_section) * (*sections_size + 1)); |
142 |
+ |
143 |
+ new_section = *sections + *sections_size; |
144 |
+ |
145 |
+ strcpy(new_section->env_name, env_name); |
146 |
+ strncpy(new_section->section_name, token, sizeof(new_section->section_name)); |
147 |
+ new_section->type = type; |
148 |
+ |
149 |
+ ++*sections_size; |
150 |
+ } |
151 |
+ free(tofree); |
152 |
+} |
153 |
+ |
154 |
+static int |
155 |
+add_sections(dialog_option_item **items, dialog_section *sections, int sections_size) |
156 |
+{ |
157 |
+ int i; |
158 |
+ char *env, buf[256]; |
159 |
+ char *token; |
160 |
+ char *temp, *tofree; |
161 |
+ dialog_section *sec = sections; |
162 |
+ |
163 |
+ for (i = 0; i < sections_size; i++, sec++) { |
164 |
+ add_item(items, get_desc(sec->section_name, sec->section_name), "", false, false, |
165 |
+ ITEM_SEPARATOR, group); |
166 |
+ |
167 |
+ snprintf(buf, sizeof(buf), "%s_%s", sec->env_name, sec->section_name); |
168 |
+ env = getenv(buf); |
169 |
+ if (env == NULL) |
170 |
+ errx(EXIT_FAILURE, "%s does not exists", buf); |
171 |
+ |
172 |
tofree = temp = strdup(env); |
173 |
- while ((token = strsep(&temp, delimiter)) != NULL) { |
174 |
+ while ((token = strsep(&temp, env_delimiter)) != NULL) { |
175 |
if (token[0] == '\0') |
176 |
continue; |
177 |
- add_item(items, get_desc(token, token), "", false, false, |
178 |
- ITEM_SEPARATOR, group); |
179 |
- |
180 |
- snprintf(buf, sizeof(buf), "%s_%s", env_name, token); |
181 |
- env = getenv(buf); |
182 |
- if (env == NULL) |
183 |
- errx(EXIT_FAILURE, "%s does not exists", buf); |
184 |
- tofree2 = temp2 = strdup(env); |
185 |
- while ((token2 = strsep(&temp2, delimiter)) != NULL) { |
186 |
- if (token2[0] == '\0') |
187 |
- continue; |
188 |
- add_item(items, token2, get_desc(token2, ""), |
189 |
- is_enable(token2), is_new(token2), type, group); |
190 |
- } |
191 |
- free(tofree2); |
192 |
- group++; |
193 |
+ add_item(items, token, get_desc(token, ""), |
194 |
+ is_enable(token), is_new(token), sec->type, group); |
195 |
} |
196 |
- |
197 |
free(tofree); |
198 |
+ group++; |
199 |
} |
200 |
+ |
201 |
if (group == 0) |
202 |
group++; |
203 |
|
204 |
return (0); |
205 |
} |
206 |
|
207 |
- |
208 |
/* prepare items for next drawing*/ |
209 |
-static dialog_mixedlist * |
210 |
+static dialog_option_item * |
211 |
prepare_items(void) |
212 |
{ |
213 |
- dialog_mixedlist *items = NULL; |
214 |
+ dialog_option_item *items = NULL; |
215 |
+ dialog_section *sections = NULL; |
216 |
+ int sections_size = 0; |
217 |
|
218 |
enable_items = parse_env_sl("PORT_OPTIONS"); |
219 |
new_items = parse_env_sl("NEW_OPTIONS"); |
220 |
|
221 |
- parsing_env(&items, "ALL_OPTIONS", ITEM_CHECK); |
222 |
- parsing_env(&items, "OPTIONS_GROUP", ITEM_CHECK); |
223 |
- parsing_env(&items, "OPTIONS_MULTI", ITEM_CHECK); |
224 |
- parsing_env(&items, "OPTIONS_SINGLE", ITEM_RADIO); |
225 |
- parsing_env(&items, "OPTIONS_RADIO", ITEM_RADIO); |
226 |
+ parse_env_all(&items, "ALL_OPTIONS", ITEM_CHECK); |
227 |
+ read_env_section(§ions, §ions_size, "OPTIONS_GROUP", ITEM_CHECK); |
228 |
+ read_env_section(§ions, §ions_size, "OPTIONS_MULTI", ITEM_CHECK); |
229 |
+ read_env_section(§ions, §ions_size, "OPTIONS_SINGLE", ITEM_RADIO); |
230 |
+ read_env_section(§ions, §ions_size, "OPTIONS_RADIO", ITEM_RADIO); |
231 |
+ |
232 |
+ order_sections(§ions, sections_size); |
233 |
+ |
234 |
+ add_sections(&items, sections, sections_size); |
235 |
+ |
236 |
+ if (sections != NULL) |
237 |
+ free(sections); |
238 |
|
239 |
return (items); |
240 |
} |
241 |
|
242 |
static int |
243 |
mixedlist_show(const char *title, const char *cprompt, int height, |
244 |
- int min_height, int width, dialog_mixedlist *items, bool align_center, |
245 |
+ int min_height, int width, dialog_option_item *items, bool align_center, |
246 |
bool fullscreen) |
247 |
{ |
248 |
int res; |
249 |
@@ -235,7 +332,7 @@ main(int argc, char *argv[]) |
250 |
bool align_center = 0; |
251 |
bool fullscreen = 0; |
252 |
char *helpfile; |
253 |
- dialog_mixedlist *items; |
254 |
+ dialog_option_item *items; |
255 |
|
256 |
setlocale(LC_ALL, ""); |
257 |
errno = 0; |