build: fix handling of string options
[oweals/opkg-lede.git] / src / opkg-cl.c
1 /* opkg-cl.c - the opkg package management system
2
3    Florian Boor
4    Copyright (C) 2003 kernel concepts
5
6    Carl D. Worth
7    Copyright 2001 University of Southern California
8
9    This program is free software; you can redistribute it and/or
10    modify it under the terms of the GNU General Public License as
11    published by the Free Software Foundation; either version 2, or (at
12    your option) any later version.
13
14    This program is distributed in the hope that it will be useful, but
15    WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17    General Public License for more details.
18
19    opkg command line frontend using libopkg
20 */
21
22 #define _GNU_SOURCE
23 #include <stdio.h>
24 #include <getopt.h>
25 #include <fnmatch.h>
26
27 #include "opkg_conf.h"
28 #include "opkg_cmd.h"
29 #include "file_util.h"
30 #include "opkg_message.h"
31 #include "opkg_download.h"
32 #include "../libbb/libbb.h"
33
34 enum {
35         ARGS_OPT_FORCE_MAINTAINER = 129,
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_FORCE_POSTINSTALL,
44         ARGS_OPT_FORCE_REMOVE,
45         ARGS_OPT_FORCE_CHECKSUM,
46         ARGS_OPT_ADD_ARCH,
47         ARGS_OPT_ADD_DEST,
48         ARGS_OPT_NOACTION,
49         ARGS_OPT_DOWNLOAD_ONLY,
50         ARGS_OPT_NODEPS,
51         ARGS_OPT_NOCASE,
52         ARGS_OPT_AUTOREMOVE,
53         ARGS_OPT_CACHE,
54         ARGS_OPT_FORCE_SIGNATURE,
55         ARGS_OPT_SIZE,
56 };
57
58 static struct option long_options[] = {
59         {"query-all", 0, 0, 'A'},
60         {"autoremove", 0, 0, ARGS_OPT_AUTOREMOVE},
61         {"cache", 1, 0, ARGS_OPT_CACHE},
62         {"conf-file", 1, 0, 'f'},
63         {"conf", 1, 0, 'f'},
64         {"dest", 1, 0, 'd'},
65         {"force-maintainer", 0, 0, ARGS_OPT_FORCE_MAINTAINER},
66         {"force_maintainer", 0, 0, ARGS_OPT_FORCE_MAINTAINER},
67         {"force-depends", 0, 0, ARGS_OPT_FORCE_DEPENDS},
68         {"force_depends", 0, 0, ARGS_OPT_FORCE_DEPENDS},
69         {"force-overwrite", 0, 0, ARGS_OPT_FORCE_OVERWRITE},
70         {"force_overwrite", 0, 0, ARGS_OPT_FORCE_OVERWRITE},
71         {"force_downgrade", 0, 0, ARGS_OPT_FORCE_DOWNGRADE},
72         {"force-downgrade", 0, 0, ARGS_OPT_FORCE_DOWNGRADE},
73         {"force-reinstall", 0, 0, ARGS_OPT_FORCE_REINSTALL},
74         {"force_reinstall", 0, 0, ARGS_OPT_FORCE_REINSTALL},
75         {"force-space", 0, 0, ARGS_OPT_FORCE_SPACE},
76         {"force_space", 0, 0, ARGS_OPT_FORCE_SPACE},
77         {"recursive", 0, 0, ARGS_OPT_FORCE_REMOVAL_OF_DEPENDENT_PACKAGES},
78         {"force-removal-of-dependent-packages", 0, 0,
79          ARGS_OPT_FORCE_REMOVAL_OF_DEPENDENT_PACKAGES},
80         {"force_removal_of_dependent_packages", 0, 0,
81          ARGS_OPT_FORCE_REMOVAL_OF_DEPENDENT_PACKAGES},
82         {"force-removal-of-essential-packages", 0, 0,
83          ARGS_OPT_FORCE_REMOVAL_OF_ESSENTIAL_PACKAGES},
84         {"force_removal_of_essential_packages", 0, 0,
85          ARGS_OPT_FORCE_REMOVAL_OF_ESSENTIAL_PACKAGES},
86         {"force-postinstall", 0, 0, ARGS_OPT_FORCE_POSTINSTALL},
87         {"force_postinstall", 0, 0, ARGS_OPT_FORCE_POSTINSTALL},
88         {"force-remove", 0, 0, ARGS_OPT_FORCE_REMOVE},
89         {"force_remove", 0, 0, ARGS_OPT_FORCE_REMOVE},
90         {"force-checksum", 0, 0, ARGS_OPT_FORCE_CHECKSUM},
91         {"force_checksum", 0, 0, ARGS_OPT_FORCE_CHECKSUM},
92         {"force-signature", 0, 0, ARGS_OPT_FORCE_SIGNATURE},
93         {"force_signature", 0, 0, ARGS_OPT_FORCE_SIGNATURE},
94         {"noaction", 0, 0, ARGS_OPT_NOACTION},
95         {"download-only", 0, 0, ARGS_OPT_DOWNLOAD_ONLY},
96         {"nodeps", 0, 0, ARGS_OPT_NODEPS},
97         {"nocase", 0, 0, ARGS_OPT_NOCASE},
98         {"offline", 1, 0, 'o'},
99         {"offline-root", 1, 0, 'o'},
100         {"add-arch", 1, 0, ARGS_OPT_ADD_ARCH},
101         {"add-dest", 1, 0, ARGS_OPT_ADD_DEST},
102         {"size", 0, 0, ARGS_OPT_SIZE},
103         {"test", 0, 0, ARGS_OPT_NOACTION},
104         {"tmp-dir", 1, 0, 't'},
105         {"tmp_dir", 1, 0, 't'},
106         {"lists-dir", 1, 0, 'l'},
107         {"lists_dir", 1, 0, 'l'},
108         {"verbosity", 2, 0, 'V'},
109         {"version", 0, 0, 'v'},
110         {0, 0, 0, 0}
111 };
112
113 static int args_parse(int argc, char *argv[])
114 {
115         int c;
116         int option_index = 0;
117         int parse_err = 0;
118         char *tuple, *targ;
119
120         while (1) {
121                 c = getopt_long_only(argc, argv, "Ad:f:ino:p:l:t:vV::",
122                                      long_options, &option_index);
123                 if (c == -1)
124                         break;
125
126                 switch (c) {
127                 case 'A':
128                         conf->query_all = 1;
129                         break;
130                 case 'd':
131                         conf->dest_str = xstrdup(optarg);
132                         break;
133                 case 'f':
134                         conf->conf_file = xstrdup(optarg);
135                         break;
136                 case 'i':
137                         conf->nocase = FNM_CASEFOLD;
138                         break;
139                 case 'o':
140                         conf->offline_root = xstrdup(optarg);
141                         break;
142                 case 't':
143                         conf->tmp_dir = xstrdup(optarg);
144                         break;
145                 case 'l':
146                         conf->lists_dir = xstrdup(optarg);
147                         break;
148                 case 'v':
149                         printf("opkg version %s\n", VERSION);
150                         exit(0);
151                 case 'V':
152                         conf->verbosity = INFO;
153                         if (optarg != NULL)
154                                 conf->verbosity = atoi(optarg);
155                         break;
156                 case ARGS_OPT_AUTOREMOVE:
157                         conf->autoremove = 1;
158                         break;
159                 case ARGS_OPT_CACHE:
160                         free(conf->cache);
161                         conf->cache = xstrdup(optarg);
162                         break;
163                 case ARGS_OPT_FORCE_MAINTAINER:
164                         conf->force_maintainer = 1;
165                         break;
166                 case ARGS_OPT_FORCE_DEPENDS:
167                         conf->force_depends = 1;
168                         break;
169                 case ARGS_OPT_FORCE_OVERWRITE:
170                         conf->force_overwrite = 1;
171                         break;
172                 case ARGS_OPT_FORCE_DOWNGRADE:
173                         conf->force_downgrade = 1;
174                         break;
175                 case ARGS_OPT_FORCE_REINSTALL:
176                         conf->force_reinstall = 1;
177                         break;
178                 case ARGS_OPT_FORCE_REMOVAL_OF_ESSENTIAL_PACKAGES:
179                         conf->force_removal_of_essential_packages = 1;
180                         break;
181                 case ARGS_OPT_FORCE_REMOVAL_OF_DEPENDENT_PACKAGES:
182                         conf->force_removal_of_dependent_packages = 1;
183                         break;
184                 case ARGS_OPT_FORCE_SPACE:
185                         conf->force_space = 1;
186                         break;
187                 case ARGS_OPT_FORCE_POSTINSTALL:
188                         conf->force_postinstall = 1;
189                         break;
190                 case ARGS_OPT_FORCE_REMOVE:
191                         conf->force_remove = 1;
192                         break;
193                 case ARGS_OPT_FORCE_CHECKSUM:
194                         conf->force_checksum = 1;
195                         break;
196                 case ARGS_OPT_NODEPS:
197                         conf->nodeps = 1;
198                         break;
199                 case ARGS_OPT_NOCASE:
200                         conf->nocase = FNM_CASEFOLD;
201                         break;
202                 case ARGS_OPT_ADD_ARCH:
203                 case ARGS_OPT_ADD_DEST:
204                         tuple = xstrdup(optarg);
205                         if ((targ = strchr(tuple, ':')) != NULL) {
206                                 *targ++ = 0;
207                                 if ((strlen(tuple) > 0) && (strlen(targ) > 0)) {
208                                         nv_pair_list_append((c ==
209                                                              ARGS_OPT_ADD_ARCH)
210                                                             ? &conf->arch_list :
211                                                             &conf->tmp_dest_list,
212                                                             tuple, targ);
213                                 }
214                         }
215                         free(tuple);
216                         break;
217                 case ARGS_OPT_SIZE:
218                         conf->size = 1;
219                         break;
220                 case ARGS_OPT_NOACTION:
221                         conf->noaction = 1;
222                         break;
223                 case ARGS_OPT_DOWNLOAD_ONLY:
224                         conf->download_only = 1;
225                         break;
226                 case ARGS_OPT_FORCE_SIGNATURE:
227                         conf->force_signature = 1;
228                         break;
229                 case ':':
230                         parse_err = -1;
231                         break;
232                 case '?':
233                         parse_err = -1;
234                         break;
235                 default:
236                         printf("Confusion: getopt_long returned %d\n", c);
237                 }
238         }
239
240         if (!conf->conf_file && !conf->offline_root)
241                 conf->conf_file = xstrdup("/etc/opkg.conf");
242
243         if (parse_err)
244                 return parse_err;
245         else
246                 return optind;
247 }
248
249 static void usage()
250 {
251         printf("usage: opkg [options...] sub-command [arguments...]\n");
252         printf("where sub-command is one of:\n");
253
254         printf("\nPackage Manipulation:\n");
255         printf
256             ("\tupdate                  Update list of available packages\n");
257         printf("\tupgrade <pkgs>                Upgrade packages\n");
258         printf("\tinstall <pkgs>                Install package(s)\n");
259         printf("\tconfigure <pkgs>      Configure unpacked package(s)\n");
260         printf("\tremove <pkgs|regexp>  Remove package(s)\n");
261         printf("\tflag <flag> <pkgs>    Flag package(s)\n");
262         printf
263             ("\t <flag>=hold|noprune|user|ok|installed|unpacked (one per invocation)\n");
264
265         printf("\nInformational Commands:\n");
266         printf("\tlist                  List available packages\n");
267         printf("\tlist-installed                List installed packages\n");
268         printf
269             ("\tlist-upgradable         List installed and upgradable packages\n");
270         printf
271             ("\tlist-changed-conffiles  List user modified configuration files\n");
272         printf("\tfiles <pkg>           List files belonging to <pkg>\n");
273         printf("\tsearch <file|regexp>  List package providing <file>\n");
274         printf
275             ("\tfind <regexp>           List packages whose name or description matches <regexp>\n");
276         printf("\tinfo [pkg|regexp]     Display all info for <pkg>\n");
277         printf("\tstatus [pkg|regexp]   Display all status for <pkg>\n");
278         printf
279             ("\tdownload <pkg>          Download <pkg> to current directory\n");
280         printf("\tcompare-versions <v1> <op> <v2>\n");
281         printf
282             ("\t                    compare versions using <= < > >= = << >>\n");
283         printf
284             ("\tprint-architecture      List installable package architectures\n");
285         printf("\tdepends [-A] [pkgname|pat]+\n");
286         printf("\twhatdepends [-A] [pkgname|pat]+\n");
287         printf("\twhatdependsrec [-A] [pkgname|pat]+\n");
288         printf("\twhatrecommends[-A] [pkgname|pat]+\n");
289         printf("\twhatsuggests[-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
294         printf("\nOptions:\n");
295         printf
296             ("\t-A                      Query all packages not just those installed\n");
297         printf("\t-V[<level>]           Set verbosity level to <level>.\n");
298         printf("\t--verbosity[=<level>] Verbosity levels:\n");
299         printf("\t                              0 errors only\n");
300         printf("\t                              1 normal messages (default)\n");
301         printf("\t                              2 informative messages\n");
302         printf("\t                              3 debug\n");
303         printf("\t                              4 debug level 2\n");
304         printf
305             ("\t-f <conf_file>          Use <conf_file> as the opkg configuration file\n");
306         printf("\t--conf <conf_file>\n");
307         printf("\t--cache <directory>   Use a package cache\n");
308         printf
309             ("\t-d <dest_name>          Use <dest_name> as the the root directory for\n");
310         printf
311             ("\t--dest <dest_name>      package installation, removal, upgrading.\n");
312         printf
313             ("                          <dest_name> should be a defined dest name from\n");
314         printf
315             ("                          the configuration file, (but can also be a\n");
316         printf("                                directory name in a pinch).\n");
317         printf("\t-o <dir>              Use <dir> as the root directory for\n");
318         printf("\t--offline-root <dir>  offline installation of packages.\n");
319         printf
320             ("\t--add-arch <arch>:<prio>        Register architecture with given priority\n");
321         printf
322             ("\t--add-dest <name>:<path>        Register destination with given path\n");
323
324         printf("\nForce Options:\n");
325         printf
326             ("\t--force-depends         Install/remove despite failed dependencies\n");
327         printf("\t--force-maintainer    Overwrite preexisting config files\n");
328         printf("\t--force-reinstall     Reinstall package(s)\n");
329         printf
330             ("\t--force-overwrite       Overwrite files from other package(s)\n");
331         printf("\t--force-downgrade     Allow opkg to downgrade packages\n");
332         printf("\t--force-space         Disable free space checks\n");
333         printf
334             ("\t--force-postinstall     Run postinstall scripts even in offline mode\n");
335         printf
336             ("\t--force-remove  Remove package even if prerm script fails\n");
337         printf("\t--force-checksum      Don't fail on checksum mismatches\n");
338         printf("\t--noaction            No action -- test only\n");
339         printf("\t--download-only       No action -- download only\n");
340         printf("\t--nodeps              Do not follow dependencies\n");
341         printf
342             ("\t--nocase                Perform case insensitive pattern matching\n");
343         printf
344             ("\t--size                  Print package size when listing available packages\n");
345         printf("\t--force-removal-of-dependent-packages\n");
346         printf("\t                      Remove package and all dependencies\n");
347         printf("\t--autoremove          Remove packages that were installed\n");
348         printf
349             ("\t                        automatically to satisfy dependencies\n");
350         printf("\t-t                    Specify tmp-dir.\n");
351         printf("\t--tmp-dir             Specify tmp-dir.\n");
352         printf("\t-l                    Specify lists-dir.\n");
353         printf("\t--lists-dir           Specify lists-dir.\n");
354
355         printf("\n");
356
357         printf
358             (" regexp could be something like 'pkgname*' '*file*' or similar\n");
359         printf
360             (" e.g. opkg info 'libstd*' or opkg search '*libop*' or opkg remove 'libncur*'\n");
361
362         /* --force-removal-of-essential-packages        Let opkg remove essential packages.
363            Using this option is almost guaranteed to break your system, hence this option
364            is not even advertised in the usage statement. */
365
366         exit(1);
367 }
368
369 int main(int argc, char *argv[])
370 {
371         int opts, err = -1;
372         char *cmd_name;
373         opkg_cmd_t *cmd;
374         int nocheckfordirorfile = 0;
375         int noreadfeedsfile = 0;
376
377         if (opkg_conf_init())
378                 goto err0;
379
380         conf->verbosity = NOTICE;
381
382         opts = args_parse(argc, argv);
383         if (opts == argc || opts < 0) {
384                 fprintf(stderr, "opkg must have one sub-command argument\n");
385                 usage();
386         }
387
388         cmd_name = argv[opts++];
389
390         if (!strcmp(cmd_name, "install") ||
391             !strcmp(cmd_name, "print-architecture") ||
392             !strcmp(cmd_name, "print_architecture") ||
393             !strcmp(cmd_name, "print-installation-architecture") ||
394             !strcmp(cmd_name, "print_installation_architecture"))
395                 nocheckfordirorfile = 1;
396
397         if (!strcmp(cmd_name, "flag") ||
398             !strcmp(cmd_name, "configure") ||
399             !strcmp(cmd_name, "install") ||
400             !strcmp(cmd_name, "remove") ||
401             !strcmp(cmd_name, "files") ||
402             !strcmp(cmd_name, "search") ||
403             !strcmp(cmd_name, "compare_versions") ||
404             !strcmp(cmd_name, "compare-versions") ||
405             !strcmp(cmd_name, "list_installed") ||
406             !strcmp(cmd_name, "list-installed") ||
407             !strcmp(cmd_name, "list_changed_conffiles") ||
408             !strcmp(cmd_name, "list-changed-conffiles") ||
409             !strcmp(cmd_name, "status"))
410                 noreadfeedsfile = 1;
411
412         cmd = opkg_cmd_find(cmd_name);
413         if (cmd == NULL) {
414                 fprintf(stderr, "%s: unknown sub-command %s\n", argv[0],
415                         cmd_name);
416                 usage();
417         }
418
419         conf->pfm = cmd->pfm;
420
421         if (opkg_conf_load())
422                 goto err0;
423
424         if (!nocheckfordirorfile) {
425                 if (!noreadfeedsfile) {
426                         if (pkg_hash_load_feeds(SF_NEED_DETAIL))
427                                 goto err1;
428                 }
429
430                 if (pkg_hash_load_status_files())
431                         goto err1;
432         }
433
434         if (cmd->requires_args && opts == argc) {
435                 fprintf(stderr,
436                         "%s: the ``%s'' command requires at least one argument\n",
437                         argv[0], cmd_name);
438                 usage();
439         }
440
441         err = opkg_cmd_exec(cmd, argc - opts, (const char **)(argv + opts));
442
443 #ifdef HAVE_CURL
444         opkg_curl_cleanup();
445 #endif
446 err1:
447         opkg_conf_deinit();
448
449 err0:
450         print_error_list();
451         free_error_list();
452
453         return err;
454 }