fields.c 20 KB

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