parsehelp.c 9.1 KB

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