LCLINT pass 0.
[platform/upstream/rpm.git] / build / parseFiles.c
1 #include "system.h"
2
3 #include "rpmbuild.h"
4
5 #include "popt/popt.h"
6
7 /* These have to be global scope to make up for *stupid* compilers */
8     static char *name;
9     static char *file;
10     static struct poptOption optionsTable[] = {
11         { NULL, 'n', POPT_ARG_STRING, &name, 'n' },
12         { NULL, 'f', POPT_ARG_STRING, &file, 'f' },
13         { 0, 0, 0, 0, 0 }
14     };
15
16 int parseFiles(Spec spec)
17 {
18     int nextPart;
19     Package pkg;
20     int rc, argc;
21     int arg;
22     char **argv = NULL;
23     int flag = PART_SUBNAME;
24     poptContext optCon = NULL;
25
26     name = file = NULL;
27
28     if ((rc = poptParseArgvString(spec->line, &argc, &argv))) {
29         rpmError(RPMERR_BADSPEC, _("line %d: Error parsing %%files: %s"),
30                  spec->lineNum, poptStrerror(rc));
31         return RPMERR_BADSPEC;
32     }
33
34     optCon = poptGetContext(NULL, argc, argv, optionsTable, 0);
35     while ((arg = poptGetNextOpt(optCon)) > 0) {
36         if (arg == 'n') {
37             flag = PART_NAME;
38         }
39     }
40
41     if (arg < -1) {
42         rpmError(RPMERR_BADSPEC, _("line %d: Bad option %s: %s"),
43                  spec->lineNum,
44                  poptBadOption(optCon, POPT_BADOPTION_NOALIAS), 
45                  spec->line);
46         FREE(argv);
47         poptFreeContext(optCon);
48         return RPMERR_BADSPEC;
49     }
50
51     if (poptPeekArg(optCon)) {
52         if (! name) {
53             name = poptGetArg(optCon);
54         }
55         if (poptPeekArg(optCon)) {
56             rpmError(RPMERR_BADSPEC, _("line %d: Too many names: %s"),
57                      spec->lineNum,
58                      spec->line);
59             FREE(argv);
60             poptFreeContext(optCon);
61             return RPMERR_BADSPEC;
62         }
63     }
64
65     if (lookupPackage(spec, name, flag, &pkg)) {
66         rpmError(RPMERR_BADSPEC, _("line %d: Package does not exist: %s"),
67                  spec->lineNum, spec->line);
68         FREE(argv);
69         poptFreeContext(optCon);
70         return RPMERR_BADSPEC;
71     }
72
73     if (pkg->fileList != NULL) {
74         rpmError(RPMERR_BADSPEC, _("line %d: Second %%files list"),
75                  spec->lineNum);
76         FREE(argv);
77         poptFreeContext(optCon);
78         return RPMERR_BADSPEC;
79     }
80
81     if (file) {
82         pkg->fileFile = strdup(file);
83     }
84     pkg->fileList = newStringBuf();
85     
86     if ((rc = readLine(spec, STRIP_COMMENTS)) > 0) {
87         nextPart = PART_NONE;
88     } else {
89         if (rc) {
90             return rc;
91         }
92         while (! (nextPart = isPart(spec->line))) {
93             appendStringBuf(pkg->fileList, spec->line);
94             if ((rc = readLine(spec, STRIP_COMMENTS)) > 0) {
95                 nextPart = PART_NONE;
96                 break;
97             }
98             if (rc) {
99                 return rc;
100             }
101         }
102     }
103
104     FREE(argv);
105     poptFreeContext(optCon);
106         
107     return nextPart;
108 }