select.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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/options.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. int
  41. getselections(const char *const *argv)
  42. {
  43. struct pkg_array array;
  44. struct pkginfo *pkg;
  45. const char *thisarg;
  46. int i, found;
  47. modstatdb_open(msdbrw_readonly);
  48. pkg_array_init_from_db(&array);
  49. pkg_array_sort(&array, pkg_sorter_by_name);
  50. if (!*argv) {
  51. for (i = 0; i < array.n_pkgs; i++) {
  52. pkg = array.pkgs[i];
  53. if (pkg->status == stat_notinstalled) continue;
  54. getsel1package(pkg);
  55. }
  56. } else {
  57. while ((thisarg= *argv++)) {
  58. found= 0;
  59. for (i = 0; i < array.n_pkgs; i++) {
  60. pkg = array.pkgs[i];
  61. if (fnmatch(thisarg,pkg->name,0)) continue;
  62. getsel1package(pkg); found++;
  63. }
  64. if (!found)
  65. fprintf(stderr,_("No packages found matching %s.\n"),thisarg);
  66. }
  67. }
  68. m_output(stdout, _("<standard output>"));
  69. m_output(stderr, _("<standard error>"));
  70. pkg_array_destroy(&array);
  71. return 0;
  72. }
  73. int
  74. setselections(const char *const *argv)
  75. {
  76. const struct namevalue *nv;
  77. struct pkginfo *pkg;
  78. const char *e;
  79. int c, lno;
  80. struct varbuf namevb = VARBUF_INIT;
  81. struct varbuf selvb = VARBUF_INIT;
  82. if (*argv)
  83. badusage(_("--%s takes no arguments"), cipaction->olong);
  84. modstatdb_open(msdbrw_write | msdbrw_available_readonly);
  85. lno= 1;
  86. for (;;) {
  87. do { c= getchar(); if (c == '\n') lno++; } while (c != EOF && isspace(c));
  88. if (c == EOF) break;
  89. if (c == '#') {
  90. do { c= getchar(); if (c == '\n') lno++; } while (c != EOF && c != '\n');
  91. continue;
  92. }
  93. varbuf_reset(&namevb);
  94. while (!isspace(c)) {
  95. varbuf_add_char(&namevb, c);
  96. c= getchar();
  97. if (c == EOF) ohshit(_("unexpected eof in package name at line %d"),lno);
  98. if (c == '\n') ohshit(_("unexpected end of line in package name at line %d"),lno);
  99. }
  100. varbuf_end_str(&namevb);
  101. while (c != EOF && isspace(c)) {
  102. c= getchar();
  103. if (c == EOF) ohshit(_("unexpected eof after package name at line %d"),lno);
  104. if (c == '\n') ohshit(_("unexpected end of line after package name at line %d"),lno);
  105. }
  106. varbuf_reset(&selvb);
  107. while (c != EOF && !isspace(c)) {
  108. varbuf_add_char(&selvb, c);
  109. c= getchar();
  110. }
  111. varbuf_end_str(&selvb);
  112. while (c != EOF && c != '\n') {
  113. c= getchar();
  114. if (!isspace(c))
  115. ohshit(_("unexpected data after package and selection at line %d"),lno);
  116. }
  117. e = pkg_name_is_illegal(namevb.buf, NULL);
  118. if (e) ohshit(_("illegal package name at line %d: %.250s"),lno,e);
  119. nv = namevalue_find_by_name(wantinfos, selvb.buf);
  120. if (nv == NULL)
  121. ohshit(_("unknown wanted status at line %d: %.250s"), lno, selvb.buf);
  122. pkg = pkg_db_find(namevb.buf);
  123. pkg->want = nv->value;
  124. if (c == EOF) break;
  125. lno++;
  126. }
  127. if (ferror(stdin)) ohshite(_("read error on standard input"));
  128. modstatdb_shutdown();
  129. varbuf_destroy(&namevb);
  130. varbuf_destroy(&selvb);
  131. return 0;
  132. }
  133. int
  134. clearselections(const char *const *argv)
  135. {
  136. struct pkgiterator *it;
  137. struct pkginfo *pkg;
  138. if (*argv)
  139. badusage(_("--%s takes no arguments"), cipaction->olong);
  140. modstatdb_open(msdbrw_write);
  141. it = pkg_db_iter_new();
  142. while ((pkg = pkg_db_iter_next(it))) {
  143. if (!pkg->installed.essential)
  144. pkg->want = want_deinstall;
  145. }
  146. pkg_db_iter_free(it);
  147. modstatdb_shutdown();
  148. return 0;
  149. }