]>
Commit | Line | Data |
---|---|---|
c9abb903 MT |
1 | /*############################################################################# |
2 | # # | |
3 | # Pakfire - The IPFire package management system # | |
4 | # Copyright (C) 2023 Pakfire development team # | |
5 | # # | |
6 | # This program is free software: you can redistribute it and/or modify # | |
7 | # it under the terms of the GNU General Public License as published by # | |
8 | # the Free Software Foundation, either version 3 of the License, or # | |
9 | # (at your option) any later version. # | |
10 | # # | |
11 | # This program is distributed in the hope that it will be useful, # | |
12 | # but WITHOUT ANY WARRANTY; without even the implied warranty of # | |
13 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # | |
14 | # GNU General Public License for more details. # | |
15 | # # | |
16 | # You should have received a copy of the GNU General Public License # | |
17 | # along with this program. If not, see <http://www.gnu.org/licenses/>. # | |
18 | # # | |
19 | #############################################################################*/ | |
20 | ||
1f81cbb2 MT |
21 | #include <argp.h> |
22 | ||
c9abb903 MT |
23 | #include <pakfire/pakfire.h> |
24 | ||
1f81cbb2 | 25 | #include "command.h" |
c9abb903 | 26 | #include "dump.h" |
1f81cbb2 | 27 | #include "pakfire.h" |
c9abb903 MT |
28 | #include "requires.h" |
29 | ||
1f81cbb2 MT |
30 | static const char* args_doc = "PATTERNS..."; |
31 | ||
32 | static const char* doc = "Search for packages that requires a certain pattern"; | |
33 | ||
34 | #define MAX_PATTERNS 256 | |
35 | ||
36 | struct config { | |
37 | char* patterns[MAX_PATTERNS]; | |
38 | unsigned int num_patterns; | |
39 | }; | |
40 | ||
d673ca6c | 41 | static error_t parse(int key, char* arg, struct argp_state* state, void* data) { |
1f81cbb2 MT |
42 | struct config* config = data; |
43 | ||
44 | switch (key) { | |
45 | case ARGP_KEY_ARG: | |
46 | if (config->num_patterns >= MAX_PATTERNS) | |
47 | return -ENOBUFS; | |
48 | ||
49 | config->patterns[config->num_patterns++] = arg; | |
50 | break; | |
51 | ||
52 | default: | |
53 | return ARGP_ERR_UNKNOWN; | |
54 | } | |
55 | ||
56 | return 0; | |
57 | } | |
58 | ||
59 | int cli_requires(void* data, int argc, char* argv[]) { | |
60 | struct pakfire* pakfire = NULL; | |
c9abb903 MT |
61 | struct pakfire_packagelist* list = NULL; |
62 | int r; | |
63 | ||
1f81cbb2 MT |
64 | struct cli_config* cli_config = data; |
65 | ||
66 | struct config config = {}; | |
67 | ||
68 | // Parse the command line | |
9b8d2c6e | 69 | r = cli_parse(NULL, NULL, args_doc, doc, parse, 0, argc, argv, &config); |
1f81cbb2 MT |
70 | if (r) |
71 | goto ERROR; | |
72 | ||
73 | // Setup pakfire | |
74 | r = cli_setup_pakfire(&pakfire, cli_config); | |
75 | if (r) | |
76 | goto ERROR; | |
77 | ||
c9abb903 | 78 | // Allocate a new packagelist |
460ce205 | 79 | r = pakfire_packagelist_create(&list, cli_config->ctx); |
c9abb903 MT |
80 | if (r) |
81 | goto ERROR; | |
82 | ||
83 | // Perform search | |
1f81cbb2 MT |
84 | for (unsigned int i = 0; i < config.num_patterns; i++) { |
85 | r = pakfire_whatrequires(pakfire, config.patterns[i], 0, list); | |
c9abb903 MT |
86 | if (r) |
87 | goto ERROR; | |
88 | } | |
89 | ||
90 | // Dump the packagelist | |
91 | r = cli_dump_packagelist(list, 0); | |
92 | ||
93 | ERROR: | |
94 | if (list) | |
95 | pakfire_packagelist_unref(list); | |
1f81cbb2 MT |
96 | if (pakfire) |
97 | pakfire_unref(pakfire); | |
c9abb903 MT |
98 | |
99 | return r; | |
100 | } |