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