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"cxx . . $ `<file>/src/common/common.h`/
25 #include "lib/util/config.h"cxx . . $ `<file>/src/lib/util/config.h`/
26 #include "lib/util/string_array.h"cxx . . $ `<file>/src/lib/util/string_array.h`/
27 #include "lib/util/file.h"cxx . . $ `<file>/src/lib/util/file.h`/
28 #include "lib/constants.h"cxx . . $ `<file>/src/lib/constants.h`/
29 #include "cli/presets.h"cxx . . $ `<file>/src/cli/presets.h`/
30
31 static const struct cli_presetcxx . . $ cli_preset# presetscxx . . $ presets.[] = {
32 {"@system-default", "Use default system configuration", AUTHSELECT_DEFAULT_CONFIG_PATHcxx . . $ `<built-in>:415:9`!},
33 {NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!, NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!, NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!}
34 };
35
36 static boolcxx . . $ `<external>/13015445228558014418/stdbool.h:36:9`!
37 cli_preset_is_availablecxx . . $ cli_preset_is_available(9a39465893b30cf9).(const struct cli_presetcxx . . $ cli_preset# *preset)
38 {
39 errno_tcxx . . $ errno_t# ret;
40
41 if (preset == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!) {
42 return falsecxx . . $ `<external>/13015445228558014418/stdbool.h:38:9`!;
43 }
44
45 ret = file_existscxx . . $ file_exists(5c4306d8553b3696).(preset->config_pathcxx . . $ cli_preset#config_path.);
46 return ret == EOKcxx . . $ `src/common/errno_t.h:34:9`!;
47 }
48
49 static errno_tcxx . . $ errno_t#
50 cli_preset_merge_featurescxx . . $ cli_preset_merge_features(4c7bfb2a09433727).(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_createcxx . . $ string_array_create(4d3fbc76ec7ad0ba).(0);
59 if (merged == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!) {
60 return ENOMEMcxx . . $ `<external>/8987895939074593727/errno-base.h:16:9`!;
61 }
62
63 /* Add preset features */
64 if (preset_features != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!) {
65 for (i = 0; preset_features[i] != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!; i++) {
66 merged = string_array_add_valuecxx . . $ string_array_add_value(db56f6d53b8c0a98).(merged, preset_features[i], truecxx . . $ `<external>/13015445228558014418/stdbool.h:37:9`!);
67 if (merged == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!) {
68 return ENOMEMcxx . . $ `<external>/8987895939074593727/errno-base.h:16:9`!;
69 }
70 }
71 }
72
73 /* Add CLI features (skip duplicates) */
74 if (cli_features != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!) {
75 for (i = 0; cli_features[i] != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!; i++) {
76 merged = string_array_add_valuecxx . . $ string_array_add_value(db56f6d53b8c0a98).(merged, cli_features[i], truecxx . . $ `<external>/13015445228558014418/stdbool.h:37:9`!);
77 if (merged == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!) {
78 return ENOMEMcxx . . $ `<external>/8987895939074593727/errno-base.h:16:9`!;
79 }
80 }
81 }
82
83 *_merged_features = merged;
84 return EOKcxx . . $ `src/common/errno_t.h:34:9`!;
85 }
86
87 boolcxx . . $ `<external>/13015445228558014418/stdbool.h:36:9`!
88 cli_preset_is_presetcxx . . $ cli_preset_is_preset(c5f7da30cbc0f4e9).(const char *profile_id)
89 {
90 return profile_id != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`! && profile_id[0] == '@';
91 }
92
93 const struct cli_presetcxx . . $ cli_preset#*
94 cli_preset_findcxx . . $ cli_preset_find(2fcd4ad798ac1b8c).(const char *name)
95 {
96 int i;
97
98 if (name == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!) {
99 return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!;
100 }
101
102 for (i = 0; presetscxx . . $ presets.[i].namecxx . . $ cli_preset#name. != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!; i++) {
103 if (strcmpcxx . . $ strcmp(5ae29d726ae57f88).(presetscxx . . $ presets.[i].namecxx . . $ cli_preset#name., name) == 0) {
104 return &presetscxx . . $ presets.[i];
105 }
106 }
107
108 return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!;
109 }
110
111 errno_tcxx . . $ errno_t#
112 cli_preset_resolvecxx . . $ cli_preset_resolve(c8377c9f8d72bd4b).(const char *preset_name,
113 const char **additional_features,
114 char **_profile_id,
115 char ***_features)
116 {
117 const struct cli_presetcxx . . $ cli_preset# *preset;
118 char **preset_features = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!;
119 char *profile_id;
120 errno_tcxx . . $ errno_t# ret;
121
122 preset = cli_preset_findcxx . . $ cli_preset_find(2fcd4ad798ac1b8c).(preset_name);
123 if (preset == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!) {
124 ERRORcxx . . $ `src/common/common.h:71:9`!("Unknown preset: %s", preset_name);
125 return ENOENTcxx . . $ `<external>/8987895939074593727/errno-base.h:6:9`!;
126 }
127
128 ret = config_readcxx . . $ config_read(2fdb31789c666a26).(preset->config_pathcxx . . $ cli_preset#config_path., &profile_id, &preset_features);
129 if (ret != EOKcxx . . $ `src/common/errno_t.h:34:9`!) {
130 ERRORcxx . . $ `src/common/common.h:71:9`!("Unable to read preset configuration from %s [%d]: %s",
131 preset->config_path, ret, strerror(ret));
132 return ret;
133 }
134
135 if (_features != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!) {
136 ret = cli_preset_merge_featurescxx . . $ cli_preset_merge_features(4c7bfb2a09433727).((const char **)preset_features,
137 additional_features,
138 _features);
139 if (ret != EOKcxx . . $ `src/common/errno_t.h:34:9`!) {
140 goto done;
141 }
142 }
143 *_profile_id = profile_id;
144
145 ret = EOKcxx . . $ `src/common/errno_t.h:34:9`!;
146
147 done:
148 string_array_freecxx . . $ string_array_free(3ed91490e421178f).(preset_features);
149 if (ret != EOKcxx . . $ `src/common/errno_t.h:34:9`!) {
150 freecxx . . $ free(5d22bdacc48458e8).(profile_id);
151 }
152
153 return ret;
154 }
155
156 struct cli_presetcxx . . $ cli_preset#*
157 cli_preset_listcxx . . $ cli_preset_list(923eb9540c629223).(void)
158 {
159 struct cli_presetcxx . . $ cli_preset# *available;
160 int count = 0;
161 int i, j;
162
163 /* Count available presets */
164 for (i = 0; presetscxx . . $ presets.[i].namecxx . . $ cli_preset#name. != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!; i++) {
165 if (cli_preset_is_availablecxx . . $ cli_preset_is_available(9a39465893b30cf9).(&presetscxx . . $ presets.[i])) {
166 count++;
167 }
168 }
169
170 /* Allocate array for available presets */
171 available = malloc_zero_arraycxx . . $ `src/common/common.h:41:9`!(struct cli_preset, count + 1);
172 if (available == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!) {
173 return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!;
174 }
175
176 /* Copy available presets */
177 j = 0;
178 for (i = 0; presetscxx . . $ presets.[i].namecxx . . $ cli_preset#name. != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!; i++) {
179 if (cli_preset_is_availablecxx . . $ cli_preset_is_available(9a39465893b30cf9).(&presetscxx . . $ presets.[i])) {
180 available[j++] = presetscxx . . $ presets.[i];
181 }
182 }
183 available[j].namecxx . . $ cli_preset#name. = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!;
184
185 return available;
186 }
187
188 void
189 cli_preset_list_freecxx . . $ cli_preset_list_free(8ed6e3eef1294f83).(struct cli_presetcxx . . $ cli_preset# *list)
190 {
191 freecxx . . $ free(5d22bdacc48458e8).(list);
192 }
193
194 int
195 cli_preset_max_name_lengthcxx . . $ cli_preset_max_name_length(b126dc7c1de90089).(void)
196 {
197 struct cli_presetcxx . . $ cli_preset# *available;
198 size_t max = 0;
199 size_t len;
200 int i;
201
202 available = cli_preset_listcxx . . $ cli_preset_list(923eb9540c629223).();
203 if (available == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!) {
204 return 0;
205 }
206
207 for (i = 0; available[i].namecxx . . $ cli_preset#name. != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!; i++) {
208 len = strlencxx . . $ strlen(751346f8406fd082).(available[i].namecxx . . $ cli_preset#name.);
209 if (max < len) {
210 max = len;
211 }
212 }
213
214 cli_preset_list_freecxx . . $ cli_preset_list_free(8ed6e3eef1294f83).(available);
215
216 return max;
217 }