parsehelp.c 8.2 KB

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