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