fields.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  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. static 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, FILE *warnto,
  210. int *warncount, struct pkginfo *pigp)
  211. {
  212. /* the code in f_conffiles ensures that value[-1]==' ', which is helpful */
  213. const char *lastspc;
  214. if (from <= value+1) goto malformed;
  215. for (lastspc= from-1; *lastspc != ' '; lastspc--);
  216. if (lastspc <= value+1 || lastspc >= endent-1) goto malformed;
  217. *new_from_r= lastspc;
  218. *word_start_r= lastspc + 1;
  219. *word_len_r= (int)(from - *word_start_r);
  220. return;
  221. malformed:
  222. parseerr(NULL,filename,lno, warnto,warncount,pigp,0,
  223. _("value for `conffiles' has malformatted line `%.*s'"),
  224. (int)(endent-value > 250 ? 250 : endent-value), value);
  225. }
  226. void f_conffiles(struct pkginfo *pigp, struct pkginfoperfile *pifp,
  227. enum parsedbflags flags,
  228. const char *filename, int lno, FILE *warnto, int *warncount,
  229. const char *value, const struct fieldinfo *fip) {
  230. static const char obsolete_str[]= "obsolete";
  231. struct conffile **lastp, *newlink;
  232. const char *endent, *endfn, *hashstart;
  233. int c, namelen, hashlen, obsolete;
  234. char *newptr;
  235. lastp= &pifp->conffiles;
  236. while (*value) {
  237. c= *value++;
  238. if (c == '\n') continue;
  239. if (c != ' ') parseerr(NULL,filename,lno, warnto,warncount,pigp,0, _("value for"
  240. " `conffiles' has line starting with non-space `%c'"), c);
  241. for (endent= value; (c= *endent)!=0 && c != '\n'; endent++);
  242. conffvalue_lastword(value, endent, endent,
  243. &hashstart, &hashlen, &endfn,
  244. filename,lno,warnto,warncount,pigp);
  245. obsolete= (hashlen == sizeof(obsolete_str)-1 &&
  246. !memcmp(hashstart, obsolete_str, hashlen));
  247. if (obsolete)
  248. conffvalue_lastword(value, endfn, endent,
  249. &hashstart, &hashlen, &endfn,
  250. filename,lno,warnto,warncount,pigp);
  251. newlink= nfmalloc(sizeof(struct conffile));
  252. value= skip_slash_dotslash(value);
  253. namelen= (int)(endfn-value);
  254. if (namelen <= 0) parseerr(NULL,filename,lno, warnto,warncount,pigp,0,
  255. _("root or null directory is listed as a conffile"));
  256. newptr = nfmalloc(namelen+2);
  257. newptr[0]= '/';
  258. memcpy(newptr+1,value,namelen);
  259. newptr[namelen+1]= 0;
  260. newlink->name= newptr;
  261. newptr= nfmalloc(hashlen+1);
  262. memcpy(newptr,hashstart,hashlen); newptr[hashlen]= 0;
  263. newlink->hash= newptr;
  264. newlink->obsolete= obsolete;
  265. newlink->next =NULL;
  266. *lastp= newlink;
  267. lastp= &newlink->next;
  268. value= endent;
  269. }
  270. }
  271. void f_dependency(struct pkginfo *pigp, struct pkginfoperfile *pifp,
  272. enum parsedbflags flags,
  273. const char *filename, int lno, FILE *warnto, int *warncount,
  274. const char *value, const struct fieldinfo *fip) {
  275. char c1, c2;
  276. const char *p, *emsg;
  277. const char *depnamestart, *versionstart;
  278. int depnamelength, versionlength;
  279. static int depnameused= 0, versionused= 0;
  280. static char *depname= NULL, *version= NULL;
  281. struct dependency *dyp, **ldypp;
  282. struct deppossi *dop, **ldopp;
  283. if (!*value) return; /* empty fields are ignored */
  284. p= value;
  285. ldypp= &pifp->depends; while (*ldypp) ldypp= &(*ldypp)->next;
  286. for (;;) { /* loop creating new struct dependency's */
  287. dyp= nfmalloc(sizeof(struct dependency));
  288. dyp->up= NULL; /* Set this to zero for now, as we don't know what our real
  289. * struct pkginfo address (in the database) is going to be yet.
  290. */
  291. dyp->next= NULL; *ldypp= dyp; ldypp= &dyp->next;
  292. dyp->list= NULL; ldopp= &dyp->list;
  293. dyp->type= fip->integer;
  294. for (;;) { /* loop creating new struct deppossi's */
  295. depnamestart= p;
  296. /* skip over package name characters */
  297. while (*p && !isspace(*p) && *p != '(' && *p != ',' && *p != '|') {
  298. p++;
  299. }
  300. depnamelength= p - depnamestart ;
  301. if (depnamelength >= depnameused) {
  302. depnameused= depnamelength;
  303. depname= realloc(depname,depnamelength+1);
  304. }
  305. strncpy(depname, depnamestart, depnamelength);
  306. *(depname + depnamelength)= 0;
  307. if (!*depname)
  308. parseerr(NULL,filename,lno, warnto,warncount,pigp,0, _("`%s' field, missing"
  309. " package name, or garbage where package name expected"), fip->name);
  310. emsg= illegal_packagename(depname,NULL);
  311. if (emsg) parseerr(NULL,filename,lno, warnto,warncount,pigp,0, _("`%s' field,"
  312. " invalid package name `%.255s': %s"),
  313. fip->name,depname,emsg);
  314. dop= nfmalloc(sizeof(struct deppossi));
  315. dop->up= dyp;
  316. dop->ed= findpackage(depname);
  317. dop->next= NULL; *ldopp= dop; ldopp= &dop->next;
  318. dop->nextrev= NULL; /* Don't link this (which is after all only `newpig' from */
  319. dop->backrev= NULL; /* the main parsing loop in parsedb) into the depended on
  320. * packages' lists yet. This will be done later when we
  321. * install this (in parse.c). For the moment we do the
  322. * `forward' links in deppossi (`ed') only, and the backward
  323. * links from the depended on packages to dop are left undone.
  324. */
  325. dop->cyclebreak= 0;
  326. /* skip whitespace after packagename */
  327. while (isspace(*p)) p++;
  328. if (*p == '(') { /* if we have a versioned relation */
  329. p++; while (isspace(*p)) p++;
  330. c1= *p;
  331. if (c1 == '<' || c1 == '>') {
  332. c2= *++p;
  333. dop->verrel= (c1 == '<') ? dvrf_earlier : dvrf_later;
  334. if (c2 == '=') {
  335. dop->verrel |= (dvrf_orequal | dvrf_builtup);
  336. p++;
  337. } else if (c2 == c1) {
  338. dop->verrel |= (dvrf_strict | dvrf_builtup);
  339. p++;
  340. } else if (c2 == '<' || c2 == '>') {
  341. parseerr(NULL,filename,lno, warnto,warncount,pigp,0,
  342. _("`%s' field, reference to `%.255s':\n"
  343. " bad version relationship %c%c"),
  344. fip->name,depname,c1,c2);
  345. dop->verrel= dvr_none;
  346. } else {
  347. parseerr(NULL,filename,lno, warnto,warncount,pigp,1,
  348. _("`%s' field, reference to `%.255s':\n"
  349. " `%c' is obsolete, use `%c=' or `%c%c' instead"),
  350. fip->name,depname,c1,c1,c1,c1);
  351. dop->verrel |= (dvrf_orequal | dvrf_builtup);
  352. }
  353. } else if (c1 == '=') {
  354. dop->verrel= dvr_exact;
  355. p++;
  356. } else {
  357. parseerr(NULL,filename,lno, warnto,warncount,pigp,1,
  358. _("`%s' field, reference to `%.255s':\n"
  359. " implicit exact match on version number, suggest using `=' instead"),
  360. fip->name,depname);
  361. dop->verrel= dvr_exact;
  362. }
  363. if ((dop->verrel!=dvr_exact) && (fip->integer==dep_provides))
  364. parseerr(NULL,filename,lno,warnto,warncount,pigp,1,
  365. _("Only exact versions may be used for Provides"));
  366. if (!isspace(*p) && !isalnum(*p)) {
  367. parseerr(NULL,filename,lno, warnto,warncount,pigp,1,
  368. _("`%s' field, reference to `%.255s':\n"
  369. " version value starts with non-alphanumeric, suggest adding a space"),
  370. fip->name,depname);
  371. }
  372. /* skip spaces between the relation and the version */
  373. while (isspace(*p)) p++;
  374. versionstart= p;
  375. while (*p && *p != ')' && *p != '(') {
  376. if (isspace(*p)) break;
  377. p++;
  378. }
  379. versionlength= p - versionstart;
  380. while (isspace(*p)) p++;
  381. if (*p == '(') parseerr(NULL,filename,lno, warnto,warncount,pigp,0,
  382. _("`%s' field, reference to `%.255s': "
  383. "version contains `%c'"), fip->name,depname, ')');
  384. else if (*p != ')') parseerr(NULL,filename,lno, warnto,warncount,pigp,0,
  385. _("`%s' field, reference to `%.255s': "
  386. "version contains `%c'"), fip->name,depname, ' ');
  387. else if (*p == 0) parseerr(NULL,filename,lno, warnto,warncount,pigp,0,
  388. _("`%s' field, reference to `%.255s': "
  389. "version unterminated"),fip->name,depname);
  390. if (versionlength >= versionused) {
  391. versionused= versionlength;
  392. version= realloc(version,versionlength+1);
  393. }
  394. strncpy(version, versionstart, versionlength);
  395. *(version + versionlength)= 0;
  396. emsg= parseversion(&dop->version,version);
  397. if (emsg) parseerr(NULL,filename,lno, warnto,warncount,pigp,0,
  398. _("`%s' field, reference to `%.255s': "
  399. "error in version: %.255s"),fip->name,depname,emsg);
  400. p++; while (isspace(*p)) p++;
  401. } else {
  402. dop->verrel= dvr_none;
  403. blankversion(&dop->version);
  404. }
  405. if (!*p || *p == ',') break;
  406. if (*p != '|')
  407. parseerr(NULL,filename,lno, warnto,warncount,pigp,0, _("`%s' field, syntax"
  408. " error after reference to package `%.255s'"),
  409. fip->name, dop->ed->name);
  410. if (fip->integer == dep_conflicts ||
  411. fip->integer == dep_breaks ||
  412. fip->integer == dep_provides ||
  413. fip->integer == dep_replaces)
  414. parseerr(NULL,filename,lno, warnto,warncount,pigp,0,
  415. _("alternatives (`|') not allowed in %s field"),
  416. fip->name);
  417. p++; while (isspace(*p)) p++;
  418. }
  419. if (!*p) break;
  420. p++; while (isspace(*p)) p++;
  421. }
  422. }