parsehelp.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /*
  2. * libdpkg - Debian packaging suite library routines
  3. * parsehelp.c - helpful routines for parsing and writing
  4. *
  5. * Copyright (C) 1995 Ian Jackson <iwj10@cus.cam.ac.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
  9. * published by the Free Software Foundation; either version 2,
  10. * or (at your option) any later version.
  11. *
  12. * This is distributed in the hope that it will be useful, but
  13. * 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
  18. * License along with dpkg; if not, write to the Free Software
  19. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  20. */
  21. #include <stdio.h>
  22. #include <ctype.h>
  23. #include <string.h>
  24. #include "config.h"
  25. #include "dpkg.h"
  26. #include "dpkg-db.h"
  27. #include "parsedump.h"
  28. void parseerr(FILE *file, const char *filename, int lno, FILE *warnto, int *warncount,
  29. const struct pkginfo *pigp, int warnonly,
  30. const char *fmt, ...) {
  31. va_list al;
  32. char buf1[768], buf2[1000], *p, *q;
  33. if (file && ferror(file)) ohshite("failed to read `%s' at line %d",filename,lno);
  34. sprintf(buf1, "%s, in file `%.255s' near line %d",
  35. warnonly ? "warning" : "parse error", filename, lno);
  36. if (pigp && pigp->name) {
  37. sprintf(buf2, " package `%.255s'", pigp->name);
  38. strcat(buf1,buf2);
  39. }
  40. for (p=buf1, q=buf2; *p; *q++= *p++) if (*p == '%') *q++= '%';
  41. strcpy(q,":\n "); strcat(q,fmt);
  42. va_start(al,fmt);
  43. if (!warnonly) ohshitv(buf2,al);
  44. if (warncount) (*warncount)++;
  45. if (warnto) {
  46. strcat(q,"\n");
  47. if (vfprintf(warnto,buf2,al) == EOF)
  48. ohshite("failed to write parsing warning");
  49. }
  50. va_end(al);
  51. }
  52. const struct namevalue booleaninfos[]= { /* Note ! These must be in order ! */
  53. { "no", 0 },
  54. { "yes", 1 },
  55. { 0 }
  56. };
  57. const struct namevalue priorityinfos[]= { /* Note ! These must be in order ! */
  58. { "required", pri_required },
  59. { "important", pri_important },
  60. { "standard", pri_standard },
  61. { "recommended", pri_recommended }, /* fixme: obsolete */
  62. { "optional", pri_optional },
  63. { "extra", pri_extra },
  64. { "contrib", pri_contrib }, /* fixme: keep? */
  65. { "this is a bug - please report", pri_other },
  66. { "unknown", pri_unknown },
  67. { "base", pri_required }, /* fixme: alias, remove */
  68. { 0 }
  69. };
  70. const struct namevalue statusinfos[]= { /* Note ! These must be in order ! */
  71. { "not-installed", stat_notinstalled },
  72. { "unpacked", stat_unpacked },
  73. { "half-configured", stat_halfconfigured },
  74. { "installed", stat_installed },
  75. { "half-installed", stat_halfinstalled },
  76. { "config-files", stat_configfiles },
  77. /* These are additional entries for reading only, in any order ... */
  78. { "postinst-failed", stat_halfconfigured }, /* fixme: backwards compat., remove */
  79. { "removal-failed", stat_halfinstalled }, /* fixme: backwards compat., remove */
  80. { 0 }
  81. };
  82. const struct namevalue eflaginfos[]= { /* Note ! These must be in order ! */
  83. { "ok", eflagv_ok },
  84. { "hold", eflagv_hold },
  85. { "reinstreq", eflagv_reinstreq },
  86. { "hold-reinstreq", eflagv_both },
  87. { 0 }
  88. };
  89. const struct namevalue wantinfos[]= { /* Note ! These must be in order ! */
  90. { "unknown", want_unknown },
  91. { "install", want_install },
  92. { "deinstall", want_deinstall },
  93. { "purge", want_purge },
  94. { 0 }
  95. };
  96. const char *illegal_packagename(const char *p, const char **ep) {
  97. static const char alsoallowed[]= "-_+.@:=%";
  98. static char buf[150];
  99. int c;
  100. if (!*p) return "may not be empty string";
  101. if (!isalnum(*p)) return "must start with an alphanumeric";
  102. if (!*++p) return "must be at least two characters";
  103. while ((c= *p++)!=0)
  104. if (!isalnum(c) && !strchr(alsoallowed,c)) break;
  105. if (!c) return 0;
  106. if (isspace(c) && ep) {
  107. while (isspace(*p)) p++;
  108. *ep= p; return 0;
  109. }
  110. sprintf(buf,
  111. "character `%c' not allowed - only letters, digits and %s allowed",
  112. c, alsoallowed);
  113. return buf;
  114. }
  115. const struct nickname nicknames[]= {
  116. /* NB: capitalisation of these strings is important. */
  117. { "Recommended", "Recommends" },
  118. { "Optional", "Suggests" },
  119. { "Class", "Priority" },
  120. { "Package-Revision", "Revision" },
  121. { "Package_Revision", "Revision" },
  122. { 0 }
  123. };
  124. void parsemustfield(FILE *file, const char *filename, int lno,
  125. FILE *warnto, int *warncount,
  126. const struct pkginfo *pigp, int warnonly,
  127. char **value, const char *what) {
  128. if (!*value) {
  129. parseerr(file,filename,lno, warnto,warncount,pigp,warnonly, "missing %s",what);
  130. *value= nfstrsave("");
  131. } else if (!**value) {
  132. parseerr(file,filename,lno, warnto,warncount,pigp,warnonly,
  133. "empty value for %s",what);
  134. }
  135. }
  136. const char *skip_slash_dotslash(const char *p) {
  137. while (p[0] == '/' || (p[0] == '.' && p[1] == '/')) p++;
  138. return p;
  139. }