select.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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 <dpkg/i18n.h>
  24. #include <stdio.h>
  25. #include <stdlib.h>
  26. #include <string.h>
  27. #include <fnmatch.h>
  28. #include <ctype.h>
  29. #include <dpkg/dpkg.h>
  30. #include <dpkg/dpkg-db.h>
  31. #include <dpkg/myopt.h>
  32. #include "pkg-array.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, head, found;
  46. modstatdb_init(admindir,msdbrw_readonly);
  47. pkg_array_init_from_db(&array);
  48. pkg_array_sort(&array, pkglistqsortcmp);
  49. head=0;
  50. if (!*argv) {
  51. for (i = 0; i < array.n_pkgs; i++) {
  52. pkg = array.pkgs[i];
  53. if (pkg->status == stat_notinstalled) continue;
  54. getsel1package(pkg);
  55. }
  56. } else {
  57. while ((thisarg= *argv++)) {
  58. found= 0;
  59. for (i = 0; i < array.n_pkgs; i++) {
  60. pkg = array.pkgs[i];
  61. if (fnmatch(thisarg,pkg->name,0)) continue;
  62. getsel1package(pkg); found++;
  63. }
  64. if (!found)
  65. fprintf(stderr,_("No packages found matching %s.\n"),thisarg);
  66. }
  67. }
  68. if (ferror(stdout)) werr("stdout");
  69. if (ferror(stderr)) werr("stderr");
  70. pkg_array_free(&array);
  71. }
  72. void setselections(const char *const *argv) {
  73. const struct namevalue *nvp;
  74. struct pkginfo *pkg;
  75. const char *e;
  76. int c, lno;
  77. struct varbuf namevb = VARBUF_INIT;
  78. struct varbuf selvb = VARBUF_INIT;
  79. if (*argv)
  80. badusage(_("--%s takes no arguments"), cipaction->olong);
  81. modstatdb_init(admindir,msdbrw_write);
  82. lno= 1;
  83. for (;;) {
  84. varbufreset(&namevb);
  85. varbufreset(&selvb);
  86. do { c= getchar(); if (c == '\n') lno++; } while (c != EOF && isspace(c));
  87. if (c == EOF) break;
  88. if (c == '#') {
  89. do { c= getchar(); if (c == '\n') lno++; } while (c != EOF && c != '\n');
  90. continue;
  91. }
  92. while (!isspace(c)) {
  93. varbufaddc(&namevb,c);
  94. c= getchar();
  95. if (c == EOF) ohshit(_("unexpected eof in package name at line %d"),lno);
  96. if (c == '\n') ohshit(_("unexpected end of line in package name at line %d"),lno);
  97. }
  98. while (c != EOF && isspace(c)) {
  99. c= getchar();
  100. if (c == EOF) ohshit(_("unexpected eof after package name at line %d"),lno);
  101. if (c == '\n') ohshit(_("unexpected end of line after package name at line %d"),lno);
  102. }
  103. while (c != EOF && !isspace(c)) {
  104. varbufaddc(&selvb,c);
  105. c= getchar();
  106. }
  107. while (c != EOF && c != '\n') {
  108. c= getchar();
  109. if (!isspace(c))
  110. ohshit(_("unexpected data after package and selection at line %d"),lno);
  111. }
  112. varbufaddc(&namevb,0);
  113. varbufaddc(&selvb,0);
  114. e = illegal_packagename(namevb.buf, NULL);
  115. if (e) ohshit(_("illegal package name at line %d: %.250s"),lno,e);
  116. for (nvp=wantinfos; nvp->name && strcmp(nvp->name,selvb.buf); nvp++);
  117. if (!nvp->name) ohshit(_("unknown wanted status at line %d: %.250s"),lno,selvb.buf);
  118. pkg= findpackage(namevb.buf);
  119. pkg->want= nvp->value;
  120. if (c == EOF) break;
  121. lno++;
  122. }
  123. if (ferror(stdin)) ohshite(_("read error on standard input"));
  124. modstatdb_shutdown();
  125. varbuffree(&namevb);
  126. varbuffree(&selvb);
  127. }
  128. void clearselections(const char *const *argv)
  129. {
  130. struct pkgiterator *it;
  131. struct pkginfo *pkg;
  132. if (*argv)
  133. badusage(_("--%s takes no arguments"), cipaction->olong);
  134. modstatdb_init(admindir, msdbrw_write);
  135. it = iterpkgstart();
  136. while ((pkg = iterpkgnext(it))) {
  137. if (!pkg->installed.essential)
  138. pkg->want = want_deinstall;
  139. }
  140. iterpkgend(it);
  141. modstatdb_shutdown();
  142. }