- fix: extra newline in many error messages (#23947).
[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 "rpmbuild.h"
9 #include "debug.h"
10
11 extern int noLang;              /* XXX FIXME: pass as arg */
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 *lang = NULL;
16
17     static struct poptOption optionsTable[] = {
18         { NULL, 'n', POPT_ARG_STRING, &name, 'n',       NULL, NULL},
19         { NULL, 'l', POPT_ARG_STRING, &lang, 'l',       NULL, NULL},
20         { 0, 0, 0, 0, 0,        NULL, NULL}
21     };
22
23 int parseDescription(Spec spec)
24 {
25     int nextPart;
26     StringBuf sb;
27     int flag = PART_SUBNAME;
28     Package pkg;
29     int rc, argc;
30     int arg;
31     const char **argv = NULL;
32     poptContext optCon = NULL;
33     struct spectag *t = NULL;
34
35     name = NULL;
36     lang = RPMBUILD_DEFAULT_LANG;
37
38     if ((rc = poptParseArgvString(spec->line, &argc, &argv))) {
39         rpmError(RPMERR_BADSPEC, _("line %d: Error parsing %%description: %s\n"),
40                  spec->lineNum, poptStrerror(rc));
41         return RPMERR_BADSPEC;
42     }
43
44     optCon = poptGetContext(NULL, argc, argv, optionsTable, 0);
45     while ((arg = poptGetNextOpt(optCon)) > 0) {
46         if (arg == 'n') {
47             flag = PART_NAME;
48         }
49     }
50
51     if (arg < -1) {
52         rpmError(RPMERR_BADSPEC, _("line %d: Bad option %s: %s\n"),
53                  spec->lineNum,
54                  poptBadOption(optCon, POPT_BADOPTION_NOALIAS), 
55                  spec->line);
56         FREE(argv);
57         poptFreeContext(optCon);
58         return RPMERR_BADSPEC;
59     }
60
61     if (poptPeekArg(optCon)) {
62         if (name == NULL)
63             name = poptGetArg(optCon);
64         if (poptPeekArg(optCon)) {
65             rpmError(RPMERR_BADSPEC, _("line %d: Too many names: %s\n"),
66                      spec->lineNum,
67                      spec->line);
68             FREE(argv);
69             poptFreeContext(optCon);
70             return RPMERR_BADSPEC;
71         }
72     }
73
74     if (lookupPackage(spec, name, flag, &pkg)) {
75         rpmError(RPMERR_BADSPEC, _("line %d: Package does not exist: %s\n"),
76                  spec->lineNum, spec->line);
77         FREE(argv);
78         poptFreeContext(optCon);
79         return RPMERR_BADSPEC;
80     }
81
82
83     /******************/
84
85 #if 0    
86     if (headerIsEntry(pkg->header, RPMTAG_DESCRIPTION)) {
87         rpmError(RPMERR_BADSPEC, _("line %d: Second description\n"),
88                 spec->lineNum);
89         FREE(argv);
90         poptFreeContext(optCon);
91         return RPMERR_BADSPEC;
92     }
93 #endif
94
95     t = stashSt(spec, pkg->header, RPMTAG_DESCRIPTION, lang);
96     
97     sb = newStringBuf();
98
99     if ((rc = readLine(spec, STRIP_TRAILINGSPACE | STRIP_COMMENTS)) > 0) {
100         nextPart = PART_NONE;
101     } else {
102         if (rc) {
103             return rc;
104         }
105         while (! (nextPart = isPart(spec->line))) {
106             appendLineStringBuf(sb, spec->line);
107             if (t) t->t_nlines++;
108             if ((rc =
109                  readLine(spec, STRIP_TRAILINGSPACE | STRIP_COMMENTS)) > 0) {
110                 nextPart = PART_NONE;
111                 break;
112             }
113             if (rc) {
114                 return rc;
115             }
116         }
117     }
118     
119     stripTrailingBlanksStringBuf(sb);
120     if (!(noLang && strcmp(lang, RPMBUILD_DEFAULT_LANG))) {
121         headerAddI18NString(pkg->header, RPMTAG_DESCRIPTION,
122                         getStringBuf(sb), lang);
123     }
124     
125     freeStringBuf(sb);
126      
127     FREE(argv);
128     poptFreeContext(optCon);
129     
130     return nextPart;
131 }