fix msm-plugin.c svace issue: make sure dupPath is not NULL before strchr()
[platform/upstream/rpm.git] / rpmspec.c
1 #include "system.h"
2
3 #include <rpm/rpmcli.h>
4 #include <rpm/rpmbuild.h>
5 #include <rpm/rpmlog.h>
6 #include <rpm/rpmts.h>
7
8 #include "cliutils.h"
9
10 #include "debug.h"
11
12 enum modes {
13     MODE_UNKNOWN        = 0,
14     MODE_QUERY          = (1 <<  0),
15     MODE_PARSE          = (1 <<  1),
16 };
17
18 static int mode = MODE_UNKNOWN;
19 static int source = RPMQV_SPECRPMS;
20 const char *target = NULL;
21 char *queryformat = NULL;
22
23 static struct poptOption specOptsTable[] = {
24     { "parse", 'P', POPT_ARG_VAL, &mode, MODE_PARSE,
25         N_("parse spec file(s) to stdout"), NULL },
26     { "query", 'q', POPT_ARG_VAL, &mode, MODE_QUERY,
27         N_("query spec file(s)"), NULL },
28     { "rpms", 0, POPT_ARG_VAL, &source, RPMQV_SPECRPMS,
29         N_("operate on binary rpms generated by spec (default)"), NULL },
30     { "builtrpms", 0, POPT_ARG_VAL, &source, RPMQV_SPECBUILTRPMS,
31         N_("operate on binary rpms that would be built from spec"), NULL },
32     { "srpm", 0, POPT_ARG_VAL, &source, RPMQV_SPECSRPM,
33         N_("operate on source rpm generated by spec"), NULL },
34     { "queryformat", 0, POPT_ARG_STRING, &queryformat, 0,
35         N_("use the following query format"), "QUERYFORMAT" },
36     { "qf", 0, (POPT_ARG_STRING | POPT_ARGFLAG_DOC_HIDDEN), &queryformat, 0,
37         NULL, NULL },
38     POPT_TABLEEND
39 };
40
41 /* the structure describing the options we take and the defaults */
42 static struct poptOption optionsTable[] = {
43     { NULL, '\0', POPT_ARG_INCLUDE_TABLE, specOptsTable, 0,
44         N_("Spec options:"), NULL },
45
46     { NULL, '\0', POPT_ARG_INCLUDE_TABLE, rpmcliAllPoptTable, 0,
47         N_("Common options for all rpm modes and executables:"), NULL },
48
49    POPT_AUTOALIAS
50    POPT_AUTOHELP
51    POPT_TABLEEND
52 };
53
54 int main(int argc, char *argv[])
55 {
56     rpmts ts = NULL;
57     QVA_t qva = &rpmQVKArgs;
58
59     poptContext optCon;
60     int ec = 0;
61
62     xsetprogname(argv[0]); /* Portability call -- see system.h */
63
64     optCon = rpmcliInit(argc, argv, optionsTable);
65
66     if (rpmcliPipeOutput && initPipe())
67         exit(EXIT_FAILURE);
68
69     if (target) {
70         rpmFreeMacros(NULL);
71         rpmFreeRpmrc();
72         rpmReadConfigFiles(rpmcliRcfile, target);
73     }
74         
75     ts = rpmtsCreate();
76     switch (mode) {
77
78     case MODE_QUERY:
79         if (!poptPeekArg(optCon))
80             argerror(_("no arguments given for query"));
81
82         qva->qva_queryFormat = queryformat;
83         qva->qva_source = source;
84         qva->qva_specQuery = rpmspecQuery;
85         ec = rpmcliQuery(ts, qva, (ARGV_const_t) poptGetArgs(optCon));
86         break;
87
88     case MODE_PARSE: {
89         const char * spath;
90         if (!poptPeekArg(optCon))
91             argerror(_("no arguments given for parse"));
92
93         while ((spath = poptGetArg(optCon)) != NULL) {
94             rpmSpec spec = rpmSpecParse(spath, (RPMSPEC_ANYARCH|RPMSPEC_FORCE), NULL);
95             if (spec == NULL) {
96                 ec++;
97                 continue;
98             }
99             fprintf(stdout, "%s", rpmSpecGetSection(spec, RPMBUILD_NONE));
100             rpmSpecFree(spec);
101         }
102         break;
103     }
104
105     case MODE_UNKNOWN:
106         if (poptPeekArg(optCon) != NULL || argc <= 1 || rpmIsVerbose()) {
107             printUsage(optCon, stderr, 0);
108             ec = argc;
109         }
110         break;
111     }
112
113     rpmtsFree(ts);
114     if (finishPipe())
115         ec = EXIT_FAILURE;
116
117     free(qva->qva_queryFormat);
118
119     rpmcliFini(optCon);
120
121     return RETVAL(ec);
122 }