select.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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 <ijackson@chiark.greenend.org.uk>
  6. * Copyright © 2006, 2008-2015 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 <string.h>
  27. #include <stdlib.h>
  28. #include <stdio.h>
  29. #include <dpkg/i18n.h>
  30. #include <dpkg/c-ctype.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. modstatdb_shutdown();
  91. return 0;
  92. }
  93. int
  94. setselections(const char *const *argv)
  95. {
  96. enum modstatdb_rw msdbflags;
  97. const struct namevalue *nv;
  98. struct pkginfo *pkg;
  99. int c, lno;
  100. struct varbuf namevb = VARBUF_INIT;
  101. struct varbuf selvb = VARBUF_INIT;
  102. bool db_possibly_outdated = false;
  103. if (*argv)
  104. badusage(_("--%s takes no arguments"), cipaction->olong);
  105. msdbflags = msdbrw_available_readonly;
  106. if (f_noact)
  107. msdbflags |= msdbrw_readonly;
  108. else
  109. msdbflags |= msdbrw_write;
  110. modstatdb_open(msdbflags);
  111. pkg_infodb_upgrade();
  112. lno= 1;
  113. for (;;) {
  114. struct dpkg_error err;
  115. do {
  116. c = getchar();
  117. if (c == '\n')
  118. lno++;
  119. } while (c != EOF && c_isspace(c));
  120. if (c == EOF) break;
  121. if (c == '#') {
  122. do { c= getchar(); if (c == '\n') lno++; } while (c != EOF && c != '\n');
  123. continue;
  124. }
  125. varbuf_reset(&namevb);
  126. while (!c_isspace(c)) {
  127. varbuf_add_char(&namevb, c);
  128. c= getchar();
  129. if (c == EOF)
  130. ohshit(_("unexpected end of file in package name at line %d"), lno);
  131. if (c == '\n') ohshit(_("unexpected end of line in package name at line %d"),lno);
  132. }
  133. varbuf_end_str(&namevb);
  134. while (c != EOF && c_isspace(c)) {
  135. c= getchar();
  136. if (c == EOF)
  137. ohshit(_("unexpected end of file after package name at line %d"), lno);
  138. if (c == '\n') ohshit(_("unexpected end of line after package name at line %d"),lno);
  139. }
  140. varbuf_reset(&selvb);
  141. while (c != EOF && !c_isspace(c)) {
  142. varbuf_add_char(&selvb, c);
  143. c= getchar();
  144. }
  145. varbuf_end_str(&selvb);
  146. while (c != EOF && c != '\n') {
  147. c= getchar();
  148. if (!c_isspace(c))
  149. ohshit(_("unexpected data after package and selection at line %d"),lno);
  150. }
  151. pkg = pkg_spec_parse_pkg(namevb.buf, &err);
  152. if (pkg == NULL)
  153. ohshit(_("illegal package name at line %d: %.250s"), lno, err.str);
  154. if (!pkg_is_informative(pkg, &pkg->installed) &&
  155. !pkg_is_informative(pkg, &pkg->available)) {
  156. db_possibly_outdated = true;
  157. warning(_("package not in status nor available database at line %d: %.250s"), lno, namevb.buf);
  158. continue;
  159. }
  160. nv = namevalue_find_by_name(wantinfos, selvb.buf);
  161. if (nv == NULL)
  162. ohshit(_("unknown wanted status at line %d: %.250s"), lno, selvb.buf);
  163. pkg_set_want(pkg, nv->value);
  164. if (c == EOF) break;
  165. lno++;
  166. }
  167. if (ferror(stdin)) ohshite(_("read error on standard input"));
  168. modstatdb_shutdown();
  169. varbuf_destroy(&namevb);
  170. varbuf_destroy(&selvb);
  171. if (db_possibly_outdated)
  172. warning(_("found unknown packages; this might mean the available database\n"
  173. "is outdated, and needs to be updated through a frontend method;\n"
  174. "please see the FAQ <https://wiki.debian.org/Teams/Dpkg/FAQ>"));
  175. return 0;
  176. }
  177. int
  178. clearselections(const char *const *argv)
  179. {
  180. enum modstatdb_rw msdbflags;
  181. struct pkgiterator *iter;
  182. struct pkginfo *pkg;
  183. if (*argv)
  184. badusage(_("--%s takes no arguments"), cipaction->olong);
  185. if (f_noact)
  186. msdbflags = msdbrw_readonly;
  187. else
  188. msdbflags = msdbrw_write;
  189. modstatdb_open(msdbflags);
  190. pkg_infodb_upgrade();
  191. iter = pkg_db_iter_new();
  192. while ((pkg = pkg_db_iter_next_pkg(iter))) {
  193. if (!pkg->installed.essential)
  194. pkg_set_want(pkg, PKG_WANT_DEINSTALL);
  195. }
  196. pkg_db_iter_free(iter);
  197. modstatdb_shutdown();
  198. return 0;
  199. }