merged in Marc's new build code
[platform/upstream/rpm.git] / build / parseFiles.c
1 #include <string.h>
2 #include <malloc.h>
3
4 #include "header.h"
5 #include "read.h"
6 #include "part.h"
7 #include "misc.h"
8 #include "rpmlib.h"
9 #include "package.h"
10 #include "stringbuf.h"
11 #include "popt/popt.h"
12
13 int parseFiles(Spec spec)
14 {
15     int nextPart;
16     Package pkg;
17     char *name = NULL;
18     char *file = NULL;
19     int rc, argc;
20     char arg, **argv = NULL;
21     int flag = PART_SUBNAME;
22     poptContext optCon = NULL;
23     struct poptOption optionsTable[] = {
24         { NULL, 'n', POPT_ARG_STRING, &name, 'n' },
25         { NULL, 'f', POPT_ARG_STRING, &file, 'f' },
26         { 0, 0, 0, 0, 0 }
27     };
28
29     if ((rc = poptParseArgvString(spec->line, &argc, &argv))) {
30         rpmError(RPMERR_BADSPEC, "line %d: Error parsing %%files: %s",
31                  spec->lineNum, poptStrerror(rc));
32         return RPMERR_BADSPEC;
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         rpmError(RPMERR_BADSPEC, "line %d: Bad option %s: %s",
44                  spec->lineNum,
45                  poptBadOption(optCon, POPT_BADOPTION_NOALIAS), 
46                  spec->line);
47         FREE(argv);
48         poptFreeContext(optCon);
49         return RPMERR_BADSPEC;
50     }
51
52     if (poptPeekArg(optCon)) {
53         if (! name) {
54             name = poptGetArg(optCon);
55         }
56         if (poptPeekArg(optCon)) {
57             rpmError(RPMERR_BADSPEC, "line %d: Too many names: %s",
58                      spec->lineNum,
59                      spec->line);
60             FREE(argv);
61             poptFreeContext(optCon);
62             return RPMERR_BADSPEC;
63         }
64     }
65
66     if (lookupPackage(spec, name, flag, &pkg)) {
67         rpmError(RPMERR_BADSPEC, "line %d: Package does not exist: %s",
68                  spec->lineNum, spec->line);
69         FREE(argv);
70         poptFreeContext(optCon);
71         return RPMERR_BADSPEC;
72     }
73
74     if (pkg->fileList) {
75         rpmError(RPMERR_BADSPEC, "line %d: Second %%files list",
76                  spec->lineNum);
77         FREE(argv);
78         poptFreeContext(optCon);
79         return RPMERR_BADSPEC;
80     }
81
82     if (file) {
83         pkg->fileFile = strdup(file);
84     }
85     pkg->fileList = newStringBuf();
86     
87     if (readLine(spec, STRIP_NOTHING) > 0) {
88         nextPart = PART_NONE;
89     } else {
90         while (! (nextPart = isPart(spec->line))) {
91             appendLineStringBuf(pkg->fileList, spec->line);
92             if (readLine(spec, STRIP_NOTHING) > 0) {
93                 nextPart = PART_NONE;
94                 break;
95             }
96         }
97     }
98
99     FREE(argv);
100     poptFreeContext(optCon);
101         
102     return nextPart;
103 }