fields.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  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 <iwj10@cus.cam.ac.uk>
  6. *
  7. * This is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as
  9. * published by the Free Software Foundation; either version 2,
  10. * or (at your option) any later version.
  11. *
  12. * This is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public
  18. * License along with dpkg; if not, write to the Free Software
  19. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  20. */
  21. #include <stdio.h>
  22. #include <ctype.h>
  23. #include <string.h>
  24. #include <config.h>
  25. #include <dpkg.h>
  26. #include <dpkg-db.h>
  27. #include "parsedump.h"
  28. static int convert_string
  29. (const char *filename, int lno, const char *what, int otherwise,
  30. FILE *warnto, int *warncount, const struct pkginfo *pigp,
  31. const char *startp, const struct namevalue *ivip,
  32. const char **endpp)
  33. {
  34. const char *ep;
  35. int c, l = 0;
  36. struct namevalue *nvip= 0;
  37. memcpy(&nvip,&ivip,sizeof(struct namevalue *));
  38. ep= startp;
  39. if (!*ep) parseerr(0,filename,lno, warnto,warncount,pigp,0, _("%s is missing"),what);
  40. while (nvip->name) {
  41. if ((l= nvip->length) == 0) {
  42. l= strlen(nvip->name);
  43. nvip->length= (const int)l;
  44. }
  45. if (strncasecmp(nvip->name,startp,l) || nvip->name[l])
  46. nvip++;
  47. else
  48. break;
  49. }
  50. if (!nvip->name) {
  51. if (otherwise != -1) return otherwise;
  52. parseerr(0,filename,lno, warnto,warncount,pigp,0, _("`%.*s' is not allowed for %s"),
  53. l > 50 ? 50 : l, startp, what);
  54. }
  55. ep = startp + l;
  56. c = *ep;
  57. while (isspace(c)) c= *++ep;
  58. if (c && !endpp)
  59. parseerr(0,filename,lno, warnto,warncount,pigp,0, _("junk after %s"),what);
  60. if (endpp) *endpp= ep;
  61. return nvip->value;
  62. }
  63. void f_name(struct pkginfo *pigp, struct pkginfoperfile *pifp, enum parsedbflags flags,
  64. const char *filename, int lno, FILE *warnto, int *warncount,
  65. const char *value, const struct fieldinfo *fip) {
  66. const char *e;
  67. if ((e= illegal_packagename(value,0)) != 0)
  68. parseerr(0,filename,lno, warnto,warncount,pigp,0, _("invalid package name (%.250s)"),e);
  69. pigp->name= nfstrsave(value);
  70. findpackage(value); /* We discard the value from findpackage, but calling it
  71. * forces an entry in the hash table to be made if it isn't
  72. * already. This is so that we don't reorder the file
  73. * unnecessarily (doing so is bad for debugging).
  74. */
  75. }
  76. void f_filecharf(struct pkginfo *pigp, struct pkginfoperfile *pifp,
  77. enum parsedbflags flags,
  78. const char *filename, int lno, FILE *warnto, int *warncount,
  79. const char *value, const struct fieldinfo *fip) {
  80. struct filedetails *fdp, **fdpp;
  81. char *cpos, *space;
  82. int allowextend;
  83. if (!*value)
  84. parseerr(0,filename,lno, warnto,warncount,pigp,0,
  85. _("empty file details field `%s'"),fip->name);
  86. if (!(flags & pdb_recordavailable))
  87. parseerr(0,filename,lno, warnto,warncount,pigp,0,
  88. _("file details field `%s' not allowed in status file"),fip->name);
  89. allowextend= !pigp->files;
  90. fdpp= &pigp->files;
  91. cpos= nfstrsave(value);
  92. while (*cpos) {
  93. space= cpos; while (*space && !isspace(*space)) space++;
  94. if (*space) *space++= 0;
  95. fdp= *fdpp;
  96. if (!fdp) {
  97. if (!allowextend)
  98. parseerr(0,filename,lno, warnto,warncount,pigp,0, _("too many values "
  99. "in file details field `%s' (compared to others)"),fip->name);
  100. fdp= nfmalloc(sizeof(struct filedetails));
  101. fdp->next= 0;
  102. fdp->name= fdp->msdosname= fdp->size= fdp->md5sum= 0;
  103. *fdpp= fdp;
  104. }
  105. FILEFFIELD(fdp,fip->integer,const char*)= cpos;
  106. fdpp= &fdp->next;
  107. while (*space && isspace(*space)) space++;
  108. cpos= space;
  109. }
  110. if (*fdpp)
  111. parseerr(0,filename,lno, warnto,warncount,pigp,0, _("too few values "
  112. "in file details field `%s' (compared to others)"),fip->name);
  113. }
  114. void f_charfield(struct pkginfo *pigp, struct pkginfoperfile *pifp,
  115. enum parsedbflags flags,
  116. const char *filename, int lno, FILE *warnto, int *warncount,
  117. const char *value, const struct fieldinfo *fip) {
  118. if (*value) PKGPFIELD(pifp,fip->integer,char*)= nfstrsave(value);
  119. }
  120. void f_boolean(struct pkginfo *pigp, struct pkginfoperfile *pifp,
  121. enum parsedbflags flags,
  122. const char *filename, int lno, FILE *warnto, int *warncount,
  123. const char *value, const struct fieldinfo *fip) {
  124. pifp->essential=
  125. *value ? convert_string(filename,lno,_("yes/no in `essential' field"), -1,
  126. warnto,warncount,pigp,
  127. value,booleaninfos,0)
  128. : 0;
  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", pri_other,
  143. warnto,warncount,pigp,
  144. value,priorityinfos,0);
  145. if (pigp->priority == pri_other) pigp->otherpriority= nfstrsave(value);
  146. }
  147. void f_status(struct pkginfo *pigp, struct pkginfoperfile *pifp,
  148. enum parsedbflags flags,
  149. const char *filename, int lno, FILE *warnto, int *warncount,
  150. const char *value, const struct fieldinfo *fip) {
  151. const char *ep;
  152. if (flags & pdb_rejectstatus)
  153. parseerr(0,filename,lno, warnto,warncount,pigp,0,
  154. _("value for `status' field not allowed in this context"));
  155. if (flags & pdb_recordavailable) return;
  156. pigp->want= convert_string(filename,lno,"first (want) word in `status' field", -1,
  157. warnto,warncount,pigp, value,wantinfos,&ep);
  158. pigp->eflag= convert_string(filename,lno,"second (error) word in `status' field", -1,
  159. warnto,warncount,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. warnto,warncount,pigp, ep,statusinfos,0);
  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) parseerr(0,filename,lno, warnto,warncount,pigp,0, _("error "
  174. "in Version string `%.250s': %.250s"),value,emsg);
  175. }
  176. void f_revision(struct pkginfo *pigp, struct pkginfoperfile *pifp,
  177. enum parsedbflags flags,
  178. const char *filename, int lno, FILE *warnto, int *warncount,
  179. const char *value, const struct fieldinfo *fip) {
  180. char *newversion;
  181. parseerr(0,filename,lno, warnto,warncount,pigp,1,
  182. _("obsolete `Revision' or `Package-Revision' field used"));
  183. if (!*value) return;
  184. if (pifp->version.revision && *pifp->version.revision) {
  185. newversion= nfmalloc(strlen(pifp->version.version)+strlen(pifp->version.revision)+2);
  186. sprintf(newversion,"%s-%s",pifp->version.version,pifp->version.revision);
  187. pifp->version.version= newversion;
  188. }
  189. pifp->version.revision= nfstrsave(value);
  190. }
  191. void f_configversion(struct pkginfo *pigp, struct pkginfoperfile *pifp,
  192. enum parsedbflags flags,
  193. const char *filename, int lno, FILE *warnto, int *warncount,
  194. const char *value, const struct fieldinfo *fip) {
  195. const char *emsg;
  196. if (flags & pdb_rejectstatus)
  197. parseerr(0,filename,lno, warnto,warncount,pigp,0,
  198. _("value for `config-version' field not allowed in this context"));
  199. if (flags & pdb_recordavailable) return;
  200. emsg= parseversion(&pigp->configversion,value);
  201. if (emsg) parseerr(0,filename,lno, warnto,warncount,pigp,0, _("error "
  202. "in Config-Version string `%.250s': %.250s"),value,emsg);
  203. }
  204. void f_conffiles(struct pkginfo *pigp, struct pkginfoperfile *pifp,
  205. enum parsedbflags flags,
  206. const char *filename, int lno, FILE *warnto, int *warncount,
  207. const char *value, const struct fieldinfo *fip) {
  208. struct conffile **lastp, *newlink;
  209. const char *endent, *endfn;
  210. int c, namelen, hashlen;
  211. char *newptr;
  212. lastp= &pifp->conffiles;
  213. while (*value) {
  214. c= *value++;
  215. if (c == '\n') continue;
  216. if (c != ' ') parseerr(0,filename,lno, warnto,warncount,pigp,0, _("value for"
  217. " `conffiles' has line starting with non-space `%c'"), c);
  218. for (endent= value; (c= *endent)!=0 && c != '\n'; endent++);
  219. for (endfn= endent; *endfn != ' '; endfn--);
  220. if (endfn <= value+1 || endfn >= endent-1)
  221. parseerr(0,filename,lno, warnto,warncount,pigp,0,
  222. _("value for `conffiles' has malformatted line `%.*s'"),
  223. (int)(endent-value > 250 ? 250 : endent-value), value);
  224. newlink= nfmalloc(sizeof(struct conffile));
  225. value= skip_slash_dotslash(value);
  226. namelen= (int)(endfn-value);
  227. if (namelen <= 0) parseerr(0,filename,lno, warnto,warncount,pigp,0,
  228. _("root or null directory is listed as a conffile"));
  229. newptr = nfmalloc(namelen+2);
  230. newptr[0]= '/';
  231. memcpy(newptr+1,value,namelen);
  232. newptr[namelen+1]= 0;
  233. newlink->name= newptr;
  234. hashlen= (int)(endent-endfn)-1; newptr= nfmalloc(hashlen+1);
  235. memcpy(newptr,endfn+1,hashlen); newptr[hashlen]= 0;
  236. newlink->hash= newptr;
  237. newlink->next =0;
  238. *lastp= newlink;
  239. lastp= &newlink->next;
  240. value= endent;
  241. }
  242. }
  243. void f_dependency(struct pkginfo *pigp, struct pkginfoperfile *pifp,
  244. enum parsedbflags flags,
  245. const char *filename, int lno, FILE *warnto, int *warncount,
  246. const char *value, const struct fieldinfo *fip) {
  247. char c1, c2;
  248. const char *p, *emsg;
  249. const char *depnamestart, *versionstart;
  250. int depnamelength, versionlength;
  251. static int depnameused= 0, versionused= 0;
  252. static char *depname= NULL, *version= NULL;
  253. struct dependency *dyp, **ldypp;
  254. struct deppossi *dop, **ldopp;
  255. if (!*value) return; /* empty fields are ignored */
  256. p= value;
  257. ldypp= &pifp->depends; while (*ldypp) ldypp= &(*ldypp)->next;
  258. for (;;) { /* loop creating new struct dependency's */
  259. dyp= nfmalloc(sizeof(struct dependency));
  260. dyp->up= 0; /* Set this to zero for now, as we don't know what our real
  261. * struct pkginfo address (in the database) is going to be yet.
  262. */
  263. dyp->next= 0; *ldypp= dyp; ldypp= &dyp->next;
  264. dyp->list= 0; ldopp= &dyp->list;
  265. dyp->type= fip->integer;
  266. for (;;) { /* loop creating new struct deppossi's */
  267. depnamestart= p;
  268. /* skip over package name characters */
  269. while (*p && !isspace(*p) && *p != '(' && *p != ',' && *p != '|') {
  270. p++;
  271. }
  272. depnamelength= p - depnamestart ;
  273. if (depnamelength >= depnameused) {
  274. depnameused= depnamelength;
  275. depname= realloc(depname,depnamelength+1);
  276. }
  277. strncpy(depname, depnamestart, depnamelength);
  278. *(depname + depnamelength)= 0;
  279. if (!*depname)
  280. parseerr(0,filename,lno, warnto,warncount,pigp,0, _("`%s' field, missing"
  281. " package name, or garbage where package name expected"), fip->name);
  282. emsg= illegal_packagename(depname,0);
  283. if (emsg) parseerr(0,filename,lno, warnto,warncount,pigp,0, _("`%s' field,"
  284. " invalid package name `%.255s': %s"),
  285. fip->name,depname,emsg);
  286. dop= nfmalloc(sizeof(struct deppossi));
  287. dop->up= dyp;
  288. dop->ed= findpackage(depname);
  289. dop->next= 0; *ldopp= dop; ldopp= &dop->next;
  290. dop->nextrev= 0; /* Don't link this (which is after all only `newpig' from */
  291. dop->backrev= 0; /* the main parsing loop in parsedb) into the depended on
  292. * packages' lists yet. This will be done later when we
  293. * install this (in parse.c). For the moment we do the
  294. * `forward' links in deppossi (`ed') only, and the backward
  295. * links from the depended on packages to dop are left undone.
  296. */
  297. dop->cyclebreak= 0;
  298. /* skip whitespace after packagename */
  299. while (isspace(*p)) p++;
  300. if (*p == '(') { /* if we have a versioned relation */
  301. p++; while (isspace(*p)) p++;
  302. c1= *p;
  303. if (c1 == '<' || c1 == '>') {
  304. c2= *++p;
  305. dop->verrel= (c1 == '<') ? dvrf_earlier : dvrf_later;
  306. if (c2 == '=') {
  307. dop->verrel |= (dvrf_orequal | dvrf_builtup);
  308. p++;
  309. } else if (c2 == c1) {
  310. dop->verrel |= (dvrf_strict | dvrf_builtup);
  311. p++;
  312. } else if (c2 == '<' || c2 == '>') {
  313. parseerr(0,filename,lno, warnto,warncount,pigp,0,
  314. _("`%s' field, reference to `%.255s':\n"
  315. " bad version relationship %c%c"),
  316. fip->name,depname,c1,c2);
  317. dop->verrel= dvr_none;
  318. } else {
  319. parseerr(0,filename,lno, warnto,warncount,pigp,1,
  320. _("`%s' field, reference to `%.255s':\n"
  321. " `%c' is obsolete, use `%c=' or `%c%c' instead"),
  322. fip->name,depname,c1,c1,c1,c1);
  323. dop->verrel |= (dvrf_orequal | dvrf_builtup);
  324. }
  325. } else if (c1 == '=') {
  326. dop->verrel= dvr_exact;
  327. p++;
  328. } else {
  329. parseerr(0,filename,lno, warnto,warncount,pigp,1,
  330. _("`%s' field, reference to `%.255s':\n"
  331. " implicit exact match on version number, suggest using `=' instead"),
  332. fip->name,depname);
  333. dop->verrel= dvr_exact;
  334. }
  335. if ((dop->verrel!=dvr_exact) && (fip->integer==dep_provides))
  336. parseerr(0,filename,lno,warnto,warncount,pigp,1,
  337. _("Only exact versions may be used for Provides"));
  338. if (!isspace(*p) && !isalnum(*p)) {
  339. parseerr(0,filename,lno, warnto,warncount,pigp,1,
  340. _("`%s' field, reference to `%.255s':\n"
  341. " version value starts with non-alphanumeric, suggest adding a space"),
  342. fip->name,depname);
  343. }
  344. /* skip spaces between the relation and the version */
  345. while (isspace(*p)) p++;
  346. versionstart= p;
  347. while (*p && *p != ')' && *p != '(') {
  348. if (isspace(*p)) break;
  349. p++;
  350. }
  351. while (isspace(*p)) p++;
  352. if (*p == '(') parseerr(0,filename,lno, warnto,warncount,pigp,0,
  353. _("`%s' field, reference to `%.255s': "
  354. "version contains `('"),fip->name,depname);
  355. else if (*p != ')') parseerr(0,filename,lno, warnto,warncount,pigp,0,
  356. _("`%s' field, reference to `%.255s': "
  357. "version contains ` '"),fip->name,depname);
  358. else if (*p == 0) parseerr(0,filename,lno, warnto,warncount,pigp,0,
  359. _("`%s' field, reference to `%.255s': "
  360. "version unterminated"),fip->name,depname);
  361. versionlength= p - versionstart;
  362. if (versionlength >= versionused) {
  363. versionused= versionlength;
  364. version= realloc(version,versionlength+1);
  365. }
  366. strncpy(version, versionstart, versionlength);
  367. *(version + versionlength)= 0;
  368. emsg= parseversion(&dop->version,version);
  369. if (emsg) parseerr(0,filename,lno, warnto,warncount,pigp,0,
  370. _("`%s' field, reference to `%.255s': "
  371. "error in version: %.255s"),fip->name,depname,emsg);
  372. p++; while (isspace(*p)) p++;
  373. } else {
  374. dop->verrel= dvr_none;
  375. blankversion(&dop->version);
  376. }
  377. if (!*p || *p == ',') break;
  378. if (*p != '|')
  379. parseerr(0,filename,lno, warnto,warncount,pigp,0, _("`%s' field, syntax"
  380. " error after reference to package `%.255s'"),
  381. fip->name, dop->ed->name);
  382. if (fip->integer == dep_conflicts ||
  383. fip->integer == dep_provides ||
  384. fip->integer == dep_replaces)
  385. parseerr(0,filename,lno, warnto,warncount,pigp,0,
  386. _("alternatives (`|') not allowed in %s field"),
  387. fip->name);
  388. p++; while (isspace(*p)) p++;
  389. }
  390. if (!*p) break;
  391. p++; while (isspace(*p)) p++;
  392. }
  393. }