select.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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-2012 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 <http://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 == want_unknown) return;
  44. pkgname = pkg_name(pkg, pnaw_nonambig);
  45. l = strlen(pkgname);
  46. l >>= 3;
  47. l = 6 - l;
  48. if (l < 1)
  49. l = 1;
  50. printf("%s%.*s%s\n", pkgname, l, "\t\t\t\t\t\t", wantinfos[pkg->want].name);
  51. }
  52. int
  53. getselections(const char *const *argv)
  54. {
  55. struct pkg_array array;
  56. struct pkginfo *pkg;
  57. const char *thisarg;
  58. int i, found;
  59. modstatdb_open(msdbrw_readonly);
  60. pkg_array_init_from_db(&array);
  61. pkg_array_sort(&array, pkg_sorter_by_nonambig_name_arch);
  62. if (!*argv) {
  63. for (i = 0; i < array.n_pkgs; i++) {
  64. pkg = array.pkgs[i];
  65. if (pkg->status == stat_notinstalled) continue;
  66. getsel1package(pkg);
  67. }
  68. } else {
  69. while ((thisarg= *argv++)) {
  70. struct pkg_spec pkgspec;
  71. found= 0;
  72. pkg_spec_init(&pkgspec, psf_patterns | psf_arch_def_wildcard);
  73. pkg_spec_parse(&pkgspec, thisarg);
  74. for (i = 0; i < array.n_pkgs; i++) {
  75. pkg = array.pkgs[i];
  76. if (!pkg_spec_match_pkg(&pkgspec, pkg, &pkg->installed))
  77. continue;
  78. getsel1package(pkg); found++;
  79. }
  80. if (!found)
  81. fprintf(stderr,_("No packages found matching %s.\n"),thisarg);
  82. pkg_spec_destroy(&pkgspec);
  83. }
  84. }
  85. m_output(stdout, _("<standard output>"));
  86. m_output(stderr, _("<standard error>"));
  87. pkg_array_destroy(&array);
  88. return 0;
  89. }
  90. int
  91. setselections(const char *const *argv)
  92. {
  93. const struct namevalue *nv;
  94. struct pkginfo *pkg;
  95. int c, lno;
  96. struct varbuf namevb = VARBUF_INIT;
  97. struct varbuf selvb = VARBUF_INIT;
  98. if (*argv)
  99. badusage(_("--%s takes no arguments"), cipaction->olong);
  100. modstatdb_open(msdbrw_write | msdbrw_available_readonly);
  101. pkg_infodb_upgrade();
  102. lno= 1;
  103. for (;;) {
  104. struct dpkg_error err;
  105. do { c= getchar(); if (c == '\n') lno++; } while (c != EOF && isspace(c));
  106. if (c == EOF) break;
  107. if (c == '#') {
  108. do { c= getchar(); if (c == '\n') lno++; } while (c != EOF && c != '\n');
  109. continue;
  110. }
  111. varbuf_reset(&namevb);
  112. while (!isspace(c)) {
  113. varbuf_add_char(&namevb, c);
  114. c= getchar();
  115. if (c == EOF) ohshit(_("unexpected eof in package name at line %d"),lno);
  116. if (c == '\n') ohshit(_("unexpected end of line in package name at line %d"),lno);
  117. }
  118. varbuf_end_str(&namevb);
  119. while (c != EOF && isspace(c)) {
  120. c= getchar();
  121. if (c == EOF) ohshit(_("unexpected eof after package name at line %d"),lno);
  122. if (c == '\n') ohshit(_("unexpected end of line after package name at line %d"),lno);
  123. }
  124. varbuf_reset(&selvb);
  125. while (c != EOF && !isspace(c)) {
  126. varbuf_add_char(&selvb, c);
  127. c= getchar();
  128. }
  129. varbuf_end_str(&selvb);
  130. while (c != EOF && c != '\n') {
  131. c= getchar();
  132. if (!isspace(c))
  133. ohshit(_("unexpected data after package and selection at line %d"),lno);
  134. }
  135. pkg = pkg_spec_parse_pkg(namevb.buf, &err);
  136. if (pkg == NULL)
  137. ohshit(_("illegal package name at line %d: %.250s"), lno, err.str);
  138. if (!pkg_is_informative(pkg, &pkg->installed) &&
  139. !pkg_is_informative(pkg, &pkg->available)) {
  140. warning(_("package not in database at line %d: %.250s"), lno, namevb.buf);
  141. continue;
  142. }
  143. nv = namevalue_find_by_name(wantinfos, selvb.buf);
  144. if (nv == NULL)
  145. ohshit(_("unknown wanted status at line %d: %.250s"), lno, selvb.buf);
  146. pkg_set_want(pkg, nv->value);
  147. if (c == EOF) break;
  148. lno++;
  149. }
  150. if (ferror(stdin)) ohshite(_("read error on standard input"));
  151. modstatdb_shutdown();
  152. varbuf_destroy(&namevb);
  153. varbuf_destroy(&selvb);
  154. return 0;
  155. }
  156. int
  157. clearselections(const char *const *argv)
  158. {
  159. struct pkgiterator *it;
  160. struct pkginfo *pkg;
  161. if (*argv)
  162. badusage(_("--%s takes no arguments"), cipaction->olong);
  163. modstatdb_open(msdbrw_write);
  164. pkg_infodb_upgrade();
  165. it = pkg_db_iter_new();
  166. while ((pkg = pkg_db_iter_next_pkg(it))) {
  167. if (!pkg->installed.essential)
  168. pkg_set_want(pkg, want_deinstall);
  169. }
  170. pkg_db_iter_free(it);
  171. modstatdb_shutdown();
  172. return 0;
  173. }