summaryrefslogtreecommitdiff
path: root/build/parsePolicies.c
blob: 64b95b1c3a57c7cb682b262982d1959e09b25915 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
/** \ingroup rpmbuild
 * \file build/parsePolicies.c
 *  Parse %policies section from spec file.
 */

#include "system.h"

#include <rpm/header.h>
#include <rpm/rpmbuild.h>
#include <rpm/rpmlog.h>
#include <rpm/rpmfileutil.h>
#include "build/rpmbuild_internal.h"
#include "debug.h"

int parsePolicies(rpmSpec spec)
{
    int res = PART_ERROR;
    Package pkg;
    int rc, argc;
    int arg;
    const char **argv = NULL;
    char *name = NULL;
    int flag = PART_SUBNAME;
    poptContext optCon = NULL;

    struct poptOption optionsTable[] = {
	{NULL, 'n', POPT_ARG_STRING, &name, 'n', NULL, NULL},
	{0, 0, 0, 0, 0, NULL, NULL}
    };

    if ((rc = poptParseArgvString(spec->line, &argc, &argv))) {
	rpmlog(RPMLOG_ERR, _("line %d: Error parsing %%policies: %s\n"),
	       spec->lineNum, poptStrerror(rc));
	goto exit;
    }

    optCon = poptGetContext(NULL, argc, argv, optionsTable, 0);
    while ((arg = poptGetNextOpt(optCon)) > 0) {
	if (arg == 'n') {
	    flag = PART_NAME;
	}
    }

    if (arg < -1) {
	rpmlog(RPMLOG_ERR, _("line %d: Bad option %s: %s\n"),
	       spec->lineNum,
	       poptBadOption(optCon, POPT_BADOPTION_NOALIAS), spec->line);
	goto exit;
    }

    if (poptPeekArg(optCon)) {
	if (name == NULL)
	    name = xstrdup(poptGetArg(optCon));
	if (poptPeekArg(optCon)) {
	    rpmlog(RPMLOG_ERR, _("line %d: Too many names: %s\n"),
		   spec->lineNum, spec->line);
	    goto exit;
	}
    }

    if (lookupPackage(spec, name, flag, &pkg))
	goto exit;

    res = parseLines(spec, (STRIP_TRAILINGSPACE | STRIP_COMMENTS),
		     &(pkg->policyList), NULL);

  exit:
    free(argv);
    free(name);
    poptFreeContext(optCon);

    return res;
}