parsehelp.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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 License
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. */
  20. #include <config.h>
  21. #include <compat.h>
  22. #include <ctype.h>
  23. #include <string.h>
  24. #include <stdlib.h>
  25. #include <stdio.h>
  26. #include <dpkg/i18n.h>
  27. #include <dpkg/dpkg.h>
  28. #include <dpkg/dpkg-db.h>
  29. #include <dpkg/string.h>
  30. #include <dpkg/parsedump.h>
  31. static void
  32. parse_error_msg(struct parsedb_state *ps, 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, ps->filename, ps->lno, pigp->name);
  38. else
  39. sprintf(buf, _("%s, in file '%.255s' near line %d:\n "),
  40. type, ps->filename, ps->lno);
  41. }
  42. void
  43. parse_error(struct parsedb_state *ps,
  44. const struct pkginfo *pigp, const char *fmt, ...)
  45. {
  46. va_list al;
  47. char buf1[768], buf2[1000], *q;
  48. parse_error_msg(ps, 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(struct parsedb_state *ps,
  56. const struct pkginfo *pigp, const char *fmt, ...)
  57. {
  58. va_list al;
  59. char buf1[768], buf2[1000], *q;
  60. parse_error_msg(ps, pigp, _("warning"), buf1);
  61. q = str_escape_fmt(buf2, buf1);
  62. strcat(q, fmt);
  63. va_start(al, fmt);
  64. ps->warncount++;
  65. if (ps->warnto) {
  66. strcat(q,"\n");
  67. if (vfprintf(ps->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. { "optional", pri_optional, 8 },
  82. { "extra", pri_extra, 5 },
  83. { "this is a bug - please report", pri_other, 28 },
  84. { "unknown", pri_unknown, 7 },
  85. { NULL }
  86. };
  87. const struct namevalue statusinfos[]= { /* Note ! These must be in order ! */
  88. { "not-installed", stat_notinstalled, 13 },
  89. { "config-files", stat_configfiles, 12 },
  90. { "half-installed", stat_halfinstalled, 14 },
  91. { "unpacked", stat_unpacked, 8 },
  92. { "half-configured", stat_halfconfigured, 15, },
  93. { "triggers-awaited", stat_triggersawaited, 16 },
  94. { "triggers-pending", stat_triggerspending, 16 },
  95. { "installed", stat_installed, 9 },
  96. /* These are additional entries for reading only, in any order ... */
  97. /* XXX: backwards compat., remove. */
  98. { "postinst-failed", stat_halfconfigured, 15 },
  99. /* XXX: backwards compat., remove. */
  100. { "removal-failed", stat_halfinstalled, 14 },
  101. { NULL }
  102. };
  103. const struct namevalue eflaginfos[]= { /* Note ! These must be in order ! */
  104. { "ok", eflag_ok, 2 },
  105. { "reinstreq", eflag_reinstreq, 9 },
  106. { NULL }
  107. };
  108. const struct namevalue wantinfos[]= { /* Note ! These must be in order ! */
  109. { "unknown", want_unknown, 7 },
  110. { "install", want_install, 7 },
  111. { "hold", want_hold, 4 },
  112. { "deinstall", want_deinstall, 9 },
  113. { "purge", want_purge, 5 },
  114. { NULL }
  115. };
  116. const char *illegal_packagename(const char *p, const char **ep) {
  117. static const char alsoallowed[]= "-+._"; /* _ is deprecated */
  118. static char buf[150];
  119. int c;
  120. if (!*p) return _("may not be empty string");
  121. if (!isalnum(*p)) return _("must start with an alphanumeric");
  122. while ((c = *p++) != '\0')
  123. if (!isalnum(c) && !strchr(alsoallowed,c)) break;
  124. if (!c) return NULL;
  125. if (isspace(c) && ep) {
  126. while (isspace(*p)) p++;
  127. *ep= p; return NULL;
  128. }
  129. snprintf(buf, sizeof(buf), _(
  130. "character `%c' not allowed (only letters, digits and characters `%s')"),
  131. c, alsoallowed);
  132. return buf;
  133. }
  134. const struct nickname nicknames[]= {
  135. /* NB: capitalisation of these strings is important. */
  136. { "Recommended", "Recommends" },
  137. { "Optional", "Suggests" },
  138. { "Class", "Priority" },
  139. { "Package-Revision", "Revision" },
  140. { "Package_Revision", "Revision" },
  141. { NULL }
  142. };
  143. int informativeversion(const struct versionrevision *version) {
  144. return (version->epoch ||
  145. (version->version && *version->version) ||
  146. (version->revision && *version->revision));
  147. }
  148. void varbufversion
  149. (struct varbuf *vb,
  150. const struct versionrevision *version,
  151. enum versiondisplayepochwhen vdew)
  152. {
  153. switch (vdew) {
  154. case vdew_never:
  155. break;
  156. case vdew_nonambig:
  157. if (!version->epoch &&
  158. (!version->version || !strchr(version->version,':')) &&
  159. (!version->revision || !strchr(version->revision,':'))) break;
  160. case vdew_always: /* FALL THROUGH */
  161. varbufprintf(vb,"%lu:",version->epoch);
  162. break;
  163. default:
  164. internerr("unknown versiondisplayepochwhen '%d'", vdew);
  165. }
  166. if (version->version) varbufaddstr(vb,version->version);
  167. if (version->revision && *version->revision) {
  168. varbufaddc(vb,'-');
  169. varbufaddstr(vb,version->revision);
  170. }
  171. }
  172. const char *versiondescribe
  173. (const struct versionrevision *version,
  174. enum versiondisplayepochwhen vdew)
  175. {
  176. static struct varbuf bufs[10];
  177. static int bufnum=0;
  178. struct varbuf *vb;
  179. if (!informativeversion(version)) return _("<none>");
  180. vb= &bufs[bufnum]; bufnum++; if (bufnum == 10) bufnum= 0;
  181. varbufreset(vb);
  182. varbufversion(vb,version,vdew);
  183. varbufaddc(vb,0);
  184. return vb->buf;
  185. }
  186. const char *parseversion(struct versionrevision *rversion, const char *string) {
  187. char *hyphen, *colon, *eepochcolon;
  188. const char *end, *ptr;
  189. unsigned long epoch;
  190. if (!*string) return _("version string is empty");
  191. /* trim leading and trailing space */
  192. while (*string && isblank(*string))
  193. 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 && !isblank(*end))
  198. end++;
  199. /* check for extra chars after trailing space */
  200. ptr = end;
  201. while (*ptr && isblank(*ptr))
  202. 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. }