Realize the remaining bits of direct rpmdb interface are dead too
[platform/upstream/rpm.git] / build / parseFiles.c
1 /** \ingroup rpmbuild
2  * \file build/parseFiles.c
3  *  Parse %files section from spec file.
4  */
5
6 #include "system.h"
7
8 #include <rpm/rpmbuild.h>
9 #include <rpm/rpmlog.h>
10 #include <rpm/rpmfileutil.h>
11 #include "debug.h"
12
13 int parseFiles(rpmSpec spec)
14 {
15     int nextPart, res = PART_ERROR;
16     Package pkg;
17     int rc, argc;
18     int arg;
19     const char ** argv = NULL;
20     const char *name = NULL;
21     int flag = PART_SUBNAME;
22     poptContext optCon = NULL;
23     struct poptOption optionsTable[] = {
24         { NULL, 'n', POPT_ARG_STRING, &name, 'n', NULL, NULL},
25         { NULL, 'f', POPT_ARG_STRING, NULL, 'f', NULL, NULL},
26         { 0, 0, 0, 0, 0, NULL, NULL}
27     };
28
29     if ((rc = poptParseArgvString(spec->line, &argc, &argv))) {
30         rpmlog(RPMLOG_ERR, _("line %d: Error parsing %%files: %s\n"),
31                  spec->lineNum, poptStrerror(rc));
32         goto exit;
33     }
34
35     optCon = poptGetContext(NULL, argc, argv, optionsTable, 0);
36     while ((arg = poptGetNextOpt(optCon)) > 0) {
37         if (arg == 'n') {
38             flag = PART_NAME;
39         }
40     }
41
42     if (arg < -1) {
43         rpmlog(RPMLOG_ERR, _("line %d: Bad option %s: %s\n"),
44                  spec->lineNum,
45                  poptBadOption(optCon, POPT_BADOPTION_NOALIAS), 
46                  spec->line);
47         goto exit;
48     }
49
50     if (poptPeekArg(optCon)) {
51         if (name == NULL)
52             name = poptGetArg(optCon);
53         if (poptPeekArg(optCon)) {
54             rpmlog(RPMLOG_ERR, _("line %d: Too many names: %s\n"),
55                      spec->lineNum,
56                      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     for (arg=1; arg<argc; arg++) {
68         if (rstreq(argv[arg], "-f") && argv[arg+1]) {
69             char *file = rpmGetPath(argv[arg+1], NULL);
70             if (!pkg->fileFile) pkg->fileFile = newStringBuf();
71             appendLineStringBuf(pkg->fileFile, file);
72             free(file);
73         }
74     }
75
76     pkg->fileList = newStringBuf();
77     
78     if ((rc = readLine(spec, STRIP_COMMENTS)) > 0) {
79         nextPart = PART_NONE;
80     } else if (rc < 0) {
81         goto exit;
82     } else {
83         while (! (nextPart = isPart(spec->line))) {
84             appendStringBuf(pkg->fileList, spec->line);
85             if ((rc = readLine(spec, STRIP_COMMENTS)) > 0) {
86                 nextPart = PART_NONE;
87                 break;
88             } else if (rc < 0) {
89                 goto exit;
90             }
91         }
92     }
93     res = nextPart;
94
95 exit:
96     argv = _free(argv);
97     optCon = poptFreeContext(optCon);
98         
99     return res;
100 }