parsehelp.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  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 <https://www.gnu.org/licenses/>.
  20. */
  21. #include <config.h>
  22. #include <compat.h>
  23. #include <errno.h>
  24. #include <limits.h>
  25. #include <string.h>
  26. #include <stdlib.h>
  27. #include <stdio.h>
  28. #include <dpkg/i18n.h>
  29. #include <dpkg/c-ctype.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. char pkgname[256];
  43. str_escape_fmt(pkgname, pkgbin_name(ps->pkg, ps->pkgbin, pnaw_nonambig),
  44. sizeof(pkgname));
  45. sprintf(msg, _("parsing file '%.255s' near line %d package '%.255s':\n"
  46. " %.255s"), filename, ps->lno, pkgname, fmt);
  47. } else
  48. sprintf(msg, _("parsing file '%.255s' near line %d:\n"
  49. " %.255s"), filename, ps->lno, fmt);
  50. return msg;
  51. }
  52. void
  53. parse_error(struct parsedb_state *ps, const char *fmt, ...)
  54. {
  55. va_list args;
  56. va_start(args, fmt);
  57. ohshitv(parse_error_msg(ps, fmt), args);
  58. }
  59. void
  60. parse_warn(struct parsedb_state *ps, const char *fmt, ...)
  61. {
  62. va_list args;
  63. va_start(args, fmt);
  64. warningv(parse_error_msg(ps, fmt), args);
  65. va_end(args);
  66. }
  67. const struct fieldinfo *
  68. find_field_info(const struct fieldinfo *fields, const char *fieldname)
  69. {
  70. const struct fieldinfo *field;
  71. for (field = fields; field->name; field++)
  72. if (strcasecmp(field->name, fieldname) == 0)
  73. return field;
  74. return NULL;
  75. }
  76. const struct arbitraryfield *
  77. find_arbfield_info(const struct arbitraryfield *arbs, const char *fieldname)
  78. {
  79. const struct arbitraryfield *arbfield;
  80. for (arbfield = arbs; arbfield; arbfield = arbfield->next)
  81. if (strcasecmp(arbfield->name, fieldname) == 0)
  82. return arbfield;
  83. return NULL;
  84. }
  85. const char *
  86. pkg_name_is_illegal(const char *p)
  87. {
  88. /* FIXME: _ is deprecated, remove sometime. */
  89. static const char alsoallowed[] = "-+._";
  90. static char buf[150];
  91. int c;
  92. if (!*p) return _("may not be empty string");
  93. if (!c_isalnum(*p))
  94. return _("must start with an alphanumeric character");
  95. while ((c = *p++) != '\0')
  96. if (!c_isalnum(c) && !strchr(alsoallowed, c))
  97. break;
  98. if (!c) return NULL;
  99. snprintf(buf, sizeof(buf), _(
  100. "character `%c' not allowed (only letters, digits and characters `%s')"),
  101. c, alsoallowed);
  102. return buf;
  103. }
  104. void varbufversion
  105. (struct varbuf *vb,
  106. const struct dpkg_version *version,
  107. enum versiondisplayepochwhen vdew)
  108. {
  109. switch (vdew) {
  110. case vdew_never:
  111. break;
  112. case vdew_nonambig:
  113. if (!version->epoch &&
  114. (!version->version || !strchr(version->version,':')) &&
  115. (!version->revision || !strchr(version->revision,':'))) break;
  116. /* Fall through. */
  117. case vdew_always:
  118. varbuf_printf(vb, "%u:", version->epoch);
  119. break;
  120. default:
  121. internerr("unknown versiondisplayepochwhen '%d'", vdew);
  122. }
  123. if (version->version)
  124. varbuf_add_str(vb, version->version);
  125. if (str_is_set(version->revision)) {
  126. varbuf_add_char(vb, '-');
  127. varbuf_add_str(vb, version->revision);
  128. }
  129. }
  130. const char *versiondescribe
  131. (const struct dpkg_version *version,
  132. enum versiondisplayepochwhen vdew)
  133. {
  134. static struct varbuf bufs[10];
  135. static int bufnum=0;
  136. struct varbuf *vb;
  137. if (!dpkg_version_is_informative(version))
  138. return C_("version", "<none>");
  139. vb= &bufs[bufnum]; bufnum++; if (bufnum == 10) bufnum= 0;
  140. varbuf_reset(vb);
  141. varbufversion(vb,version,vdew);
  142. varbuf_end_str(vb);
  143. return vb->buf;
  144. }
  145. /**
  146. * Parse a version string and check for invalid syntax.
  147. *
  148. * Distinguish between lax (warnings) and strict (error) parsing.
  149. *
  150. * @param rversion The parsed version.
  151. * @param string The version string to parse.
  152. * @param err The warning or error message if any.
  153. *
  154. * @retval 0 On success.
  155. * @retval -1 On failure, and err is set accordingly.
  156. */
  157. int
  158. parseversion(struct dpkg_version *rversion, const char *string,
  159. struct dpkg_error *err)
  160. {
  161. char *hyphen, *colon, *eepochcolon;
  162. const char *end, *ptr;
  163. /* Trim leading and trailing space. */
  164. while (*string && c_isblank(*string))
  165. string++;
  166. if (!*string)
  167. return dpkg_put_error(err, _("version string is empty"));
  168. /* String now points to the first non-whitespace char. */
  169. end = string;
  170. /* Find either the end of the string, or a whitespace char. */
  171. while (*end && !c_isblank(*end))
  172. end++;
  173. /* Check for extra chars after trailing space. */
  174. ptr = end;
  175. while (*ptr && c_isblank(*ptr))
  176. ptr++;
  177. if (*ptr)
  178. return dpkg_put_error(err, _("version string has embedded spaces"));
  179. colon= strchr(string,':');
  180. if (colon) {
  181. long epoch;
  182. errno = 0;
  183. epoch = strtol(string, &eepochcolon, 10);
  184. if (colon != eepochcolon)
  185. return dpkg_put_error(err, _("epoch in version is not number"));
  186. if (epoch < 0)
  187. return dpkg_put_error(err, _("epoch in version is negative"));
  188. if (epoch > INT_MAX || errno == ERANGE)
  189. return dpkg_put_error(err, _("epoch in version is too big"));
  190. if (!*++colon)
  191. return dpkg_put_error(err, _("nothing after colon in version number"));
  192. string= colon;
  193. rversion->epoch= epoch;
  194. } else {
  195. rversion->epoch= 0;
  196. }
  197. rversion->version= nfstrnsave(string,end-string);
  198. hyphen= strrchr(rversion->version,'-');
  199. if (hyphen)
  200. *hyphen++ = '\0';
  201. rversion->revision= hyphen ? hyphen : "";
  202. /* XXX: Would be faster to use something like cisversion and cisrevision. */
  203. ptr = rversion->version;
  204. if (*ptr && !c_isdigit(*ptr++))
  205. return dpkg_put_warn(err, _("version number does not start with digit"));
  206. for (; *ptr; ptr++) {
  207. if (!c_isdigit(*ptr) && !c_isalpha(*ptr) && strchr(".-+~:", *ptr) == NULL)
  208. return dpkg_put_warn(err, _("invalid character in version number"));
  209. }
  210. for (ptr = rversion->revision; *ptr; ptr++) {
  211. if (!c_isdigit(*ptr) && !c_isalpha(*ptr) && strchr(".+~", *ptr) == NULL)
  212. return dpkg_put_warn(err, _("invalid character in revision number"));
  213. }
  214. return 0;
  215. }
  216. /**
  217. * Parse a version string coming from a database file.
  218. *
  219. * It parses a version string, and prints a warning or an error depending
  220. * on the parse options.
  221. *
  222. * @param ps The parsedb state.
  223. * @param version The version to parse into.
  224. * @param value The version string to parse from.
  225. * @param fmt The error format string.
  226. */
  227. void
  228. parse_db_version(struct parsedb_state *ps, struct dpkg_version *version,
  229. const char *value, const char *fmt, ...)
  230. {
  231. struct dpkg_error err;
  232. va_list args;
  233. char buf[1000];
  234. if (parseversion(version, value, &err) == 0)
  235. return;
  236. va_start(args, fmt);
  237. vsnprintf(buf, sizeof(buf), fmt, args);
  238. va_end(args);
  239. if (err.type == DPKG_MSG_WARN && (ps->flags & pdb_lax_version_parser))
  240. parse_warn(ps, "%s: %.250s", buf, err.str);
  241. else
  242. parse_error(ps, "%s: %.250s", buf, err.str);
  243. dpkg_error_destroy(&err);
  244. }
  245. void
  246. parse_must_have_field(struct parsedb_state *ps,
  247. const char *value, const char *what)
  248. {
  249. if (!value)
  250. parse_error(ps, _("missing %s"), what);
  251. else if (!*value)
  252. parse_error(ps, _("empty value for %s"), what);
  253. }
  254. void
  255. parse_ensure_have_field(struct parsedb_state *ps,
  256. const char **value, const char *what)
  257. {
  258. static const char empty[] = "";
  259. if (!*value) {
  260. parse_warn(ps, _("missing %s"), what);
  261. *value = empty;
  262. } else if (!**value) {
  263. parse_warn(ps, _("empty value for %s"), what);
  264. }
  265. }