Attempt to escape hg messed up the repo once more.
[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 <rpmcli.h>
10 #include <fts.h>
11
12 #include "debug.h"
13
14 #define POPT_SHOWVERSION        -999
15 #define POPT_SHOWRC             -998
16 #define POPT_QUERYTAGS          -997
17 #define POPT_PREDEFINE          -996
18 #ifdef  NOTYET
19 #define POPT_RCFILE             -995
20 #endif
21
22 static int _debug = 0;
23
24 extern int _rpmds_nopromote;
25
26 extern int _fps_debug;
27
28 extern int _fsm_debug;
29
30 extern int _fsm_threads;
31
32 extern int _hdr_debug;
33
34 extern int _print_pkts;
35
36 extern int _psm_debug;
37
38 extern int _psm_threads;
39
40 extern int _rpmal_debug;
41
42 extern int _rpmdb_debug;
43
44 extern int _rpmds_debug;
45
46 /* XXX avoid -lrpmbuild linkage. */
47        int _rpmfc_debug;
48
49 extern int _rpmfi_debug;
50
51 extern int _rpmgi_debug;
52
53 extern int _rpmps_debug;
54
55 extern int _rpmsq_debug;
56
57 extern int _rpmte_debug;
58
59 extern int _rpmts_debug;
60
61 extern int _rpmts_stats;
62
63 extern int noLibio;
64
65 const char * rpmcliPipeOutput = NULL;
66
67 const char * rpmcliRcfile = NULL;
68
69 const char * rpmcliRootDir = "/";
70
71 rpmQueryFlags rpmcliQueryFlags;
72
73 extern int noLibio;
74
75 extern int _rpmio_debug;
76
77 static int rpmcliInitialized = -1;
78
79 /**
80  * Display rpm version.
81  */
82 static void printVersion(FILE * fp)
83 {
84     fprintf(fp, _("RPM version %s\n"), rpmEVR);
85 }
86
87 /**
88  * Make sure that config files have been read.
89  * @warning Options like --rcfile and --verbose must precede callers option.
90  */
91 void rpmcliConfigured(void)
92 {
93
94     if (rpmcliInitialized < 0)
95         rpmcliInitialized = rpmReadConfigFiles(rpmcliRcfile, NULL);
96     if (rpmcliInitialized)
97         exit(EXIT_FAILURE);
98 }
99
100 /**
101  */
102 static void rpmcliAllArgCallback( poptContext con,
103                 enum poptCallbackReason reason,
104                 const struct poptOption * opt, const char * arg,
105                 const void * data)
106 {
107
108     /* XXX avoid accidental collisions with POPT_BIT_SET for flags */
109     if (opt->arg == NULL)
110     switch (opt->val) {
111     case 'q':
112         rpmSetVerbosity(RPMMESS_QUIET);
113         break;
114     case 'v':
115         rpmIncreaseVerbosity();
116         break;
117     case POPT_PREDEFINE:
118         (void) rpmDefineMacro(NULL, arg, RMIL_CMDLINE);
119         break;
120     case 'D':
121         /* XXX Predefine macro if not initialized yet. */
122         if (rpmcliInitialized < 0)
123             (void) rpmDefineMacro(NULL, arg, RMIL_CMDLINE);
124         rpmcliConfigured();
125         (void) rpmDefineMacro(NULL, arg, RMIL_CMDLINE);
126         (void) rpmDefineMacro(rpmCLIMacroContext, arg, RMIL_CMDLINE);
127         break;
128     case 'E':
129         rpmcliConfigured();
130         {   const char *val = rpmExpand(arg, NULL);
131             fprintf(stdout, "%s\n", val);
132             val = _free(val);
133         }
134         break;
135     case POPT_SHOWVERSION:
136         printVersion(stdout);
137         exit(EXIT_SUCCESS);
138         break;
139     case POPT_SHOWRC:
140         rpmcliConfigured();
141         (void) rpmShowRC(stdout);
142         exit(EXIT_SUCCESS);
143         break;
144     case POPT_QUERYTAGS:
145         rpmDisplayQueryTags(stdout);
146         exit(EXIT_SUCCESS);
147         break;
148 #if defined(POPT_RCFILE)
149     case POPT_RCFILE:           /* XXX FIXME: noop for now */
150         break;
151 #endif
152     case RPMCLI_POPT_NODIGEST:
153         rpmcliQueryFlags |= VERIFY_DIGEST;
154         break;
155
156     case RPMCLI_POPT_NOSIGNATURE:
157         rpmcliQueryFlags |= VERIFY_SIGNATURE;
158         break;
159
160     case RPMCLI_POPT_NOHDRCHK:
161         rpmcliQueryFlags |= VERIFY_HDRCHK;
162         break;
163     }
164 }
165
166 int ftsOpts = 0;
167
168 struct poptOption rpmcliFtsPoptTable[] = {
169  { "comfollow", '\0', POPT_BIT_SET,     &ftsOpts, FTS_COMFOLLOW,
170         N_("FTS_COMFOLLOW: follow command line symlinks"), NULL },
171  { "logical", '\0', POPT_BIT_SET,       &ftsOpts, FTS_LOGICAL,
172         N_("FTS_LOGICAL: logical walk"), NULL },
173  { "nochdir", '\0', POPT_BIT_SET,       &ftsOpts, FTS_NOCHDIR,
174         N_("FTS_NOCHDIR: don't change directories"), NULL },
175  { "nostat", '\0', POPT_BIT_SET,        &ftsOpts, FTS_NOSTAT,
176         N_("FTS_NOSTAT: don't get stat info"), NULL },
177  { "physical", '\0', POPT_BIT_SET,      &ftsOpts, FTS_PHYSICAL,
178         N_("FTS_PHYSICAL: physical walk"), NULL },
179  { "seedot", '\0', POPT_BIT_SET,        &ftsOpts, FTS_SEEDOT,
180         N_("FTS_SEEDOT: return dot and dot-dot"), NULL },
181  { "xdev", '\0', POPT_BIT_SET,          &ftsOpts, FTS_XDEV,
182         N_("FTS_XDEV: don't cross devices"), NULL },
183  { "whiteout", '\0', POPT_BIT_SET,      &ftsOpts, FTS_WHITEOUT,
184         N_("FTS_WHITEOUT: return whiteout information"), NULL },
185    POPT_TABLEEND
186 };
187
188 struct poptOption rpmcliAllPoptTable[] = {
189 /* FIX: cast? */
190  { NULL, '\0', POPT_ARG_CALLBACK | POPT_CBFLAG_INC_DATA | POPT_CBFLAG_CONTINUE,
191         rpmcliAllArgCallback, 0, NULL, NULL },
192
193  { "debug", 'd', POPT_ARG_VAL|POPT_ARGFLAG_DOC_HIDDEN, &_debug, -1,
194         NULL, NULL },
195
196  { "predefine", 'D', POPT_ARG_STRING|POPT_ARGFLAG_DOC_HIDDEN, 0, POPT_PREDEFINE,
197         N_("predefine MACRO with value EXPR"),
198         N_("'MACRO EXPR'") },
199  { "define", 'D', POPT_ARG_STRING, 0, 'D',
200         N_("define MACRO with value EXPR"),
201         N_("'MACRO EXPR'") },
202  { "eval", 'E', POPT_ARG_STRING, 0, 'E',
203         N_("print macro expansion of EXPR"),
204         N_("'EXPR'") },
205  { "macros", '\0', POPT_ARG_STRING, &macrofiles, 0,
206         N_("read <FILE:...> instead of default file(s)"),
207         N_("<FILE:...>") },
208
209  { "nodigest", '\0', 0, 0, RPMCLI_POPT_NODIGEST,
210         N_("don't verify package digest(s)"), NULL },
211  { "nohdrchk", '\0', POPT_ARGFLAG_DOC_HIDDEN, 0, RPMCLI_POPT_NOHDRCHK,
212         N_("don't verify database header(s) when retrieved"), NULL },
213 #if HAVE_LIBIO_H && defined(_G_IO_IO_FILE_VERSION)
214  { "nolibio", '\0', POPT_ARG_VAL|POPT_ARGFLAG_DOC_HIDDEN, &noLibio, 1,
215         N_("disable use of libio(3) API"), NULL},
216 #endif
217  { "nosignature", '\0', 0, 0, RPMCLI_POPT_NOSIGNATURE,
218         N_("don't verify package signature(s)"), NULL },
219
220  { "pipe", '\0', POPT_ARG_STRING|POPT_ARGFLAG_DOC_HIDDEN, &rpmcliPipeOutput, 0,
221         N_("send stdout to CMD"),
222         N_("CMD") },
223 #if !defined(POPT_RCFILE)
224  { "rcfile", '\0', POPT_ARG_STRING, &rpmcliRcfile, 0,
225         N_("read <FILE:...> instead of default file(s)"),
226         N_("<FILE:...>") },
227 #else
228  { "rcfile", '\0', 0, NULL, POPT_RCFILE,        
229         N_("read <FILE:...> instead of default file(s)"),
230         N_("<FILE:...>") },
231 #endif
232  { "root", 'r', POPT_ARG_STRING|POPT_ARGFLAG_SHOW_DEFAULT, &rpmcliRootDir, 0,
233         N_("use ROOT as top level directory"),
234         N_("ROOT") },
235
236  { "querytags", '\0', 0, 0, POPT_QUERYTAGS,
237         N_("display known query tags"), NULL },
238  { "showrc", '\0', 0, NULL, POPT_SHOWRC,
239         N_("display final rpmrc and macro configuration"), NULL },
240  { "quiet", '\0', 0, NULL, 'q',
241         N_("provide less detailed output"), NULL},
242  { "verbose", 'v', 0, NULL, 'v',
243         N_("provide more detailed output"), NULL},
244  { "version", '\0', 0, NULL, POPT_SHOWVERSION,
245         N_("print the version of rpm being used"), NULL },
246
247 #if HAVE_LIBIO_H && defined(_G_IO_IO_FILE_VERSION)
248  { "nolibio", '\0', POPT_ARG_VAL|POPT_ARGFLAG_DOC_HIDDEN, &noLibio, 1,
249        N_("disable use of libio(3) API"), NULL},
250 #endif
251
252  { "promoteepoch", '\0', POPT_ARG_VAL|POPT_ARGFLAG_DOC_HIDDEN, &_rpmds_nopromote, 0,
253         NULL, NULL},
254
255  { "fpsdebug", '\0', POPT_ARG_VAL|POPT_ARGFLAG_DOC_HIDDEN, &_fps_debug, -1,
256         NULL, NULL},
257  { "fsmdebug", '\0', POPT_ARG_VAL|POPT_ARGFLAG_DOC_HIDDEN, &_fsm_debug, -1,
258         N_("debug payload file state machine"), NULL},
259  { "fsmthreads", '\0', POPT_ARG_VAL|POPT_ARGFLAG_DOC_HIDDEN, &_fsm_threads, -1,
260         N_("use threads for file state machine"), NULL},
261  { "hdrdebug", '\0', POPT_ARG_VAL|POPT_ARGFLAG_DOC_HIDDEN, &_hdr_debug, -1,
262         NULL, NULL},
263 #ifdef  DYING
264  { "poptdebug", '\0', POPT_ARG_VAL|POPT_ARGFLAG_DOC_HIDDEN, &_popt_debug, -1,
265         N_("debug option/argument processing"), NULL},
266 #endif
267  { "prtpkts", '\0', POPT_ARG_VAL|POPT_ARGFLAG_DOC_HIDDEN, &_print_pkts, -1,
268         NULL, NULL},
269  { "psmdebug", '\0', POPT_ARG_VAL|POPT_ARGFLAG_DOC_HIDDEN, &_psm_debug, -1,
270         N_("debug package state machine"), NULL},
271  { "psmthreads", '\0', POPT_ARG_VAL|POPT_ARGFLAG_DOC_HIDDEN, &_psm_threads, -1,
272         N_("use threads for package state machine"), NULL},
273  { "rpmaldebug", '\0', POPT_ARG_VAL|POPT_ARGFLAG_DOC_HIDDEN, &_rpmal_debug, -1,
274         NULL, NULL},
275  { "rpmdbdebug", '\0', POPT_ARG_VAL|POPT_ARGFLAG_DOC_HIDDEN, &_rpmdb_debug, -1,
276         NULL, NULL},
277  { "rpmdsdebug", '\0', POPT_ARG_VAL|POPT_ARGFLAG_DOC_HIDDEN, &_rpmds_debug, -1,
278         NULL, NULL},
279  { "rpmfcdebug", '\0', POPT_ARG_VAL|POPT_ARGFLAG_DOC_HIDDEN, &_rpmfc_debug, -1,
280         NULL, NULL},
281  { "rpmfidebug", '\0', POPT_ARG_VAL|POPT_ARGFLAG_DOC_HIDDEN, &_rpmfi_debug, -1,
282         NULL, NULL},
283  { "rpmgidebug", '\0', POPT_ARG_VAL|POPT_ARGFLAG_DOC_HIDDEN, &_rpmgi_debug, -1,
284         NULL, NULL},
285  { "rpmiodebug", '\0', POPT_ARG_VAL|POPT_ARGFLAG_DOC_HIDDEN, &_rpmio_debug, -1,
286         N_("debug rpmio I/O"), NULL},
287  { "rpmpsdebug", '\0', POPT_ARG_VAL|POPT_ARGFLAG_DOC_HIDDEN, &_rpmps_debug, -1,
288         NULL, NULL},
289  { "rpmsqdebug", '\0', POPT_ARG_VAL|POPT_ARGFLAG_DOC_HIDDEN, &_rpmsq_debug, -1,
290         NULL, NULL},
291  { "rpmtedebug", '\0', POPT_ARG_VAL|POPT_ARGFLAG_DOC_HIDDEN, &_rpmte_debug, -1,
292         NULL, NULL},
293  { "rpmtsdebug", '\0', POPT_ARG_VAL|POPT_ARGFLAG_DOC_HIDDEN, &_rpmts_debug, -1,
294         NULL, NULL},
295  { "stats", '\0', POPT_ARG_VAL|POPT_ARGFLAG_DOC_HIDDEN, &_rpmts_stats, -1,
296         NULL, NULL},
297  { "urldebug", '\0', POPT_ARG_VAL|POPT_ARGFLAG_DOC_HIDDEN, &_url_debug, -1,
298         N_("debug URL cache handling"), NULL},
299
300    POPT_TABLEEND
301 };
302
303 poptContext
304 rpmcliFini(poptContext optCon)
305 {
306     optCon = poptFreeContext(optCon);
307
308 #if HAVE_MCHECK_H && HAVE_MTRACE
309     muntrace();   /* Trace malloc only if MALLOC_TRACE=mtrace-output-file. */
310 #endif
311
312     return NULL;
313 }
314
315 poptContext
316 rpmcliInit(int argc, char *const argv[], struct poptOption * optionsTable)
317 {
318     const char * optArg;
319     poptContext optCon;
320     int rc;
321
322 #if HAVE_MCHECK_H && HAVE_MTRACE
323     mtrace();   /* Trace malloc only if MALLOC_TRACE=mtrace-output-file. */
324 #endif
325     setprogname(argv[0]);       /* Retrofit glibc __progname */
326
327     /* XXX glibc churn sanity */
328     if (__progname == NULL) {
329         if ((__progname = strrchr(argv[0], '/')) != NULL) __progname++;
330         else __progname = argv[0];
331     }
332
333 #if defined(ENABLE_NLS)
334     (void) setlocale(LC_ALL, "" );
335     (void) bindtextdomain(PACKAGE, LOCALEDIR);
336     (void) textdomain(PACKAGE);
337 #endif
338
339     rpmSetVerbosity(RPMMESS_NORMAL);
340
341     if (optionsTable == NULL) {
342         /* Read rpm configuration (if not already read). */
343         rpmcliConfigured();
344         return NULL;
345     }
346
347     optCon = poptGetContext(__progname, argc, (const char **)argv, optionsTable, 0);
348     (void) poptReadConfigFile(optCon, LIBRPMALIAS_FILENAME);
349     (void) poptReadDefaultConfig(optCon, 1);
350     poptSetExecPath(optCon, RPMCONFIGDIR, 1);
351
352     /* Process all options, whine if unknown. */
353     while ((rc = poptGetNextOpt(optCon)) > 0) {
354         optArg = poptGetOptArg(optCon);
355         switch (rc) {
356         default:
357             fprintf(stderr, _("%s: option table misconfigured (%d)\n"),
358                 __progname, rc);
359             exit(EXIT_FAILURE);
360
361             break;
362         }
363     }
364
365     if (rc < -1) {
366         fprintf(stderr, "%s: %s: %s\n", __progname,
367                 poptBadOption(optCon, POPT_BADOPTION_NOALIAS),
368                 poptStrerror(rc));
369         exit(EXIT_FAILURE);
370     }
371
372     /* Read rpm configuration (if not already read). */
373     rpmcliConfigured();
374
375     if (_debug) {
376         rpmIncreaseVerbosity();
377         rpmIncreaseVerbosity();
378     }
379
380     return optCon;
381 }