parsehelp.c 9.0 KB

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