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