parsehelp.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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. { "config-files", stat_configfiles, 12 },
  79. { "half-installed", stat_halfinstalled, 14 },
  80. { "unpacked", stat_unpacked, 8 },
  81. { "half-configured", stat_halfconfigured, 15, },
  82. { "triggers-awaited", stat_triggersawaited, 16 },
  83. { "triggers-pending", stat_triggerspending, 16 },
  84. { "installed", stat_installed, 9 },
  85. /* These are additional entries for reading only, in any order ... */
  86. { "postinst-failed", stat_halfconfigured, 15 }, /* fixme: backwards compat., remove */
  87. { "removal-failed", stat_halfinstalled, 14 }, /* fixme: backwards compat., remove */
  88. { NULL }
  89. };
  90. const struct namevalue eflaginfos[]= { /* Note ! These must be in order ! */
  91. { "ok", eflagv_ok, 2 },
  92. { "reinstreq", eflagv_reinstreq, 9 },
  93. { "hold", eflagv_obsoletehold, 4 },
  94. { "hold-reinstreq", eflagv_obsoleteboth, 14 },
  95. { NULL }
  96. };
  97. const struct namevalue wantinfos[]= { /* Note ! These must be in order ! */
  98. { "unknown", want_unknown, 7 },
  99. { "install", want_install, 7 },
  100. { "hold", want_hold, 4 },
  101. { "deinstall", want_deinstall, 9 },
  102. { "purge", want_purge, 5 },
  103. { NULL }
  104. };
  105. const char *illegal_packagename(const char *p, const char **ep) {
  106. static const char alsoallowed[]= "-+._"; /* _ is deprecated */
  107. static char buf[150];
  108. int c;
  109. if (!*p) return _("may not be empty string");
  110. if (!isalnum(*p)) return _("must start with an alphanumeric");
  111. while ((c= *p++)!=0)
  112. if (!isalnum(c) && !strchr(alsoallowed,c)) break;
  113. if (!c) return NULL;
  114. if (isspace(c) && ep) {
  115. while (isspace(*p)) p++;
  116. *ep= p; return NULL;
  117. }
  118. snprintf(buf, sizeof(buf), _(
  119. "character `%c' not allowed (only letters, digits and characters `%s')"),
  120. c, alsoallowed);
  121. return buf;
  122. }
  123. const struct nickname nicknames[]= {
  124. /* NB: capitalisation of these strings is important. */
  125. { "Recommended", "Recommends" },
  126. { "Optional", "Suggests" },
  127. { "Class", "Priority" },
  128. { "Package-Revision", "Revision" },
  129. { "Package_Revision", "Revision" },
  130. { NULL }
  131. };
  132. int informativeversion(const struct versionrevision *version) {
  133. return (version->epoch ||
  134. (version->version && *version->version) ||
  135. (version->revision && *version->revision));
  136. }
  137. void varbufversion
  138. (struct varbuf *vb,
  139. const struct versionrevision *version,
  140. enum versiondisplayepochwhen vdew)
  141. {
  142. switch (vdew) {
  143. case vdew_never:
  144. break;
  145. case vdew_nonambig:
  146. if (!version->epoch &&
  147. (!version->version || !strchr(version->version,':')) &&
  148. (!version->revision || !strchr(version->revision,':'))) break;
  149. case vdew_always: /* FALL THROUGH */
  150. varbufprintf(vb,"%lu:",version->epoch);
  151. break;
  152. default:
  153. internerr("bad versiondisplayepochwhen in varbufversion");
  154. }
  155. if (version->version) varbufaddstr(vb,version->version);
  156. if (version->revision && *version->revision) {
  157. varbufaddc(vb,'-');
  158. varbufaddstr(vb,version->revision);
  159. }
  160. }
  161. const char *versiondescribe
  162. (const struct versionrevision *version,
  163. enum versiondisplayepochwhen vdew)
  164. {
  165. static struct varbuf bufs[10];
  166. static int bufnum=0;
  167. struct varbuf *vb;
  168. if (!informativeversion(version)) return _("<none>");
  169. vb= &bufs[bufnum]; bufnum++; if (bufnum == 10) bufnum= 0;
  170. varbufreset(vb);
  171. varbufversion(vb,version,vdew);
  172. varbufaddc(vb,0);
  173. return vb->buf;
  174. }
  175. const char *parseversion(struct versionrevision *rversion, const char *string) {
  176. char *hyphen, *colon, *eepochcolon;
  177. const char *end, *ptr;
  178. unsigned long epoch;
  179. if (!*string) return _("version string is empty");
  180. /* trim leading and trailing space */
  181. while (*string && (*string == ' ' || *string == '\t') ) string++;
  182. /* string now points to the first non-whitespace char */
  183. end = string;
  184. /* find either the end of the string, or a whitespace char */
  185. while (*end && *end != ' ' && *end != '\t' ) end++;
  186. /* check for extra chars after trailing space */
  187. ptr = end;
  188. while (*ptr && ( *ptr == ' ' || *ptr == '\t' ) ) ptr++;
  189. if (*ptr) return _("version string has embedded spaces");
  190. colon= strchr(string,':');
  191. if (colon) {
  192. epoch= strtoul(string,&eepochcolon,10);
  193. if (colon != eepochcolon) return _("epoch in version is not number");
  194. if (!*++colon) return _("nothing after colon in version number");
  195. string= colon;
  196. rversion->epoch= epoch;
  197. } else {
  198. rversion->epoch= 0;
  199. }
  200. rversion->version= nfstrnsave(string,end-string);
  201. hyphen= strrchr(rversion->version,'-');
  202. if (hyphen) *hyphen++= 0;
  203. rversion->revision= hyphen ? hyphen : "";
  204. return NULL;
  205. }
  206. void parsemustfield
  207. (FILE *file, const char *filename, int lno,
  208. FILE *warnto, int *warncount,
  209. const struct pkginfo *pigp, int warnonly,
  210. const char **value, const char *what)
  211. {
  212. static const char *empty = "";
  213. if (!*value) {
  214. parseerr(file,filename,lno, warnto,warncount,pigp,warnonly, _("missing %s"),what);
  215. *value= empty;
  216. } else if (!**value) {
  217. parseerr(file,filename,lno, warnto,warncount,pigp,warnonly,
  218. _("empty value for %s"),what);
  219. }
  220. }
  221. const char *skip_slash_dotslash(const char *p) {
  222. while (p[0] == '/' || (p[0] == '.' && p[1] == '/')) p++;
  223. return p;
  224. }