parsehelp.c 9.5 KB

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