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