select.c 4.2 KB

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