fields.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  1. /*
  2. * libdpkg - Debian packaging suite library routines
  3. * fields.c - parsing of all the different fields, when reading in
  4. *
  5. * Copyright (C) 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 <stdio.h>
  24. #include <ctype.h>
  25. #include <string.h>
  26. #include <dpkg.h>
  27. #include <dpkg-db.h>
  28. #include "parsedump.h"
  29. static int convert_string
  30. (const char *filename, int lno, const char *what, int otherwise,
  31. FILE *warnto, int *warncount, const struct pkginfo *pigp,
  32. const char *startp, const struct namevalue *ivip,
  33. const char **endpp)
  34. {
  35. const char *ep;
  36. int c, l = 0;
  37. struct namevalue *nvip= NULL;
  38. memcpy(&nvip,&ivip,sizeof(struct namevalue *));
  39. ep= startp;
  40. if (!*ep) parseerr(NULL,filename,lno, warnto,warncount,pigp,0, _("%s is missing"),what);
  41. while (nvip->name) {
  42. if ((l= nvip->length) == 0) {
  43. l= strlen(nvip->name);
  44. nvip->length= (const int)l;
  45. }
  46. if (strncasecmp(nvip->name,startp,l) || nvip->name[l])
  47. nvip++;
  48. else
  49. break;
  50. }
  51. if (!nvip->name) {
  52. if (otherwise != -1) return otherwise;
  53. parseerr(NULL,filename,lno, warnto,warncount,pigp,0, _("`%.*s' is not allowed for %s"),
  54. l > 50 ? 50 : l, startp, what);
  55. }
  56. ep = startp + l;
  57. c = *ep;
  58. while (isspace(c)) c= *++ep;
  59. if (c && !endpp)
  60. parseerr(NULL,filename,lno, warnto,warncount,pigp,0, _("junk after %s"),what);
  61. if (endpp) *endpp= ep;
  62. return nvip->value;
  63. }
  64. void f_name(struct pkginfo *pigp, struct pkginfoperfile *pifp, enum parsedbflags flags,
  65. const char *filename, int lno, FILE *warnto, int *warncount,
  66. const char *value, const struct fieldinfo *fip) {
  67. const char *e;
  68. if ((e= illegal_packagename(value,NULL)) != NULL)
  69. parseerr(NULL,filename,lno, warnto,warncount,pigp,0, _("invalid package name (%.250s)"),e);
  70. pigp->name= findpackage(value)->name;
  71. /* We use the new name, as findpackage() may have
  72. done a tolower for us.
  73. */
  74. }
  75. void f_filecharf(struct pkginfo *pigp, struct pkginfoperfile *pifp,
  76. enum parsedbflags flags,
  77. const char *filename, int lno, FILE *warnto, int *warncount,
  78. const char *value, const struct fieldinfo *fip) {
  79. struct filedetails *fdp, **fdpp;
  80. char *cpos, *space;
  81. int allowextend;
  82. if (!*value)
  83. parseerr(NULL,filename,lno, warnto,warncount,pigp,0,
  84. _("empty file details field `%s'"),fip->name);
  85. if (!(flags & pdb_recordavailable))
  86. parseerr(NULL,filename,lno, warnto,warncount,pigp,0,
  87. _("file details field `%s' not allowed in status file"),fip->name);
  88. allowextend= !pigp->files;
  89. fdpp= &pigp->files;
  90. cpos= nfstrsave(value);
  91. while (*cpos) {
  92. space= cpos; while (*space && !isspace(*space)) space++;
  93. if (*space) *space++= 0;
  94. fdp= *fdpp;
  95. if (!fdp) {
  96. if (!allowextend)
  97. parseerr(NULL,filename,lno, warnto,warncount,pigp,0, _("too many values "
  98. "in file details field `%s' (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. parseerr(NULL,filename,lno, warnto,warncount,pigp,0, _("too few values "
  111. "in file details field `%s' (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. pifp->essential=
  124. *value ? convert_string(filename,lno,_("yes/no in boolean field"), -1,
  125. warnto,warncount,pigp,
  126. value,booleaninfos,NULL)
  127. : 0;
  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, warnto, warncount, pigp,
  143. 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. parseerr(NULL,filename,lno, warnto,warncount,pigp,0,
  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. warnto,warncount,pigp, value,wantinfos,&ep);
  158. pigp->eflag = convert_string(filename, lno,
  159. _("second (error) word in `status' field"), -1,
  160. warnto,warncount,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. warnto,warncount,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) parseerr(NULL,filename,lno, warnto,warncount,pigp,0, _("error "
  175. "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. parseerr(NULL,filename,lno, warnto,warncount,pigp,1,
  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. parseerr(NULL,filename,lno, warnto,warncount,pigp,0,
  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) parseerr(NULL,filename,lno, warnto,warncount,pigp,0, _("error "
  203. "in Config-Version string `%.250s': %.250s"),value,emsg);
  204. }
  205. void conffvalue_lastword(const char *value, const char *from,
  206. const char *endent,
  207. const char **word_start_r, int *word_len_r,
  208. const char **new_from_r,
  209. const char *filename, int lno,
  210. FILE *warnto, int *warncount, struct pkginfo *pigp) {
  211. /* the code in f_conffiles ensures that value[-1]==' ', which is helpful */
  212. const char *lastspc;
  213. if (from <= value+1) goto malformed;
  214. for (lastspc= from-1; *lastspc != ' '; lastspc--);
  215. if (lastspc <= value+1 || lastspc >= endent-1) goto malformed;
  216. *new_from_r= lastspc;
  217. *word_start_r= lastspc + 1;
  218. *word_len_r= (int)(from - *word_start_r);
  219. return;
  220. malformed:
  221. parseerr(NULL,filename,lno, warnto,warncount,pigp,0,
  222. _("value for `conffiles' has malformatted line `%.*s'"),
  223. (int)(endent-value > 250 ? 250 : endent-value), value);
  224. }
  225. void f_conffiles(struct pkginfo *pigp, struct pkginfoperfile *pifp,
  226. enum parsedbflags flags,
  227. const char *filename, int lno, FILE *warnto, int *warncount,
  228. const char *value, const struct fieldinfo *fip) {
  229. static const char obsolete_str[]= "obsolete";
  230. struct conffile **lastp, *newlink;
  231. const char *endent, *endfn, *hashstart;
  232. int c, namelen, hashlen, obsolete;
  233. char *newptr;
  234. lastp= &pifp->conffiles;
  235. while (*value) {
  236. c= *value++;
  237. if (c == '\n') continue;
  238. if (c != ' ') parseerr(NULL,filename,lno, warnto,warncount,pigp,0, _("value for"
  239. " `conffiles' has line starting with non-space `%c'"), c);
  240. for (endent= value; (c= *endent)!=0 && c != '\n'; endent++);
  241. conffvalue_lastword(value, endent, endent,
  242. &hashstart, &hashlen, &endfn,
  243. filename,lno,warnto,warncount,pigp);
  244. obsolete= (hashlen == sizeof(obsolete_str)-1 &&
  245. !memcmp(hashstart, obsolete_str, hashlen));
  246. if (obsolete)
  247. conffvalue_lastword(value, endfn, endent,
  248. &hashstart, &hashlen, &endfn,
  249. filename,lno,warnto,warncount,pigp);
  250. newlink= nfmalloc(sizeof(struct conffile));
  251. value= skip_slash_dotslash(value);
  252. namelen= (int)(endfn-value);
  253. if (namelen <= 0) parseerr(NULL,filename,lno, warnto,warncount,pigp,0,
  254. _("root or null directory is listed as a conffile"));
  255. newptr = nfmalloc(namelen+2);
  256. newptr[0]= '/';
  257. memcpy(newptr+1,value,namelen);
  258. newptr[namelen+1]= 0;
  259. newlink->name= newptr;
  260. newptr= nfmalloc(hashlen+1);
  261. memcpy(newptr,hashstart,hashlen); 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. enum parsedbflags flags,
  272. const char *filename, int lno, FILE *warnto, int *warncount,
  273. const char *value, const struct fieldinfo *fip) {
  274. char c1, c2;
  275. const char *p, *emsg;
  276. const char *depnamestart, *versionstart;
  277. int depnamelength, versionlength;
  278. static int depnameused= 0, versionused= 0;
  279. static char *depname= NULL, *version= NULL;
  280. struct dependency *dyp, **ldypp;
  281. struct deppossi *dop, **ldopp;
  282. if (!*value) return; /* empty fields are ignored */
  283. p= value;
  284. ldypp= &pifp->depends; while (*ldypp) ldypp= &(*ldypp)->next;
  285. for (;;) { /* loop creating new struct dependency's */
  286. dyp= nfmalloc(sizeof(struct dependency));
  287. dyp->up= NULL; /* Set this to zero for now, as we don't know what our real
  288. * struct pkginfo address (in the database) is going to be yet.
  289. */
  290. dyp->next= NULL; *ldypp= dyp; ldypp= &dyp->next;
  291. dyp->list= NULL; ldopp= &dyp->list;
  292. dyp->type= fip->integer;
  293. for (;;) { /* loop creating new struct deppossi's */
  294. depnamestart= p;
  295. /* skip over package name characters */
  296. while (*p && !isspace(*p) && *p != '(' && *p != ',' && *p != '|') {
  297. p++;
  298. }
  299. depnamelength= p - depnamestart ;
  300. if (depnamelength >= depnameused) {
  301. depnameused= depnamelength;
  302. depname= realloc(depname,depnamelength+1);
  303. }
  304. strncpy(depname, depnamestart, depnamelength);
  305. *(depname + depnamelength)= 0;
  306. if (!*depname)
  307. parseerr(NULL,filename,lno, warnto,warncount,pigp,0, _("`%s' field, missing"
  308. " package name, or garbage where package name expected"), fip->name);
  309. emsg= illegal_packagename(depname,NULL);
  310. if (emsg) parseerr(NULL,filename,lno, warnto,warncount,pigp,0, _("`%s' field,"
  311. " invalid package name `%.255s': %s"),
  312. fip->name,depname,emsg);
  313. dop= nfmalloc(sizeof(struct deppossi));
  314. dop->up= dyp;
  315. dop->ed= findpackage(depname);
  316. dop->next= NULL; *ldopp= dop; ldopp= &dop->next;
  317. dop->nextrev= NULL; /* Don't link this (which is after all only `newpig' from */
  318. dop->backrev= NULL; /* the main parsing loop in parsedb) into the depended on
  319. * packages' lists yet. This will be done later when we
  320. * install this (in parse.c). For the moment we do the
  321. * `forward' links in deppossi (`ed') only, and the backward
  322. * links from the depended on packages to dop are left undone.
  323. */
  324. dop->cyclebreak= 0;
  325. /* skip whitespace after packagename */
  326. while (isspace(*p)) p++;
  327. if (*p == '(') { /* if we have a versioned relation */
  328. p++; while (isspace(*p)) p++;
  329. c1= *p;
  330. if (c1 == '<' || c1 == '>') {
  331. c2= *++p;
  332. dop->verrel= (c1 == '<') ? dvrf_earlier : dvrf_later;
  333. if (c2 == '=') {
  334. dop->verrel |= (dvrf_orequal | dvrf_builtup);
  335. p++;
  336. } else if (c2 == c1) {
  337. dop->verrel |= (dvrf_strict | dvrf_builtup);
  338. p++;
  339. } else if (c2 == '<' || c2 == '>') {
  340. parseerr(NULL,filename,lno, warnto,warncount,pigp,0,
  341. _("`%s' field, reference to `%.255s':\n"
  342. " bad version relationship %c%c"),
  343. fip->name,depname,c1,c2);
  344. dop->verrel= dvr_none;
  345. } else {
  346. parseerr(NULL,filename,lno, warnto,warncount,pigp,1,
  347. _("`%s' field, reference to `%.255s':\n"
  348. " `%c' is obsolete, use `%c=' or `%c%c' instead"),
  349. fip->name,depname,c1,c1,c1,c1);
  350. dop->verrel |= (dvrf_orequal | dvrf_builtup);
  351. }
  352. } else if (c1 == '=') {
  353. dop->verrel= dvr_exact;
  354. p++;
  355. } else {
  356. parseerr(NULL,filename,lno, warnto,warncount,pigp,1,
  357. _("`%s' field, reference to `%.255s':\n"
  358. " implicit exact match on version number, suggest using `=' instead"),
  359. fip->name,depname);
  360. dop->verrel= dvr_exact;
  361. }
  362. if ((dop->verrel!=dvr_exact) && (fip->integer==dep_provides))
  363. parseerr(NULL,filename,lno,warnto,warncount,pigp,1,
  364. _("Only exact versions may be used for Provides"));
  365. if (!isspace(*p) && !isalnum(*p)) {
  366. parseerr(NULL,filename,lno, warnto,warncount,pigp,1,
  367. _("`%s' field, reference to `%.255s':\n"
  368. " version value starts with non-alphanumeric, suggest adding a space"),
  369. fip->name,depname);
  370. }
  371. /* skip spaces between the relation and the version */
  372. while (isspace(*p)) p++;
  373. versionstart= p;
  374. while (*p && *p != ')' && *p != '(') {
  375. if (isspace(*p)) break;
  376. p++;
  377. }
  378. versionlength= p - versionstart;
  379. while (isspace(*p)) p++;
  380. if (*p == '(') parseerr(NULL,filename,lno, warnto,warncount,pigp,0,
  381. _("`%s' field, reference to `%.255s': "
  382. "version contains `%c'"), fip->name,depname, ')');
  383. else if (*p != ')') parseerr(NULL,filename,lno, warnto,warncount,pigp,0,
  384. _("`%s' field, reference to `%.255s': "
  385. "version contains `%c'"), fip->name,depname, ' ');
  386. else if (*p == 0) parseerr(NULL,filename,lno, warnto,warncount,pigp,0,
  387. _("`%s' field, reference to `%.255s': "
  388. "version unterminated"),fip->name,depname);
  389. if (versionlength >= versionused) {
  390. versionused= versionlength;
  391. version= realloc(version,versionlength+1);
  392. }
  393. strncpy(version, versionstart, versionlength);
  394. *(version + versionlength)= 0;
  395. emsg= parseversion(&dop->version,version);
  396. if (emsg) parseerr(NULL,filename,lno, warnto,warncount,pigp,0,
  397. _("`%s' field, reference to `%.255s': "
  398. "error in version: %.255s"),fip->name,depname,emsg);
  399. p++; while (isspace(*p)) p++;
  400. } else {
  401. dop->verrel= dvr_none;
  402. blankversion(&dop->version);
  403. }
  404. if (!*p || *p == ',') break;
  405. if (*p != '|')
  406. parseerr(NULL,filename,lno, warnto,warncount,pigp,0, _("`%s' field, syntax"
  407. " error after reference to package `%.255s'"),
  408. fip->name, dop->ed->name);
  409. if (fip->integer == dep_conflicts ||
  410. fip->integer == dep_breaks ||
  411. fip->integer == dep_provides ||
  412. fip->integer == dep_replaces)
  413. parseerr(NULL,filename,lno, warnto,warncount,pigp,0,
  414. _("alternatives (`|') not allowed in %s field"),
  415. fip->name);
  416. p++; while (isspace(*p)) p++;
  417. }
  418. if (!*p) break;
  419. p++; while (isspace(*p)) p++;
  420. }
  421. }