select.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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. notice(_("no packages found matching %s"), 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. bool db_possibly_outdated = false;
  99. if (*argv)
  100. badusage(_("--%s takes no arguments"), cipaction->olong);
  101. modstatdb_open(msdbrw_write | msdbrw_available_readonly);
  102. pkg_infodb_upgrade();
  103. lno= 1;
  104. for (;;) {
  105. struct dpkg_error err;
  106. do { c= getchar(); if (c == '\n') lno++; } while (c != EOF && isspace(c));
  107. if (c == EOF) break;
  108. if (c == '#') {
  109. do { c= getchar(); if (c == '\n') lno++; } while (c != EOF && c != '\n');
  110. continue;
  111. }
  112. varbuf_reset(&namevb);
  113. while (!isspace(c)) {
  114. varbuf_add_char(&namevb, c);
  115. c= getchar();
  116. if (c == EOF) ohshit(_("unexpected eof in package name at line %d"),lno);
  117. if (c == '\n') ohshit(_("unexpected end of line in package name at line %d"),lno);
  118. }
  119. varbuf_end_str(&namevb);
  120. while (c != EOF && isspace(c)) {
  121. c= getchar();
  122. if (c == EOF) ohshit(_("unexpected eof after package name at line %d"),lno);
  123. if (c == '\n') ohshit(_("unexpected end of line after package name at line %d"),lno);
  124. }
  125. varbuf_reset(&selvb);
  126. while (c != EOF && !isspace(c)) {
  127. varbuf_add_char(&selvb, c);
  128. c= getchar();
  129. }
  130. varbuf_end_str(&selvb);
  131. while (c != EOF && c != '\n') {
  132. c= getchar();
  133. if (!isspace(c))
  134. ohshit(_("unexpected data after package and selection at line %d"),lno);
  135. }
  136. pkg = pkg_spec_parse_pkg(namevb.buf, &err);
  137. if (pkg == NULL)
  138. ohshit(_("illegal package name at line %d: %.250s"), lno, err.str);
  139. if (!pkg_is_informative(pkg, &pkg->installed) &&
  140. !pkg_is_informative(pkg, &pkg->available)) {
  141. db_possibly_outdated = true;
  142. warning(_("package not in database at line %d: %.250s"), lno, namevb.buf);
  143. continue;
  144. }
  145. nv = namevalue_find_by_name(wantinfos, selvb.buf);
  146. if (nv == NULL)
  147. ohshit(_("unknown wanted status at line %d: %.250s"), lno, selvb.buf);
  148. pkg_set_want(pkg, nv->value);
  149. if (c == EOF) break;
  150. lno++;
  151. }
  152. if (ferror(stdin)) ohshite(_("read error on standard input"));
  153. modstatdb_shutdown();
  154. varbuf_destroy(&namevb);
  155. varbuf_destroy(&selvb);
  156. if (db_possibly_outdated)
  157. warning(_("found unknown packages; this might mean the available database\n"
  158. "is outdated, and needs to be updated through a frontend method"));
  159. return 0;
  160. }
  161. int
  162. clearselections(const char *const *argv)
  163. {
  164. struct pkgiterator *it;
  165. struct pkginfo *pkg;
  166. if (*argv)
  167. badusage(_("--%s takes no arguments"), cipaction->olong);
  168. modstatdb_open(msdbrw_write);
  169. pkg_infodb_upgrade();
  170. it = pkg_db_iter_new();
  171. while ((pkg = pkg_db_iter_next_pkg(it))) {
  172. if (!pkg->installed.essential)
  173. pkg_set_want(pkg, want_deinstall);
  174. }
  175. pkg_db_iter_free(it);
  176. modstatdb_shutdown();
  177. return 0;
  178. }