993aa66d2a2c65a878ef5dec71bd9ade3ddd0694
[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 #include "config.h"
23
24 #include <stdio.h>
25 #include <getopt.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_ADD_ARCH,
46         ARGS_OPT_ADD_DEST,
47         ARGS_OPT_NOACTION,
48         ARGS_OPT_DOWNLOAD_ONLY,
49         ARGS_OPT_NODEPS,
50         ARGS_OPT_AUTOREMOVE,
51         ARGS_OPT_CACHE,
52 };
53
54 static struct option long_options[] = {
55         {"query-all", 0, 0, 'A'},
56         {"autoremove", 0, 0, ARGS_OPT_AUTOREMOVE},
57         {"cache", 1, 0, ARGS_OPT_CACHE},
58         {"conf-file", 1, 0, 'f'},
59         {"conf", 1, 0, 'f'},
60         {"dest", 1, 0, 'd'},
61         {"force-maintainer", 0, 0, ARGS_OPT_FORCE_MAINTAINER},
62         {"force_maintainer", 0, 0, ARGS_OPT_FORCE_MAINTAINER},
63         {"force-depends", 0, 0, ARGS_OPT_FORCE_DEPENDS},
64         {"force_depends", 0, 0, ARGS_OPT_FORCE_DEPENDS},
65         {"force-overwrite", 0, 0, ARGS_OPT_FORCE_OVERWRITE},
66         {"force_overwrite", 0, 0, ARGS_OPT_FORCE_OVERWRITE},
67         {"force_downgrade", 0, 0, ARGS_OPT_FORCE_DOWNGRADE},
68         {"force-downgrade", 0, 0, ARGS_OPT_FORCE_DOWNGRADE},
69         {"force-reinstall", 0, 0, ARGS_OPT_FORCE_REINSTALL},
70         {"force_reinstall", 0, 0, ARGS_OPT_FORCE_REINSTALL},
71         {"force-space", 0, 0, ARGS_OPT_FORCE_SPACE},
72         {"force_space", 0, 0, ARGS_OPT_FORCE_SPACE},
73         {"recursive", 0, 0, ARGS_OPT_FORCE_REMOVAL_OF_DEPENDENT_PACKAGES},
74         {"force-removal-of-dependent-packages", 0, 0,
75                 ARGS_OPT_FORCE_REMOVAL_OF_DEPENDENT_PACKAGES},
76         {"force_removal_of_dependent_packages", 0, 0,
77                 ARGS_OPT_FORCE_REMOVAL_OF_DEPENDENT_PACKAGES},
78         {"force-removal-of-essential-packages", 0, 0,
79                 ARGS_OPT_FORCE_REMOVAL_OF_ESSENTIAL_PACKAGES},
80         {"force_removal_of_essential_packages", 0, 0,
81                 ARGS_OPT_FORCE_REMOVAL_OF_ESSENTIAL_PACKAGES},
82         {"force-postinstall", 0, 0, ARGS_OPT_FORCE_POSTINSTALL},
83         {"force_postinstall", 0, 0, ARGS_OPT_FORCE_POSTINSTALL},
84         {"force-remove", 0, 0, ARGS_OPT_FORCE_REMOVE},
85         {"force_remove", 0, 0, ARGS_OPT_FORCE_REMOVE},
86         {"noaction", 0, 0, ARGS_OPT_NOACTION},
87         {"download-only", 0, 0, ARGS_OPT_DOWNLOAD_ONLY},
88         {"nodeps", 0, 0, ARGS_OPT_NODEPS},
89         {"offline", 1, 0, 'o'},
90         {"offline-root", 1, 0, 'o'},
91         {"add-arch", 1, 0, ARGS_OPT_ADD_ARCH},
92         {"add-dest", 1, 0, ARGS_OPT_ADD_DEST},
93         {"test", 0, 0, ARGS_OPT_NOACTION},
94         {"tmp-dir", 1, 0, 't'},
95         {"tmp_dir", 1, 0, 't'},
96         {"verbosity", 2, 0, 'V'},
97         {"version", 0, 0, 'v'},
98         {0, 0, 0, 0}
99 };
100
101 static int
102 args_parse(int argc, char *argv[])
103 {
104         int c;
105         int option_index = 0;
106         int parse_err = 0;
107         char *tuple, *targ;
108
109         while (1) {
110                 c = getopt_long_only(argc, argv, "Ad:f:no:p:t:vV::",
111                                 long_options, &option_index);
112                 if (c == -1)
113                         break;
114
115                 switch (c) {
116                 case 'A':
117                         conf->query_all = 1;
118                         break;
119                 case 'd':
120                         conf->dest_str = xstrdup(optarg);
121                         break;
122                 case 'f':
123                         conf->conf_file = xstrdup(optarg);
124                         break;
125                 case 'o':
126                         conf->offline_root = xstrdup(optarg);
127                         break;
128                 case 't':
129                         conf->tmp_dir = xstrdup(optarg);
130                         break;
131                 case 'v':
132                         printf("opkg version %s\n", VERSION);
133                         exit(0);
134                 case 'V':
135                         conf->verbosity = INFO;
136                         if (optarg != NULL)
137                                 conf->verbosity = atoi(optarg);
138                         break;
139                 case ARGS_OPT_AUTOREMOVE:
140                         conf->autoremove = 1;
141                         break;
142                 case ARGS_OPT_CACHE:
143                         free(conf->cache);
144                         conf->cache = xstrdup(optarg);
145                         break;
146                 case ARGS_OPT_FORCE_MAINTAINER:
147                         conf->force_maintainer = 1;
148                         break;
149                 case ARGS_OPT_FORCE_DEPENDS:
150                         conf->force_depends = 1;
151                         break;
152                 case ARGS_OPT_FORCE_OVERWRITE:
153                         conf->force_overwrite = 1;
154                         break;
155                 case ARGS_OPT_FORCE_DOWNGRADE:
156                         conf->force_downgrade = 1;
157                         break;
158                 case ARGS_OPT_FORCE_REINSTALL:
159                         conf->force_reinstall = 1;
160                         break;
161                 case ARGS_OPT_FORCE_REMOVAL_OF_ESSENTIAL_PACKAGES:
162                         conf->force_removal_of_essential_packages = 1;
163                         break;
164                 case ARGS_OPT_FORCE_REMOVAL_OF_DEPENDENT_PACKAGES:
165                         conf->force_removal_of_dependent_packages = 1;
166                         break;
167                 case ARGS_OPT_FORCE_SPACE:
168                         conf->force_space = 1;
169                         break;
170                 case ARGS_OPT_FORCE_POSTINSTALL:
171                         conf->force_postinstall = 1;
172                         break;
173                 case ARGS_OPT_FORCE_REMOVE:
174                         conf->force_remove = 1;
175                         break;
176                 case ARGS_OPT_NODEPS:
177                         conf->nodeps = 1;
178                         break;
179                 case ARGS_OPT_ADD_ARCH:
180                 case ARGS_OPT_ADD_DEST:
181                         tuple = xstrdup(optarg);
182                         if ((targ = strchr(tuple, ':')) != NULL) {
183                                 *targ++ = 0;
184                                 if ((strlen(tuple) > 0) && (strlen(targ) > 0)) {
185                                         nv_pair_list_append(
186                                                 (c == ARGS_OPT_ADD_ARCH)
187                                                         ? &conf->arch_list : &conf->tmp_dest_list,
188                                                 tuple, targ);
189                                 }
190                         }
191                         free(tuple);
192                         break;
193                 case ARGS_OPT_NOACTION:
194                         conf->noaction = 1;
195                         break;
196         case ARGS_OPT_DOWNLOAD_ONLY:
197                         conf->download_only = 1;
198                         break;
199                 case ':':
200                         parse_err = -1;
201                         break;
202                 case '?':
203                         parse_err = -1;
204                         break;
205                 default:
206                         printf("Confusion: getopt_long returned %d\n", c);
207                 }
208         }
209
210         if (parse_err)
211                 return parse_err;
212         else
213                 return optind;
214 }
215
216 static void
217 usage()
218 {
219         printf("usage: opkg [options...] sub-command [arguments...]\n");
220         printf("where sub-command is one of:\n");
221
222         printf("\nPackage Manipulation:\n");
223         printf("\tupdate                        Update list of available packages\n");
224         printf("\tupgrade                       Upgrade installed packages\n");
225         printf("\tinstall <pkgs>                Install package(s)\n");
226         printf("\tconfigure <pkgs>      Configure unpacked package(s)\n");
227         printf("\tremove <pkgs|regexp>  Remove package(s)\n");
228         printf("\tflag <flag> <pkgs>    Flag package(s)\n");
229         printf("\t <flag>=hold|noprune|user|ok|installed|unpacked (one per invocation)\n");
230
231         printf("\nInformational Commands:\n");
232         printf("\tlist                  List available packages\n");
233         printf("\tlist-installed                List installed packages\n");
234         printf("\tlist-upgradable               List installed and upgradable packages\n");
235         printf("\tlist-changed-conffiles        List user modified configuration files\n");
236         printf("\tfiles <pkg>           List files belonging to <pkg>\n");
237         printf("\tsearch <file|regexp>  List package providing <file>\n");
238         printf("\tinfo [pkg|regexp]     Display all info for <pkg>\n");
239         printf("\tstatus [pkg|regexp]   Display all status for <pkg>\n");
240         printf("\tdownload <pkg>                Download <pkg> to current directory\n");
241         printf("\tcompare-versions <v1> <op> <v2>\n");
242         printf("\t                    compare versions using <= < > >= = << >>\n");
243         printf("\tprint-architecture    List installable package architectures\n");
244         printf("\twhatdepends [-A] [pkgname|pat]+\n");
245         printf("\twhatdependsrec [-A] [pkgname|pat]+\n");
246         printf("\twhatprovides [-A] [pkgname|pat]+\n");
247         printf("\twhatconflicts [-A] [pkgname|pat]+\n");
248         printf("\twhatreplaces [-A] [pkgname|pat]+\n");
249
250         printf("\nOptions:\n");
251         printf("\t-A                    Query all packages not just those installed\n");
252         printf("\t-V[<level>]           Set verbosity level to <level>.\n");
253         printf("\t--verbosity[=<level>] Verbosity levels:\n");
254         printf("\t                              0 errors only\n");
255         printf("\t                              1 normal messages (default)\n");
256         printf("\t                              2 informative messages\n");
257         printf("\t                              3 debug\n");
258         printf("\t                              4 debug level 2\n");
259         printf("\t-f <conf_file>                Use <conf_file> as the opkg configuration file\n");
260         printf("\t--conf <conf_file>\n");
261         printf("\t--cache <directory>   Use a package cache\n");
262         printf("\t-d <dest_name>                Use <dest_name> as the the root directory for\n");
263         printf("\t--dest <dest_name>    package installation, removal, upgrading.\n");
264         printf("                                <dest_name> should be a defined dest name from\n");
265         printf("                                the configuration file, (but can also be a\n");
266         printf("                                directory name in a pinch).\n");
267         printf("\t-o <dir>              Use <dir> as the root directory for\n");
268         printf("\t--offline-root <dir>  offline installation of packages.\n");
269         printf("\t--add-arch <arch>:<prio>      Register architecture with given priority\n");
270         printf("\t--add-dest <name>:<path>      Register destination with given path\n");
271
272         printf("\nForce Options:\n");
273         printf("\t--force-depends               Install/remove despite failed dependencies\n");
274         printf("\t--force-maintainer    Overwrite preexisting config files\n");
275         printf("\t--force-reinstall     Reinstall package(s)\n");
276         printf("\t--force-overwrite     Overwrite files from other package(s)\n");
277         printf("\t--force-downgrade     Allow opkg to downgrade packages\n");
278         printf("\t--force-space         Disable free space checks\n");
279         printf("\t--force-postinstall   Run postinstall scripts even in offline mode\n");
280         printf("\t--force-remove        Remove package even if prerm script fails\n");
281         printf("\t--noaction            No action -- test only\n");
282         printf("\t--download-only       No action -- download only\n");
283         printf("\t--nodeps              Do not follow dependencies\n");
284         printf("\t--force-removal-of-dependent-packages\n");
285         printf("\t                      Remove package and all dependencies\n");
286         printf("\t--autoremove          Remove packages that were installed\n");
287         printf("\t                      automatically to satisfy dependencies\n");
288         printf("\t-t                    Specify tmp-dir.\n");
289         printf("\t--tmp-dir             Specify tmp-dir.\n");
290
291         printf("\n");
292
293         printf(" regexp could be something like 'pkgname*' '*file*' or similar\n");
294         printf(" e.g. opkg info 'libstd*' or opkg search '*libop*' or opkg remove 'libncur*'\n");
295
296         /* --force-removal-of-essential-packages        Let opkg remove essential packages.
297                 Using this option is almost guaranteed to break your system, hence this option
298                 is not even advertised in the usage statement. */
299
300         exit(1);
301 }
302
303 int
304 main(int argc, char *argv[])
305 {
306         int opts, err = -1;
307         char *cmd_name;
308         opkg_cmd_t *cmd;
309         int nocheckfordirorfile = 0;
310         int noreadfeedsfile = 0;
311
312         if (opkg_conf_init())
313                 goto err0;
314
315         conf->verbosity = NOTICE;
316
317         opts = args_parse(argc, argv);
318         if (opts == argc || opts < 0) {
319                 fprintf(stderr, "opkg must have one sub-command argument\n");
320                 usage();
321         }
322
323         cmd_name = argv[opts++];
324
325         if (!strcmp(cmd_name,"print-architecture") ||
326             !strcmp(cmd_name,"print_architecture") ||
327             !strcmp(cmd_name,"print-installation-architecture") ||
328             !strcmp(cmd_name,"print_installation_architecture") )
329                 nocheckfordirorfile = 1;
330
331         if (!strcmp(cmd_name,"flag") ||
332             !strcmp(cmd_name,"configure") ||
333             !strcmp(cmd_name,"remove") ||
334             !strcmp(cmd_name,"files") ||
335             !strcmp(cmd_name,"search") ||
336             !strcmp(cmd_name,"compare_versions") ||
337             !strcmp(cmd_name,"compare-versions") ||
338             !strcmp(cmd_name,"list_installed") ||
339             !strcmp(cmd_name,"list-installed") ||
340             !strcmp(cmd_name,"list_changed_conffiles") ||
341             !strcmp(cmd_name,"list-changed-conffiles") ||
342             !strcmp(cmd_name,"status") )
343                 noreadfeedsfile = 1;
344
345         cmd = opkg_cmd_find(cmd_name);
346         if (cmd == NULL) {
347                 fprintf(stderr, "%s: unknown sub-command %s\n", argv[0],
348                          cmd_name);
349                 usage();
350         }
351
352         conf->pfm = cmd->pfm;
353
354         if (opkg_conf_load())
355                 goto err0;
356
357         if (!nocheckfordirorfile) {
358                 if (!noreadfeedsfile) {
359                         if (pkg_hash_load_feeds())
360                                 goto err1;
361                 }
362
363                 if (pkg_hash_load_status_files())
364                         goto err1;
365         }
366
367         if (cmd->requires_args && opts == argc) {
368                 fprintf(stderr,
369                          "%s: the ``%s'' command requires at least one argument\n",
370                          argv[0], cmd_name);
371                 usage();
372         }
373
374         err = opkg_cmd_exec(cmd, argc - opts, (const char **) (argv + opts));
375
376 #ifdef HAVE_CURL
377         opkg_curl_cleanup();
378 #endif
379 err1:
380         opkg_conf_deinit();
381
382 err0:
383         print_error_list();
384         free_error_list();
385
386         return err;
387 }