select.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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_destroy(&array);
  69. }
  70. void setselections(const char *const *argv) {
  71. const struct namevalue *nv;
  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 | msdbrw_available);
  80. lno= 1;
  81. for (;;) {
  82. do { c= getchar(); if (c == '\n') lno++; } while (c != EOF && isspace(c));
  83. if (c == EOF) break;
  84. if (c == '#') {
  85. do { c= getchar(); if (c == '\n') lno++; } while (c != EOF && c != '\n');
  86. continue;
  87. }
  88. varbuf_reset(&namevb);
  89. while (!isspace(c)) {
  90. varbuf_add_char(&namevb, c);
  91. c= getchar();
  92. if (c == EOF) ohshit(_("unexpected eof in package name at line %d"),lno);
  93. if (c == '\n') ohshit(_("unexpected end of line in package name at line %d"),lno);
  94. }
  95. varbuf_end_str(&namevb);
  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. varbuf_reset(&selvb);
  102. while (c != EOF && !isspace(c)) {
  103. varbuf_add_char(&selvb, c);
  104. c= getchar();
  105. }
  106. varbuf_end_str(&selvb);
  107. while (c != EOF && c != '\n') {
  108. c= getchar();
  109. if (!isspace(c))
  110. ohshit(_("unexpected data after package and selection at line %d"),lno);
  111. }
  112. e = pkg_name_is_illegal(namevb.buf, NULL);
  113. if (e) ohshit(_("illegal package name at line %d: %.250s"),lno,e);
  114. nv = namevalue_find_by_name(wantinfos, selvb.buf);
  115. if (nv == NULL)
  116. ohshit(_("unknown wanted status at line %d: %.250s"), lno, selvb.buf);
  117. pkg = pkg_db_find(namevb.buf);
  118. pkg->want = nv->value;
  119. if (c == EOF) break;
  120. lno++;
  121. }
  122. if (ferror(stdin)) ohshite(_("read error on standard input"));
  123. modstatdb_shutdown();
  124. varbuf_destroy(&namevb);
  125. varbuf_destroy(&selvb);
  126. }
  127. void clearselections(const char *const *argv)
  128. {
  129. struct pkgiterator *it;
  130. struct pkginfo *pkg;
  131. if (*argv)
  132. badusage(_("--%s takes no arguments"), cipaction->olong);
  133. modstatdb_init(admindir, msdbrw_write);
  134. it = pkg_db_iter_new();
  135. while ((pkg = pkg_db_iter_next(it))) {
  136. if (!pkg->installed.essential)
  137. pkg->want = want_deinstall;
  138. }
  139. pkg_db_iter_free(it);
  140. modstatdb_shutdown();
  141. }