fields.c 20 KB

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