f260a38c4d052bd4840dd5df5542490eca4548c5
[platform/upstream/rpm.git] / tools / rpmdeps.c
1 #include "system.h"
2
3 #include <rpm/rpmbuild.h>
4 #include <rpm/argv.h>
5 #include <rpm/rpmds.h>
6 #include <rpm/rpmfc.h>
7
8 #include "debug.h"
9
10 static int print_provides;
11
12 static int print_requires;
13
14 static int print_recommends;
15
16 static int print_suggests;
17
18 static int print_supplements;
19
20 static int print_enhances;
21
22 static int print_conflicts;
23
24 static int print_obsoletes;
25
26 static int print_alldeps;
27
28 static void rpmdsPrint(const char * msg, rpmds ds, FILE * fp)
29 {
30     if (fp == NULL) fp = stderr;
31
32     if (msg)
33         fprintf(fp, "===================================== %s\n", msg);
34
35     ds = rpmdsInit(ds);
36     while (rpmdsNext(ds) >= 0)
37         fprintf(fp, "%s\n", rpmdsDNEVR(ds)+2);
38 }
39
40 static struct poptOption optionsTable[] = {
41
42  { NULL, '\0', POPT_ARG_INCLUDE_TABLE, rpmcliAllPoptTable, 0,
43         N_("Common options for all rpm modes and executables:"),
44         NULL }, 
45
46  { "provides", 'P', POPT_ARG_VAL, &print_provides, -1,
47         NULL, NULL },
48  { "requires", 'R', POPT_ARG_VAL, &print_requires, -1,
49         NULL, NULL },
50  { "recommends", '\0', POPT_ARG_VAL, &print_recommends, -1,
51         NULL, NULL },
52  { "suggests", '\0', POPT_ARG_VAL, &print_suggests, -1,
53         NULL, NULL },
54  { "supplements", '\0', POPT_ARG_VAL, &print_supplements, -1,
55         NULL, NULL },
56  { "enhances", '\0', POPT_ARG_VAL, &print_enhances, -1,
57         NULL, NULL },
58  { "conflicts", '\0', POPT_ARG_VAL, &print_conflicts, -1,
59         NULL, NULL },
60  { "obsoletes", '\0', POPT_ARG_VAL, &print_obsoletes, -1,
61         NULL, NULL },
62  { "alldeps", '\0', POPT_ARG_VAL, &print_alldeps, -1,
63         NULL, NULL },
64
65    POPT_AUTOALIAS
66    POPT_AUTOHELP
67    POPT_TABLEEND
68 };
69
70 int
71 main(int argc, char *argv[])
72 {
73     poptContext optCon = NULL;
74     ARGV_t av = NULL;
75     rpmfc fc = NULL;
76     int ec = 1;
77     char buf[BUFSIZ];
78
79     xsetprogname(argv[0]); /* Portability call -- see system.h */
80
81     optCon = rpmcliInit(argc, argv, optionsTable);
82     if (optCon == NULL)
83         goto exit;
84
85     /* normally files get passed through stdin but also accept files as args */
86     if (poptPeekArg(optCon)) {
87         const char *arg;
88         while ((arg = poptGetArg(optCon)) != NULL) {
89             argvAdd(&av, arg);
90         }
91     } else {
92         while (fgets(buf, sizeof(buf), stdin) != NULL) {
93             char *be = buf + strlen(buf) - 1;
94             while (strchr("\r\n", *be) != NULL)
95                 *be-- = '\0';
96             argvAdd(&av, buf);
97         }
98     }
99     /* Make sure file names are sorted. */
100     argvSort(av, NULL);
101
102     /* Build file/package class and dependency dictionaries. */
103     fc = rpmfcCreate(getenv("RPM_BUILD_ROOT"), 0);
104     if (rpmfcClassify(fc, av, NULL) || rpmfcApply(fc))
105         goto exit;
106
107     if (print_alldeps || _rpmfc_debug)
108         rpmfcPrint(NULL, fc, print_alldeps ? stdout : NULL);
109
110     if (!print_alldeps) {
111         if (print_provides)
112             rpmdsPrint(NULL, rpmfcProvides(fc), stdout);
113         if (print_requires)
114             rpmdsPrint(NULL, rpmfcRequires(fc), stdout);
115         if (print_recommends)
116             rpmdsPrint(NULL, rpmfcRecommends(fc), stdout);
117         if (print_suggests)
118             rpmdsPrint(NULL, rpmfcSuggests(fc), stdout);
119         if (print_supplements)
120             rpmdsPrint(NULL, rpmfcSupplements(fc), stdout);
121         if (print_enhances)
122             rpmdsPrint(NULL, rpmfcEnhances(fc), stdout);
123         if (print_conflicts)
124             rpmdsPrint(NULL, rpmfcConflicts(fc), stdout);
125         if (print_obsoletes)
126             rpmdsPrint(NULL, rpmfcObsoletes(fc), stdout);
127     }
128
129     ec = 0;
130
131 exit:
132     argvFree(av);
133     rpmfcFree(fc);
134     rpmcliFini(optCon);
135     return ec;
136 }