parsehelp.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  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 void
  32. parse_error_msg(struct parsedb_state *ps, const struct pkginfo *pigp,
  33. const char *type, char *buf)
  34. {
  35. if (pigp && pigp->name)
  36. sprintf(buf, _("%s, in file '%.255s' near line %d package '%.255s':\n "),
  37. type, ps->filename, ps->lno, pigp->name);
  38. else
  39. sprintf(buf, _("%s, in file '%.255s' near line %d:\n "),
  40. type, ps->filename, ps->lno);
  41. }
  42. void
  43. parse_error(struct parsedb_state *ps,
  44. const struct pkginfo *pigp, const char *fmt, ...)
  45. {
  46. va_list args;
  47. char buf1[768], buf2[1000], *q;
  48. parse_error_msg(ps, pigp, _("parse error"), buf1);
  49. q = str_escape_fmt(buf2, buf1);
  50. strcat(q,fmt);
  51. va_start(args, fmt);
  52. ohshitv(buf2, 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. char buf1[768], buf2[1000], *q;
  60. parse_error_msg(ps, pigp, _("warning"), buf1);
  61. q = str_escape_fmt(buf2, buf1);
  62. strcat(q, fmt);
  63. va_start(args, fmt);
  64. ps->warncount++;
  65. strcat(q, "\n");
  66. if (vfprintf(stderr, buf2, args) == EOF)
  67. ohshite(_("failed to write parsing warning"));
  68. va_end(args);
  69. }
  70. const struct namevalue booleaninfos[] = {
  71. NAMEVALUE_DEF("no", false),
  72. NAMEVALUE_DEF("yes", true),
  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_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, const char **ep)
  111. {
  112. static const char alsoallowed[]= "-+._"; /* _ is deprecated */
  113. static char buf[150];
  114. int c;
  115. if (!*p) return _("may not be empty string");
  116. if (!isalnum(*p)) return _("must start with an alphanumeric");
  117. while ((c = *p++) != '\0')
  118. if (!isalnum(c) && !strchr(alsoallowed,c)) break;
  119. if (!c) return NULL;
  120. if (isspace(c) && ep) {
  121. while (isspace(*p)) p++;
  122. *ep= p; return NULL;
  123. }
  124. snprintf(buf, sizeof(buf), _(
  125. "character `%c' not allowed (only letters, digits and characters `%s')"),
  126. c, alsoallowed);
  127. return buf;
  128. }
  129. const struct nickname nicknames[]= {
  130. /* NB: capitalisation of these strings is important. */
  131. { .nick = "Recommended", .canon = "Recommends" },
  132. { .nick = "Optional", .canon = "Suggests" },
  133. { .nick = "Class", .canon = "Priority" },
  134. { .nick = "Package-Revision", .canon = "Revision" },
  135. { .nick = "Package_Revision", .canon = "Revision" },
  136. { .nick = NULL }
  137. };
  138. bool
  139. informativeversion(const struct versionrevision *version)
  140. {
  141. return (version->epoch ||
  142. (version->version && *version->version) ||
  143. (version->revision && *version->revision));
  144. }
  145. void varbufversion
  146. (struct varbuf *vb,
  147. const struct versionrevision *version,
  148. enum versiondisplayepochwhen vdew)
  149. {
  150. switch (vdew) {
  151. case vdew_never:
  152. break;
  153. case vdew_nonambig:
  154. if (!version->epoch &&
  155. (!version->version || !strchr(version->version,':')) &&
  156. (!version->revision || !strchr(version->revision,':'))) break;
  157. case vdew_always: /* FALL THROUGH */
  158. varbufprintf(vb,"%lu:",version->epoch);
  159. break;
  160. default:
  161. internerr("unknown versiondisplayepochwhen '%d'", vdew);
  162. }
  163. if (version->version) varbufaddstr(vb,version->version);
  164. if (version->revision && *version->revision) {
  165. varbufaddc(vb,'-');
  166. varbufaddstr(vb,version->revision);
  167. }
  168. }
  169. const char *versiondescribe
  170. (const struct versionrevision *version,
  171. enum versiondisplayepochwhen vdew)
  172. {
  173. static struct varbuf bufs[10];
  174. static int bufnum=0;
  175. struct varbuf *vb;
  176. if (!informativeversion(version)) return _("<none>");
  177. vb= &bufs[bufnum]; bufnum++; if (bufnum == 10) bufnum= 0;
  178. varbufreset(vb);
  179. varbufversion(vb,version,vdew);
  180. varbufaddc(vb,0);
  181. return vb->buf;
  182. }
  183. static const char *
  184. parseversion_lax(struct versionrevision *rversion, const char *string)
  185. {
  186. char *hyphen, *colon, *eepochcolon;
  187. const char *end, *ptr;
  188. unsigned long epoch;
  189. if (!*string) return _("version string is empty");
  190. /* trim leading and trailing space */
  191. while (*string && isblank(*string))
  192. string++;
  193. /* string now points to the first non-whitespace char */
  194. end = string;
  195. /* find either the end of the string, or a whitespace char */
  196. while (*end && !isblank(*end))
  197. end++;
  198. /* check for extra chars after trailing space */
  199. ptr = end;
  200. while (*ptr && isblank(*ptr))
  201. ptr++;
  202. if (*ptr) return _("version string has embedded spaces");
  203. colon= strchr(string,':');
  204. if (colon) {
  205. epoch= strtoul(string,&eepochcolon,10);
  206. if (colon != eepochcolon) return _("epoch in version is not number");
  207. if (!*++colon) return _("nothing after colon in version number");
  208. string= colon;
  209. rversion->epoch= epoch;
  210. } else {
  211. rversion->epoch= 0;
  212. }
  213. rversion->version= nfstrnsave(string,end-string);
  214. hyphen= strrchr(rversion->version,'-');
  215. if (hyphen)
  216. *hyphen++ = '\0';
  217. rversion->revision= hyphen ? hyphen : "";
  218. return NULL;
  219. }
  220. /**
  221. * Check for invalid syntax in version structure.
  222. *
  223. * The rest of the syntax has been already checked in parseversion_lax(). So
  224. * we only do the stricter checks here.
  225. *
  226. * @param rversion The version to verify.
  227. *
  228. * @return An error string, or NULL if eveyrthing was ok.
  229. */
  230. static const char *
  231. version_strict_check(struct versionrevision *rversion)
  232. {
  233. const char *ptr;
  234. /* XXX: Would be faster to use something like cisversion and cisrevision. */
  235. for (ptr = rversion->version; *ptr; ptr++) {
  236. if (!cisdigit(*ptr) && !cisalpha(*ptr) && strchr(".-+~:", *ptr) == NULL)
  237. return _("invalid character in version number");
  238. }
  239. for (ptr = rversion->revision; *ptr; ptr++) {
  240. if (!cisdigit(*ptr) && !cisalpha(*ptr) && strchr(".-+~", *ptr) == NULL)
  241. return _("invalid character in revision number");
  242. }
  243. return NULL;
  244. }
  245. const char *
  246. parseversion(struct versionrevision *rversion, const char *string)
  247. {
  248. const char *emsg;
  249. emsg = parseversion_lax(rversion, string);
  250. if (emsg)
  251. return emsg;
  252. return version_strict_check(rversion);
  253. }
  254. /**
  255. * Parse a version string coming from a database file.
  256. *
  257. * It parses a version string, and prints a warning or an error depending
  258. * on the parse options.
  259. *
  260. * @param ps The parsedb state.
  261. * @param pkg The package being parsed.
  262. * @param version The version to parse into.
  263. * @param value The version string to parse from.
  264. * @param fmt The error format string.
  265. */
  266. void
  267. parse_db_version(struct parsedb_state *ps, const struct pkginfo *pkg,
  268. struct versionrevision *version, const char *value,
  269. const char *fmt, ...)
  270. {
  271. const char *msg;
  272. bool warn_msg = false;
  273. msg = parseversion_lax(version, value);
  274. if (msg == NULL) {
  275. msg = version_strict_check(version);
  276. if (ps->flags & pdb_lax_parser)
  277. warn_msg = true;
  278. }
  279. if (msg) {
  280. va_list args;
  281. char buf[1000];
  282. va_start(args, fmt);
  283. vsnprintf(buf, sizeof(buf), fmt, args);
  284. if (warn_msg)
  285. parse_warn(ps, pkg, "%s: %.250s", buf, msg);
  286. else
  287. parse_error(ps, pkg, "%s: %.250s", buf, msg);
  288. va_end(args);
  289. }
  290. }
  291. void
  292. parse_must_have_field(struct parsedb_state *ps,
  293. const struct pkginfo *pigp,
  294. const char *value, const char *what)
  295. {
  296. if (!value)
  297. parse_error(ps, pigp, _("missing %s"), what);
  298. else if (!*value)
  299. parse_error(ps, pigp, _("empty value for %s"), what);
  300. }
  301. void
  302. parse_ensure_have_field(struct parsedb_state *ps,
  303. const struct pkginfo *pigp,
  304. const char **value, const char *what)
  305. {
  306. static const char empty[] = "";
  307. if (!*value) {
  308. parse_warn(ps, pigp, _("missing %s"), what);
  309. *value = empty;
  310. } else if (!**value) {
  311. parse_warn(ps, pigp, _("empty value for %s"), what);
  312. }
  313. }