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