parsehelp.c 8.3 KB

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