fields.c 18 KB

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