parsehelp.c 8.9 KB

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