parsehelp.c 7.7 KB

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