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