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