Add support for global LDFLAGS
[platform/upstream/rpm.git] / lib / poptALL.c
1 /** \ingroup rpmcli
2  * \file lib/poptALL.c
3  *  Popt tables for all rpm modes.
4  */
5
6 #include "system.h"
7 const char *__progname;
8
9 #include <rpm/rpmcli.h>
10 #include <rpm/rpmlib.h>         /* rpmEVR, rpmReadConfigFiles etc */
11 #include <rpm/rpmlog.h>
12 #include <rpm/rpmstring.h>
13 #include <rpm/rpmfileutil.h>
14
15 #include "debug.h"
16
17 #define POPT_SHOWVERSION        -999
18 #define POPT_SHOWRC             -998
19 #define POPT_QUERYTAGS          -997
20 #define POPT_PREDEFINE          -996
21 #define POPT_DBPATH             -995
22 #define POPT_UNDEFINE           -994
23
24 static int _debug = 0;
25
26 extern int _rpmds_nopromote;
27
28 extern int _fsm_debug;
29
30 extern int _print_pkts;
31
32 extern int _psm_debug;
33
34 /* XXX avoid -lrpmbuild linkage. */
35        int _rpmfc_debug;
36
37 extern int _rpmts_stats;
38
39 const char * rpmcliPipeOutput = NULL;
40
41 const char * rpmcliRcfile = NULL;
42
43 const char * rpmcliRootDir = "/";
44
45 rpmQueryFlags rpmcliQueryFlags;
46
47 extern int _rpmio_debug;
48
49 static int rpmcliInitialized = -1;
50
51 /**
52  * Display rpm version.
53  */
54 static void printVersion(FILE * fp)
55 {
56     fprintf(fp, _("RPM version %s\n"), rpmEVR);
57 }
58
59 /**
60  * Make sure that config files have been read.
61  * @warning Options like --rcfile and --verbose must precede callers option.
62  */
63 void rpmcliConfigured(void)
64 {
65
66     if (rpmcliInitialized < 0)
67         rpmcliInitialized = rpmReadConfigFiles(rpmcliRcfile, NULL);
68     if (rpmcliInitialized)
69         exit(EXIT_FAILURE);
70 }
71
72 /**
73  */
74 static void rpmcliAllArgCallback( poptContext con,
75                 enum poptCallbackReason reason,
76                 const struct poptOption * opt, const char * arg,
77                 const void * data)
78 {
79
80     /* XXX avoid accidental collisions with POPT_BIT_SET for flags */
81     if (opt->arg == NULL)
82     switch (opt->val) {
83     case 'q':
84         rpmSetVerbosity(RPMLOG_WARNING);
85         break;
86     case 'v':
87         rpmIncreaseVerbosity();
88         break;
89     case POPT_PREDEFINE:
90         (void) rpmDefineMacro(NULL, arg, RMIL_CMDLINE);
91         break;
92     case 'D':
93     {   char *s, *t;
94         /* XXX Convert '-' in macro name to underscore, skip leading %. */
95         s = t = xstrdup(arg);
96         while (*t && !risspace(*t)) {
97             if (*t == '-') *t = '_';
98             t++;
99         }
100         t = s;
101         if (*t == '%') t++;
102         /* XXX Predefine macro if not initialized yet. */
103         if (rpmcliInitialized < 0)
104             (void) rpmDefineMacro(NULL, t, RMIL_CMDLINE);
105         rpmcliConfigured();
106         (void) rpmDefineMacro(NULL, t, RMIL_CMDLINE);
107         (void) rpmDefineMacro(rpmCLIMacroContext, t, RMIL_CMDLINE);
108         free(s);
109         break;
110     }
111     case POPT_UNDEFINE:
112         rpmcliConfigured();
113         if (*arg == '%')
114             arg++;
115         delMacro(NULL, arg);
116         break;
117     case 'E':
118         rpmcliConfigured();
119         {   char *val = rpmExpand(arg, NULL);
120             fprintf(stdout, "%s\n", val);
121             free(val);
122         }
123         break;
124     case POPT_DBPATH:
125         rpmcliConfigured();
126         addMacro(NULL, "_dbpath", NULL, arg, RMIL_CMDLINE);
127         break;
128     case POPT_SHOWVERSION:
129         printVersion(stdout);
130         exit(EXIT_SUCCESS);
131         break;
132     case POPT_SHOWRC:
133         rpmcliConfigured();
134         (void) rpmShowRC(stdout);
135         exit(EXIT_SUCCESS);
136         break;
137     case POPT_QUERYTAGS:
138         rpmDisplayQueryTags(stdout);
139         exit(EXIT_SUCCESS);
140         break;
141     case RPMCLI_POPT_NODIGEST:
142         rpmcliQueryFlags |= VERIFY_DIGEST;
143         break;
144
145     case RPMCLI_POPT_NOSIGNATURE:
146         rpmcliQueryFlags |= VERIFY_SIGNATURE;
147         break;
148
149     case RPMCLI_POPT_NOHDRCHK:
150         rpmcliQueryFlags |= VERIFY_HDRCHK;
151         break;
152     }
153 }
154
155 struct poptOption rpmcliAllPoptTable[] = {
156 /* FIX: cast? */
157  { NULL, '\0', POPT_ARG_CALLBACK | POPT_CBFLAG_INC_DATA | POPT_CBFLAG_CONTINUE,
158         rpmcliAllArgCallback, 0, NULL, NULL },
159
160  { "debug", 'd', POPT_ARG_VAL|POPT_ARGFLAG_DOC_HIDDEN, &_debug, -1,
161         NULL, NULL },
162
163  { "predefine", '\0', POPT_ARG_STRING|POPT_ARGFLAG_DOC_HIDDEN, 0, POPT_PREDEFINE,
164         N_("predefine MACRO with value EXPR"),
165         N_("'MACRO EXPR'") },
166  { "define", 'D', POPT_ARG_STRING, 0, 'D',
167         N_("define MACRO with value EXPR"),
168         N_("'MACRO EXPR'") },
169  { "undefine", '\0', POPT_ARG_STRING, 0, POPT_UNDEFINE,
170         N_("undefine MACRO"),
171         N_("MACRO") },
172  { "eval", 'E', POPT_ARG_STRING, 0, 'E',
173         N_("print macro expansion of EXPR"),
174         N_("'EXPR'") },
175  { "macros", '\0', POPT_ARG_STRING, &macrofiles, 0,
176         N_("read <FILE:...> instead of default file(s)"),
177         N_("<FILE:...>") },
178
179  { "nodigest", '\0', 0, 0, RPMCLI_POPT_NODIGEST,
180         N_("don't verify package digest(s)"), NULL },
181  { "nohdrchk", '\0', POPT_ARGFLAG_DOC_HIDDEN, 0, RPMCLI_POPT_NOHDRCHK,
182         N_("don't verify database header(s) when retrieved"), NULL },
183  { "nosignature", '\0', 0, 0, RPMCLI_POPT_NOSIGNATURE,
184         N_("don't verify package signature(s)"), NULL },
185
186  { "pipe", '\0', POPT_ARG_STRING|POPT_ARGFLAG_DOC_HIDDEN, &rpmcliPipeOutput, 0,
187         N_("send stdout to CMD"),
188         N_("CMD") },
189  { "rcfile", '\0', POPT_ARG_STRING, &rpmcliRcfile, 0,
190         N_("read <FILE:...> instead of default file(s)"),
191         N_("<FILE:...>") },
192  { "root", 'r', POPT_ARG_STRING|POPT_ARGFLAG_SHOW_DEFAULT, &rpmcliRootDir, 0,
193         N_("use ROOT as top level directory"),
194         N_("ROOT") },
195  { "dbpath", '\0', POPT_ARG_STRING, 0, POPT_DBPATH,
196         N_("use database in DIRECTORY"),
197         N_("DIRECTORY") },
198
199  { "querytags", '\0', 0, 0, POPT_QUERYTAGS,
200         N_("display known query tags"), NULL },
201  { "showrc", '\0', 0, NULL, POPT_SHOWRC,
202         N_("display final rpmrc and macro configuration"), NULL },
203  { "quiet", '\0', 0, NULL, 'q',
204         N_("provide less detailed output"), NULL},
205  { "verbose", 'v', 0, NULL, 'v',
206         N_("provide more detailed output"), NULL},
207  { "version", '\0', 0, NULL, POPT_SHOWVERSION,
208         N_("print the version of rpm being used"), NULL },
209
210  { "promoteepoch", '\0', POPT_ARG_VAL|POPT_ARGFLAG_DOC_HIDDEN, &_rpmds_nopromote, 0,
211         NULL, NULL},
212
213  { "fsmdebug", '\0', POPT_ARG_VAL|POPT_ARGFLAG_DOC_HIDDEN, &_fsm_debug, -1,
214         N_("debug payload file state machine"), NULL},
215  { "prtpkts", '\0', POPT_ARG_VAL|POPT_ARGFLAG_DOC_HIDDEN, &_print_pkts, -1,
216         NULL, NULL},
217  { "rpmfcdebug", '\0', POPT_ARG_VAL|POPT_ARGFLAG_DOC_HIDDEN, &_rpmfc_debug, -1,
218         NULL, NULL},
219  { "rpmiodebug", '\0', POPT_ARG_VAL|POPT_ARGFLAG_DOC_HIDDEN, &_rpmio_debug, -1,
220         N_("debug rpmio I/O"), NULL},
221  { "stats", '\0', POPT_ARG_VAL|POPT_ARGFLAG_DOC_HIDDEN, &_rpmts_stats, -1,
222         NULL, NULL},
223
224    POPT_TABLEEND
225 };
226
227 poptContext
228 rpmcliFini(poptContext optCon)
229 {
230     poptFreeContext(optCon);
231     rpmFreeMacros(NULL);
232     rpmFreeMacros(rpmCLIMacroContext);
233     rpmFreeRpmrc();
234     rpmlogClose();
235     rpmcliInitialized = -1;
236
237     return NULL;
238 }
239
240 poptContext
241 rpmcliInit(int argc, char *const argv[], struct poptOption * optionsTable)
242 {
243     poptContext optCon;
244     int rc;
245     const char *ctx, *execPath;
246
247     setprogname(argv[0]);       /* Retrofit glibc __progname */
248
249     /* XXX glibc churn sanity */
250     if (__progname == NULL) {
251         if ((__progname = strrchr(argv[0], '/')) != NULL) __progname++;
252         else __progname = argv[0];
253     }
254
255 #if defined(ENABLE_NLS)
256     (void) setlocale(LC_ALL, "" );
257
258     (void) bindtextdomain(PACKAGE, LOCALEDIR);
259     (void) textdomain(PACKAGE);
260 #endif
261
262     rpmSetVerbosity(RPMLOG_NOTICE);
263
264     if (optionsTable == NULL) {
265         /* Read rpm configuration (if not already read). */
266         rpmcliConfigured();
267         return NULL;
268     }
269
270     /* XXX hack to get popt working from build tree wrt lt-foo names */
271     ctx = rstreqn(__progname, "lt-", 3) ? __progname + 3 : __progname;
272
273     optCon = poptGetContext(ctx, argc, (const char **)argv, optionsTable, 0);
274     {
275         char *poptfile = rpmGenPath(rpmConfigDir(), LIBRPMALIAS_FILENAME, NULL);
276         (void) poptReadConfigFile(optCon, poptfile);
277         free(poptfile);
278     }
279     (void) poptReadDefaultConfig(optCon, 1);
280
281     if ((execPath = getenv("RPM_POPTEXEC_PATH")) == NULL)
282         execPath = LIBRPMALIAS_EXECPATH;
283     poptSetExecPath(optCon, execPath, 1);
284
285     /* Process all options, whine if unknown. */
286     while ((rc = poptGetNextOpt(optCon)) > 0) {
287         fprintf(stderr, _("%s: option table misconfigured (%d)\n"),
288                 __progname, rc);
289         exit(EXIT_FAILURE);
290     }
291
292     if (rc < -1) {
293         fprintf(stderr, "%s: %s: %s\n", __progname,
294                 poptBadOption(optCon, POPT_BADOPTION_NOALIAS),
295                 poptStrerror(rc));
296         exit(EXIT_FAILURE);
297     }
298
299     /* Read rpm configuration (if not already read). */
300     rpmcliConfigured();
301
302     if (_debug) {
303         rpmIncreaseVerbosity();
304         rpmIncreaseVerbosity();
305     }
306
307     return optCon;
308 }