Add --qf to rpmgi.
[tools/librpm-tizen.git] / lib / tgi.c
1 #include "system.h"
2
3 #include <rpmio_internal.h>
4 #define _RPMGI_INTERNAL
5 #include <rpmgi.h>
6 #include <rpmcli.h>
7
8 #include <rpmmacro.h>
9 #include <rpmmessages.h>
10 #include <popt.h>
11
12 #include "debug.h"
13
14 static int gitag = RPMGI_FTSWALK;
15 static int ftsOpts = 0;
16
17 static const char * queryFormat = NULL;
18
19 static struct poptOption optionsTable[] = {
20  { "rpmgidebug", 'd', POPT_ARG_VAL|POPT_ARGFLAG_DOC_HIDDEN, &_rpmgi_debug, -1,
21         N_("debug generalized iterator"), NULL},
22
23  { "rpmdb", '\0', POPT_ARG_VAL, &gitag, RPMGI_RPMDB,
24         N_("iterate rpmdb"), NULL },
25  { "hdlist", '\0', POPT_ARG_VAL, &gitag, RPMGI_HDLIST,
26         N_("iterate hdlist"), NULL },
27  { "arglist", '\0', POPT_ARG_VAL, &gitag, RPMGI_ARGLIST,
28         N_("iterate arglist"), NULL },
29  { "ftswalk", '\0', POPT_ARG_VAL, &gitag, RPMGI_FTSWALK,
30         N_("iterate fts(3) walk"), NULL },
31
32  { "qf", '\0', POPT_ARG_STRING, &queryFormat, 0,
33         N_("use the following query format"), "QUERYFORMAT" },
34  { "queryformat", '\0', POPT_ARG_STRING, &queryFormat, 0,
35         N_("use the following query format"), "QUERYFORMAT" },
36
37  { "comfollow", '\0', POPT_BIT_SET,     &ftsOpts, FTS_COMFOLLOW,
38         N_("follow command line symlinks"), NULL },
39  { "logical", '\0', POPT_BIT_SET,       &ftsOpts, FTS_LOGICAL,
40         N_("logical walk"), NULL },
41  { "nochdir", '\0', POPT_BIT_SET,       &ftsOpts, FTS_NOCHDIR,
42         N_("don't change directories"), NULL },
43  { "nostat", '\0', POPT_BIT_SET,        &ftsOpts, FTS_NOSTAT,
44         N_("don't get stat info"), NULL },
45  { "physical", '\0', POPT_BIT_SET,      &ftsOpts, FTS_PHYSICAL,
46         N_("physical walk"), NULL },
47  { "seedot", '\0', POPT_BIT_SET,        &ftsOpts, FTS_SEEDOT,
48         N_("return dot and dot-dot"), NULL },
49  { "xdev", '\0', POPT_BIT_SET,          &ftsOpts, FTS_XDEV,
50         N_("don't cross devices"), NULL },
51  { "whiteout", '\0', POPT_BIT_SET,      &ftsOpts, FTS_WHITEOUT,
52         N_("return whiteout information"), NULL },
53
54  { NULL, '\0', POPT_ARG_INCLUDE_TABLE, rpmcliAllPoptTable, 0,
55         N_("Common options for all rpm modes and executables:"),
56         NULL },
57
58   POPT_AUTOALIAS
59   POPT_AUTOHELP
60   POPT_TABLEEND
61 };
62
63 int
64 main(int argc, char *const argv[])
65 {
66     poptContext optCon;
67     rpmts ts = NULL;
68     rpmgi gi = NULL;
69     const char ** av;
70     const char * arg;
71     int ac;
72     int rc = 0;
73
74     optCon = rpmcliInit(argc, argv, optionsTable);
75     if (optCon == NULL)
76         exit(EXIT_FAILURE);
77
78     if (ftsOpts == 0)
79         ftsOpts = (FTS_COMFOLLOW | FTS_LOGICAL | FTS_NOSTAT);
80
81     ts = rpmtsCreate();
82     av = poptGetArgs(optCon);
83     gi = rpmgiNew(ts, gitag, av, ftsOpts);
84     (void) rpmgiSetQueryFormat(gi, queryFormat);
85
86     ac = 0;
87     while ((arg = rpmgiNext(gi)) != NULL) {
88         fprintf(stderr, "%5d %s\n", ac, arg);
89         arg = _free(arg);
90         ac++;
91     }
92
93     gi = rpmgiFree(gi);
94     ts = rpmtsFree(ts);
95     optCon = rpmcliFini(optCon);
96
97     return rc;
98 }