parsehelp.c 10.0 KB

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