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