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