select.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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. * Copyright © 2006,2008-2014 Guillem Jover <guillem@debian.org>
  7. * Copyright © 2011 Linaro Limited
  8. * Copyright © 2011 Raphaël Hertzog <hertzog@debian.org>
  9. *
  10. * This is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  22. */
  23. #include <config.h>
  24. #include <compat.h>
  25. #include <fnmatch.h>
  26. #include <ctype.h>
  27. #include <string.h>
  28. #include <stdlib.h>
  29. #include <stdio.h>
  30. #include <dpkg/i18n.h>
  31. #include <dpkg/dpkg.h>
  32. #include <dpkg/dpkg-db.h>
  33. #include <dpkg/pkg-array.h>
  34. #include <dpkg/pkg-show.h>
  35. #include <dpkg/pkg-spec.h>
  36. #include <dpkg/options.h>
  37. #include "filesdb.h"
  38. #include "infodb.h"
  39. #include "main.h"
  40. static void getsel1package(struct pkginfo *pkg) {
  41. const char *pkgname;
  42. int l;
  43. if (pkg->want == PKG_WANT_UNKNOWN)
  44. return;
  45. pkgname = pkg_name(pkg, pnaw_nonambig);
  46. l = strlen(pkgname);
  47. l >>= 3;
  48. l = 6 - l;
  49. if (l < 1)
  50. l = 1;
  51. printf("%s%.*s%s\n", pkgname, l, "\t\t\t\t\t\t", pkg_want_name(pkg));
  52. }
  53. int
  54. getselections(const char *const *argv)
  55. {
  56. struct pkg_array array;
  57. struct pkginfo *pkg;
  58. const char *thisarg;
  59. int i, found;
  60. modstatdb_open(msdbrw_readonly);
  61. pkg_array_init_from_db(&array);
  62. pkg_array_sort(&array, pkg_sorter_by_nonambig_name_arch);
  63. if (!*argv) {
  64. for (i = 0; i < array.n_pkgs; i++) {
  65. pkg = array.pkgs[i];
  66. if (pkg->status == PKG_STAT_NOTINSTALLED)
  67. continue;
  68. getsel1package(pkg);
  69. }
  70. } else {
  71. while ((thisarg= *argv++)) {
  72. struct pkg_spec pkgspec;
  73. found= 0;
  74. pkg_spec_init(&pkgspec, PKG_SPEC_PATTERNS | PKG_SPEC_ARCH_WILDCARD);
  75. pkg_spec_parse(&pkgspec, thisarg);
  76. for (i = 0; i < array.n_pkgs; i++) {
  77. pkg = array.pkgs[i];
  78. if (!pkg_spec_match_pkg(&pkgspec, pkg, &pkg->installed))
  79. continue;
  80. getsel1package(pkg); found++;
  81. }
  82. if (!found)
  83. notice(_("no packages found matching %s"), thisarg);
  84. pkg_spec_destroy(&pkgspec);
  85. }
  86. }
  87. m_output(stdout, _("<standard output>"));
  88. m_output(stderr, _("<standard error>"));
  89. pkg_array_destroy(&array);
  90. return 0;
  91. }
  92. int
  93. setselections(const char *const *argv)
  94. {
  95. const struct namevalue *nv;
  96. struct pkginfo *pkg;
  97. int c, lno;
  98. struct varbuf namevb = VARBUF_INIT;
  99. struct varbuf selvb = VARBUF_INIT;
  100. bool db_possibly_outdated = false;
  101. if (*argv)
  102. badusage(_("--%s takes no arguments"), cipaction->olong);
  103. modstatdb_open(msdbrw_write | msdbrw_available_readonly);
  104. pkg_infodb_upgrade();
  105. lno= 1;
  106. for (;;) {
  107. struct dpkg_error err;
  108. do { c= getchar(); if (c == '\n') lno++; } while (c != EOF && isspace(c));
  109. if (c == EOF) break;
  110. if (c == '#') {
  111. do { c= getchar(); if (c == '\n') lno++; } while (c != EOF && c != '\n');
  112. continue;
  113. }
  114. varbuf_reset(&namevb);
  115. while (!isspace(c)) {
  116. varbuf_add_char(&namevb, c);
  117. c= getchar();
  118. if (c == EOF) ohshit(_("unexpected eof in package name at line %d"),lno);
  119. if (c == '\n') ohshit(_("unexpected end of line in package name at line %d"),lno);
  120. }
  121. varbuf_end_str(&namevb);
  122. while (c != EOF && isspace(c)) {
  123. c= getchar();
  124. if (c == EOF) ohshit(_("unexpected eof after package name at line %d"),lno);
  125. if (c == '\n') ohshit(_("unexpected end of line after package name at line %d"),lno);
  126. }
  127. varbuf_reset(&selvb);
  128. while (c != EOF && !isspace(c)) {
  129. varbuf_add_char(&selvb, c);
  130. c= getchar();
  131. }
  132. varbuf_end_str(&selvb);
  133. while (c != EOF && c != '\n') {
  134. c= getchar();
  135. if (!isspace(c))
  136. ohshit(_("unexpected data after package and selection at line %d"),lno);
  137. }
  138. pkg = pkg_spec_parse_pkg(namevb.buf, &err);
  139. if (pkg == NULL)
  140. ohshit(_("illegal package name at line %d: %.250s"), lno, err.str);
  141. if (!pkg_is_informative(pkg, &pkg->installed) &&
  142. !pkg_is_informative(pkg, &pkg->available)) {
  143. db_possibly_outdated = true;
  144. warning(_("package not in database at line %d: %.250s"), lno, namevb.buf);
  145. continue;
  146. }
  147. nv = namevalue_find_by_name(wantinfos, selvb.buf);
  148. if (nv == NULL)
  149. ohshit(_("unknown wanted status at line %d: %.250s"), lno, selvb.buf);
  150. pkg_set_want(pkg, nv->value);
  151. if (c == EOF) break;
  152. lno++;
  153. }
  154. if (ferror(stdin)) ohshite(_("read error on standard input"));
  155. modstatdb_shutdown();
  156. varbuf_destroy(&namevb);
  157. varbuf_destroy(&selvb);
  158. if (db_possibly_outdated)
  159. warning(_("found unknown packages; this might mean the available database\n"
  160. "is outdated, and needs to be updated through a frontend method"));
  161. return 0;
  162. }
  163. int
  164. clearselections(const char *const *argv)
  165. {
  166. struct pkgiterator *it;
  167. struct pkginfo *pkg;
  168. if (*argv)
  169. badusage(_("--%s takes no arguments"), cipaction->olong);
  170. modstatdb_open(msdbrw_write);
  171. pkg_infodb_upgrade();
  172. it = pkg_db_iter_new();
  173. while ((pkg = pkg_db_iter_next_pkg(it))) {
  174. if (!pkg->installed.essential)
  175. pkg_set_want(pkg, PKG_WANT_DEINSTALL);
  176. }
  177. pkg_db_iter_free(it);
  178. modstatdb_shutdown();
  179. return 0;
  180. }