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