rpm  5.4.10
parseDescription.c
Go to the documentation of this file.
1 
6 #include "system.h"
7 
8 #include <rpmio.h>
9 #include <rpmiotypes.h>
10 #include <rpmlog.h>
11 #include "rpmbuild.h"
12 #include "debug.h"
13 
14 /*@-exportheadervar@*/
15 /*@unchecked@*/
16 extern int noLang;
17 /*@=exportheadervar@*/
18 
19 /* These have to be global scope to make up for *stupid* compilers */
20 /*@unchecked@*/
21  /*@observer@*/ /*@null@*/ static const char *name = NULL;
22 /*@unchecked@*/
23  /*@observer@*/ /*@null@*/ static const char *lang = NULL;
24 
25 /*@unchecked@*/
26  static struct poptOption optionsTable[] = {
27  { NULL, 'n', POPT_ARG_STRING, &name, 0, NULL, NULL},
28  { NULL, 'l', POPT_ARG_STRING, &lang, 0, NULL, NULL},
29  { 0, 0, 0, 0, 0, NULL, NULL}
30  };
31 
33  /*@globals name, lang @*/
34  /*@modifies name, lang @*/
35 {
36  HE_t he = (HE_t) memset(alloca(sizeof(*he)), 0, sizeof(*he));
37  rpmParseState nextPart = (rpmParseState) RPMRC_FAIL; /* assume error */
38  rpmiob iob = NULL;
39  int flag = PART_SUBNAME;
40  Package pkg;
41  int rc, argc;
42  int arg;
43  const char **argv = NULL;
44  poptContext optCon = NULL;
45  spectag t = NULL;
46  int xx;
47 
48  { char * se = strchr(spec->line, '#');
49  if (se) {
50  *se = '\0';
51  while (--se >= spec->line && strchr(" \t\n\r", *se) != NULL)
52  *se = '\0';
53  }
54  }
55 
56  if ((rc = poptParseArgvString(spec->line, &argc, &argv))) {
57  rpmlog(RPMLOG_ERR, _("line %d: Error parsing %%description: %s\n"),
58  spec->lineNum, poptStrerror(rc));
59  goto exit;
60  }
61 
62  name = NULL;
64  optCon = poptGetContext(NULL, argc, argv, optionsTable, 0);
65  while ((arg = poptGetNextOpt(optCon)) > 0)
66  {;}
67  if (name != NULL)
68  flag = PART_NAME;
69 
70  if (arg < -1) {
71  rpmlog(RPMLOG_ERR, _("line %d: Bad option %s: %s\n"),
72  spec->lineNum,
73  poptBadOption(optCon, POPT_BADOPTION_NOALIAS),
74  spec->line);
75  goto exit;
76  }
77 
78  if (poptPeekArg(optCon)) {
79  if (name == NULL)
80  name = poptGetArg(optCon);
81  if (poptPeekArg(optCon)) {
82  rpmlog(RPMLOG_ERR, _("line %d: Too many names: %s\n"),
83  spec->lineNum, spec->line);
84  goto exit;
85  }
86  }
87 
88  if (lookupPackage(spec, name, flag, &pkg) != RPMRC_OK) {
89  rpmlog(RPMLOG_ERR, _("line %d: Package does not exist: %s\n"),
90  spec->lineNum, spec->line);
91  goto exit;
92  }
93 
94  /* Lose the inheirited %description (if present). */
95  if (spec->packages->header != pkg->header) {
96  he->tag = RPMTAG_DESCRIPTION;
97  xx = headerGet(pkg->header, he, 0);
98  he->p.ptr = _free(he->p.ptr);
99  if (xx && he->t == RPM_STRING_TYPE)
100  xx = headerDel(pkg->header, he, 0);
101  }
102 
103  t = stashSt(spec, pkg->header, RPMTAG_DESCRIPTION, lang);
104 
105  iob = rpmiobNew(0);
106 
107  if ((rc = readLine(spec, STRIP_TRAILINGSPACE | STRIP_COMMENTS)) > 0) {
108  nextPart = PART_NONE;
109  goto exit;
110  }
111  if (rc < 0) {
112  nextPart = (rpmParseState) RPMRC_FAIL;
113  goto exit;
114  }
115 
116  while ((nextPart = isPart(spec)) == PART_NONE) {
117  iob = rpmiobAppend(iob, spec->line, 1);
118  if (t) t->t_nlines++;
119  if ((rc = readLine(spec, STRIP_TRAILINGSPACE | STRIP_COMMENTS)) > 0) {
120  nextPart = PART_NONE;
121  break;
122  }
123  if (rc) {
124  nextPart = (rpmParseState) RPMRC_FAIL;
125  goto exit;
126  }
127  }
128 
129  iob = rpmiobRTrim(iob);
130  if (!(noLang && strcmp(lang, RPMBUILD_DEFAULT_LANG))) {
131 #if defined(SUPPORT_I18NSTRING_TYPE)
132  const char * s = rpmiobStr(iob);
133  (void) headerAddI18NString(pkg->header, RPMTAG_DESCRIPTION, s, lang);
134 #else
135  if (!strcmp(lang, RPMBUILD_DEFAULT_LANG)) {
136  he->tag = RPMTAG_DESCRIPTION;
137  he->t = RPM_STRING_TYPE;
138  he->p.str = rpmiobStr(iob);
139  he->c = 1;
140  xx = headerPut(pkg->header, he, 0);
141  }
142 #endif
143  }
144 
145 exit:
146  iob = rpmiobFree(iob);
147  argv = _free(argv);
148  optCon = poptFreeContext(optCon);
149  return nextPart;
150 }