parsehelp.c 8.6 KB

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