parsehelp.c 8.8 KB

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