parsehelp.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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. (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, 2 },
  56. { "yes", 1, 3 },
  57. { NULL }
  58. };
  59. const struct namevalue priorityinfos[]= { /* Note ! These must be in order ! */
  60. { "required", pri_required, 8 },
  61. { "important", pri_important, 9 },
  62. { "standard", pri_standard, 8 },
  63. { "recommended", pri_recommended, 11 }, /* fixme: obsolete */
  64. { "optional", pri_optional, 8 },
  65. { "extra", pri_extra, 5 },
  66. { "contrib", pri_contrib, 7 }, /* fixme: keep? */
  67. { "this is a bug - please report", pri_other, 28 },
  68. { "unknown", pri_unknown, 7 },
  69. { "base", pri_required, 4 }, /* fixme: alias, remove */
  70. { NULL }
  71. };
  72. const struct namevalue statusinfos[]= { /* Note ! These must be in order ! */
  73. { "not-installed", stat_notinstalled, 13 },
  74. { "unpacked", stat_unpacked, 8 },
  75. { "half-configured", stat_halfconfigured, 15, },
  76. { "installed", stat_installed, 9 },
  77. { "half-installed", stat_halfinstalled, 14 },
  78. { "config-files", stat_configfiles, 12 },
  79. /* These are additional entries for reading only, in any order ... */
  80. { "postinst-failed", stat_halfconfigured, 15 }, /* fixme: backwards compat., remove */
  81. { "removal-failed", stat_halfinstalled, 14 }, /* fixme: backwards compat., remove */
  82. { NULL }
  83. };
  84. const struct namevalue eflaginfos[]= { /* Note ! These must be in order ! */
  85. { "ok", eflagv_ok, 2 },
  86. { "reinstreq", eflagv_reinstreq, 9 },
  87. { "hold", eflagv_obsoletehold, 4 },
  88. { "hold-reinstreq", eflagv_obsoleteboth, 14 },
  89. { NULL }
  90. };
  91. const struct namevalue wantinfos[]= { /* Note ! These must be in order ! */
  92. { "unknown", want_unknown, 7 },
  93. { "install", want_install, 7 },
  94. { "hold", want_hold, 4 },
  95. { "deinstall", want_deinstall, 9 },
  96. { "purge", want_purge, 5 },
  97. { NULL }
  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 NULL;
  109. if (isspace(c) && ep) {
  110. while (isspace(*p)) p++;
  111. *ep= p; return NULL;
  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. { NULL }
  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. const char *end, *ptr;
  173. unsigned long epoch;
  174. if (!*string) return _("version string is empty");
  175. /* trim leading and trailing space */
  176. while (*string && (*string == ' ' || *string == '\t') ) string++;
  177. /* string now points to the first non-whitespace char */
  178. end = string;
  179. /* find either the end of the string, or a whitespace char */
  180. while (*end && *end != ' ' && *end != '\t' ) end++;
  181. /* check for extra chars after trailing space */
  182. ptr = end;
  183. while (*ptr && ( *ptr == ' ' || *ptr == '\t' ) ) ptr++;
  184. if (*ptr) return _("version string has embedded spaces");
  185. colon= strchr(string,':');
  186. if (colon) {
  187. epoch= strtoul(string,&eepochcolon,10);
  188. if (colon != eepochcolon) return _("epoch in version is not number");
  189. if (!*++colon) return _("nothing after colon in version number");
  190. string= colon;
  191. rversion->epoch= epoch;
  192. } else {
  193. rversion->epoch= 0;
  194. }
  195. rversion->version= nfstrnsave(string,end-string);
  196. hyphen= strrchr(rversion->version,'-');
  197. if (hyphen) *hyphen++= 0;
  198. rversion->revision= hyphen ? hyphen : "";
  199. return NULL;
  200. }
  201. void parsemustfield
  202. (FILE *file, const char *filename, int lno,
  203. FILE *warnto, int *warncount,
  204. const struct pkginfo *pigp, int warnonly,
  205. const char **value, const char *what)
  206. {
  207. static const char *empty = "";
  208. if (!*value) {
  209. parseerr(file,filename,lno, warnto,warncount,pigp,warnonly, _("missing %s"),what);
  210. *value= empty;
  211. } else if (!**value) {
  212. parseerr(file,filename,lno, warnto,warncount,pigp,warnonly,
  213. _("empty value for %s"),what);
  214. }
  215. }
  216. const char *skip_slash_dotslash(const char *p) {
  217. while (p[0] == '/' || (p[0] == '.' && p[1] == '/')) p++;
  218. return p;
  219. }