fields.c 18 KB

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