- globalize _free(3) wrapper in rpmlib.h, consistent usage throughout.
[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 "rpmbuild.h"
9 #include "debug.h"
10
11 /*@access StringBuf @*/         /* compared with NULL */
12
13 /* These have to be global scope to make up for *stupid* compilers */
14     /*@observer@*/ /*@null@*/ static const char *name = NULL;
15     /*@observer@*/ /*@null@*/ static const char *file = NULL;
16     static struct poptOption optionsTable[] = {
17         { NULL, 'n', POPT_ARG_STRING, &name, 'n',       NULL, NULL},
18         { NULL, 'f', POPT_ARG_STRING, &file, 'f',       NULL, NULL},
19         { 0, 0, 0, 0, 0,        NULL, NULL}
20     };
21
22 int parseFiles(Spec spec)
23 {
24     int nextPart;
25     Package pkg;
26     int rc, argc;
27     int arg;
28     const char **argv = NULL;
29     int flag = PART_SUBNAME;
30     poptContext optCon = NULL;
31
32     name = file = NULL;
33
34     if ((rc = poptParseArgvString(spec->line, &argc, &argv))) {
35         rpmError(RPMERR_BADSPEC, _("line %d: Error parsing %%files: %s\n"),
36                  spec->lineNum, poptStrerror(rc));
37         rc = RPMERR_BADSPEC;
38         goto exit;
39     }
40
41     optCon = poptGetContext(NULL, argc, argv, optionsTable, 0);
42     while ((arg = poptGetNextOpt(optCon)) > 0) {
43         if (arg == 'n') {
44             flag = PART_NAME;
45         }
46     }
47
48     if (arg < -1) {
49         rpmError(RPMERR_BADSPEC, _("line %d: Bad option %s: %s\n"),
50                  spec->lineNum,
51                  poptBadOption(optCon, POPT_BADOPTION_NOALIAS), 
52                  spec->line);
53         rc = RPMERR_BADSPEC;
54         goto exit;
55     }
56
57     if (poptPeekArg(optCon)) {
58         if (name == NULL)
59             name = poptGetArg(optCon);
60         if (poptPeekArg(optCon)) {
61             rpmError(RPMERR_BADSPEC, _("line %d: Too many names: %s\n"),
62                      spec->lineNum,
63                      spec->line);
64             rc = RPMERR_BADSPEC;
65             goto exit;
66         }
67     }
68
69     if (lookupPackage(spec, name, flag, &pkg)) {
70         rpmError(RPMERR_BADSPEC, _("line %d: Package does not exist: %s\n"),
71                  spec->lineNum, spec->line);
72         rc = RPMERR_BADSPEC;
73         goto exit;
74     }
75
76     if (pkg->fileList != NULL) {
77         rpmError(RPMERR_BADSPEC, _("line %d: Second %%files list\n"),
78                  spec->lineNum);
79         rc = RPMERR_BADSPEC;
80         goto exit;
81     }
82
83     if (file)  {
84     /* XXX not necessary as readline has expanded already, but won't hurt.  */
85         pkg->fileFile = rpmGetPath(file, NULL);
86     }
87
88     pkg->fileList = newStringBuf();
89     
90     if ((rc = readLine(spec, STRIP_COMMENTS)) > 0) {
91         nextPart = PART_NONE;
92     } else {
93         if (rc)
94             goto exit;
95         while (! (nextPart = isPart(spec->line))) {
96             appendStringBuf(pkg->fileList, spec->line);
97             if ((rc = readLine(spec, STRIP_COMMENTS)) > 0) {
98                 nextPart = PART_NONE;
99                 break;
100             }
101             if (rc)
102                 goto exit;
103         }
104     }
105     rc = nextPart;
106
107 exit:
108     argv = _free(argv);
109     if (optCon)
110         poptFreeContext(optCon);
111         
112     return rc;
113 }