129541635571194f25c5fabed084a60023602cc0
[platform/upstream/rpm.git] / build / parsePolicies.c
1 /** \ingroup rpmbuild
2  * \file build/parsePolicies.c
3  *  Parse %policies section from spec file.
4  */
5
6 #include "system.h"
7
8 #include <rpm/header.h>
9 #include <rpm/rpmbuild.h>
10 #include <rpm/rpmlog.h>
11 #include <rpm/rpmfileutil.h>
12 #include "build/rpmbuild_internal.h"
13 #include "debug.h"
14
15 int parsePolicies(rpmSpec spec)
16 {
17     int nextPart, res = PART_ERROR;
18     Package pkg;
19     int rc, argc;
20     int arg;
21     const char **argv = NULL;
22     const char *name = NULL;
23     int flag = PART_SUBNAME;
24     poptContext optCon = NULL;
25
26     struct poptOption optionsTable[] = {
27         {NULL, 'n', POPT_ARG_STRING, &name, 'n', NULL, NULL},
28         {0, 0, 0, 0, 0, NULL, NULL}
29     };
30
31     if ((rc = poptParseArgvString(spec->line, &argc, &argv))) {
32         rpmlog(RPMLOG_ERR, _("line %d: Error parsing %%policies: %s\n"),
33                spec->lineNum, poptStrerror(rc));
34         goto exit;
35     }
36
37     optCon = poptGetContext(NULL, argc, argv, optionsTable, 0);
38     while ((arg = poptGetNextOpt(optCon)) > 0) {
39         if (arg == 'n') {
40             flag = PART_NAME;
41         }
42     }
43
44     if (arg < -1) {
45         rpmlog(RPMLOG_ERR, _("line %d: Bad option %s: %s\n"),
46                spec->lineNum,
47                poptBadOption(optCon, POPT_BADOPTION_NOALIAS), spec->line);
48         goto exit;
49     }
50
51     if (poptPeekArg(optCon)) {
52         if (name == NULL)
53             name = poptGetArg(optCon);
54         if (poptPeekArg(optCon)) {
55             rpmlog(RPMLOG_ERR, _("line %d: Too many names: %s\n"),
56                    spec->lineNum, spec->line);
57             goto exit;
58         }
59     }
60
61     if (lookupPackage(spec, name, flag, &pkg)) {
62         rpmlog(RPMLOG_ERR, _("line %d: Package does not exist: %s\n"),
63                spec->lineNum, spec->line);
64         goto exit;
65     }
66
67     if ((rc = readLine(spec, STRIP_TRAILINGSPACE | STRIP_COMMENTS)) > 0) {
68         nextPart = PART_NONE;
69     } else if (rc < 0) {
70         goto exit;
71     } else {
72         while (!(nextPart = isPart(spec->line))) {
73             argvAdd(&(pkg->policyList), spec->line);
74             if ((rc = readLine(spec, STRIP_TRAILINGSPACE | STRIP_COMMENTS)) > 0) {
75                 nextPart = PART_NONE;
76                 break;
77             } else if (rc < 0) {
78                 goto exit;
79             }
80         }
81     }
82     res = nextPart;
83
84   exit:
85     free(argv);
86     poptFreeContext(optCon);
87
88     return res;
89 }