Add macro %isu_package to generate ISU Package
[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
8 #include <rpm/rpmcli.h>
9 #include <rpm/rpmlib.h>         /* rpmEVR, rpmReadConfigFiles etc */
10 #include <rpm/rpmlog.h>
11 #include <rpm/rpmstring.h>
12 #include <rpm/rpmfileutil.h>
13
14 #include "debug.h"
15
16 #define POPT_SHOWVERSION        -999
17 #define POPT_SHOWRC             -998
18 #define POPT_QUERYTAGS          -997
19 #define POPT_PREDEFINE          -996
20 #define POPT_DBPATH             -995
21 #define POPT_UNDEFINE           -994
22 #define POPT_PIPE               -993
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 static int cliDefine(const char *arg, int predefine)
73 {
74     int rc;
75     char *s, *t;
76     /* XXX Convert '-' in macro name to underscore, skip leading %. */
77     s = t = xstrdup(arg);
78     while (*t && !risspace(*t)) {
79         if (*t == '-') *t = '_';
80         t++;
81     }
82     t = s;
83     if (*t == '%') t++;
84
85     rc = rpmDefineMacro(NULL, t, RMIL_CMDLINE);
86     if (!predefine && rc == 0)
87         (void) rpmDefineMacro(rpmCLIMacroContext, t, RMIL_CMDLINE);
88
89     free(s);
90     return rc;
91 }
92
93 /**
94  */
95 static void rpmcliAllArgCallback( poptContext con,
96                 enum poptCallbackReason reason,
97                 const struct poptOption * opt, const char * arg,
98                 const void * data)
99 {
100
101     /* XXX avoid accidental collisions with POPT_BIT_SET for flags */
102     if (opt->arg == NULL)
103     switch (opt->val) {
104     case 'q':
105         rpmSetVerbosity(RPMLOG_WARNING);
106         break;
107     case 'v':
108         rpmIncreaseVerbosity();
109         break;
110     case POPT_PREDEFINE:
111         if (cliDefine(arg, 1))
112             exit(EXIT_FAILURE);
113         break;
114     case 'D':
115         rpmcliConfigured();
116         if (cliDefine(arg, 0))
117             exit(EXIT_FAILURE);
118         break;
119     case POPT_UNDEFINE:
120         rpmcliConfigured();
121         if (*arg == '%')
122             arg++;
123         rpmPopMacro(NULL, arg);
124         break;
125     case 'E':
126         rpmcliConfigured();
127         {   char *val = NULL;
128             if (rpmExpandMacros(NULL, arg, &val, 0) < 0)
129                 exit(EXIT_FAILURE);
130             fprintf(stdout, "%s\n", val);
131             free(val);
132         }
133         break;
134     case POPT_DBPATH:
135         rpmcliConfigured();
136         rpmPushMacro(NULL, "_dbpath", NULL, arg, RMIL_CMDLINE);
137         break;
138     case POPT_SHOWVERSION:
139         printVersion(stdout);
140         exit(EXIT_SUCCESS);
141         break;
142     case POPT_SHOWRC:
143         rpmcliConfigured();
144         (void) rpmShowRC(stdout);
145         exit(EXIT_SUCCESS);
146         break;
147     case POPT_QUERYTAGS:
148         rpmDisplayQueryTags(stdout);
149         exit(EXIT_SUCCESS);
150         break;
151     case POPT_PIPE:
152         if (rpmcliPipeOutput) {
153             fprintf(stderr,
154                     _("%s: error: more than one --pipe specified "
155                       "(incompatible popt aliases?)\n"), xgetprogname());
156             exit(EXIT_FAILURE);
157         }
158         rpmcliPipeOutput = xstrdup(arg);
159         break;
160         
161     case RPMCLI_POPT_NODIGEST:
162         rpmcliQueryFlags |= VERIFY_DIGEST;
163         break;
164
165     case RPMCLI_POPT_NOSIGNATURE:
166         rpmcliQueryFlags |= VERIFY_SIGNATURE;
167         break;
168
169     case RPMCLI_POPT_NOHDRCHK:
170         rpmcliQueryFlags |= VERIFY_HDRCHK;
171         break;
172
173     case RPMCLI_POPT_TARGETPLATFORM:
174         rpmcliInitialized = rpmReadConfigFiles(rpmcliRcfile, arg);
175         break;
176     }
177 }
178
179 struct poptOption rpmcliAllPoptTable[] = {
180 /* FIX: cast? */
181  { NULL, '\0', POPT_ARG_CALLBACK | POPT_CBFLAG_INC_DATA | POPT_CBFLAG_CONTINUE,
182         rpmcliAllArgCallback, 0, NULL, NULL },
183
184  { "debug", 'd', POPT_ARG_VAL|POPT_ARGFLAG_DOC_HIDDEN, &_debug, -1,
185         NULL, NULL },
186
187  { "predefine", '\0', POPT_ARG_STRING|POPT_ARGFLAG_DOC_HIDDEN, 0, POPT_PREDEFINE,
188         N_("predefine MACRO with value EXPR"),
189         N_("'MACRO EXPR'") },
190  { "define", 'D', POPT_ARG_STRING, 0, 'D',
191         N_("define MACRO with value EXPR"),
192         N_("'MACRO EXPR'") },
193  { "undefine", '\0', POPT_ARG_STRING, 0, POPT_UNDEFINE,
194         N_("undefine MACRO"),
195         N_("MACRO") },
196  { "eval", 'E', POPT_ARG_STRING, 0, 'E',
197         N_("print macro expansion of EXPR"),
198         N_("'EXPR'") },
199  { "target", '\0', POPT_ARG_STRING, NULL,  RPMCLI_POPT_TARGETPLATFORM,
200         N_("Specify target platform"), N_("CPU-VENDOR-OS") },
201  { "macros", '\0', POPT_ARG_STRING, &macrofiles, 0,
202         N_("read <FILE:...> instead of default file(s)"),
203         N_("<FILE:...>") },
204
205  /* XXX this is a bit out of place here but kinda unavoidable... */
206  { "noplugins", '\0', POPT_BIT_SET,
207         &rpmIArgs.transFlags, RPMTRANS_FLAG_NOPLUGINS,
208         N_("don't enable any plugins"), NULL },
209
210  { "nodigest", '\0', 0, 0, RPMCLI_POPT_NODIGEST,
211         N_("don't verify package digest(s)"), NULL },
212  { "nohdrchk", '\0', POPT_ARGFLAG_DOC_HIDDEN, 0, RPMCLI_POPT_NOHDRCHK,
213         N_("don't verify database header(s) when retrieved"), NULL },
214  { "nosignature", '\0', 0, 0, RPMCLI_POPT_NOSIGNATURE,
215         N_("don't verify package signature(s)"), NULL },
216
217  { "pipe", '\0', POPT_ARG_STRING|POPT_ARGFLAG_DOC_HIDDEN, 0, POPT_PIPE,
218         N_("send stdout to CMD"),
219         N_("CMD") },
220  { "rcfile", '\0', POPT_ARG_STRING, &rpmcliRcfile, 0,
221         N_("read <FILE:...> instead of default file(s)"),
222         N_("<FILE:...>") },
223  { "root", 'r', POPT_ARG_STRING|POPT_ARGFLAG_SHOW_DEFAULT, &rpmcliRootDir, 0,
224         N_("use ROOT as top level directory"),
225         N_("ROOT") },
226  { "dbpath", '\0', POPT_ARG_STRING, 0, POPT_DBPATH,
227         N_("use database in DIRECTORY"),
228         N_("DIRECTORY") },
229
230  { "querytags", '\0', 0, 0, POPT_QUERYTAGS,
231         N_("display known query tags"), NULL },
232  { "showrc", '\0', 0, NULL, POPT_SHOWRC,
233         N_("display final rpmrc and macro configuration"), NULL },
234  { "quiet", '\0', 0, NULL, 'q',
235         N_("provide less detailed output"), NULL},
236  { "verbose", 'v', 0, NULL, 'v',
237         N_("provide more detailed output"), NULL},
238  { "version", '\0', 0, NULL, POPT_SHOWVERSION,
239         N_("print the version of rpm being used"), NULL },
240
241  { "promoteepoch", '\0', POPT_ARG_VAL|POPT_ARGFLAG_DOC_HIDDEN, &_rpmds_nopromote, 0,
242         NULL, NULL},
243
244  { "fsmdebug", '\0', POPT_ARG_VAL|POPT_ARGFLAG_DOC_HIDDEN, &_fsm_debug, -1,
245         N_("debug payload file state machine"), NULL},
246  { "prtpkts", '\0', POPT_ARG_VAL|POPT_ARGFLAG_DOC_HIDDEN, &_print_pkts, -1,
247         NULL, NULL},
248  { "rpmfcdebug", '\0', POPT_ARG_VAL|POPT_ARGFLAG_DOC_HIDDEN, &_rpmfc_debug, -1,
249         NULL, NULL},
250  { "rpmiodebug", '\0', POPT_ARG_VAL|POPT_ARGFLAG_DOC_HIDDEN, &_rpmio_debug, -1,
251         N_("debug rpmio I/O"), NULL},
252  { "stats", '\0', POPT_ARG_VAL|POPT_ARGFLAG_DOC_HIDDEN, &_rpmts_stats, -1,
253         NULL, NULL},
254
255    POPT_TABLEEND
256 };
257
258 poptContext
259 rpmcliFini(poptContext optCon)
260 {
261     poptFreeContext(optCon);
262     rpmFreeMacros(NULL);
263     rpmFreeMacros(rpmCLIMacroContext);
264     rpmFreeRpmrc();
265     rpmlogClose();
266     rpmcliInitialized = -1;
267
268     return NULL;
269 }
270
271 poptContext
272 rpmcliInit(int argc, char *const argv[], struct poptOption * optionsTable)
273 {
274     poptContext optCon;
275     int rc;
276     const char *ctx, *execPath;
277
278 #if defined(ENABLE_NLS)
279     (void) setlocale(LC_ALL, "" );
280
281     (void) bindtextdomain(PACKAGE, LOCALEDIR);
282     (void) textdomain(PACKAGE);
283 #endif
284
285     rpmSetVerbosity(RPMLOG_NOTICE);
286
287     if (optionsTable == NULL) {
288         /* Read rpm configuration (if not already read). */
289         rpmcliConfigured();
290         return NULL;
291     }
292
293     /* XXX hack to get popt working from build tree wrt lt-foo names */
294     ctx = rstreqn(xgetprogname(), "lt-", 3) ? xgetprogname() + 3 : xgetprogname();
295
296     optCon = poptGetContext(ctx, argc, (const char **)argv, optionsTable, 0);
297     {
298         char *poptfile = rpmGenPath(rpmConfigDir(), LIBRPMALIAS_FILENAME, NULL);
299         (void) poptReadConfigFile(optCon, poptfile);
300         free(poptfile);
301     }
302     (void) poptReadDefaultConfig(optCon, 1);
303
304     if ((execPath = getenv("RPM_POPTEXEC_PATH")) == NULL)
305         execPath = LIBRPMALIAS_EXECPATH;
306     poptSetExecPath(optCon, execPath, 1);
307
308     /* Process all options, whine if unknown. */
309     while ((rc = poptGetNextOpt(optCon)) > 0) {
310         fprintf(stderr, _("%s: option table misconfigured (%d)\n"),
311                 xgetprogname(), rc);
312         exit(EXIT_FAILURE);
313     }
314
315     if (rc < -1) {
316         fprintf(stderr, "%s: %s: %s\n", xgetprogname(),
317                 poptBadOption(optCon, POPT_BADOPTION_NOALIAS),
318                 poptStrerror(rc));
319         exit(EXIT_FAILURE);
320     }
321
322     /* Read rpm configuration (if not already read). */
323     rpmcliConfigured();
324
325     if (_debug) {
326         rpmIncreaseVerbosity();
327         rpmIncreaseVerbosity();
328     }
329
330     return optCon;
331 }