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