parsehelp.c 9.0 KB

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