Debian Codegraph @

src/cli/presets.c

raw symbols SCIP
1 /*
2 Authors:
3 Pavel Březina <[email protected]>
4
5 Copyright (C) 2024 Red Hat
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>.
19 */
20
21 #include <string.h>
22 #include <stdlib.h>
23
24 #include "common/common.h"
25 #include "lib/util/config.h"
26 #include "lib/util/string_array.h"
27 #include "lib/util/file.h"
28 #include "lib/constants.h"
29 #include "cli/presets.h"
30
31 static const struct cli_preset presets[] = {
32 {"@system-default", "Use default system configuration", AUTHSELECT_DEFAULT_CONFIG_PATH},
33 {NULL, NULL, NULL}
34 };
35
36 static bool
37 cli_preset_is_available(const struct cli_preset *preset)
38 {
39 errno_t ret;
40
41 if (preset == NULL) {
42 return false;
43 }
44
45 ret = file_exists(preset->config_path);
46 return ret == EOK;
47 }
48
49 static errno_t
50 cli_preset_merge_features(const char **preset_features,
51 const char **cli_features,
52 char ***_merged_features)
53 {
54 char **merged;
55 int i;
56
57 /* Create new empty array */
58 merged = string_array_create(0);
59 if (merged == NULL) {
60 return ENOMEM;
61 }
62
63 /* Add preset features */
64 if (preset_features != NULL) {
65 for (i = 0; preset_features[i] != NULL; i++) {
66 merged = string_array_add_value(merged, preset_features[i], true);
67 if (merged == NULL) {
68 return ENOMEM;
69 }
70 }
71 }
72
73 /* Add CLI features (skip duplicates) */
74 if (cli_features != NULL) {
75 for (i = 0; cli_features[i] != NULL; i++) {
76 merged = string_array_add_value(merged, cli_features[i], true);
77 if (merged == NULL) {
78 return ENOMEM;
79 }
80 }
81 }
82
83 *_merged_features = merged;
84 return EOK;
85 }
86
87 bool
88 cli_preset_is_preset(const char *profile_id)
89 {
90 return profile_id != NULL && profile_id[0] == '@';
91 }
92
93 const struct cli_preset*
94 cli_preset_find(const char *name)
95 {
96 int i;
97
98 if (name == NULL) {
99 return NULL;
100 }
101
102 for (i = 0; presets[i].name != NULL; i++) {
103 if (strcmp(presets[i].name, name) == 0) {
104 return &presets[i];
105 }
106 }
107
108 return NULL;
109 }
110
111 errno_t
112 cli_preset_resolve(const char *preset_name,
113 const char **additional_features,
114 char **_profile_id,
115 char ***_features)
116 {
117 const struct cli_preset *preset;
118 char **preset_features = NULL;
119 char *profile_id;
120 errno_t ret;
121
122 preset = cli_preset_find(preset_name);
123 if (preset == NULL) {
124 ERROR("Unknown preset: %s", preset_name);
125 return ENOENT;
126 }
127
128 ret = config_read(preset->config_path, &profile_id, &preset_features);
129 if (ret != EOK) {
130 ERROR("Unable to read preset configuration from %s [%d]: %s",
131 preset->config_path, ret, strerror(ret));
132 return ret;
133 }
134
135 if (_features != NULL) {
136 ret = cli_preset_merge_features((const char **)preset_features,
137 additional_features,
138 _features);
139 if (ret != EOK) {
140 goto done;
141 }
142 }
143 *_profile_id = profile_id;
144
145 ret = EOK;
146
147 done:
148 string_array_free(preset_features);
149 if (ret != EOK) {
150 free(profile_id);
151 }
152
153 return ret;
154 }
155
156 struct cli_preset*
157 cli_preset_list(void)
158 {
159 struct cli_preset *available;
160 int count = 0;
161 int i, j;
162
163 /* Count available presets */
164 for (i = 0; presets[i].name != NULL; i++) {
165 if (cli_preset_is_available(&presets[i])) {
166 count++;
167 }
168 }
169
170 /* Allocate array for available presets */
171 available = malloc_zero_array(struct cli_preset, count + 1);
172 if (available == NULL) {
173 return NULL;
174 }
175
176 /* Copy available presets */
177 j = 0;
178 for (i = 0; presets[i].name != NULL; i++) {
179 if (cli_preset_is_available(&presets[i])) {
180 available[j++] = presets[i];
181 }
182 }
183 available[j].name = NULL;
184
185 return available;
186 }
187
188 void
189 cli_preset_list_free(struct cli_preset *list)
190 {
191 free(list);
192 }
193
194 int
195 cli_preset_max_name_length(void)
196 {
197 struct cli_preset *available;
198 size_t max = 0;
199 size_t len;
200 int i;
201
202 available = cli_preset_list();
203 if (available == NULL) {
204 return 0;
205 }
206
207 for (i = 0; available[i].name != NULL; i++) {
208 len = strlen(available[i].name);
209 if (max < len) {
210 max = len;
211 }
212 }
213
214 cli_preset_list_free(available);
215
216 return max;
217 }