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