parsehelp.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. /*
  2. * libdpkg - Debian packaging suite library routines
  3. * parsehelp.c - helpful routines for parsing and writing
  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. void parseerr(FILE *file, const char *filename, int lno, FILE *warnto, int *warncount,
  29. const struct pkginfo *pigp, int warnonly,
  30. const char *fmt, ...) {
  31. va_list al;
  32. char buf1[768], buf2[1000], *p, *q;
  33. if (file && ferror(file)) ohshite("failed to read `%s' at line %d",filename,lno);
  34. sprintf(buf1, "%s, in file `%.255s' near line %d",
  35. warnonly ? "warning" : "parse error", filename, lno);
  36. if (pigp && pigp->name) {
  37. sprintf(buf2, " package `%.255s'", pigp->name);
  38. strcat(buf1,buf2);
  39. }
  40. for (p=buf1, q=buf2; *p; *q++= *p++) if (*p == '%') *q++= '%';
  41. strcpy(q,":\n "); strcat(q,fmt);
  42. va_start(al,fmt);
  43. if (!warnonly) ohshitv(buf2,al);
  44. if (warncount) (*warncount)++;
  45. if (warnto) {
  46. strcat(q,"\n");
  47. if (vfprintf(warnto,buf2,al) == EOF)
  48. ohshite("failed to write parsing warning");
  49. }
  50. va_end(al);
  51. }
  52. const struct namevalue booleaninfos[]= { /* Note ! These must be in order ! */
  53. { "no", 0 },
  54. { "yes", 1 },
  55. { 0 }
  56. };
  57. const struct namevalue priorityinfos[]= { /* Note ! These must be in order ! */
  58. { "required", pri_required },
  59. { "important", pri_important },
  60. { "standard", pri_standard },
  61. { "recommended", pri_recommended }, /* fixme: obsolete */
  62. { "optional", pri_optional },
  63. { "extra", pri_extra },
  64. { "contrib", pri_contrib }, /* fixme: keep? */
  65. { "this is a bug - please report", pri_other },
  66. { "unknown", pri_unknown },
  67. { "base", pri_required }, /* fixme: alias, remove */
  68. { 0 }
  69. };
  70. const struct namevalue statusinfos[]= { /* Note ! These must be in order ! */
  71. { "not-installed", stat_notinstalled },
  72. { "unpacked", stat_unpacked },
  73. { "half-configured", stat_halfconfigured },
  74. { "installed", stat_installed },
  75. { "half-installed", stat_halfinstalled },
  76. { "config-files", stat_configfiles },
  77. /* These are additional entries for reading only, in any order ... */
  78. { "postinst-failed", stat_halfconfigured }, /* fixme: backwards compat., remove */
  79. { "removal-failed", stat_halfinstalled }, /* fixme: backwards compat., remove */
  80. { 0 }
  81. };
  82. const struct namevalue eflaginfos[]= { /* Note ! These must be in order ! */
  83. { "ok", eflagv_ok },
  84. { "reinstreq", eflagv_reinstreq },
  85. { "hold", eflagv_obsoletehold },
  86. { "hold-reinstreq", eflagv_obsoleteboth },
  87. { 0 }
  88. };
  89. const struct namevalue wantinfos[]= { /* Note ! These must be in order ! */
  90. { "unknown", want_unknown },
  91. { "install", want_install },
  92. { "hold", want_hold },
  93. { "deinstall", want_deinstall },
  94. { "purge", want_purge },
  95. { 0 }
  96. };
  97. const char *illegal_packagename(const char *p, const char **ep) {
  98. static const char alsoallowed[]= "-+._"; /* _ is deprecated */
  99. static char buf[150];
  100. int c;
  101. if (!*p) return "may not be empty string";
  102. if (!isalnum(*p)) return "must start with an alphanumeric";
  103. if (!*++p) return "must be at least two characters";
  104. while ((c= *p++)!=0)
  105. if (!isalnum(c) && !strchr(alsoallowed,c)) break;
  106. if (!c) return 0;
  107. if (isspace(c) && ep) {
  108. while (isspace(*p)) p++;
  109. *ep= p; return 0;
  110. }
  111. sprintf(buf,
  112. "character `%c' not allowed - only letters, digits and %s allowed",
  113. c, alsoallowed);
  114. return buf;
  115. }
  116. const struct nickname nicknames[]= {
  117. /* NB: capitalisation of these strings is important. */
  118. { "Recommended", "Recommends" },
  119. { "Optional", "Suggests" },
  120. { "Class", "Priority" },
  121. { "Package-Revision", "Revision" },
  122. { "Package_Revision", "Revision" },
  123. { 0 }
  124. };
  125. int informativeversion(const struct versionrevision *version) {
  126. return (version->epoch ||
  127. (version->version && *version->version) ||
  128. (version->revision && *version->revision));
  129. }
  130. void varbufversion(struct varbuf *vb,
  131. const struct versionrevision *version,
  132. enum versiondisplayepochwhen vdew) {
  133. switch (vdew) {
  134. case vdew_never:
  135. break;
  136. case vdew_nonambig:
  137. if (!version->epoch &&
  138. (!version->version || !strchr(version->version,':')) &&
  139. (!version->revision || !strchr(version->revision,':'))) break;
  140. case vdew_always: /* FALL THROUGH */
  141. varbufprintf(vb,"%lu:",version->epoch);
  142. break;
  143. default:
  144. internerr("bad versiondisplayepochwhen in varbufversion");
  145. }
  146. if (version->version) varbufaddstr(vb,version->version);
  147. if (version->revision && *version->revision) {
  148. varbufaddc(vb,'-');
  149. varbufaddstr(vb,version->revision);
  150. }
  151. }
  152. const char *versiondescribe(const struct versionrevision *version,
  153. enum versiondisplayepochwhen vdew) {
  154. static struct varbuf bufs[10];
  155. static int bufnum=0;
  156. struct varbuf *vb;
  157. if (!informativeversion(version)) return "<none>";
  158. vb= &bufs[bufnum]; bufnum++; if (bufnum == 10) bufnum= 0;
  159. varbufreset(vb);
  160. varbufversion(vb,version,vdew);
  161. varbufaddc(vb,0);
  162. return vb->buf;
  163. }
  164. const char *parseversion(struct versionrevision *rversion, const char *string) {
  165. char *hyphen, *colon, *eepochcolon;
  166. unsigned long epoch;
  167. if (!*string) return "version string is empty";
  168. colon= strchr(string,':');
  169. if (colon) {
  170. epoch= strtoul(string,&eepochcolon,10);
  171. if (colon != eepochcolon) return "epoch in version is not number";
  172. if (!*++colon) return "nothing after colon in version number";
  173. string= colon+1;
  174. rversion->epoch= epoch;
  175. } else {
  176. rversion->epoch= 0;
  177. }
  178. rversion->version= nfstrsave(string);
  179. hyphen= strrchr(rversion->version,'-');
  180. if (hyphen) *hyphen++= 0;
  181. rversion->revision= hyphen ? hyphen : nfstrsave("");
  182. return 0;
  183. }
  184. void parsemustfield(FILE *file, const char *filename, int lno,
  185. FILE *warnto, int *warncount,
  186. const struct pkginfo *pigp, int warnonly,
  187. char **value, const char *what) {
  188. if (!*value) {
  189. parseerr(file,filename,lno, warnto,warncount,pigp,warnonly, "missing %s",what);
  190. *value= nfstrsave("");
  191. } else if (!**value) {
  192. parseerr(file,filename,lno, warnto,warncount,pigp,warnonly,
  193. "empty value for %s",what);
  194. }
  195. }
  196. const char *skip_slash_dotslash(const char *p) {
  197. while (p[0] == '/' || (p[0] == '.' && p[1] == '/')) p++;
  198. return p;
  199. }