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