c3112eba58bff54bea1161f097527ba2264ae015
[platform/upstream/rpm.git] / tools / rpmdeps.c
1 #include "system.h"
2 const char *__progname;
3
4 #include <rpm/rpmbuild.h>
5 #include <rpm/argv.h>
6 #include <rpm/rpmds.h>
7 #include <rpm/rpmfc.h>
8
9 #include "debug.h"
10
11 char *progname;
12
13 static int print_provides;
14
15 static int print_requires;
16
17 static void rpmdsPrint(const char * msg, rpmds ds, FILE * fp)
18 {
19     if (fp == NULL) fp = stderr;
20
21     if (msg)
22         fprintf(fp, "===================================== %s\n", msg);
23
24     ds = rpmdsInit(ds);
25     while (rpmdsNext(ds) >= 0)
26         fprintf(fp, "%s\n", rpmdsDNEVR(ds)+2);
27 }
28
29 static struct poptOption optionsTable[] = {
30
31  { NULL, '\0', POPT_ARG_INCLUDE_TABLE, rpmcliAllPoptTable, 0,
32         N_("Common options for all rpm modes and executables:"),
33         NULL }, 
34
35  { "provides", 'P', POPT_ARG_VAL, &print_provides, -1,
36         NULL, NULL },
37  { "requires", 'R', POPT_ARG_VAL, &print_requires, -1,
38         NULL, NULL },
39
40    POPT_AUTOALIAS
41    POPT_AUTOHELP
42    POPT_TABLEEND
43 };
44
45 int
46 main(int argc, char *argv[])
47 {
48     poptContext optCon = NULL;
49     ARGV_t av = NULL;
50     rpmfc fc = NULL;
51     int ec = 1;
52     char buf[BUFSIZ];
53
54     if ((progname = strrchr(argv[0], '/')) != NULL)
55         progname++;
56     else
57         progname = argv[0];
58
59     optCon = rpmcliInit(argc, argv, optionsTable);
60     if (optCon == NULL)
61         goto exit;
62
63     /* normally files get passed through stdin but also accept files as args */
64     if (poptPeekArg(optCon)) {
65         const char *arg;
66         while ((arg = poptGetArg(optCon)) != NULL) {
67             argvAdd(&av, arg);
68         }
69     } else {
70         while (fgets(buf, sizeof(buf), stdin) != NULL) {
71             char *be = buf + strlen(buf) - 1;
72             while (strchr("\r\n", *be) != NULL)
73                 *be-- = '\0';
74             argvAdd(&av, buf);
75         }
76     }
77     /* Make sure file names are sorted. */
78     argvSort(av, NULL);
79
80     /* Build file/package class and dependency dictionaries. */
81     fc = rpmfcCreate(getenv("RPM_BUILD_ROOT"), 0);
82     if (rpmfcClassify(fc, av, NULL) || rpmfcApply(fc))
83         goto exit;
84
85     if (_rpmfc_debug)
86         rpmfcPrint(buf, fc, NULL);
87
88     if (print_provides)
89         rpmdsPrint(NULL, rpmfcProvides(fc), stdout);
90     if (print_requires)
91         rpmdsPrint(NULL, rpmfcRequires(fc), stdout);
92
93     ec = 0;
94
95 exit:
96     argvFree(av);
97     rpmfcFree(fc);
98     rpmcliFini(optCon);
99     return ec;
100 }