parsehelp.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  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. * Copyright © 2006-2012 Guillem Jover <guillem@debian.org>
  7. *
  8. * This is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. */
  21. #include <config.h>
  22. #include <compat.h>
  23. #include <errno.h>
  24. #include <ctype.h>
  25. #include <limits.h>
  26. #include <string.h>
  27. #include <stdlib.h>
  28. #include <stdio.h>
  29. #include <dpkg/i18n.h>
  30. #include <dpkg/dpkg.h>
  31. #include <dpkg/dpkg-db.h>
  32. #include <dpkg/string.h>
  33. #include <dpkg/error.h>
  34. #include <dpkg/parsedump.h>
  35. static const char *
  36. parse_error_msg(struct parsedb_state *ps, const char *fmt)
  37. {
  38. static char msg[1024];
  39. char filename[256];
  40. str_escape_fmt(filename, ps->filename, sizeof(filename));
  41. if (ps->pkg && ps->pkg->set->name)
  42. sprintf(msg, _("parsing file '%.255s' near line %d package '%.255s':\n"
  43. " %.255s"), filename, ps->lno,
  44. pkgbin_name(ps->pkg, ps->pkgbin, pnaw_nonambig), fmt);
  45. else
  46. sprintf(msg, _("parsing file '%.255s' near line %d:\n"
  47. " %.255s"), filename, ps->lno, fmt);
  48. return msg;
  49. }
  50. void
  51. parse_error(struct parsedb_state *ps, const char *fmt, ...)
  52. {
  53. va_list args;
  54. va_start(args, fmt);
  55. ohshitv(parse_error_msg(ps, fmt), args);
  56. }
  57. void
  58. parse_warn(struct parsedb_state *ps, const char *fmt, ...)
  59. {
  60. va_list args;
  61. va_start(args, fmt);
  62. warningv(parse_error_msg(ps, fmt), args);
  63. va_end(args);
  64. }
  65. const struct namevalue booleaninfos[] = {
  66. NAMEVALUE_DEF("no", false),
  67. NAMEVALUE_DEF("yes", true),
  68. { .name = NULL }
  69. };
  70. const struct namevalue multiarchinfos[] = {
  71. NAMEVALUE_DEF("no", multiarch_no),
  72. NAMEVALUE_DEF("same", multiarch_same),
  73. NAMEVALUE_DEF("allowed", multiarch_allowed),
  74. NAMEVALUE_DEF("foreign", multiarch_foreign),
  75. { .name = NULL }
  76. };
  77. const struct namevalue priorityinfos[] = {
  78. NAMEVALUE_DEF("required", pri_required),
  79. NAMEVALUE_DEF("important", pri_important),
  80. NAMEVALUE_DEF("standard", pri_standard),
  81. NAMEVALUE_DEF("optional", pri_optional),
  82. NAMEVALUE_DEF("extra", pri_extra),
  83. NAMEVALUE_FALLBACK_DEF("this is a bug - please report", pri_other),
  84. NAMEVALUE_DEF("unknown", pri_unknown),
  85. { .name = NULL }
  86. };
  87. const struct namevalue statusinfos[] = {
  88. NAMEVALUE_DEF("not-installed", stat_notinstalled),
  89. NAMEVALUE_DEF("config-files", stat_configfiles),
  90. NAMEVALUE_DEF("half-installed", stat_halfinstalled),
  91. NAMEVALUE_DEF("unpacked", stat_unpacked),
  92. NAMEVALUE_DEF("half-configured", stat_halfconfigured),
  93. NAMEVALUE_DEF("triggers-awaited", stat_triggersawaited),
  94. NAMEVALUE_DEF("triggers-pending", stat_triggerspending),
  95. NAMEVALUE_DEF("installed", stat_installed),
  96. { .name = NULL }
  97. };
  98. const struct namevalue eflaginfos[] = {
  99. NAMEVALUE_DEF("ok", eflag_ok),
  100. NAMEVALUE_DEF("reinstreq", eflag_reinstreq),
  101. { .name = NULL }
  102. };
  103. const struct namevalue wantinfos[] = {
  104. NAMEVALUE_DEF("unknown", want_unknown),
  105. NAMEVALUE_DEF("install", want_install),
  106. NAMEVALUE_DEF("hold", want_hold),
  107. NAMEVALUE_DEF("deinstall", want_deinstall),
  108. NAMEVALUE_DEF("purge", want_purge),
  109. { .name = NULL }
  110. };
  111. const char *
  112. pkg_name_is_illegal(const char *p)
  113. {
  114. /* FIXME: _ is deprecated, remove sometime. */
  115. static const char alsoallowed[] = "-+._";
  116. static char buf[150];
  117. int c;
  118. if (!*p) return _("may not be empty string");
  119. if (!isalnum(*p))
  120. return _("must start with an alphanumeric character");
  121. while ((c = *p++) != '\0')
  122. if (!isalnum(c) && !strchr(alsoallowed,c)) break;
  123. if (!c) return NULL;
  124. snprintf(buf, sizeof(buf), _(
  125. "character `%c' not allowed (only letters, digits and characters `%s')"),
  126. c, alsoallowed);
  127. return buf;
  128. }
  129. void varbufversion
  130. (struct varbuf *vb,
  131. const struct dpkg_version *version,
  132. enum versiondisplayepochwhen vdew)
  133. {
  134. switch (vdew) {
  135. case vdew_never:
  136. break;
  137. case vdew_nonambig:
  138. if (!version->epoch &&
  139. (!version->version || !strchr(version->version,':')) &&
  140. (!version->revision || !strchr(version->revision,':'))) break;
  141. /* Fall through. */
  142. case vdew_always:
  143. varbuf_printf(vb, "%u:", version->epoch);
  144. break;
  145. default:
  146. internerr("unknown versiondisplayepochwhen '%d'", vdew);
  147. }
  148. if (version->version)
  149. varbuf_add_str(vb, version->version);
  150. if (str_is_set(version->revision)) {
  151. varbuf_add_char(vb, '-');
  152. varbuf_add_str(vb, version->revision);
  153. }
  154. }
  155. const char *versiondescribe
  156. (const struct dpkg_version *version,
  157. enum versiondisplayepochwhen vdew)
  158. {
  159. static struct varbuf bufs[10];
  160. static int bufnum=0;
  161. struct varbuf *vb;
  162. if (!dpkg_version_is_informative(version))
  163. return C_("version", "<none>");
  164. vb= &bufs[bufnum]; bufnum++; if (bufnum == 10) bufnum= 0;
  165. varbuf_reset(vb);
  166. varbufversion(vb,version,vdew);
  167. varbuf_end_str(vb);
  168. return vb->buf;
  169. }
  170. /**
  171. * Parse a version string and check for invalid syntax.
  172. *
  173. * Distinguish between lax (warnings) and strict (error) parsing.
  174. *
  175. * @param rversion The parsed version.
  176. * @param string The version string to parse.
  177. * @param err The warning or error message if any.
  178. *
  179. * @retval 0 On success.
  180. * @retval -1 On failure, and err is set accordingly.
  181. */
  182. int
  183. parseversion(struct dpkg_version *rversion, const char *string,
  184. struct dpkg_error *err)
  185. {
  186. char *hyphen, *colon, *eepochcolon;
  187. const char *end, *ptr;
  188. if (!*string)
  189. return dpkg_put_error(err, _("version string is empty"));
  190. /* Trim leading and trailing space. */
  191. while (*string && isblank(*string))
  192. string++;
  193. /* String now points to the first non-whitespace char. */
  194. end = string;
  195. /* Find either the end of the string, or a whitespace char. */
  196. while (*end && !isblank(*end))
  197. end++;
  198. /* Check for extra chars after trailing space. */
  199. ptr = end;
  200. while (*ptr && isblank(*ptr))
  201. ptr++;
  202. if (*ptr)
  203. return dpkg_put_error(err, _("version string has embedded spaces"));
  204. colon= strchr(string,':');
  205. if (colon) {
  206. long epoch;
  207. errno = 0;
  208. epoch = strtol(string, &eepochcolon, 10);
  209. if (colon != eepochcolon)
  210. return dpkg_put_error(err, _("epoch in version is not number"));
  211. if (epoch < 0)
  212. return dpkg_put_error(err, _("epoch in version is negative"));
  213. if (epoch > INT_MAX || errno == ERANGE)
  214. return dpkg_put_error(err, _("epoch in version is too big"));
  215. if (!*++colon)
  216. return dpkg_put_error(err, _("nothing after colon in version number"));
  217. string= colon;
  218. rversion->epoch= epoch;
  219. } else {
  220. rversion->epoch= 0;
  221. }
  222. rversion->version= nfstrnsave(string,end-string);
  223. hyphen= strrchr(rversion->version,'-');
  224. if (hyphen)
  225. *hyphen++ = '\0';
  226. rversion->revision= hyphen ? hyphen : "";
  227. /* XXX: Would be faster to use something like cisversion and cisrevision. */
  228. ptr = rversion->version;
  229. if (*ptr && !cisdigit(*ptr++))
  230. return dpkg_put_warn(err, _("version number does not start with digit"));
  231. for (; *ptr; ptr++) {
  232. if (!cisdigit(*ptr) && !cisalpha(*ptr) && strchr(".-+~:", *ptr) == NULL)
  233. return dpkg_put_warn(err, _("invalid character in version number"));
  234. }
  235. for (ptr = rversion->revision; *ptr; ptr++) {
  236. if (!cisdigit(*ptr) && !cisalpha(*ptr) && strchr(".+~", *ptr) == NULL)
  237. return dpkg_put_warn(err, _("invalid character in revision number"));
  238. }
  239. return 0;
  240. }
  241. /**
  242. * Parse a version string coming from a database file.
  243. *
  244. * It parses a version string, and prints a warning or an error depending
  245. * on the parse options.
  246. *
  247. * @param ps The parsedb state.
  248. * @param version The version to parse into.
  249. * @param value The version string to parse from.
  250. * @param fmt The error format string.
  251. */
  252. void
  253. parse_db_version(struct parsedb_state *ps, struct dpkg_version *version,
  254. const char *value, const char *fmt, ...)
  255. {
  256. struct dpkg_error err;
  257. va_list args;
  258. char buf[1000];
  259. if (parseversion(version, value, &err) == 0)
  260. return;
  261. va_start(args, fmt);
  262. vsnprintf(buf, sizeof(buf), fmt, args);
  263. va_end(args);
  264. if (err.type == DPKG_MSG_WARN && (ps->flags & pdb_lax_version_parser))
  265. parse_warn(ps, "%s: %.250s", buf, err.str);
  266. else
  267. parse_error(ps, "%s: %.250s", buf, err.str);
  268. dpkg_error_destroy(&err);
  269. }
  270. void
  271. parse_must_have_field(struct parsedb_state *ps,
  272. const char *value, const char *what)
  273. {
  274. if (!value)
  275. parse_error(ps, _("missing %s"), what);
  276. else if (!*value)
  277. parse_error(ps, _("empty value for %s"), what);
  278. }
  279. void
  280. parse_ensure_have_field(struct parsedb_state *ps,
  281. const char **value, const char *what)
  282. {
  283. static const char empty[] = "";
  284. if (!*value) {
  285. parse_warn(ps, _("missing %s"), what);
  286. *value = empty;
  287. } else if (!**value) {
  288. parse_warn(ps, _("empty value for %s"), what);
  289. }
  290. }