parsehelp.c 9.8 KB

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