parsehelp.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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. while ((c= *p++)!=0)
  106. if (!isalnum(c) && !strchr(alsoallowed,c)) break;
  107. if (!c) return NULL;
  108. if (isspace(c) && ep) {
  109. while (isspace(*p)) p++;
  110. *ep= p; return NULL;
  111. }
  112. snprintf(buf, sizeof(buf),
  113. _("character `%c' not allowed - only letters, digits and %s allowed"),
  114. c, alsoallowed);
  115. return buf;
  116. }
  117. const struct nickname nicknames[]= {
  118. /* NB: capitalisation of these strings is important. */
  119. { "Recommended", "Recommends" },
  120. { "Optional", "Suggests" },
  121. { "Class", "Priority" },
  122. { "Package-Revision", "Revision" },
  123. { "Package_Revision", "Revision" },
  124. { NULL }
  125. };
  126. int informativeversion(const struct versionrevision *version) {
  127. return (version->epoch ||
  128. (version->version && *version->version) ||
  129. (version->revision && *version->revision));
  130. }
  131. void varbufversion
  132. (struct varbuf *vb,
  133. const struct versionrevision *version,
  134. enum versiondisplayepochwhen vdew)
  135. {
  136. switch (vdew) {
  137. case vdew_never:
  138. break;
  139. case vdew_nonambig:
  140. if (!version->epoch &&
  141. (!version->version || !strchr(version->version,':')) &&
  142. (!version->revision || !strchr(version->revision,':'))) break;
  143. case vdew_always: /* FALL THROUGH */
  144. varbufprintf(vb,"%lu:",version->epoch);
  145. break;
  146. default:
  147. internerr("bad versiondisplayepochwhen in varbufversion");
  148. }
  149. if (version->version) varbufaddstr(vb,version->version);
  150. if (version->revision && *version->revision) {
  151. varbufaddc(vb,'-');
  152. varbufaddstr(vb,version->revision);
  153. }
  154. }
  155. const char *versiondescribe
  156. (const struct versionrevision *version,
  157. enum versiondisplayepochwhen vdew)
  158. {
  159. static struct varbuf bufs[10];
  160. static int bufnum=0;
  161. struct varbuf *vb;
  162. if (!informativeversion(version)) return _("<none>");
  163. vb= &bufs[bufnum]; bufnum++; if (bufnum == 10) bufnum= 0;
  164. varbufreset(vb);
  165. varbufversion(vb,version,vdew);
  166. varbufaddc(vb,0);
  167. return vb->buf;
  168. }
  169. const char *parseversion(struct versionrevision *rversion, const char *string) {
  170. char *hyphen, *colon, *eepochcolon;
  171. const char *end, *ptr;
  172. unsigned long epoch;
  173. if (!*string) return _("version string is empty");
  174. /* trim leading and trailing space */
  175. while (*string && (*string == ' ' || *string == '\t') ) string++;
  176. /* string now points to the first non-whitespace char */
  177. end = string;
  178. /* find either the end of the string, or a whitespace char */
  179. while (*end && *end != ' ' && *end != '\t' ) end++;
  180. /* check for extra chars after trailing space */
  181. ptr = end;
  182. while (*ptr && ( *ptr == ' ' || *ptr == '\t' ) ) ptr++;
  183. if (*ptr) return _("version string has embedded spaces");
  184. colon= strchr(string,':');
  185. if (colon) {
  186. epoch= strtoul(string,&eepochcolon,10);
  187. if (colon != eepochcolon) return _("epoch in version is not number");
  188. if (!*++colon) return _("nothing after colon in version number");
  189. string= colon;
  190. rversion->epoch= epoch;
  191. } else {
  192. rversion->epoch= 0;
  193. }
  194. rversion->version= nfstrnsave(string,end-string);
  195. hyphen= strrchr(rversion->version,'-');
  196. if (hyphen) *hyphen++= 0;
  197. rversion->revision= hyphen ? hyphen : "";
  198. return NULL;
  199. }
  200. void parsemustfield
  201. (FILE *file, const char *filename, int lno,
  202. FILE *warnto, int *warncount,
  203. const struct pkginfo *pigp, int warnonly,
  204. const char **value, const char *what)
  205. {
  206. static const char *empty = "";
  207. if (!*value) {
  208. parseerr(file,filename,lno, warnto,warncount,pigp,warnonly, _("missing %s"),what);
  209. *value= empty;
  210. } else if (!**value) {
  211. parseerr(file,filename,lno, warnto,warncount,pigp,warnonly,
  212. _("empty value for %s"),what);
  213. }
  214. }
  215. const char *skip_slash_dotslash(const char *p) {
  216. while (p[0] == '/' || (p[0] == '.' && p[1] == '/')) p++;
  217. return p;
  218. }