csomh / source-git / rpm

Forked from source-git/rpm 4 years ago
Clone
2ff057
#include "system.h"
2ff057
2ff057
#include <rpm/rpmcli.h>
2ff057
#include <rpm/rpmbuild.h>
2ff057
#include <rpm/rpmlog.h>
2ff057
#include <rpm/rpmts.h>
2ff057
2ff057
#include "cliutils.h"
2ff057
2ff057
#include "debug.h"
2ff057
2ff057
enum modes {
2ff057
    MODE_UNKNOWN	= 0,
2ff057
    MODE_QUERY		= (1 <<  0),
2ff057
    MODE_PARSE		= (1 <<  1),
2ff057
};
2ff057
2ff057
static int mode = MODE_UNKNOWN;
2ff057
static int source = RPMQV_SPECRPMS;
2ff057
const char *target = NULL;
2ff057
char *queryformat = NULL;
2ff057
2ff057
static struct poptOption specOptsTable[] = {
2ff057
    { "parse", 'P', POPT_ARG_VAL, &mode, MODE_PARSE,
2ff057
	N_("parse spec file(s) to stdout"), NULL },
2ff057
    { "query", 'q', POPT_ARG_VAL, &mode, MODE_QUERY,
2ff057
	N_("query spec file(s)"), NULL },
2ff057
    { "rpms", 0, POPT_ARG_VAL, &source, RPMQV_SPECRPMS,
2ff057
	N_("operate on binary rpms generated by spec (default)"), NULL },
2ff057
    { "builtrpms", 0, POPT_ARG_VAL, &source, RPMQV_SPECBUILTRPMS,
2ff057
	N_("operate on binary rpms that would be built from spec"), NULL },
2ff057
    { "srpm", 0, POPT_ARG_VAL, &source, RPMQV_SPECSRPM,
2ff057
	N_("operate on source rpm generated by spec"), NULL },
2ff057
    { "queryformat", 0, POPT_ARG_STRING, &queryformat, 0,
2ff057
	N_("use the following query format"), "QUERYFORMAT" },
2ff057
    { "qf", 0, (POPT_ARG_STRING | POPT_ARGFLAG_DOC_HIDDEN), &queryformat, 0,
2ff057
	NULL, NULL },
2ff057
    POPT_TABLEEND
2ff057
};
2ff057
2ff057
/* the structure describing the options we take and the defaults */
2ff057
static struct poptOption optionsTable[] = {
2ff057
    { NULL, '\0', POPT_ARG_INCLUDE_TABLE, specOptsTable, 0,
2ff057
	N_("Spec options:"), NULL },
2ff057
2ff057
    { NULL, '\0', POPT_ARG_INCLUDE_TABLE, rpmcliAllPoptTable, 0,
2ff057
	N_("Common options for all rpm modes and executables:"), NULL },
2ff057
2ff057
   POPT_AUTOALIAS
2ff057
   POPT_AUTOHELP
2ff057
   POPT_TABLEEND
2ff057
};
2ff057
2ff057
int main(int argc, char *argv[])
2ff057
{
2ff057
    rpmts ts = NULL;
2ff057
    QVA_t qva = &rpmQVKArgs;
2ff057
2ff057
    poptContext optCon;
2ff057
    int ec = 0;
2ff057
2ff057
    xsetprogname(argv[0]); /* Portability call -- see system.h */
2ff057
2ff057
    optCon = rpmcliInit(argc, argv, optionsTable);
2ff057
2ff057
    if (rpmcliPipeOutput && initPipe())
2ff057
	exit(EXIT_FAILURE);
2ff057
2ff057
    if (target) {
2ff057
	rpmFreeMacros(NULL);
2ff057
	rpmFreeRpmrc();
2ff057
	rpmReadConfigFiles(rpmcliRcfile, target);
2ff057
    }
2ff057
	
2ff057
    ts = rpmtsCreate();
2ff057
    switch (mode) {
2ff057
2ff057
    case MODE_QUERY:
2ff057
	if (!poptPeekArg(optCon))
2ff057
	    argerror(_("no arguments given for query"));
2ff057
2ff057
	qva->qva_queryFormat = queryformat;
2ff057
	qva->qva_source = source;
2ff057
	qva->qva_specQuery = rpmspecQuery;
2ff057
	ec = rpmcliQuery(ts, qva, (ARGV_const_t) poptGetArgs(optCon));
2ff057
	break;
2ff057
2ff057
    case MODE_PARSE: {
2ff057
	const char * spath;
2ff057
	if (!poptPeekArg(optCon))
2ff057
	    argerror(_("no arguments given for parse"));
2ff057
2ff057
	while ((spath = poptGetArg(optCon)) != NULL) {
2ff057
	    rpmSpec spec = rpmSpecParse(spath, (RPMSPEC_ANYARCH|RPMSPEC_FORCE), NULL);
2ff057
	    if (spec == NULL) {
2ff057
		ec++;
2ff057
		continue;
2ff057
	    }
2ff057
	    fprintf(stdout, "%s", rpmSpecGetSection(spec, RPMBUILD_NONE));
2ff057
	    rpmSpecFree(spec);
2ff057
	}
2ff057
	break;
2ff057
    }
2ff057
2ff057
    case MODE_UNKNOWN:
2ff057
	if (poptPeekArg(optCon) != NULL || argc <= 1 || rpmIsVerbose()) {
2ff057
	    printUsage(optCon, stderr, 0);
2ff057
	    ec = argc;
2ff057
	}
2ff057
	break;
2ff057
    }
2ff057
2ff057
    rpmtsFree(ts);
2ff057
    if (finishPipe())
2ff057
	ec = EXIT_FAILURE;
2ff057
2ff057
    free(qva->qva_queryFormat);
2ff057
2ff057
    rpmcliFini(optCon);
2ff057
2ff057
    return RETVAL(ec);
2ff057
}