Permit file objects in python header constructor
[platform/upstream/rpm.git] / build / parseDescription.c
1 /** \ingroup rpmbuild
2  * \file build/parseDescription.c
3  *  Parse %description section from spec file.
4  */
5
6 #include "system.h"
7
8 #include <rpm/header.h>
9 #include <rpm/rpmbuild.h>
10 #include <rpm/rpmlog.h>
11 #include "debug.h"
12
13 extern int noLang;
14
15 int parseDescription(rpmSpec spec)
16 {
17     int nextPart = PART_ERROR;  /* assume error */
18     StringBuf sb;
19     int flag = PART_SUBNAME;
20     Package pkg;
21     int rc, argc;
22     int arg;
23     const char **argv = NULL;
24     const char *name = NULL;
25     const char *lang = RPMBUILD_DEFAULT_LANG;
26     poptContext optCon = NULL;
27     spectag t = NULL;
28     struct poptOption optionsTable[] = {
29         { NULL, 'n', POPT_ARG_STRING, &name, 'n', NULL, NULL},
30         { NULL, 'l', POPT_ARG_STRING, &lang, 'l', NULL, NULL},
31         { 0, 0, 0, 0, 0, NULL, NULL}
32     };
33
34     if ((rc = poptParseArgvString(spec->line, &argc, &argv))) {
35         rpmlog(RPMLOG_ERR, _("line %d: Error parsing %%description: %s\n"),
36                  spec->lineNum, poptStrerror(rc));
37         return PART_ERROR;
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         rpmlog(RPMLOG_ERR, _("line %d: Bad option %s: %s\n"),
49                  spec->lineNum,
50                  poptBadOption(optCon, POPT_BADOPTION_NOALIAS), 
51                  spec->line);
52         goto exit;
53     }
54
55     if (poptPeekArg(optCon)) {
56         if (name == NULL)
57             name = poptGetArg(optCon);
58         if (poptPeekArg(optCon)) {
59             rpmlog(RPMLOG_ERR, _("line %d: Too many names: %s\n"),
60                      spec->lineNum,
61                      spec->line);
62             goto exit;
63         }
64     }
65
66     if (lookupPackage(spec, name, flag, &pkg)) {
67         rpmlog(RPMLOG_ERR, _("line %d: Package does not exist: %s\n"),
68                  spec->lineNum, spec->line);
69         goto exit;
70     }
71
72
73     /******************/
74
75 #if 0    
76     if (headerIsEntry(pkg->header, RPMTAG_DESCRIPTION)) {
77         rpmlog(RPMLOG_ERR, _("line %d: Second description\n"),
78                 spec->lineNum);
79         goto exit;
80     }
81 #endif
82
83     t = stashSt(spec, pkg->header, RPMTAG_DESCRIPTION, lang);
84     
85     sb = newStringBuf();
86
87     if ((rc = readLine(spec, STRIP_TRAILINGSPACE | STRIP_COMMENTS)) > 0) {
88         nextPart = PART_NONE;
89     } else if (rc < 0) {
90             nextPart = PART_ERROR;
91             goto exit;
92     } else {
93         while (! (nextPart = isPart(spec->line))) {
94             appendLineStringBuf(sb, spec->line);
95             if (t) t->t_nlines++;
96             if ((rc =
97                 readLine(spec, STRIP_TRAILINGSPACE | STRIP_COMMENTS)) > 0) {
98                 nextPart = PART_NONE;
99                 break;
100             } else if (rc < 0) {
101                 nextPart = PART_ERROR;
102                 goto exit;
103             }
104         }
105     }
106     
107     stripTrailingBlanksStringBuf(sb);
108     if (!(noLang && !rstreq(lang, RPMBUILD_DEFAULT_LANG))) {
109         (void) headerAddI18NString(pkg->header, RPMTAG_DESCRIPTION,
110                         getStringBuf(sb), lang);
111     }
112     
113     sb = freeStringBuf(sb);
114      
115 exit:
116     argv = _free(argv);
117     optCon = poptFreeContext(optCon);
118     return nextPart;
119 }