fields.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  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(const char *filename, int lno, const char *what, int otherwise,
  29. FILE *warnto, int *warncount, const struct pkginfo *pigp,
  30. const char *startp, const struct namevalue *nvip,
  31. const char **endpp) {
  32. const char *ep;
  33. int c, l;
  34. ep= startp;
  35. if (!*ep) parseerr(0,filename,lno, warnto,warncount,pigp,0, "%s is missing",what);
  36. while ((c= *ep) && !isspace(c)) ep++;
  37. l= (int)(ep-startp);
  38. while (nvip->name && (strncasecmp(nvip->name,startp,l) || nvip->name[l])) nvip++;
  39. if (!nvip->name) {
  40. if (otherwise != -1) return otherwise;
  41. parseerr(0,filename,lno, warnto,warncount,pigp,0, "`%.*s' is not allowed for %s",
  42. l > 50 ? 50 : l, startp, what);
  43. }
  44. while (isspace(c)) c= *++ep;
  45. if (c && !endpp)
  46. parseerr(0,filename,lno, warnto,warncount,pigp,0, "junk after %s",what);
  47. if (endpp) *endpp= ep;
  48. return nvip->value;
  49. }
  50. void f_name(struct pkginfo *pigp, struct pkginfoperfile *pifp, enum parsedbflags flags,
  51. const char *filename, int lno, FILE *warnto, int *warncount,
  52. const char *value, const struct fieldinfo *fip) {
  53. const char *e;
  54. if ((e= illegal_packagename(value,0)) != 0)
  55. parseerr(0,filename,lno, warnto,warncount,pigp,0, "invalid package name (%.250s)",e);
  56. pigp->name= nfstrsave(value);
  57. findpackage(value); /* We discard the value from findpackage, but calling it
  58. * forces an entry in the hash table to be made if it isn't
  59. * already. This is so that we don't reorder the file
  60. * unnecessarily (doing so is bad for debugging).
  61. */
  62. }
  63. void f_filecharf(struct pkginfo *pigp, struct pkginfoperfile *pifp,
  64. enum parsedbflags flags,
  65. const char *filename, int lno, FILE *warnto, int *warncount,
  66. const char *value, const struct fieldinfo *fip) {
  67. struct filedetails *fdp, **fdpp;
  68. char *cpos, *space;
  69. int allowextend;
  70. if (!*value)
  71. parseerr(0,filename,lno, warnto,warncount,pigp,0,
  72. "empty file details field `%s'",fip->name);
  73. if (!(flags & pdb_recordavailable))
  74. parseerr(0,filename,lno, warnto,warncount,pigp,0,
  75. "file details field `%s' not allowed in status file",fip->name);
  76. allowextend= !pigp->files;
  77. fdpp= &pigp->files;
  78. cpos= nfstrsave(value);
  79. while (*cpos) {
  80. space= cpos; while (*space && !isspace(*space)) space++;
  81. if (*space) *space++= 0;
  82. fdp= *fdpp;
  83. if (!fdp) {
  84. if (!allowextend)
  85. parseerr(0,filename,lno, warnto,warncount,pigp,0, "too many values "
  86. "in file details field `%s' (compared to others)",fip->name);
  87. fdp= nfmalloc(sizeof(struct filedetails));
  88. fdp->next= 0;
  89. fdp->name= fdp->msdosname= fdp->size= fdp->md5sum= 0;
  90. *fdpp= fdp;
  91. }
  92. FILEFFIELD(fdp,fip->integer,char*)= cpos;
  93. fdpp= &fdp->next;
  94. while (*space && isspace(*space)) space++;
  95. cpos= space;
  96. }
  97. if (*fdpp)
  98. parseerr(0,filename,lno, warnto,warncount,pigp,0, "too few values "
  99. "in file details field `%s' (compared to others)",fip->name);
  100. }
  101. void f_charfield(struct pkginfo *pigp, struct pkginfoperfile *pifp,
  102. enum parsedbflags flags,
  103. const char *filename, int lno, FILE *warnto, int *warncount,
  104. const char *value, const struct fieldinfo *fip) {
  105. if (*value) PKGPFIELD(pifp,fip->integer,char*)= nfstrsave(value);
  106. }
  107. void f_boolean(struct pkginfo *pigp, struct pkginfoperfile *pifp,
  108. enum parsedbflags flags,
  109. const char *filename, int lno, FILE *warnto, int *warncount,
  110. const char *value, const struct fieldinfo *fip) {
  111. pifp->essential=
  112. *value ? convert_string(filename,lno,"yes/no in `essential' field", -1,
  113. warnto,warncount,pigp,
  114. value,booleaninfos,0)
  115. : 0;
  116. }
  117. void f_section(struct pkginfo *pigp, struct pkginfoperfile *pifp,
  118. enum parsedbflags flags,
  119. const char *filename, int lno, FILE *warnto, int *warncount,
  120. const char *value, const struct fieldinfo *fip) {
  121. if (!*value) return;
  122. pigp->section= nfstrsave(value);
  123. }
  124. void f_priority(struct pkginfo *pigp, struct pkginfoperfile *pifp,
  125. enum parsedbflags flags,
  126. const char *filename, int lno, FILE *warnto, int *warncount,
  127. const char *value, const struct fieldinfo *fip) {
  128. if (!*value) return;
  129. pigp->priority= convert_string(filename,lno,"word in `priority' field", pri_other,
  130. warnto,warncount,pigp,
  131. value,priorityinfos,0);
  132. if (pigp->priority == pri_other) pigp->otherpriority= nfstrsave(value);
  133. }
  134. void f_status(struct pkginfo *pigp, struct pkginfoperfile *pifp,
  135. enum parsedbflags flags,
  136. const char *filename, int lno, FILE *warnto, int *warncount,
  137. const char *value, const struct fieldinfo *fip) {
  138. const char *ep;
  139. if (flags & pdb_rejectstatus)
  140. parseerr(0,filename,lno, warnto,warncount,pigp,0,
  141. "value for `status' field not allowed in this context");
  142. if (flags & pdb_recordavailable) return;
  143. pigp->want= convert_string(filename,lno,"first (want) word in `status' field", -1,
  144. warnto,warncount,pigp, value,wantinfos,&ep);
  145. pigp->eflag= convert_string(filename,lno,"second (error) word in `status' field", -1,
  146. warnto,warncount,pigp, ep,eflaginfos,&ep);
  147. if (pigp->eflag & eflagf_obsoletehold) {
  148. pigp->want= want_hold;
  149. pigp->eflag &= ~eflagf_obsoletehold;
  150. }
  151. pigp->status= convert_string(filename,lno,"third (status) word in `status' field", -1,
  152. warnto,warncount,pigp, ep,statusinfos,0);
  153. }
  154. void f_version(struct pkginfo *pigp, struct pkginfoperfile *pifp,
  155. enum parsedbflags flags,
  156. const char *filename, int lno, FILE *warnto, int *warncount,
  157. const char *value, const struct fieldinfo *fip) {
  158. const char *emsg;
  159. emsg= parseversion(&pifp->version,value);
  160. if (emsg) parseerr(0,filename,lno, warnto,warncount,pigp,0, "error "
  161. "in Version string `%.250s': %.250s",value,emsg);
  162. }
  163. void f_revision(struct pkginfo *pigp, struct pkginfoperfile *pifp,
  164. enum parsedbflags flags,
  165. const char *filename, int lno, FILE *warnto, int *warncount,
  166. const char *value, const struct fieldinfo *fip) {
  167. char *newversion;
  168. parseerr(0,filename,lno, warnto,warncount,pigp,1,
  169. "obsolete `Revision' or `Package-Revision' field used");
  170. if (!*value) return;
  171. if (pifp->version.revision && *pifp->version.revision) {
  172. newversion= nfmalloc(strlen(pifp->version.version)+strlen(pifp->version.revision)+2);
  173. sprintf(newversion,"%s-%s",pifp->version.version,pifp->version.revision);
  174. }
  175. pifp->version.revision= nfstrsave(value);
  176. }
  177. void f_configversion(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. const char *emsg;
  182. if (flags & pdb_rejectstatus)
  183. parseerr(0,filename,lno, warnto,warncount,pigp,0,
  184. "value for `config-version' field not allowed in this context");
  185. if (flags & pdb_recordavailable) return;
  186. emsg= parseversion(&pigp->configversion,value);
  187. if (emsg) parseerr(0,filename,lno, warnto,warncount,pigp,0, "error "
  188. "in Config-Version string `%.250s': %.250s",value,emsg);
  189. }
  190. void f_conffiles(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. struct conffile **lastp, *newlink;
  195. const char *endent, *endfn;
  196. int c, namelen, hashlen;
  197. lastp= &pifp->conffiles;
  198. while (*value) {
  199. c= *value++;
  200. if (c == '\n') continue;
  201. if (c != ' ') parseerr(0,filename,lno, warnto,warncount,pigp,0, "value for"
  202. " `conffiles' has line starting with non-space `%c'", c);
  203. for (endent= value; (c= *endent)!=0 && c != '\n'; endent++);
  204. for (endfn= endent; *endfn != ' '; endfn--);
  205. if (endfn <= value+1 || endfn >= endent-1)
  206. parseerr(0,filename,lno, warnto,warncount,pigp,0,
  207. "value for `conffiles' has malformatted line `%.*s'",
  208. (int)(endent-value > 250 ? 250 : endent-value), value);
  209. newlink= nfmalloc(sizeof(struct conffile));
  210. value= skip_slash_dotslash(value);
  211. namelen= (int)(endfn-value);
  212. if (namelen <= 0) parseerr(0,filename,lno, warnto,warncount,pigp,0,
  213. "root or null directory is listed as a conffile");
  214. newlink->name= nfmalloc(namelen+2);
  215. newlink->name[0]= '/';
  216. memcpy(newlink->name+1,value,namelen);
  217. newlink->name[namelen+1]= 0;
  218. hashlen= (int)(endent-endfn)-1; newlink->hash= nfmalloc(hashlen+1);
  219. memcpy(newlink->hash,endfn+1,hashlen); newlink->hash[hashlen]= 0;
  220. newlink->next =0;
  221. *lastp= newlink;
  222. lastp= &newlink->next;
  223. value= endent;
  224. }
  225. }
  226. void f_dependency(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. char c1, c2;
  231. const char *p, *emsg;
  232. struct varbuf depname, version;
  233. struct dependency *dyp, **ldypp;
  234. struct deppossi *dop, **ldopp;
  235. if (!*value) return; /* empty fields are ignored */
  236. varbufinit(&depname); varbufinit(&version);
  237. p= value;
  238. ldypp= &pifp->depends; while (*ldypp) ldypp= &(*ldypp)->next;
  239. for (;;) { /* loop creating new struct dependency's */
  240. dyp= nfmalloc(sizeof(struct dependency));
  241. dyp->up= 0; /* Set this to zero for now, as we don't know what our real
  242. * struct pkginfo address (in the database) is going to be yet.
  243. */
  244. dyp->next= 0; *ldypp= dyp; ldypp= &dyp->next;
  245. dyp->list= 0; ldopp= &dyp->list;
  246. dyp->type= fip->integer;
  247. for (;;) { /* loop creating new struct deppossi's */
  248. varbufreset(&depname);
  249. while (*p && !isspace(*p) && *p != '(' && *p != ',' && *p != '|') {
  250. varbufaddc(&depname,*p); p++;
  251. }
  252. varbufaddc(&depname,0);
  253. if (!*depname.buf)
  254. parseerr(0,filename,lno, warnto,warncount,pigp,0, "`%s' field, missing"
  255. " package name, or garbage where package name expected", fip->name);
  256. emsg= illegal_packagename(depname.buf,0);
  257. if (emsg) parseerr(0,filename,lno, warnto,warncount,pigp,0, "`%s' field,"
  258. " invalid package name `%.255s': %s",
  259. fip->name,depname.buf,emsg);
  260. dop= nfmalloc(sizeof(struct deppossi));
  261. dop->up= dyp;
  262. dop->ed= findpackage(depname.buf);
  263. dop->next= 0; *ldopp= dop; ldopp= &dop->next;
  264. dop->nextrev= 0; /* Don't link this (which is after all only `newpig' from
  265. dop->backrev= 0; * the main parsing loop in parsedb) into the depended on
  266. * packages' lists yet. This will be done later when we
  267. * install this (in parse.c). For the moment we do the
  268. * `forward' links in deppossi (`ed') only, and the backward
  269. * links from the depended on packages to dop are left undone.
  270. */
  271. dop->cyclebreak= 0;
  272. while (isspace(*p)) p++;
  273. if (*p == '(') {
  274. varbufreset(&version);
  275. p++; while (isspace(*p)) p++;
  276. c1= *p;
  277. if (c1 == '<' || c1 == '>') {
  278. c2= *++p;
  279. dop->verrel= (c1 == '<') ? dvrf_earlier : dvrf_later;
  280. if (c2 == '=') {
  281. dop->verrel |= (dvrf_orequal | dvrf_builtup);
  282. p++;
  283. } else if (c2 == c1) {
  284. dop->verrel |= (dvrf_strict | dvrf_builtup);
  285. p++;
  286. } else if (c2 == '<' || c2 == '>') {
  287. parseerr(0,filename,lno, warnto,warncount,pigp,0,
  288. "`%s' field, reference to `%.255s':\n"
  289. " bad version relationship %c%c",
  290. fip->name,depname.buf,c1,c2);
  291. dop->verrel= dvr_none;
  292. } else {
  293. parseerr(0,filename,lno, warnto,warncount,pigp,1,
  294. "`%s' field, reference to `%.255s':\n"
  295. " `%c' is obsolete, use `%c=' or `%c%c' instead",
  296. fip->name,depname.buf,c1,c1,c1,c1);
  297. dop->verrel |= (dvrf_orequal | dvrf_builtup);
  298. }
  299. } else if (c1 == '=') {
  300. dop->verrel= dvr_exact;
  301. p++;
  302. } else {
  303. parseerr(0,filename,lno, warnto,warncount,pigp,1,
  304. "`%s' field, reference to `%.255s':\n"
  305. " implicit exact match on version number, suggest using `=' instead",
  306. fip->name,depname.buf);
  307. dop->verrel= dvr_exact;
  308. }
  309. if (!isspace(*p) && !isalnum(*p)) {
  310. parseerr(0,filename,lno, warnto,warncount,pigp,1,
  311. "`%s' field, reference to `%.255s':\n"
  312. " version value starts with non-alphanumeric, suggest adding a space",
  313. fip->name,depname.buf);
  314. }
  315. while (isspace(*p)) p++;
  316. while (*p && *p != ')' && *p != '(') {
  317. if (!isspace(*p)) varbufaddc(&version,*p);
  318. p++;
  319. }
  320. if (*p == '(') parseerr(0,filename,lno, warnto,warncount,pigp,0,
  321. "`%s' field, reference to `%.255s': "
  322. "version contains `('",fip->name,depname.buf);
  323. else if (*p == 0) parseerr(0,filename,lno, warnto,warncount,pigp,0,
  324. "`%s' field, reference to `%.255s': "
  325. "version unterminated",fip->name,depname.buf);
  326. varbufaddc(&version,0);
  327. emsg= parseversion(&dop->version,version.buf);
  328. if (emsg) parseerr(0,filename,lno, warnto,warncount,pigp,0,
  329. "`%s' field, reference to `%.255s': "
  330. "error in version: %.255s",fip->name,depname.buf,emsg);
  331. p++; while (isspace(*p)) p++;
  332. } else {
  333. dop->verrel= dvr_none;
  334. blankversion(&dop->version);
  335. }
  336. if (!*p || *p == ',') break;
  337. if (*p != '|')
  338. parseerr(0,filename,lno, warnto,warncount,pigp,0, "`%s' field, syntax"
  339. " error after reference to package `%.255s'",
  340. fip->name, dop->ed->name);
  341. if (fip->integer == dep_conflicts ||
  342. fip->integer == dep_provides ||
  343. fip->integer == dep_replaces)
  344. parseerr(0,filename,lno, warnto,warncount,pigp,0,
  345. "alternatives (`|') not allowed in %s field",
  346. fip->name);
  347. p++; while (isspace(*p)) p++;
  348. }
  349. if (!*p) break;
  350. p++; while (isspace(*p)) p++;
  351. }
  352. }