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