select.c 5.9 KB

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