select.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /*
  2. * dpkg - main program for package management
  3. * select.c - by-hand (rather than dselect-based) package selection
  4. *
  5. * Copyright (C) 1995,1996 Ian Jackson <ijackson@gnu.ai.mit.edu>
  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 <stdio.h>
  22. #include <string.h>
  23. #include <assert.h>
  24. #include <fnmatch.h>
  25. #include <ctype.h>
  26. #include <config.h>
  27. #include <dpkg.h>
  28. #include <dpkg-db.h>
  29. #include "filesdb.h"
  30. #include "main.h"
  31. static void getsel1package(struct pkginfo *pkg) {
  32. int l;
  33. if (pkg->want == want_unknown) return;
  34. l= strlen(pkg->name); l >>= 3; l= 6-l; if (l<1) l=1;
  35. printf("%s%.*s%s\n",pkg->name,l,"\t\t\t\t\t\t",wantinfos[pkg->want].name);
  36. }
  37. void getselections(const char *const *argv) {
  38. struct pkgiterator *it;
  39. struct pkginfo *pkg;
  40. struct pkginfo **pkgl;
  41. const char *thisarg;
  42. int np, i, head, found;
  43. modstatdb_init(admindir,msdbrw_readonly);
  44. np= countpackages();
  45. pkgl= m_malloc(sizeof(struct pkginfo*)*np);
  46. it= iterpkgstart(); i=0;
  47. while ((pkg= iterpkgnext(it))) {
  48. assert(i<np);
  49. pkgl[i++]= pkg;
  50. }
  51. iterpkgend(it);
  52. assert(i==np);
  53. qsort(pkgl,np,sizeof(struct pkginfo*),pkglistqsortcmp);
  54. head=0;
  55. if (!*argv) {
  56. for (i=0; i<np; i++) {
  57. pkg= pkgl[i];
  58. if (pkg->status == stat_notinstalled) continue;
  59. getsel1package(pkg);
  60. }
  61. } else {
  62. while ((thisarg= *argv++)) {
  63. found= 0;
  64. for (i=0; i<np; i++) {
  65. pkg= pkgl[i];
  66. if (fnmatch(thisarg,pkg->name,0)) continue;
  67. getsel1package(pkg); found++;
  68. }
  69. if (!found)
  70. fprintf(stderr,_("No packages found matching %s.\n"),thisarg);
  71. }
  72. }
  73. if (ferror(stdout)) werr("stdout");
  74. if (ferror(stderr)) werr("stderr");
  75. }
  76. void setselections(const char *const *argv) {
  77. const struct namevalue *nvp;
  78. struct pkginfo *pkg;
  79. const char *e;
  80. int c, lno;
  81. struct varbuf namevb;
  82. struct varbuf selvb;
  83. if (*argv) badusage(_("--set-selections does not take any argument"));
  84. modstatdb_init(admindir,msdbrw_write);
  85. varbufinit(&namevb);
  86. varbufinit(&selvb);
  87. lno= 1;
  88. for (;;) {
  89. varbufreset(&namevb);
  90. varbufreset(&selvb);
  91. do { c= getchar(); if (c == '\n') lno++; } while (c != EOF && isspace(c));
  92. if (c == EOF) break;
  93. if (c == '#') {
  94. do { c= getchar(); if (c == '\n') lno++; } while (c != EOF && c != '\n');
  95. continue;
  96. }
  97. while (!isspace(c)) {
  98. varbufaddc(&namevb,c);
  99. c= getchar();
  100. if (c == EOF) ohshit(_("unexpected eof in package name at line %d"),lno);
  101. if (c == '\n') ohshit(_("unexpected end of line in package name at line %d"),lno);
  102. }
  103. while (c != EOF && isspace(c)) {
  104. c= getchar();
  105. if (c == EOF) ohshit(_("unexpected eof after package name at line %d"),lno);
  106. if (c == '\n') ohshit(_("unexpected end of line after package name at line %d"),lno);
  107. }
  108. while (c != EOF && !isspace(c)) {
  109. varbufaddc(&selvb,c);
  110. c= getchar();
  111. }
  112. while (c != EOF && c != '\n') {
  113. c= getchar();
  114. if (!isspace(c))
  115. ohshit(_("unexpected data after package and selection at line %d"),lno);
  116. }
  117. varbufaddc(&namevb,0);
  118. varbufaddc(&selvb,0);
  119. e= illegal_packagename(namevb.buf,0);
  120. if (e) ohshit(_("illegal package name at line %d: %.250s"),lno,e);
  121. for (nvp=wantinfos; nvp->name && strcmp(nvp->name,selvb.buf); nvp++);
  122. if (!nvp->name) ohshit(_("unknown wanted status at line %d: %.250s"),lno,selvb.buf);
  123. pkg= findpackage(namevb.buf);
  124. pkg->want= nvp->value;
  125. if (c == EOF) break;
  126. lno++;
  127. }
  128. if (ferror(stdin)) ohshite(_("read error on standard input"));
  129. modstatdb_shutdown();
  130. varbufreset(&namevb);
  131. varbufreset(&selvb);
  132. }