]> git.ipfire.org Git - people/ms/pakfire.git/blob - src/cli/lib/shell.c
cli: Check for root privileges when needed
[people/ms/pakfire.git] / src / cli / lib / shell.c
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
21 #include <argp.h>
22
23 #include "command.h"
24 #include "pakfire.h"
25 #include "shell.h"
26
27 #include <pakfire/build.h>
28 #include <pakfire/pakfire.h>
29
30 #define MAX_PACKAGES 128
31
32 struct config {
33 int flags;
34
35 // Packages
36 const char* packages[MAX_PACKAGES];
37 unsigned int num_packages;
38 };
39
40 static const char* doc =
41 "Creates a build environment and launches an interactive shell in it";
42
43 enum {
44 OPT_DISABLE_SNAPSHOT = 1,
45 OPT_INSTALL = 2,
46 };
47
48 static struct argp_option options[] = {
49 { "disable-snapshot", OPT_DISABLE_SNAPSHOT, NULL, 0, "Do not use the snapshot", 0 },
50 { "install", OPT_INSTALL, "PACKAGE", 0, "Install this package", 0 },
51 { NULL },
52 };
53
54 static error_t parse(int key, char* arg, struct argp_state* state, void* data) {
55 struct config* config = data;
56
57 switch (key) {
58 case OPT_DISABLE_SNAPSHOT:
59 config->flags |= PAKFIRE_BUILD_DISABLE_SNAPSHOT;
60 break;
61
62 case ARGP_KEY_ARG:
63 if (config->num_packages >= MAX_PACKAGES)
64 return -ENOBUFS;
65
66 config->packages[config->num_packages++] = arg;
67 break;
68
69 default:
70 return ARGP_ERR_UNKNOWN;
71 }
72
73 return 0;
74 }
75
76 int cli_shell(void* data, int argc, char* argv[]) {
77 struct pakfire* pakfire = NULL;
78 int r;
79
80 struct cli_config* cli_config = data;
81
82 struct config config = {
83 .flags = 0,
84 .packages = { NULL },
85 .num_packages = 0,
86 };
87
88 // Parse the command line
89 r = cli_parse(options, NULL, NULL, doc, parse, 0, argc, argv, &config);
90 if (r)
91 goto ERROR;
92
93 // Setup pakfire
94 r = cli_setup_pakfire(&pakfire, cli_config);
95 if (r)
96 goto ERROR;
97
98 // Run the shell
99 r = pakfire_shell(pakfire, config.packages, config.flags);
100 if (r)
101 goto ERROR;
102
103 ERROR:
104 if (pakfire)
105 pakfire_unref(pakfire);
106
107 return r;
108 }