fields.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  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.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 <stdio.h>
  23. #include <ctype.h>
  24. #include <string.h>
  25. #include <config.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", pri_other,
  142. 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,"first (want) word in `status' field", -1,
  156. warnto,warncount,pigp, value,wantinfos,&ep);
  157. pigp->eflag= convert_string(filename,lno,"second (error) word in `status' field", -1,
  158. warnto,warncount,pigp, ep,eflaginfos,&ep);
  159. if (pigp->eflag & eflagf_obsoletehold) {
  160. pigp->want= want_hold;
  161. pigp->eflag &= ~eflagf_obsoletehold;
  162. }
  163. pigp->status= convert_string(filename,lno,_("third (status) word in `status' field"), -1,
  164. warnto,warncount,pigp, ep,statusinfos,NULL);
  165. }
  166. void f_version(struct pkginfo *pigp, struct pkginfoperfile *pifp,
  167. enum parsedbflags flags,
  168. const char *filename, int lno, FILE *warnto, int *warncount,
  169. const char *value, const struct fieldinfo *fip) {
  170. const char *emsg;
  171. emsg= parseversion(&pifp->version,value);
  172. if (emsg) parseerr(NULL,filename,lno, warnto,warncount,pigp,0, _("error "
  173. "in Version string `%.250s': %.250s"),value,emsg);
  174. }
  175. void f_revision(struct pkginfo *pigp, struct pkginfoperfile *pifp,
  176. enum parsedbflags flags,
  177. const char *filename, int lno, FILE *warnto, int *warncount,
  178. const char *value, const struct fieldinfo *fip) {
  179. char *newversion;
  180. parseerr(NULL,filename,lno, warnto,warncount,pigp,1,
  181. _("obsolete `Revision' or `Package-Revision' field used"));
  182. if (!*value) return;
  183. if (pifp->version.revision && *pifp->version.revision) {
  184. newversion= nfmalloc(strlen(pifp->version.version)+strlen(pifp->version.revision)+2);
  185. sprintf(newversion,"%s-%s",pifp->version.version,pifp->version.revision);
  186. pifp->version.version= newversion;
  187. }
  188. pifp->version.revision= nfstrsave(value);
  189. }
  190. void f_configversion(struct pkginfo *pigp, struct pkginfoperfile *pifp,
  191. enum parsedbflags flags,
  192. const char *filename, int lno, FILE *warnto, int *warncount,
  193. const char *value, const struct fieldinfo *fip) {
  194. const char *emsg;
  195. if (flags & pdb_rejectstatus)
  196. parseerr(NULL,filename,lno, warnto,warncount,pigp,0,
  197. _("value for `config-version' field not allowed in this context"));
  198. if (flags & pdb_recordavailable) return;
  199. emsg= parseversion(&pigp->configversion,value);
  200. if (emsg) parseerr(NULL,filename,lno, warnto,warncount,pigp,0, _("error "
  201. "in Config-Version string `%.250s': %.250s"),value,emsg);
  202. }
  203. void f_conffiles(struct pkginfo *pigp, struct pkginfoperfile *pifp,
  204. enum parsedbflags flags,
  205. const char *filename, int lno, FILE *warnto, int *warncount,
  206. const char *value, const struct fieldinfo *fip) {
  207. struct conffile **lastp, *newlink;
  208. const char *endent, *endfn;
  209. int c, namelen, hashlen;
  210. char *newptr;
  211. lastp= &pifp->conffiles;
  212. while (*value) {
  213. c= *value++;
  214. if (c == '\n') continue;
  215. if (c != ' ') parseerr(NULL,filename,lno, warnto,warncount,pigp,0, _("value for"
  216. " `conffiles' has line starting with non-space `%c'"), c);
  217. for (endent= value; (c= *endent)!=0 && c != '\n'; endent++);
  218. for (endfn= endent; *endfn != ' '; endfn--);
  219. if (endfn <= value+1 || endfn >= endent-1)
  220. parseerr(NULL,filename,lno, warnto,warncount,pigp,0,
  221. _("value for `conffiles' has malformatted line `%.*s'"),
  222. (int)(endent-value > 250 ? 250 : endent-value), value);
  223. newlink= nfmalloc(sizeof(struct conffile));
  224. value= skip_slash_dotslash(value);
  225. namelen= (int)(endfn-value);
  226. if (namelen <= 0) parseerr(NULL,filename,lno, warnto,warncount,pigp,0,
  227. _("root or null directory is listed as a conffile"));
  228. newptr = nfmalloc(namelen+2);
  229. newptr[0]= '/';
  230. memcpy(newptr+1,value,namelen);
  231. newptr[namelen+1]= 0;
  232. newlink->name= newptr;
  233. hashlen= (int)(endent-endfn)-1; newptr= nfmalloc(hashlen+1);
  234. memcpy(newptr,endfn+1,hashlen); newptr[hashlen]= 0;
  235. newlink->hash= newptr;
  236. newlink->next =NULL;
  237. *lastp= newlink;
  238. lastp= &newlink->next;
  239. value= endent;
  240. }
  241. }
  242. void f_dependency(struct pkginfo *pigp, struct pkginfoperfile *pifp,
  243. enum parsedbflags flags,
  244. const char *filename, int lno, FILE *warnto, int *warncount,
  245. const char *value, const struct fieldinfo *fip) {
  246. char c1, c2;
  247. const char *p, *emsg;
  248. const char *depnamestart, *versionstart;
  249. int depnamelength, versionlength;
  250. static int depnameused= 0, versionused= 0;
  251. static char *depname= NULL, *version= NULL;
  252. struct dependency *dyp, **ldypp;
  253. struct deppossi *dop, **ldopp;
  254. if (!*value) return; /* empty fields are ignored */
  255. p= value;
  256. ldypp= &pifp->depends; while (*ldypp) ldypp= &(*ldypp)->next;
  257. for (;;) { /* loop creating new struct dependency's */
  258. dyp= nfmalloc(sizeof(struct dependency));
  259. dyp->up= NULL; /* Set this to zero for now, as we don't know what our real
  260. * struct pkginfo address (in the database) is going to be yet.
  261. */
  262. dyp->next= NULL; *ldypp= dyp; ldypp= &dyp->next;
  263. dyp->list= NULL; ldopp= &dyp->list;
  264. dyp->type= fip->integer;
  265. for (;;) { /* loop creating new struct deppossi's */
  266. depnamestart= p;
  267. /* skip over package name characters */
  268. while (*p && !isspace(*p) && *p != '(' && *p != ',' && *p != '|') {
  269. p++;
  270. }
  271. depnamelength= p - depnamestart ;
  272. if (depnamelength >= depnameused) {
  273. depnameused= depnamelength;
  274. depname= realloc(depname,depnamelength+1);
  275. }
  276. strncpy(depname, depnamestart, depnamelength);
  277. *(depname + depnamelength)= 0;
  278. if (!*depname)
  279. parseerr(NULL,filename,lno, warnto,warncount,pigp,0, _("`%s' field, missing"
  280. " package name, or garbage where package name expected"), fip->name);
  281. emsg= illegal_packagename(depname,NULL);
  282. if (emsg) parseerr(NULL,filename,lno, warnto,warncount,pigp,0, _("`%s' field,"
  283. " invalid package name `%.255s': %s"),
  284. fip->name,depname,emsg);
  285. dop= nfmalloc(sizeof(struct deppossi));
  286. dop->up= dyp;
  287. dop->ed= findpackage(depname);
  288. dop->next= NULL; *ldopp= dop; ldopp= &dop->next;
  289. dop->nextrev= NULL; /* Don't link this (which is after all only `newpig' from */
  290. dop->backrev= NULL; /* the main parsing loop in parsedb) into the depended on
  291. * packages' lists yet. This will be done later when we
  292. * install this (in parse.c). For the moment we do the
  293. * `forward' links in deppossi (`ed') only, and the backward
  294. * links from the depended on packages to dop are left undone.
  295. */
  296. dop->cyclebreak= 0;
  297. /* skip whitespace after packagename */
  298. while (isspace(*p)) p++;
  299. if (*p == '(') { /* if we have a versioned relation */
  300. p++; while (isspace(*p)) p++;
  301. c1= *p;
  302. if (c1 == '<' || c1 == '>') {
  303. c2= *++p;
  304. dop->verrel= (c1 == '<') ? dvrf_earlier : dvrf_later;
  305. if (c2 == '=') {
  306. dop->verrel |= (dvrf_orequal | dvrf_builtup);
  307. p++;
  308. } else if (c2 == c1) {
  309. dop->verrel |= (dvrf_strict | dvrf_builtup);
  310. p++;
  311. } else if (c2 == '<' || c2 == '>') {
  312. parseerr(NULL,filename,lno, warnto,warncount,pigp,0,
  313. _("`%s' field, reference to `%.255s':\n"
  314. " bad version relationship %c%c"),
  315. fip->name,depname,c1,c2);
  316. dop->verrel= dvr_none;
  317. } else {
  318. parseerr(NULL,filename,lno, warnto,warncount,pigp,1,
  319. _("`%s' field, reference to `%.255s':\n"
  320. " `%c' is obsolete, use `%c=' or `%c%c' instead"),
  321. fip->name,depname,c1,c1,c1,c1);
  322. dop->verrel |= (dvrf_orequal | dvrf_builtup);
  323. }
  324. } else if (c1 == '=') {
  325. dop->verrel= dvr_exact;
  326. p++;
  327. } else {
  328. parseerr(NULL,filename,lno, warnto,warncount,pigp,1,
  329. _("`%s' field, reference to `%.255s':\n"
  330. " implicit exact match on version number, suggest using `=' instead"),
  331. fip->name,depname);
  332. dop->verrel= dvr_exact;
  333. }
  334. if ((dop->verrel!=dvr_exact) && (fip->integer==dep_provides))
  335. parseerr(NULL,filename,lno,warnto,warncount,pigp,1,
  336. _("Only exact versions may be used for Provides"));
  337. if (!isspace(*p) && !isalnum(*p)) {
  338. parseerr(NULL,filename,lno, warnto,warncount,pigp,1,
  339. _("`%s' field, reference to `%.255s':\n"
  340. " version value starts with non-alphanumeric, suggest adding a space"),
  341. fip->name,depname);
  342. }
  343. /* skip spaces between the relation and the version */
  344. while (isspace(*p)) p++;
  345. versionstart= p;
  346. while (*p && *p != ')' && *p != '(') {
  347. if (isspace(*p)) break;
  348. p++;
  349. }
  350. while (isspace(*p)) p++;
  351. if (*p == '(') parseerr(NULL,filename,lno, warnto,warncount,pigp,0,
  352. _("`%s' field, reference to `%.255s': "
  353. "version contains `('"),fip->name,depname);
  354. else if (*p != ')') parseerr(NULL,filename,lno, warnto,warncount,pigp,0,
  355. _("`%s' field, reference to `%.255s': "
  356. "version contains ` '"),fip->name,depname);
  357. else if (*p == 0) parseerr(NULL,filename,lno, warnto,warncount,pigp,0,
  358. _("`%s' field, reference to `%.255s': "
  359. "version unterminated"),fip->name,depname);
  360. versionlength= p - versionstart;
  361. if (versionlength >= versionused) {
  362. versionused= versionlength;
  363. version= realloc(version,versionlength+1);
  364. }
  365. strncpy(version, versionstart, versionlength);
  366. *(version + versionlength)= 0;
  367. emsg= parseversion(&dop->version,version);
  368. if (emsg) parseerr(NULL,filename,lno, warnto,warncount,pigp,0,
  369. _("`%s' field, reference to `%.255s': "
  370. "error in version: %.255s"),fip->name,depname,emsg);
  371. p++; while (isspace(*p)) p++;
  372. } else {
  373. dop->verrel= dvr_none;
  374. blankversion(&dop->version);
  375. }
  376. if (!*p || *p == ',') break;
  377. if (*p != '|')
  378. parseerr(NULL,filename,lno, warnto,warncount,pigp,0, _("`%s' field, syntax"
  379. " error after reference to package `%.255s'"),
  380. fip->name, dop->ed->name);
  381. if (fip->integer == dep_conflicts ||
  382. fip->integer == dep_provides ||
  383. fip->integer == dep_replaces)
  384. parseerr(NULL,filename,lno, warnto,warncount,pigp,0,
  385. _("alternatives (`|') not allowed in %s field"),
  386. fip->name);
  387. p++; while (isspace(*p)) p++;
  388. }
  389. if (!*p) break;
  390. p++; while (isspace(*p)) p++;
  391. }
  392. }