parsehelp.c 8.2 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 published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This is distributed in the hope that it will be useful,
  13. * but 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. #define NAMEVALUE_DEF(n, v) \
  73. [v] = { .name = n, .value = v, .length = sizeof(n) - 1 }
  74. const struct namevalue booleaninfos[] = {
  75. NAMEVALUE_DEF("no", 0),
  76. NAMEVALUE_DEF("yes", 1),
  77. { .name = NULL }
  78. };
  79. const struct namevalue priorityinfos[] = {
  80. NAMEVALUE_DEF("required", pri_required),
  81. NAMEVALUE_DEF("important", pri_important),
  82. NAMEVALUE_DEF("standard", pri_standard),
  83. NAMEVALUE_DEF("optional", pri_optional),
  84. NAMEVALUE_DEF("extra", pri_extra),
  85. NAMEVALUE_DEF("this is a bug - please report", pri_other),
  86. NAMEVALUE_DEF("unknown", pri_unknown),
  87. { .name = NULL }
  88. };
  89. const struct namevalue statusinfos[] = {
  90. NAMEVALUE_DEF("not-installed", stat_notinstalled),
  91. NAMEVALUE_DEF("config-files", stat_configfiles),
  92. NAMEVALUE_DEF("half-installed", stat_halfinstalled),
  93. NAMEVALUE_DEF("unpacked", stat_unpacked),
  94. NAMEVALUE_DEF("half-configured", stat_halfconfigured),
  95. NAMEVALUE_DEF("triggers-awaited", stat_triggersawaited),
  96. NAMEVALUE_DEF("triggers-pending", stat_triggerspending),
  97. NAMEVALUE_DEF("installed", stat_installed),
  98. /* These are additional entries for reading only, in any order ... */
  99. /* XXX: backwards compat., remove. */
  100. { .name = "postinst-failed", .value = stat_halfconfigured, .length = 15 },
  101. /* XXX: backwards compat., remove. */
  102. { .name = "removal-failed", .value = stat_halfinstalled, .length = 14 },
  103. { .name = NULL }
  104. };
  105. const struct namevalue eflaginfos[] = {
  106. NAMEVALUE_DEF("ok", eflag_ok),
  107. NAMEVALUE_DEF("reinstreq", eflag_reinstreq),
  108. { .name = NULL }
  109. };
  110. const struct namevalue wantinfos[] = {
  111. NAMEVALUE_DEF("unknown", want_unknown),
  112. NAMEVALUE_DEF("install", want_install),
  113. NAMEVALUE_DEF("hold", want_hold),
  114. NAMEVALUE_DEF("deinstall", want_deinstall),
  115. NAMEVALUE_DEF("purge", want_purge),
  116. { .name = NULL }
  117. };
  118. const char *illegal_packagename(const char *p, const char **ep) {
  119. static const char alsoallowed[]= "-+._"; /* _ is deprecated */
  120. static char buf[150];
  121. int c;
  122. if (!*p) return _("may not be empty string");
  123. if (!isalnum(*p)) return _("must start with an alphanumeric");
  124. while ((c = *p++) != '\0')
  125. if (!isalnum(c) && !strchr(alsoallowed,c)) break;
  126. if (!c) return NULL;
  127. if (isspace(c) && ep) {
  128. while (isspace(*p)) p++;
  129. *ep= p; return NULL;
  130. }
  131. snprintf(buf, sizeof(buf), _(
  132. "character `%c' not allowed (only letters, digits and characters `%s')"),
  133. c, alsoallowed);
  134. return buf;
  135. }
  136. const struct nickname nicknames[]= {
  137. /* NB: capitalisation of these strings is important. */
  138. { .nick = "Recommended", .canon = "Recommends" },
  139. { .nick = "Optional", .canon = "Suggests" },
  140. { .nick = "Class", .canon = "Priority" },
  141. { .nick = "Package-Revision", .canon = "Revision" },
  142. { .nick = "Package_Revision", .canon = "Revision" },
  143. { .nick = NULL }
  144. };
  145. int informativeversion(const struct versionrevision *version) {
  146. return (version->epoch ||
  147. (version->version && *version->version) ||
  148. (version->revision && *version->revision));
  149. }
  150. void varbufversion
  151. (struct varbuf *vb,
  152. const struct versionrevision *version,
  153. enum versiondisplayepochwhen vdew)
  154. {
  155. switch (vdew) {
  156. case vdew_never:
  157. break;
  158. case vdew_nonambig:
  159. if (!version->epoch &&
  160. (!version->version || !strchr(version->version,':')) &&
  161. (!version->revision || !strchr(version->revision,':'))) break;
  162. case vdew_always: /* FALL THROUGH */
  163. varbufprintf(vb,"%lu:",version->epoch);
  164. break;
  165. default:
  166. internerr("unknown versiondisplayepochwhen '%d'", vdew);
  167. }
  168. if (version->version) varbufaddstr(vb,version->version);
  169. if (version->revision && *version->revision) {
  170. varbufaddc(vb,'-');
  171. varbufaddstr(vb,version->revision);
  172. }
  173. }
  174. const char *versiondescribe
  175. (const struct versionrevision *version,
  176. enum versiondisplayepochwhen vdew)
  177. {
  178. static struct varbuf bufs[10];
  179. static int bufnum=0;
  180. struct varbuf *vb;
  181. if (!informativeversion(version)) return _("<none>");
  182. vb= &bufs[bufnum]; bufnum++; if (bufnum == 10) bufnum= 0;
  183. varbufreset(vb);
  184. varbufversion(vb,version,vdew);
  185. varbufaddc(vb,0);
  186. return vb->buf;
  187. }
  188. const char *parseversion(struct versionrevision *rversion, const char *string) {
  189. char *hyphen, *colon, *eepochcolon;
  190. const char *end, *ptr;
  191. unsigned long epoch;
  192. if (!*string) return _("version string is empty");
  193. /* trim leading and trailing space */
  194. while (*string && isblank(*string))
  195. 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 && !isblank(*end))
  200. end++;
  201. /* check for extra chars after trailing space */
  202. ptr = end;
  203. while (*ptr && isblank(*ptr))
  204. ptr++;
  205. if (*ptr) return _("version string has embedded spaces");
  206. colon= strchr(string,':');
  207. if (colon) {
  208. epoch= strtoul(string,&eepochcolon,10);
  209. if (colon != eepochcolon) return _("epoch in version is not number");
  210. if (!*++colon) return _("nothing after colon in version number");
  211. string= colon;
  212. rversion->epoch= epoch;
  213. } else {
  214. rversion->epoch= 0;
  215. }
  216. rversion->version= nfstrnsave(string,end-string);
  217. hyphen= strrchr(rversion->version,'-');
  218. if (hyphen)
  219. *hyphen++ = '\0';
  220. rversion->revision= hyphen ? hyphen : "";
  221. return NULL;
  222. }
  223. void
  224. parse_must_have_field(struct parsedb_state *ps,
  225. const struct pkginfo *pigp,
  226. const char *value, const char *what)
  227. {
  228. if (!value)
  229. parse_error(ps, pigp, _("missing %s"), what);
  230. else if (!*value)
  231. parse_error(ps, pigp, _("empty value for %s"), what);
  232. }
  233. void
  234. parse_ensure_have_field(struct parsedb_state *ps,
  235. const struct pkginfo *pigp,
  236. const char **value, const char *what)
  237. {
  238. static const char empty[] = "";
  239. if (!*value) {
  240. parse_warn(ps, pigp, _("missing %s"), what);
  241. *value = empty;
  242. } else if (!**value) {
  243. parse_warn(ps, pigp, _("empty value for %s"), what);
  244. }
  245. }