parsehelp.c 9.0 KB

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