fields.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544
  1. /*
  2. * libdpkg - Debian packaging suite library routines
  3. * fields.c - parsing of all the different fields, when reading in
  4. *
  5. * Copyright © 1995 Ian Jackson <ian@chiark.greenend.org.uk>
  6. * Copyright © 2001 Wichert Akkerman
  7. *
  8. * This is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as
  10. * published by the Free Software Foundation; either version 2,
  11. * or (at your option) any later version.
  12. *
  13. * This is distributed in the hope that it will be useful, but
  14. * WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. */
  21. #include <config.h>
  22. #include <compat.h>
  23. #include <ctype.h>
  24. #include <string.h>
  25. #include <stdio.h>
  26. #include <dpkg/i18n.h>
  27. #include <dpkg/dpkg.h>
  28. #include <dpkg/dpkg-db.h>
  29. #include <dpkg/path.h>
  30. #include <dpkg/parsedump.h>
  31. static int
  32. convert_string(struct parsedb_state *ps, const char *what, int otherwise,
  33. const struct pkginfo *pigp,
  34. const char *startp, const struct namevalue *ivip,
  35. const char **endpp)
  36. {
  37. const char *ep;
  38. const struct namevalue *nvip = ivip;
  39. if (!*startp)
  40. parse_error(ps, pigp, _("%s is missing"), what);
  41. while (nvip->name) {
  42. if (strncasecmp(nvip->name, startp, nvip->length))
  43. nvip++;
  44. else
  45. break;
  46. }
  47. if (!nvip->name) {
  48. if (otherwise != -1) return otherwise;
  49. parse_error(ps, pigp, _("`%.*s' is not allowed for %s"),
  50. (int)strnlen(startp, 50), startp, what);
  51. }
  52. ep = startp + nvip->length;
  53. while (isspace(*ep))
  54. ep++;
  55. if (*ep && !endpp)
  56. parse_error(ps, pigp, _("junk after %s"), what);
  57. if (endpp) *endpp= ep;
  58. return nvip->value;
  59. }
  60. void
  61. f_name(struct pkginfo *pigp, struct pkginfoperfile *pifp,
  62. struct parsedb_state *ps,
  63. const char *value, const struct fieldinfo *fip)
  64. {
  65. const char *e;
  66. if ((e= illegal_packagename(value,NULL)) != NULL)
  67. parse_error(ps, pigp, _("invalid package name (%.250s)"), e);
  68. pigp->name= findpackage(value)->name;
  69. /* We use the new name, as findpackage() may have
  70. done a tolower for us.
  71. */
  72. }
  73. void f_filecharf(struct pkginfo *pigp, struct pkginfoperfile *pifp,
  74. struct parsedb_state *ps,
  75. const char *value, const struct fieldinfo *fip) {
  76. struct filedetails *fdp, **fdpp;
  77. char *cpos, *space;
  78. int allowextend;
  79. if (!*value)
  80. parse_error(ps, pigp, _("empty file details field `%s'"), fip->name);
  81. if (!(ps->flags & pdb_recordavailable))
  82. parse_error(ps, pigp,
  83. _("file details field `%s' not allowed in status file"),
  84. fip->name);
  85. allowextend= !pigp->files;
  86. fdpp= &pigp->files;
  87. cpos= nfstrsave(value);
  88. while (*cpos) {
  89. space= cpos; while (*space && !isspace(*space)) space++;
  90. if (*space)
  91. *space++ = '\0';
  92. fdp= *fdpp;
  93. if (!fdp) {
  94. if (!allowextend)
  95. parse_error(ps, pigp,
  96. _("too many values in file details field `%s' "
  97. "(compared to others)"), fip->name);
  98. fdp= nfmalloc(sizeof(struct filedetails));
  99. fdp->next= NULL;
  100. fdp->name= fdp->msdosname= fdp->size= fdp->md5sum= NULL;
  101. *fdpp= fdp;
  102. }
  103. FILEFFIELD(fdp,fip->integer,const char*)= cpos;
  104. fdpp= &fdp->next;
  105. while (*space && isspace(*space)) space++;
  106. cpos= space;
  107. }
  108. if (*fdpp)
  109. parse_error(ps, pigp,
  110. _("too few values in file details field `%s' "
  111. "(compared to others)"), fip->name);
  112. }
  113. void f_charfield(struct pkginfo *pigp, struct pkginfoperfile *pifp,
  114. struct parsedb_state *ps,
  115. const char *value, const struct fieldinfo *fip) {
  116. if (*value) PKGPFIELD(pifp,fip->integer,char*)= nfstrsave(value);
  117. }
  118. void f_boolean(struct pkginfo *pigp, struct pkginfoperfile *pifp,
  119. struct parsedb_state *ps,
  120. const char *value, const struct fieldinfo *fip) {
  121. int boolean;
  122. if (!*value)
  123. return;
  124. boolean = convert_string(ps, _("yes/no in boolean field"),
  125. -1, pigp, value, booleaninfos, NULL);
  126. PKGPFIELD(pifp, fip->integer, int) = boolean;
  127. }
  128. void f_section(struct pkginfo *pigp, struct pkginfoperfile *pifp,
  129. struct parsedb_state *ps,
  130. const char *value, const struct fieldinfo *fip) {
  131. if (!*value) return;
  132. pigp->section= nfstrsave(value);
  133. }
  134. void f_priority(struct pkginfo *pigp, struct pkginfoperfile *pifp,
  135. struct parsedb_state *ps,
  136. const char *value, const struct fieldinfo *fip) {
  137. if (!*value) return;
  138. pigp->priority = convert_string(ps, _("word in `priority' field"),
  139. pri_other, pigp, value, priorityinfos, NULL);
  140. if (pigp->priority == pri_other) pigp->otherpriority= nfstrsave(value);
  141. }
  142. void f_status(struct pkginfo *pigp, struct pkginfoperfile *pifp,
  143. struct parsedb_state *ps,
  144. const char *value, const struct fieldinfo *fip) {
  145. const char *ep;
  146. if (ps->flags & pdb_rejectstatus)
  147. parse_error(ps, pigp,
  148. _("value for `status' field not allowed in this context"));
  149. if (ps->flags & pdb_recordavailable)
  150. return;
  151. pigp->want = convert_string(ps, _("first (want) word in `status' field"),
  152. -1, pigp, value, wantinfos, &ep);
  153. pigp->eflag = convert_string(ps, _("second (error) word in `status' field"),
  154. -1, pigp, ep, eflaginfos, &ep);
  155. pigp->status = convert_string(ps, _("third (status) word in `status' field"),
  156. -1, pigp, ep, statusinfos, NULL);
  157. }
  158. void f_version(struct pkginfo *pigp, struct pkginfoperfile *pifp,
  159. struct parsedb_state *ps,
  160. const char *value, const struct fieldinfo *fip) {
  161. const char *emsg;
  162. emsg= parseversion(&pifp->version,value);
  163. if (emsg)
  164. parse_error(ps, pigp,
  165. _("error in Version string `%.250s': %.250s"), value, emsg);
  166. }
  167. void f_revision(struct pkginfo *pigp, struct pkginfoperfile *pifp,
  168. struct parsedb_state *ps,
  169. const char *value, const struct fieldinfo *fip) {
  170. char *newversion;
  171. parse_warn(ps, pigp,
  172. _("obsolete `Revision' or `Package-Revision' field used"));
  173. if (!*value) return;
  174. if (pifp->version.revision && *pifp->version.revision) {
  175. newversion= nfmalloc(strlen(pifp->version.version)+strlen(pifp->version.revision)+2);
  176. sprintf(newversion,"%s-%s",pifp->version.version,pifp->version.revision);
  177. pifp->version.version= newversion;
  178. }
  179. pifp->version.revision= nfstrsave(value);
  180. }
  181. void f_configversion(struct pkginfo *pigp, struct pkginfoperfile *pifp,
  182. struct parsedb_state *ps,
  183. const char *value, const struct fieldinfo *fip) {
  184. const char *emsg;
  185. if (ps->flags & pdb_rejectstatus)
  186. parse_error(ps, pigp,
  187. _("value for `config-version' field not allowed in this context"));
  188. if (ps->flags & pdb_recordavailable)
  189. return;
  190. emsg= parseversion(&pigp->configversion,value);
  191. if (emsg)
  192. parse_error(ps, pigp,
  193. _("error in Config-Version string `%.250s': %.250s"),
  194. value, emsg);
  195. }
  196. static void conffvalue_lastword(const char *value, const char *from,
  197. const char *endent,
  198. const char **word_start_r, int *word_len_r,
  199. const char **new_from_r,
  200. struct parsedb_state *ps,
  201. struct pkginfo *pigp)
  202. {
  203. /* the code in f_conffiles ensures that value[-1]==' ', which is helpful */
  204. const char *lastspc;
  205. if (from <= value+1) goto malformed;
  206. for (lastspc= from-1; *lastspc != ' '; lastspc--);
  207. if (lastspc <= value+1 || lastspc >= endent-1) goto malformed;
  208. *new_from_r= lastspc;
  209. *word_start_r= lastspc + 1;
  210. *word_len_r= (int)(from - *word_start_r);
  211. return;
  212. malformed:
  213. parse_error(ps, pigp,
  214. _("value for `conffiles' has malformatted line `%.*s'"),
  215. (int)min(endent - value, 250), value);
  216. }
  217. void f_conffiles(struct pkginfo *pigp, struct pkginfoperfile *pifp,
  218. struct parsedb_state *ps,
  219. const char *value, const struct fieldinfo *fip) {
  220. static const char obsolete_str[]= "obsolete";
  221. struct conffile **lastp, *newlink;
  222. const char *endent, *endfn, *hashstart;
  223. int c, namelen, hashlen, obsolete;
  224. char *newptr;
  225. lastp= &pifp->conffiles;
  226. while (*value) {
  227. c= *value++;
  228. if (c == '\n') continue;
  229. if (c != ' ')
  230. parse_error(ps, pigp,
  231. _("value for `conffiles' has line starting with non-space `%c'"),
  232. c);
  233. for (endent = value; (c = *endent) != '\0' && c != '\n'; endent++) ;
  234. conffvalue_lastword(value, endent, endent,
  235. &hashstart, &hashlen, &endfn,
  236. ps, pigp);
  237. obsolete= (hashlen == sizeof(obsolete_str)-1 &&
  238. !memcmp(hashstart, obsolete_str, hashlen));
  239. if (obsolete)
  240. conffvalue_lastword(value, endfn, endent,
  241. &hashstart, &hashlen, &endfn,
  242. ps, pigp);
  243. newlink= nfmalloc(sizeof(struct conffile));
  244. value = path_skip_slash_dotslash(value);
  245. namelen= (int)(endfn-value);
  246. if (namelen <= 0)
  247. parse_error(ps, pigp,
  248. _("root or null directory is listed as a conffile"));
  249. newptr = nfmalloc(namelen+2);
  250. newptr[0]= '/';
  251. memcpy(newptr+1,value,namelen);
  252. newptr[namelen+1] = '\0';
  253. newlink->name= newptr;
  254. newptr= nfmalloc(hashlen+1);
  255. memcpy(newptr, hashstart, hashlen);
  256. newptr[hashlen] = '\0';
  257. newlink->hash= newptr;
  258. newlink->obsolete= obsolete;
  259. newlink->next =NULL;
  260. *lastp= newlink;
  261. lastp= &newlink->next;
  262. value= endent;
  263. }
  264. }
  265. void f_dependency(struct pkginfo *pigp, struct pkginfoperfile *pifp,
  266. struct parsedb_state *ps,
  267. const char *value, const struct fieldinfo *fip) {
  268. char c1, c2;
  269. const char *p, *emsg;
  270. const char *depnamestart, *versionstart;
  271. int depnamelength, versionlength;
  272. static struct varbuf depname, version;
  273. struct dependency *dyp, **ldypp;
  274. struct deppossi *dop, **ldopp;
  275. if (!*value) return; /* empty fields are ignored */
  276. p= value;
  277. ldypp= &pifp->depends; while (*ldypp) ldypp= &(*ldypp)->next;
  278. for (;;) { /* loop creating new struct dependency's */
  279. dyp= nfmalloc(sizeof(struct dependency));
  280. dyp->up= NULL; /* Set this to zero for now, as we don't know what our real
  281. * struct pkginfo address (in the database) is going to be yet.
  282. */
  283. dyp->next= NULL; *ldypp= dyp; ldypp= &dyp->next;
  284. dyp->list= NULL; ldopp= &dyp->list;
  285. dyp->type= fip->integer;
  286. for (;;) { /* loop creating new struct deppossi's */
  287. depnamestart= p;
  288. /* skip over package name characters */
  289. while (*p && !isspace(*p) && *p != '(' && *p != ',' && *p != '|') {
  290. p++;
  291. }
  292. depnamelength= p - depnamestart ;
  293. varbufreset(&depname);
  294. varbufaddbuf(&depname, depnamestart, depnamelength);
  295. varbufaddc(&depname, '\0');
  296. if (!depname.buf[0])
  297. parse_error(ps, pigp,
  298. _("`%s' field, missing package name, or garbage where "
  299. "package name expected"), fip->name);
  300. emsg = illegal_packagename(depname.buf, NULL);
  301. if (emsg)
  302. parse_error(ps, pigp,
  303. _("`%s' field, invalid package name `%.255s': %s"),
  304. fip->name, depname.buf, emsg);
  305. dop= nfmalloc(sizeof(struct deppossi));
  306. dop->up= dyp;
  307. dop->ed = findpackage(depname.buf);
  308. dop->next= NULL; *ldopp= dop; ldopp= &dop->next;
  309. dop->nextrev= NULL; /* Don't link this (which is after all only `newpig' from */
  310. dop->backrev= NULL; /* the main parsing loop in parsedb) into the depended on
  311. * packages' lists yet. This will be done later when we
  312. * install this (in parse.c). For the moment we do the
  313. * `forward' links in deppossi (`ed') only, and the backward
  314. * links from the depended on packages to dop are left undone.
  315. */
  316. dop->cyclebreak= 0;
  317. /* skip whitespace after packagename */
  318. while (isspace(*p)) p++;
  319. if (*p == '(') { /* if we have a versioned relation */
  320. p++; while (isspace(*p)) p++;
  321. c1= *p;
  322. if (c1 == '<' || c1 == '>') {
  323. c2= *++p;
  324. dop->verrel= (c1 == '<') ? dvrf_earlier : dvrf_later;
  325. if (c2 == '=') {
  326. dop->verrel |= (dvrf_orequal | dvrf_builtup);
  327. p++;
  328. } else if (c2 == c1) {
  329. dop->verrel |= (dvrf_strict | dvrf_builtup);
  330. p++;
  331. } else if (c2 == '<' || c2 == '>') {
  332. parse_error(ps, pigp,
  333. _("`%s' field, reference to `%.255s':\n"
  334. " bad version relationship %c%c"),
  335. fip->name, depname.buf, c1, c2);
  336. dop->verrel= dvr_none;
  337. } else {
  338. parse_warn(ps, pigp,
  339. _("`%s' field, reference to `%.255s':\n"
  340. " `%c' is obsolete, use `%c=' or `%c%c' instead"),
  341. fip->name, depname.buf, c1, c1, c1, c1);
  342. dop->verrel |= (dvrf_orequal | dvrf_builtup);
  343. }
  344. } else if (c1 == '=') {
  345. dop->verrel= dvr_exact;
  346. p++;
  347. } else {
  348. parse_warn(ps, pigp,
  349. _("`%s' field, reference to `%.255s':\n"
  350. " implicit exact match on version number, "
  351. "suggest using `=' instead"),
  352. fip->name, depname.buf);
  353. dop->verrel= dvr_exact;
  354. }
  355. if ((dop->verrel!=dvr_exact) && (fip->integer==dep_provides))
  356. parse_warn(ps, pigp,
  357. _("Only exact versions may be used for Provides"));
  358. if (!isspace(*p) && !isalnum(*p)) {
  359. parse_warn(ps, pigp,
  360. _("`%s' field, reference to `%.255s':\n"
  361. " version value starts with non-alphanumeric, "
  362. "suggest adding a space"),
  363. fip->name, depname.buf);
  364. }
  365. /* skip spaces between the relation and the version */
  366. while (isspace(*p)) p++;
  367. versionstart= p;
  368. while (*p && *p != ')' && *p != '(') {
  369. if (isspace(*p)) break;
  370. p++;
  371. }
  372. versionlength= p - versionstart;
  373. while (isspace(*p)) p++;
  374. if (*p == '(')
  375. parse_error(ps, pigp,
  376. _("`%s' field, reference to `%.255s': "
  377. "version contains `%c'"), fip->name, depname.buf, ')');
  378. else if (*p != ')')
  379. parse_error(ps, pigp,
  380. _("`%s' field, reference to `%.255s': "
  381. "version contains `%c'"), fip->name, depname.buf, ' ');
  382. else if (*p == '\0')
  383. parse_error(ps, pigp,
  384. _("`%s' field, reference to `%.255s': "
  385. "version unterminated"), fip->name, depname.buf);
  386. varbufreset(&version);
  387. varbufaddbuf(&version, versionstart, versionlength);
  388. varbufaddc(&version, '\0');
  389. emsg = parseversion(&dop->version, version.buf);
  390. if (emsg)
  391. parse_error(ps, pigp,
  392. _("`%s' field, reference to `%.255s': "
  393. "error in version: %.255s"),
  394. fip->name, depname.buf, emsg);
  395. p++; while (isspace(*p)) p++;
  396. } else {
  397. dop->verrel= dvr_none;
  398. blankversion(&dop->version);
  399. }
  400. if (!*p || *p == ',') break;
  401. if (*p != '|')
  402. parse_error(ps, pigp,
  403. _("`%s' field, syntax error after reference to package `%.255s'"),
  404. fip->name, dop->ed->name);
  405. if (fip->integer == dep_conflicts ||
  406. fip->integer == dep_breaks ||
  407. fip->integer == dep_provides ||
  408. fip->integer == dep_replaces)
  409. parse_error(ps, pigp,
  410. _("alternatives (`|') not allowed in %s field"), fip->name);
  411. p++; while (isspace(*p)) p++;
  412. }
  413. if (!*p) break;
  414. p++; while (isspace(*p)) p++;
  415. }
  416. }
  417. static const char *
  418. scan_word(const char **valp)
  419. {
  420. static struct varbuf word;
  421. const char *p, *start, *end;
  422. p = *valp;
  423. for (;;) {
  424. if (!*p) {
  425. *valp = p;
  426. return NULL;
  427. }
  428. if (cisspace(*p)) {
  429. p++;
  430. continue;
  431. }
  432. start = p;
  433. break;
  434. }
  435. for (;;) {
  436. if (*p && !cisspace(*p)) {
  437. p++;
  438. continue;
  439. }
  440. end = p;
  441. break;
  442. }
  443. varbufreset(&word);
  444. varbufaddbuf(&word, start, end - start);
  445. varbufaddc(&word, '\0');
  446. *valp = p;
  447. return word.buf;
  448. }
  449. void
  450. f_trigpend(struct pkginfo *pend, struct pkginfoperfile *pifp,
  451. struct parsedb_state *ps,
  452. const char *value, const struct fieldinfo *fip)
  453. {
  454. const char *word, *emsg;
  455. if (ps->flags & pdb_rejectstatus)
  456. parse_error(ps, pend,
  457. _("value for `triggers-pending' field not allowed in "
  458. "this context"));
  459. while ((word = scan_word(&value))) {
  460. emsg = illegal_triggername(word);
  461. if (emsg)
  462. parse_error(ps, pend,
  463. _("illegal pending trigger name `%.255s': %s"), word, emsg);
  464. if (!trig_note_pend_core(pend, nfstrsave(word)))
  465. parse_error(ps, pend,
  466. _("duplicate pending trigger `%.255s'"), word);
  467. }
  468. }
  469. void
  470. f_trigaw(struct pkginfo *aw, struct pkginfoperfile *pifp,
  471. struct parsedb_state *ps,
  472. const char *value, const struct fieldinfo *fip)
  473. {
  474. const char *word, *emsg;
  475. struct pkginfo *pend;
  476. if (ps->flags & pdb_rejectstatus)
  477. parse_error(ps, aw,
  478. _("value for `triggers-awaited' field not allowed in "
  479. "this context"));
  480. while ((word = scan_word(&value))) {
  481. emsg = illegal_packagename(word, NULL);
  482. if (emsg)
  483. parse_error(ps, aw,
  484. _("illegal package name in awaited trigger `%.255s': %s"),
  485. word, emsg);
  486. pend = findpackage(word);
  487. if (!trig_note_aw(pend, aw))
  488. parse_error(ps, aw,
  489. _("duplicate awaited trigger package `%.255s'"), word);
  490. trig_enqueue_awaited_pend(pend);
  491. }
  492. }