s/strdup/xstrdup/ - check memory allocations for failure.
[project/opkg-lede.git] / libopkg / args.c
1 /* args.c - parse command-line args
2
3 Carl D. Worth
4
5 Copyright 2001 University of Southern California
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 2, or (at your option)
10 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
18 #include <getopt.h>
19 #include <stdlib.h>
20 #include <string.h>
21 #include <unistd.h>
22
23 #include "includes.h"
24
25 #include "config.h"
26 #include "args.h"
27 #include "sprintf_alloc.h"
28 #include "libbb/libbb.h"
29
30 static void print_version(void);
31
32 enum long_args_opt
33 {
34 ARGS_OPT_FORCE_DEFAULTS = 129,
35 ARGS_OPT_FORCE_MAINTAINER,
36 ARGS_OPT_FORCE_DEPENDS,
37 ARGS_OPT_FORCE_OVERWRITE,
38 ARGS_OPT_FORCE_DOWNGRADE,
39 ARGS_OPT_FORCE_REINSTALL,
40 ARGS_OPT_FORCE_REMOVAL_OF_DEPENDENT_PACKAGES,
41 ARGS_OPT_FORCE_REMOVAL_OF_ESSENTIAL_PACKAGES,
42 ARGS_OPT_FORCE_SPACE,
43 ARGS_OPT_NOACTION,
44 ARGS_OPT_NODEPS,
45 ARGS_OPT_VERBOSITY,
46 ARGS_OPT_MULTIPLE_PROVIDERS,
47 ARGS_OPT_AUTOREMOVE,
48 ARGS_OPT_CACHE,
49 };
50
51 char *conf_file_dir;
52
53 int args_init(args_t *args)
54 {
55 if (!args) {
56 return EFAULT;
57 }
58 memset(args, 0, sizeof(args_t));
59
60 args->dest = ARGS_DEFAULT_DEST;
61
62 conf_file_dir = getenv("OPKG_CONF_DIR");
63 if (conf_file_dir == NULL || conf_file_dir[0] == '\0') {
64 conf_file_dir = ARGS_DEFAULT_CONF_FILE_DIR;
65 }
66 sprintf_alloc(&args->conf_file, "%s/%s", OPKGETCDIR,
67 ARGS_DEFAULT_CONF_FILE_NAME);
68
69 args->force_defaults = ARGS_DEFAULT_FORCE_DEFAULTS;
70 args->force_maintainer = ARGS_DEFAULT_FORCE_MAINTAINER;
71 args->force_depends = ARGS_DEFAULT_FORCE_DEPENDS;
72 args->force_overwrite = ARGS_DEFAULT_FORCE_OVERWRITE;
73 args->force_downgrade = ARGS_DEFAULT_FORCE_DOWNGRADE;
74 args->force_reinstall = ARGS_DEFAULT_FORCE_REINSTALL;
75 args->force_removal_of_dependent_packages = ARGS_DEFAULT_FORCE_REMOVAL_OF_DEPENDENT_PACKAGES;
76 args->force_removal_of_essential_packages = ARGS_DEFAULT_FORCE_REMOVAL_OF_ESSENTIAL_PACKAGES;
77 args->autoremove = ARGS_DEFAULT_AUTOREMOVE;
78 args->noaction = ARGS_DEFAULT_NOACTION;
79 args->nodeps = ARGS_DEFAULT_NODEPS;
80 args->verbosity = ARGS_DEFAULT_VERBOSITY;
81 args->offline_root = ARGS_DEFAULT_OFFLINE_ROOT;
82 args->offline_root_path = ARGS_DEFAULT_OFFLINE_ROOT_PATH;
83 args->offline_root_pre_script_cmd = ARGS_DEFAULT_OFFLINE_ROOT_PRE_SCRIPT_CMD;
84 args->offline_root_post_script_cmd = ARGS_DEFAULT_OFFLINE_ROOT_POST_SCRIPT_CMD;
85 args->multiple_providers = 0;
86 args->nocheckfordirorfile = 0;
87 args->noreadfeedsfile = 0;
88
89 return 0;
90 }
91
92 void args_deinit(args_t *args)
93 {
94 free (args->offline_root);
95 free (args->offline_root_path);
96 free (args->offline_root_pre_script_cmd);
97 free (args->offline_root_post_script_cmd);
98
99 free (args->dest);
100 free (args->tmp_dir);
101 free (args->cache);
102 free(args->conf_file);
103 args->conf_file = NULL;
104 }
105
106 int args_parse(args_t *args, int argc, char *argv[])
107 {
108 int c;
109 int option_index = 0;
110 int parse_err = 0;
111 static struct option long_options[] = {
112 {"query-all", 0, 0, 'A'},
113 {"autoremove", 0, 0, ARGS_OPT_AUTOREMOVE},
114 {"cache", 1, 0, ARGS_OPT_CACHE},
115 {"conf-file", 1, 0, 'f'},
116 {"conf", 1, 0, 'f'},
117 {"dest", 1, 0, 'd'},
118 {"force-defaults", 0, 0, ARGS_OPT_FORCE_DEFAULTS},
119 {"force_defaults", 0, 0, ARGS_OPT_FORCE_DEFAULTS},
120 {"force-maintainer", 0, 0, ARGS_OPT_FORCE_MAINTAINER},
121 {"force_maintainer", 0, 0, ARGS_OPT_FORCE_MAINTAINER},
122 {"force-depends", 0, 0, ARGS_OPT_FORCE_DEPENDS},
123 {"force_depends", 0, 0, ARGS_OPT_FORCE_DEPENDS},
124 {"force-overwrite", 0, 0, ARGS_OPT_FORCE_OVERWRITE},
125 {"force_overwrite", 0, 0, ARGS_OPT_FORCE_OVERWRITE},
126 {"force_downgrade", 0, 0, ARGS_OPT_FORCE_DOWNGRADE},
127 {"force-downgrade", 0, 0, ARGS_OPT_FORCE_DOWNGRADE},
128 {"force-reinstall", 0, 0, ARGS_OPT_FORCE_REINSTALL},
129 {"force_reinstall", 0, 0, ARGS_OPT_FORCE_REINSTALL},
130 {"force-space", 0, 0, ARGS_OPT_FORCE_SPACE},
131 {"force_space", 0, 0, ARGS_OPT_FORCE_SPACE},
132 {"recursive", 0, 0,
133 ARGS_OPT_FORCE_REMOVAL_OF_DEPENDENT_PACKAGES},
134 {"force-removal-of-dependent-packages", 0, 0,
135 ARGS_OPT_FORCE_REMOVAL_OF_DEPENDENT_PACKAGES},
136 {"force_removal_of_dependent_packages", 0, 0,
137 ARGS_OPT_FORCE_REMOVAL_OF_DEPENDENT_PACKAGES},
138 {"force-removal-of-essential-packages", 0, 0,
139 ARGS_OPT_FORCE_REMOVAL_OF_ESSENTIAL_PACKAGES},
140 {"force_removal_of_essential_packages", 0, 0,
141 ARGS_OPT_FORCE_REMOVAL_OF_ESSENTIAL_PACKAGES},
142 {"multiple-providers", 0, 0, ARGS_OPT_MULTIPLE_PROVIDERS},
143 {"multiple_providers", 0, 0, ARGS_OPT_MULTIPLE_PROVIDERS},
144 {"noaction", 0, 0, ARGS_OPT_NOACTION},
145 {"nodeps", 0, 0, ARGS_OPT_NODEPS},
146 {"offline", 1, 0, 'o'},
147 {"offline-root", 1, 0, 'o'},
148 {"offline-path", 1, 0, 'p'},
149 {"offline-root-path", 1, 0, 'p'},
150 {"test", 0, 0, ARGS_OPT_NOACTION},
151 {"tmp-dir", 1, 0, 't'},
152 {"verbosity", 2, 0, 'V'},
153 {"version", 0, 0, 'v'},
154 {0, 0, 0, 0}
155 };
156
157 while (1) {
158 c = getopt_long_only(argc, argv, "Ad:f:no:p:t:vV:", long_options, &option_index);
159 if (c == -1)
160 break;
161
162 switch (c) {
163 case 'A':
164 args->query_all = 1;
165 break;
166 case 'd':
167 args->dest = xstrdup(optarg);
168 break;
169 case 'f':
170 free(args->conf_file);
171 args->conf_file = xstrdup(optarg);
172 break;
173 case 'o':
174 args->offline_root = xstrdup(optarg);
175 break;
176 case 'p':
177 args->offline_root_path = xstrdup(optarg);
178 break;
179 case 'n':
180 args->noaction = 1;
181 break;
182 case 't':
183 args->tmp_dir = xstrdup(optarg);
184 break;
185 case 'v':
186 print_version();
187 exit(0);
188 case 'V':
189 case ARGS_OPT_VERBOSITY:
190 if (optarg)
191 args->verbosity = atoi(optarg);
192 else
193 args->verbosity += 1;
194 break;
195 case ARGS_OPT_AUTOREMOVE:
196 args->autoremove = 1;
197 break;
198 case ARGS_OPT_CACHE:
199 free(args->cache);
200 args->cache = xstrdup(optarg);
201 break;
202 case ARGS_OPT_FORCE_DEFAULTS:
203 args->force_defaults = 1;
204 break;
205 case ARGS_OPT_FORCE_MAINTAINER:
206 args->force_maintainer = 1;
207 break;
208 case ARGS_OPT_FORCE_DEPENDS:
209 args->force_depends = 1;
210 break;
211 case ARGS_OPT_FORCE_OVERWRITE:
212 args->force_overwrite = 1;
213 break;
214 case ARGS_OPT_FORCE_DOWNGRADE:
215 args->force_downgrade = 1;
216 break;
217 case ARGS_OPT_FORCE_REINSTALL:
218 args->force_reinstall = 1;
219 break;
220 case ARGS_OPT_FORCE_REMOVAL_OF_ESSENTIAL_PACKAGES:
221 args->force_removal_of_essential_packages = 1;
222 break;
223 case ARGS_OPT_FORCE_REMOVAL_OF_DEPENDENT_PACKAGES:
224 args->force_removal_of_dependent_packages = 1;
225 break;
226 case ARGS_OPT_FORCE_SPACE:
227 args->force_space = 1;
228 break;
229 case ARGS_OPT_MULTIPLE_PROVIDERS:
230 args->multiple_providers = 1;
231 break;
232 case ARGS_OPT_NODEPS:
233 args->nodeps = 1;
234 break;
235 case ARGS_OPT_NOACTION:
236 args->noaction = 1;
237 break;
238 case ':':
239 parse_err++;
240 break;
241 case '?':
242 parse_err++;
243 break;
244 default:
245 printf("Confusion: getopt_long returned %d\n", c);
246 }
247 }
248
249 if (parse_err) {
250 return -parse_err;
251 } else {
252 return optind;
253 }
254 }
255
256 void args_usage(char *complaint)
257 {
258 if (complaint) {
259 printf("opkg: %s\n", complaint);
260 }
261 print_version();
262 printf("usage: opkg [options...] sub-command [arguments...]\n");
263 printf("where sub-command is one of:\n");
264
265 printf("\nPackage Manipulation:\n");
266 printf("\tupdate Update list of available packages\n");
267 printf("\tupgrade Upgrade all installed packages to latest version\n");
268 printf("\tinstall <pkg> Download and install <pkg> (and dependencies)\n");
269 printf("\tinstall <file.opk> Install package <file.opk>\n");
270 printf("\tconfigure [<pkg>] Configure unpacked packages\n");
271 printf("\tremove <pkg|regexp> Remove package <pkg|packages following regexp>\n");
272 printf("\tflag <flag> <pkg> ... Flag package(s) <pkg>\n");
273 printf("\t <flag>=hold|noprune|user|ok|installed|unpacked (one per invocation) \n");
274
275 printf("\nInformational Commands:\n");
276 printf("\tlist List available packages and descriptions\n");
277 printf("\tlist_installed List all and only the installed packages and description \n");
278 printf("\tlist_upgradable List all the installed and upgradable packages\n");
279 printf("\tfiles <pkg> List all files belonging to <pkg>\n");
280 printf("\tsearch <file|regexp> Search for a package providing <file>\n");
281 printf("\tinfo [pkg|regexp] Display all info for <pkg>\n");
282 printf("\tstatus [pkg|regexp] Display all status for <pkg>\n");
283 printf("\tdownload <pkg> Download <pkg> to current directory.\n");
284 printf("\tcompare_versions <v1> <op> <v2>\n");
285 printf("\t compare versions using <= < > >= = << >>\n");
286 printf("\tprint_architecture prints the architecture.\n");
287 printf("\tprint_installation_architecture\n");
288 printf("\twhatdepends [-A] [pkgname|pat]+\n");
289 printf("\twhatdependsrec [-A] [pkgname|pat]+\n");
290 printf("\twhatprovides [-A] [pkgname|pat]+\n");
291 printf("\twhatconflicts [-A] [pkgname|pat]+\n");
292 printf("\twhatreplaces [-A] [pkgname|pat]+\n");
293 printf("\t prints the installation architecture.\n");
294 printf("\nOptions:\n");
295 printf("\t-A Query all packages with whatdepends, whatprovides, whatreplaces, whatconflicts\n");
296 printf("\t-V <level> Set verbosity level to <level>. If no value is\n");
297 printf("\t--verbosity <level> provided increase verbosity by one. Verbosity levels:\n");
298 printf("\t 0 errors only\n");
299 printf("\t 1 normal messages (default)\n");
300 printf("\t 2 informative messages\n");
301 printf("\t 3 debug output\n");
302 printf("\t-f <conf_file> Use <conf_file> as the opkg configuration file\n");
303 printf("\t--cache <directory> Use a package cache\n");
304 printf("\t-conf <conf_file> Default configuration file location\n");
305 printf(" is %s/%s\n", ARGS_DEFAULT_CONF_FILE_DIR, ARGS_DEFAULT_CONF_FILE_NAME);
306 printf("\t-d <dest_name> Use <dest_name> as the the root directory for\n");
307 printf("\t-dest <dest_name> package installation, removal, upgrading.\n");
308 printf(" <dest_name> should be a defined dest name from\n");
309 printf(" the configuration file, (but can also be a\n");
310 printf(" directory name in a pinch).\n");
311 printf("\t-o <offline_root> Use <offline_root> as the root directory for\n");
312 printf("\t-offline <offline_root> offline installation of packages.\n");
313 printf("\t-p <path> Path to utilities for runing postinst\n");
314 printf("\t-offline-path <path> script in offline mode.\n");
315
316 printf("\nForce Options (use when opkg is too smart for its own good):\n");
317 printf("\t-force-depends Make dependency checks warnings instead of errors\n");
318 printf("\t Install/remove package in spite of failed dependences\n");
319 printf("\t-force-defaults Use default options for questions asked by opkg.\n");
320 printf(" (no prompts). Note that this will not prevent\n");
321 printf(" package installation scripts from prompting.\n");
322 printf("\t-force-reinstall Allow opkg to reinstall a package.\n");
323 printf("\t-force-overwrite Allow opkg to overwrite files from another package during an install.\n");
324 printf("\t-force-downgrade Allow opkg to downgrade packages.\n");
325 printf("\t-force_space Install even if there does not seem to be enough space.\n");
326 printf("\t-noaction No action -- test only\n");
327 printf("\t-nodeps Do not follow dependences\n");
328 printf("\t-force-removal-of-dependent-packages\n");
329 printf("\t-recursive Allow opkg to remove package and all that depend on it.\n");
330 printf("\t-autoremove Allow opkg to remove packages that where installed automatically to satisfy dependencies.\n");
331 printf("\t-test No action -- test only\n");
332 printf("\t-t Specify tmp-dir.\n");
333 printf("\t--tmp-dir Specify tmp-dir.\n");
334 printf("\n");
335 printf("\tregexp could be something like 'pkgname*' '*file*' or similar\n");
336 printf("\teg: opkg info 'libstd*' or opkg search '*libop*' or opkg remove 'libncur*'\n");
337 /* -force-removal-of-essential-packages Let opkg remove essential packages.
338 Using this option is almost guaranteed to break your system, hence this option
339 is not even advertised in the usage statement. */
340 exit(1);
341 }
342
343 static void print_version(void)
344 {
345 printf("opkg version %s\n", VERSION);
346 }