fields.c 18 KB

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