Merge commit 'grg' into HEAD
[oweals/opkg-lede.git] / libopkg / args.c
1 /* args.c - parse command-line args
2  
3   Carl D. Worth
4
5   Copyright 2001 University of Southern California
6  
7   This program is free software; you can redistribute it and/or modify
8   it under the terms of the GNU General Public License as published by
9   the Free Software Foundation; either version 2, or (at your option)
10   any later version.
11  
12   This program is distributed in the hope that it will be useful,
13   but WITHOUT ANY WARRANTY; without even the implied warranty of
14   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15   GNU General Public License for more details.
16  */
17
18 #include <getopt.h>
19 #include <stdlib.h>
20 #include <string.h>
21 #include <unistd.h>
22
23 #include "includes.h"
24
25 #include "config.h"
26 #include "args.h"
27 #include "sprintf_alloc.h"
28 #include "libbb/libbb.h"
29 #include "opkg_conf.h"
30
31 static void print_version(void);
32
33 enum long_args_opt
34 {
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_NOACTION,
44      ARGS_OPT_NODEPS,
45      ARGS_OPT_AUTOREMOVE,
46      ARGS_OPT_CACHE,
47 };
48
49 void args_init(args_t *args)
50 {
51      memset(args, 0, sizeof(args_t));
52
53      args->dest = ARGS_DEFAULT_DEST;
54
55      sprintf_alloc(&args->conf_file, "%s/%s", OPKGETCDIR,
56                    ARGS_DEFAULT_CONF_FILE_NAME);
57      conf->verbosity = ARGS_DEFAULT_VERBOSITY;
58 }
59
60 void args_deinit(args_t *args)
61 {
62      free (args->dest);
63      free(args->conf_file);
64      args->conf_file = NULL;
65 }
66
67 int args_parse(args_t *args, int argc, char *argv[])
68 {
69      int c;
70      int option_index = 0;
71      int parse_err = 0;
72      static struct option long_options[] = {
73           {"query-all", 0, 0, 'A'},
74           {"autoremove", 0, 0, ARGS_OPT_AUTOREMOVE},
75           {"cache", 1, 0, ARGS_OPT_CACHE},
76           {"conf-file", 1, 0, 'f'},
77           {"conf", 1, 0, 'f'},
78           {"dest", 1, 0, 'd'},
79           {"force-maintainer", 0, 0, ARGS_OPT_FORCE_MAINTAINER},
80           {"force_maintainer", 0, 0, ARGS_OPT_FORCE_MAINTAINER},
81           {"force-depends", 0, 0, ARGS_OPT_FORCE_DEPENDS},
82           {"force_depends", 0, 0, ARGS_OPT_FORCE_DEPENDS},
83           {"force-overwrite", 0, 0, ARGS_OPT_FORCE_OVERWRITE},
84           {"force_overwrite", 0, 0, ARGS_OPT_FORCE_OVERWRITE},
85           {"force_downgrade", 0, 0, ARGS_OPT_FORCE_DOWNGRADE},
86           {"force-downgrade", 0, 0, ARGS_OPT_FORCE_DOWNGRADE},
87           {"force-reinstall", 0, 0, ARGS_OPT_FORCE_REINSTALL},
88           {"force_reinstall", 0, 0, ARGS_OPT_FORCE_REINSTALL},
89           {"force-space", 0, 0, ARGS_OPT_FORCE_SPACE},
90           {"force_space", 0, 0, ARGS_OPT_FORCE_SPACE},
91           {"recursive", 0, 0,
92            ARGS_OPT_FORCE_REMOVAL_OF_DEPENDENT_PACKAGES},
93           {"force-removal-of-dependent-packages", 0, 0,
94            ARGS_OPT_FORCE_REMOVAL_OF_DEPENDENT_PACKAGES},
95           {"force_removal_of_dependent_packages", 0, 0,
96            ARGS_OPT_FORCE_REMOVAL_OF_DEPENDENT_PACKAGES},
97           {"force-removal-of-essential-packages", 0, 0,
98            ARGS_OPT_FORCE_REMOVAL_OF_ESSENTIAL_PACKAGES},
99           {"force_removal_of_essential_packages", 0, 0,
100            ARGS_OPT_FORCE_REMOVAL_OF_ESSENTIAL_PACKAGES},
101           {"noaction", 0, 0, ARGS_OPT_NOACTION},
102           {"nodeps", 0, 0, ARGS_OPT_NODEPS},
103           {"offline", 1, 0, 'o'},
104           {"offline-root", 1, 0, 'o'},
105           {"test", 0, 0, ARGS_OPT_NOACTION},
106           {"tmp-dir", 1, 0, 't'},
107           {"tmp_dir", 1, 0, 't'},
108           {"verbosity", 2, 0, 'V'},
109           {"version", 0, 0, 'v'},
110           {0, 0, 0, 0}
111      };
112
113      while (1) {
114           c = getopt_long_only(argc, argv, "Ad:f:no:p:t:vV:", long_options, &option_index);
115           if (c == -1)
116                break;
117
118           switch (c) {
119           case 'A':
120                conf->query_all = 1;
121                break;
122           case 'd':
123                args->dest = xstrdup(optarg);
124                break;
125           case 'f':
126                free(args->conf_file);
127                args->conf_file = xstrdup(optarg);
128                break;
129           case 'o':
130                conf->offline_root = xstrdup(optarg);
131                break;
132           case 't':
133                conf->tmp_dir = xstrdup(optarg);
134                break;
135           case 'v':
136                print_version();
137                exit(0);
138           case 'V':
139                conf->verbosity = atoi(optarg);
140                break;
141           case ARGS_OPT_AUTOREMOVE:
142                conf->autoremove = 1;
143                break;
144           case ARGS_OPT_CACHE:
145                free(conf->cache);
146                conf->cache = xstrdup(optarg);
147                break;
148           case ARGS_OPT_FORCE_MAINTAINER:
149                conf->force_maintainer = 1;
150                break;
151           case ARGS_OPT_FORCE_DEPENDS:
152                conf->force_depends = 1;
153                break;
154           case ARGS_OPT_FORCE_OVERWRITE:
155                conf->force_overwrite = 1;
156                break;
157           case ARGS_OPT_FORCE_DOWNGRADE:
158                conf->force_downgrade = 1;
159                break;
160           case ARGS_OPT_FORCE_REINSTALL:
161                conf->force_reinstall = 1;
162                break;
163           case ARGS_OPT_FORCE_REMOVAL_OF_ESSENTIAL_PACKAGES:
164                conf->force_removal_of_essential_packages = 1;
165                break;
166           case ARGS_OPT_FORCE_REMOVAL_OF_DEPENDENT_PACKAGES:
167                conf->force_removal_of_dependent_packages = 1;
168                break;
169           case ARGS_OPT_FORCE_SPACE:
170                conf->force_space = 1;
171                break;
172           case ARGS_OPT_NODEPS:
173                conf->nodeps = 1;
174                break;
175           case ARGS_OPT_NOACTION:
176                conf->noaction = 1;
177                break;
178           case ':':
179                parse_err = -1;
180                break;
181           case '?':
182                parse_err = -1;
183                break;
184           default:
185                printf("Confusion: getopt_long returned %d\n", c);
186           }
187      }
188     
189      if (parse_err) {
190           return parse_err;
191      } else {
192           return optind;
193      }
194 }
195
196 void args_usage(const char *complaint)
197 {
198      if (complaint) {
199           printf("opkg: %s\n", complaint);
200      }
201      print_version();
202      printf("usage: opkg [options...] sub-command [arguments...]\n");
203      printf("where sub-command is one of:\n");
204     
205      printf("\nPackage Manipulation:\n");
206      printf("\tupdate                   Update list of available packages\n");
207      printf("\tupgrade                  Upgrade installed packages\n");
208      printf("\tinstall <pkgs>           Install package(s)\n");
209      printf("\tconfigure <pkgs> Configure unpacked package(s)\n");
210      printf("\tremove <pkgs|regexp>     Remove package(s)\n");
211      printf("\tflag <flag> <pkgs>       Flag package(s)\n");
212      printf("\t <flag>=hold|noprune|user|ok|installed|unpacked (one per invocation)\n");
213     
214      printf("\nInformational Commands:\n");
215      printf("\tlist                     List available packages\n");
216      printf("\tlist-installed           List installed packages\n");
217      printf("\tlist-upgradable          List installed and upgradable packages\n");
218      printf("\tfiles <pkg>              List files belonging to <pkg>\n");
219      printf("\tsearch <file|regexp>     List package providing <file>\n");
220      printf("\tinfo [pkg|regexp]        Display all info for <pkg>\n");
221      printf("\tstatus [pkg|regexp]      Display all status for <pkg>\n");
222      printf("\tdownload <pkg>           Download <pkg> to current directory\n");
223      printf("\tcompare-versions <v1> <op> <v2>\n");
224      printf("\t                    compare versions using <= < > >= = << >>\n");
225      printf("\tprint-architecture       List installable package architectures\n");
226      printf("\twhatdepends [-A] [pkgname|pat]+\n");
227      printf("\twhatdependsrec [-A] [pkgname|pat]+\n");
228      printf("\twhatprovides [-A] [pkgname|pat]+\n");
229      printf("\twhatconflicts [-A] [pkgname|pat]+\n");
230      printf("\twhatreplaces [-A] [pkgname|pat]+\n");
231
232      printf("\nOptions:\n");
233      printf("\t-A                       Query all packages not just those installed\n"); 
234      printf("\t-V <level>               Set verbosity level to <level>.\n");
235      printf("\t--verbosity <level>      Verbosity levels:\n");
236      printf("\t                         0 errors only\n");
237      printf("\t                         1 normal messages (default)\n");
238      printf("\t                         2 informative messages\n");
239      printf("\t                         3 debug\n");
240      printf("\t                         4 debug level 2\n");
241      printf("\t-f <conf_file>           Use <conf_file> as the opkg configuration file\n");
242      printf("\t--conf <conf_file>       Default configuration file location\n");
243      printf("                           is %s/%s\n", ARGS_DEFAULT_CONF_FILE_DIR, ARGS_DEFAULT_CONF_FILE_NAME);
244      printf("\t--cache <directory>      Use a package cache\n");
245      printf("\t-d <dest_name>           Use <dest_name> as the the root directory for\n");
246      printf("\t--dest <dest_name>       package installation, removal, upgrading.\n");
247      printf("                           <dest_name> should be a defined dest name from\n");
248      printf("                           the configuration file, (but can also be a\n");
249      printf("                           directory name in a pinch).\n");
250      printf("\t-o <dir>         Use <dir> as the root directory for\n");
251      printf("\t--offline-root <dir>     offline installation of packages.\n");
252
253      printf("\nForce Options:\n");
254      printf("\t--force-depends          Install/remove despite failed dependences\n");
255      printf("\t--force-maintainer       Overwrite preexisting config files\n");
256      printf("\t--force-reinstall        Reinstall package(s)\n");
257      printf("\t--force-overwrite        Overwrite files from other package(s)\n");
258      printf("\t--force-downgrade        Allow opkg to downgrade packages\n");
259      printf("\t--force-space            Disable free space checks\n");
260      printf("\t--noaction               No action -- test only\n");
261      printf("\t--nodeps         Do not follow dependences\n");
262      printf("\t--force-removal-of-dependent-packages\n");
263      printf("\t                 Remove package and all dependencies\n");
264      printf("\t--autoremove             Remove packages that were installed\n");
265      printf("\t                 automatically to satisfy dependencies\n");
266      printf("\t-t                       Specify tmp-dir.\n");
267      printf("\t--tmp-dir                Specify tmp-dir.\n");
268
269      printf("\n");
270
271      printf(" regexp could be something like 'pkgname*' '*file*' or similar\n");
272      printf(" e.g. opkg info 'libstd*' or opkg search '*libop*' or opkg remove 'libncur*'\n");
273      /* -force-removal-of-essential-packages    Let opkg remove essential packages. 
274         Using this option is almost guaranteed to break your system, hence this option
275         is not even advertised in the usage statement. */
276      exit(1);
277 }
278
279 static void print_version(void)
280 {
281      printf("opkg version %s\n", VERSION);
282 }