select.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /*
  2. * dpkg - main program for package management
  3. * select.c - by-hand (rather than dselect-based) package selection
  4. *
  5. * Copyright © 1995,1996 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 <fnmatch.h>
  23. #include <ctype.h>
  24. #include <string.h>
  25. #include <stdlib.h>
  26. #include <stdio.h>
  27. #include <dpkg/i18n.h>
  28. #include <dpkg/dpkg.h>
  29. #include <dpkg/dpkg-db.h>
  30. #include <dpkg/pkg-array.h>
  31. #include <dpkg/myopt.h>
  32. #include "filesdb.h"
  33. #include "main.h"
  34. static void getsel1package(struct pkginfo *pkg) {
  35. int l;
  36. if (pkg->want == want_unknown) return;
  37. l= strlen(pkg->name); l >>= 3; l= 6-l; if (l<1) l=1;
  38. printf("%s%.*s%s\n",pkg->name,l,"\t\t\t\t\t\t",wantinfos[pkg->want].name);
  39. }
  40. void getselections(const char *const *argv) {
  41. struct pkg_array array;
  42. struct pkginfo *pkg;
  43. const char *thisarg;
  44. int i, found;
  45. modstatdb_init(admindir,msdbrw_readonly);
  46. pkg_array_init_from_db(&array);
  47. pkg_array_sort(&array, pkg_sorter_by_name);
  48. if (!*argv) {
  49. for (i = 0; i < array.n_pkgs; i++) {
  50. pkg = array.pkgs[i];
  51. if (pkg->status == stat_notinstalled) continue;
  52. getsel1package(pkg);
  53. }
  54. } else {
  55. while ((thisarg= *argv++)) {
  56. found= 0;
  57. for (i = 0; i < array.n_pkgs; i++) {
  58. pkg = array.pkgs[i];
  59. if (fnmatch(thisarg,pkg->name,0)) continue;
  60. getsel1package(pkg); found++;
  61. }
  62. if (!found)
  63. fprintf(stderr,_("No packages found matching %s.\n"),thisarg);
  64. }
  65. }
  66. m_output(stdout, _("<standard output>"));
  67. m_output(stderr, _("<standard error>"));
  68. pkg_array_free(&array);
  69. }
  70. void setselections(const char *const *argv) {
  71. const struct namevalue *nvp;
  72. struct pkginfo *pkg;
  73. const char *e;
  74. int c, lno;
  75. struct varbuf namevb = VARBUF_INIT;
  76. struct varbuf selvb = VARBUF_INIT;
  77. if (*argv)
  78. badusage(_("--%s takes no arguments"), cipaction->olong);
  79. modstatdb_init(admindir,msdbrw_write);
  80. lno= 1;
  81. for (;;) {
  82. varbufreset(&namevb);
  83. varbufreset(&selvb);
  84. do { c= getchar(); if (c == '\n') lno++; } while (c != EOF && isspace(c));
  85. if (c == EOF) break;
  86. if (c == '#') {
  87. do { c= getchar(); if (c == '\n') lno++; } while (c != EOF && c != '\n');
  88. continue;
  89. }
  90. while (!isspace(c)) {
  91. varbufaddc(&namevb,c);
  92. c= getchar();
  93. if (c == EOF) ohshit(_("unexpected eof in package name at line %d"),lno);
  94. if (c == '\n') ohshit(_("unexpected end of line in package name at line %d"),lno);
  95. }
  96. while (c != EOF && isspace(c)) {
  97. c= getchar();
  98. if (c == EOF) ohshit(_("unexpected eof after package name at line %d"),lno);
  99. if (c == '\n') ohshit(_("unexpected end of line after package name at line %d"),lno);
  100. }
  101. while (c != EOF && !isspace(c)) {
  102. varbufaddc(&selvb,c);
  103. c= getchar();
  104. }
  105. while (c != EOF && c != '\n') {
  106. c= getchar();
  107. if (!isspace(c))
  108. ohshit(_("unexpected data after package and selection at line %d"),lno);
  109. }
  110. varbufaddc(&namevb,0);
  111. varbufaddc(&selvb,0);
  112. e = illegal_packagename(namevb.buf, NULL);
  113. if (e) ohshit(_("illegal package name at line %d: %.250s"),lno,e);
  114. for (nvp=wantinfos; nvp->name && strcmp(nvp->name,selvb.buf); nvp++);
  115. if (!nvp->name) ohshit(_("unknown wanted status at line %d: %.250s"),lno,selvb.buf);
  116. pkg= findpackage(namevb.buf);
  117. pkg->want= nvp->value;
  118. if (c == EOF) break;
  119. lno++;
  120. }
  121. if (ferror(stdin)) ohshite(_("read error on standard input"));
  122. modstatdb_shutdown();
  123. varbuffree(&namevb);
  124. varbuffree(&selvb);
  125. }
  126. void clearselections(const char *const *argv)
  127. {
  128. struct pkgiterator *it;
  129. struct pkginfo *pkg;
  130. if (*argv)
  131. badusage(_("--%s takes no arguments"), cipaction->olong);
  132. modstatdb_init(admindir, msdbrw_write);
  133. it = iterpkgstart();
  134. while ((pkg = iterpkgnext(it))) {
  135. if (!pkg->installed.essential)
  136. pkg->want = want_deinstall;
  137. }
  138. iterpkgend(it);
  139. modstatdb_shutdown();
  140. }