parsehelp.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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. { "recommended", pri_recommended, 11 }, /* fixme: obsolete */
  80. { "optional", pri_optional, 8 },
  81. { "extra", pri_extra, 5 },
  82. { "contrib", pri_contrib, 7 }, /* fixme: keep? */
  83. { "this is a bug - please report", pri_other, 28 },
  84. { "unknown", pri_unknown, 7 },
  85. { "base", pri_required, 4 }, /* fixme: alias, remove */
  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. { "postinst-failed", stat_halfconfigured, 15 }, /* fixme: backwards compat., remove */
  99. { "removal-failed", stat_halfinstalled, 14 }, /* fixme: backwards compat., remove */
  100. { NULL }
  101. };
  102. const struct namevalue eflaginfos[]= { /* Note ! These must be in order ! */
  103. { "ok", eflagv_ok, 2 },
  104. { "reinstreq", eflagv_reinstreq, 9 },
  105. { "hold", eflagv_obsoletehold, 4 },
  106. { "hold-reinstreq", eflagv_obsoleteboth, 14 },
  107. { NULL }
  108. };
  109. const struct namevalue wantinfos[]= { /* Note ! These must be in order ! */
  110. { "unknown", want_unknown, 7 },
  111. { "install", want_install, 7 },
  112. { "hold", want_hold, 4 },
  113. { "deinstall", want_deinstall, 9 },
  114. { "purge", want_purge, 5 },
  115. { NULL }
  116. };
  117. const char *illegal_packagename(const char *p, const char **ep) {
  118. static const char alsoallowed[]= "-+._"; /* _ is deprecated */
  119. static char buf[150];
  120. int c;
  121. if (!*p) return _("may not be empty string");
  122. if (!isalnum(*p)) return _("must start with an alphanumeric");
  123. while ((c= *p++)!=0)
  124. if (!isalnum(c) && !strchr(alsoallowed,c)) break;
  125. if (!c) return NULL;
  126. if (isspace(c) && ep) {
  127. while (isspace(*p)) p++;
  128. *ep= p; return NULL;
  129. }
  130. snprintf(buf, sizeof(buf), _(
  131. "character `%c' not allowed (only letters, digits and characters `%s')"),
  132. c, alsoallowed);
  133. return buf;
  134. }
  135. const struct nickname nicknames[]= {
  136. /* NB: capitalisation of these strings is important. */
  137. { "Recommended", "Recommends" },
  138. { "Optional", "Suggests" },
  139. { "Class", "Priority" },
  140. { "Package-Revision", "Revision" },
  141. { "Package_Revision", "Revision" },
  142. { NULL }
  143. };
  144. int informativeversion(const struct versionrevision *version) {
  145. return (version->epoch ||
  146. (version->version && *version->version) ||
  147. (version->revision && *version->revision));
  148. }
  149. void varbufversion
  150. (struct varbuf *vb,
  151. const struct versionrevision *version,
  152. enum versiondisplayepochwhen vdew)
  153. {
  154. switch (vdew) {
  155. case vdew_never:
  156. break;
  157. case vdew_nonambig:
  158. if (!version->epoch &&
  159. (!version->version || !strchr(version->version,':')) &&
  160. (!version->revision || !strchr(version->revision,':'))) break;
  161. case vdew_always: /* FALL THROUGH */
  162. varbufprintf(vb,"%lu:",version->epoch);
  163. break;
  164. default:
  165. internerr("bad versiondisplayepochwhen in varbufversion");
  166. }
  167. if (version->version) varbufaddstr(vb,version->version);
  168. if (version->revision && *version->revision) {
  169. varbufaddc(vb,'-');
  170. varbufaddstr(vb,version->revision);
  171. }
  172. }
  173. const char *versiondescribe
  174. (const struct versionrevision *version,
  175. enum versiondisplayepochwhen vdew)
  176. {
  177. static struct varbuf bufs[10];
  178. static int bufnum=0;
  179. struct varbuf *vb;
  180. if (!informativeversion(version)) return _("<none>");
  181. vb= &bufs[bufnum]; bufnum++; if (bufnum == 10) bufnum= 0;
  182. varbufreset(vb);
  183. varbufversion(vb,version,vdew);
  184. varbufaddc(vb,0);
  185. return vb->buf;
  186. }
  187. const char *parseversion(struct versionrevision *rversion, const char *string) {
  188. char *hyphen, *colon, *eepochcolon;
  189. const char *end, *ptr;
  190. unsigned long epoch;
  191. if (!*string) return _("version string is empty");
  192. /* trim leading and trailing space */
  193. while (*string && (*string == ' ' || *string == '\t') ) string++;
  194. /* string now points to the first non-whitespace char */
  195. end = string;
  196. /* find either the end of the string, or a whitespace char */
  197. while (*end && *end != ' ' && *end != '\t' ) end++;
  198. /* check for extra chars after trailing space */
  199. ptr = end;
  200. while (*ptr && ( *ptr == ' ' || *ptr == '\t' ) ) ptr++;
  201. if (*ptr) return _("version string has embedded spaces");
  202. colon= strchr(string,':');
  203. if (colon) {
  204. epoch= strtoul(string,&eepochcolon,10);
  205. if (colon != eepochcolon) return _("epoch in version is not number");
  206. if (!*++colon) return _("nothing after colon in version number");
  207. string= colon;
  208. rversion->epoch= epoch;
  209. } else {
  210. rversion->epoch= 0;
  211. }
  212. rversion->version= nfstrnsave(string,end-string);
  213. hyphen= strrchr(rversion->version,'-');
  214. if (hyphen) *hyphen++= 0;
  215. rversion->revision= hyphen ? hyphen : "";
  216. return NULL;
  217. }
  218. void
  219. parse_must_have_field(const char *filename, int lno,
  220. const struct pkginfo *pigp,
  221. const char *value, const char *what)
  222. {
  223. if (!value)
  224. parse_error(filename, lno, pigp, _("missing %s"), what);
  225. else if (!*value)
  226. parse_error(filename, lno, pigp, _("empty value for %s"), what);
  227. }
  228. void
  229. parse_ensure_have_field(const char *filename, int lno,
  230. FILE *warnto, int *warncount,
  231. const struct pkginfo *pigp,
  232. const char **value, const char *what)
  233. {
  234. static const char *empty = "";
  235. if (!*value) {
  236. parse_warn(filename, lno, warnto, warncount, pigp, _("missing %s"), what);
  237. *value = empty;
  238. } else if (!**value) {
  239. parse_warn(filename, lno, warnto, warncount, pigp, _("empty value for %s"), what);
  240. }
  241. }