]> git.ipfire.org Git - people/ms/pakfire.git/blame - src/cli/lib/shell.c
cli: Check for root privileges when needed
[people/ms/pakfire.git] / src / cli / lib / shell.c
CommitLineData
beee2bda
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
15b7db71 21#include <argp.h>
beee2bda
MT
22
23#include "command.h"
15b7db71 24#include "pakfire.h"
beee2bda
MT
25#include "shell.h"
26
27#include <pakfire/build.h>
28#include <pakfire/pakfire.h>
29
30#define MAX_PACKAGES 128
31
32struct config {
33 int flags;
15b7db71
MT
34
35 // Packages
beee2bda
MT
36 const char* packages[MAX_PACKAGES];
37 unsigned int num_packages;
38};
39
15b7db71
MT
40static const char* doc =
41 "Creates a build environment and launches an interactive shell in it";
beee2bda 42
15b7db71
MT
43enum {
44 OPT_DISABLE_SNAPSHOT = 1,
45 OPT_INSTALL = 2,
46};
beee2bda 47
15b7db71
MT
48static 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};
beee2bda 53
d673ca6c 54static error_t parse(int key, char* arg, struct argp_state* state, void* data) {
15b7db71 55 struct config* config = data;
beee2bda 56
15b7db71
MT
57 switch (key) {
58 case OPT_DISABLE_SNAPSHOT:
59 config->flags |= PAKFIRE_BUILD_DISABLE_SNAPSHOT;
60 break;
beee2bda 61
15b7db71
MT
62 case ARGP_KEY_ARG:
63 if (config->num_packages >= MAX_PACKAGES)
64 return -ENOBUFS;
beee2bda 65
15b7db71
MT
66 config->packages[config->num_packages++] = arg;
67 break;
beee2bda 68
15b7db71
MT
69 default:
70 return ARGP_ERR_UNKNOWN;
beee2bda
MT
71 }
72
73 return 0;
74}
75
15b7db71
MT
76int 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
beee2bda 82 struct config config = {
15b7db71
MT
83 .flags = 0,
84 .packages = { NULL },
beee2bda
MT
85 .num_packages = 0,
86 };
beee2bda 87
15b7db71 88 // Parse the command line
9b8d2c6e 89 r = cli_parse(options, NULL, NULL, doc, parse, 0, argc, argv, &config);
beee2bda 90 if (r)
15b7db71
MT
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
103ERROR:
104 if (pakfire)
105 pakfire_unref(pakfire);
beee2bda 106
15b7db71 107 return r;
beee2bda 108}