select.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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. *
  7. * This is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as
  9. * published by the Free Software Foundation; either version 2,
  10. * or (at your option) any later version.
  11. *
  12. * This is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public
  18. * License along with dpkg; if not, write to the Free Software
  19. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  20. */
  21. #include <config.h>
  22. #include <compat.h>
  23. #include <fnmatch.h>
  24. #include <ctype.h>
  25. #include <string.h>
  26. #include <stdlib.h>
  27. #include <stdio.h>
  28. #include <dpkg/i18n.h>
  29. #include <dpkg/dpkg.h>
  30. #include <dpkg/dpkg-db.h>
  31. #include <dpkg/pkg-array.h>
  32. #include <dpkg/myopt.h>
  33. #include "filesdb.h"
  34. #include "main.h"
  35. static void getsel1package(struct pkginfo *pkg) {
  36. int l;
  37. if (pkg->want == want_unknown) return;
  38. l= strlen(pkg->name); l >>= 3; l= 6-l; if (l<1) l=1;
  39. printf("%s%.*s%s\n",pkg->name,l,"\t\t\t\t\t\t",wantinfos[pkg->want].name);
  40. }
  41. void getselections(const char *const *argv) {
  42. struct pkg_array array;
  43. struct pkginfo *pkg;
  44. const char *thisarg;
  45. int i, found;
  46. modstatdb_init(admindir,msdbrw_readonly);
  47. pkg_array_init_from_db(&array);
  48. pkg_array_sort(&array, pkg_sorter_by_name);
  49. if (!*argv) {
  50. for (i = 0; i < array.n_pkgs; i++) {
  51. pkg = array.pkgs[i];
  52. if (pkg->status == stat_notinstalled) continue;
  53. getsel1package(pkg);
  54. }
  55. } else {
  56. while ((thisarg= *argv++)) {
  57. found= 0;
  58. for (i = 0; i < array.n_pkgs; i++) {
  59. pkg = array.pkgs[i];
  60. if (fnmatch(thisarg,pkg->name,0)) continue;
  61. getsel1package(pkg); found++;
  62. }
  63. if (!found)
  64. fprintf(stderr,_("No packages found matching %s.\n"),thisarg);
  65. }
  66. }
  67. m_output(stdout, _("<standard output>"));
  68. m_output(stderr, _("<standard error>"));
  69. pkg_array_free(&array);
  70. }
  71. void setselections(const char *const *argv) {
  72. const struct namevalue *nvp;
  73. struct pkginfo *pkg;
  74. const char *e;
  75. int c, lno;
  76. struct varbuf namevb = VARBUF_INIT;
  77. struct varbuf selvb = VARBUF_INIT;
  78. if (*argv)
  79. badusage(_("--%s takes no arguments"), cipaction->olong);
  80. modstatdb_init(admindir,msdbrw_write);
  81. lno= 1;
  82. for (;;) {
  83. varbufreset(&namevb);
  84. varbufreset(&selvb);
  85. do { c= getchar(); if (c == '\n') lno++; } while (c != EOF && isspace(c));
  86. if (c == EOF) break;
  87. if (c == '#') {
  88. do { c= getchar(); if (c == '\n') lno++; } while (c != EOF && c != '\n');
  89. continue;
  90. }
  91. while (!isspace(c)) {
  92. varbufaddc(&namevb,c);
  93. c= getchar();
  94. if (c == EOF) ohshit(_("unexpected eof in package name at line %d"),lno);
  95. if (c == '\n') ohshit(_("unexpected end of line in package name at line %d"),lno);
  96. }
  97. while (c != EOF && isspace(c)) {
  98. c= getchar();
  99. if (c == EOF) ohshit(_("unexpected eof after package name at line %d"),lno);
  100. if (c == '\n') ohshit(_("unexpected end of line after package name at line %d"),lno);
  101. }
  102. while (c != EOF && !isspace(c)) {
  103. varbufaddc(&selvb,c);
  104. c= getchar();
  105. }
  106. while (c != EOF && c != '\n') {
  107. c= getchar();
  108. if (!isspace(c))
  109. ohshit(_("unexpected data after package and selection at line %d"),lno);
  110. }
  111. varbufaddc(&namevb,0);
  112. varbufaddc(&selvb,0);
  113. e = illegal_packagename(namevb.buf, NULL);
  114. if (e) ohshit(_("illegal package name at line %d: %.250s"),lno,e);
  115. for (nvp=wantinfos; nvp->name && strcmp(nvp->name,selvb.buf); nvp++);
  116. if (!nvp->name) ohshit(_("unknown wanted status at line %d: %.250s"),lno,selvb.buf);
  117. pkg= findpackage(namevb.buf);
  118. pkg->want= nvp->value;
  119. if (c == EOF) break;
  120. lno++;
  121. }
  122. if (ferror(stdin)) ohshite(_("read error on standard input"));
  123. modstatdb_shutdown();
  124. varbuffree(&namevb);
  125. varbuffree(&selvb);
  126. }
  127. void clearselections(const char *const *argv)
  128. {
  129. struct pkgiterator *it;
  130. struct pkginfo *pkg;
  131. if (*argv)
  132. badusage(_("--%s takes no arguments"), cipaction->olong);
  133. modstatdb_init(admindir, msdbrw_write);
  134. it = iterpkgstart();
  135. while ((pkg = iterpkgnext(it))) {
  136. if (!pkg->installed.essential)
  137. pkg->want = want_deinstall;
  138. }
  139. iterpkgend(it);
  140. modstatdb_shutdown();
  141. }