You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

220 lines
6.1 KiB

  1. /* $Id: tasksel.c,v 1.14 2001/11/22 17:53:48 tausq Rel $ */
  2. #include "tasksel.h"
  3. #include <stdio.h>
  4. #include <string.h>
  5. #include <signal.h>
  6. #include <libintl.h>
  7. #include <locale.h>
  8. #include <stdlib.h>
  9. #include <unistd.h>
  10. #include "slangui.h"
  11. #include "data.h"
  12. #include "macros.h"
  13. void tasksel_signalhandler(int sig)
  14. {
  15. switch (sig) {
  16. case SIGWINCH:
  17. ui_resize();
  18. break;
  19. case SIGINT:
  20. ui_shutdown();
  21. exit(0);
  22. default:
  23. DPRINTF("%s\n", _("Unknown signal seen"));
  24. }
  25. }
  26. void help(void)
  27. {
  28. fprintf(stderr, _("tasksel install <task>\n"));
  29. fprintf(stderr, _("tasksel [options]; where options is any combination of:\n"));
  30. fprintf(stderr, "\t%s\n", _("-t -- test mode; don't actually run apt-get on exit"));
  31. fprintf(stderr, "\t%s\n", _("-q -- queue installs; do not install packages with apt-get;\n\t\tjust queue them in dpkg"));
  32. fprintf(stderr, "\t%s\n", _("-r -- install all required-priority packages"));
  33. fprintf(stderr, "\t%s\n", _("-i -- install all important-priority packages"));
  34. fprintf(stderr, "\t%s\n", _("-s -- install all standard-priority packages"));
  35. fprintf(stderr, "\t%s\n", _("-n -- don't show UI; use with -r or -i usually"));
  36. fprintf(stderr, "\t%s\n", _("-a -- show all tasks, even those with no packages in them"));
  37. fprintf(stderr, "\n");
  38. exit(0);
  39. }
  40. int doinstall(struct task_t **tasklist, int taskcount,
  41. struct package_t **pkglist, int pkgcount,
  42. unsigned char queueinstalls, unsigned char testmode)
  43. {
  44. int i, c = 0;
  45. FILE *todpkg;
  46. char buf[20480];
  47. if (queueinstalls) {
  48. if (testmode)
  49. todpkg = stdout;
  50. else
  51. todpkg = popen("dpkg --set-selections", "w");
  52. if (!todpkg) PERROR("Cannot send output to dpkg");
  53. for (i = 0; i < pkgcount; i++) {
  54. if (pkglist[i]->selected > 0) {
  55. fprintf(todpkg, "%s install\n", pkglist[i]->name);
  56. c++;
  57. }
  58. }
  59. for (i = 0; i < taskcount; i++) {
  60. if (tasklist[i]->selected > 0) {
  61. int j;
  62. if (tasklist[i]->task_pkg && ! tasklist[i]->task_pkg->pseudopackage) {
  63. fprintf(todpkg, "%s install\n", tasklist[i]->task_pkg->name);
  64. }
  65. for (j = 0; j < tasklist[i]->packagescount; j++) {
  66. fprintf(todpkg, "%s install\n", tasklist[i]->packages[j]);
  67. }
  68. c++;
  69. }
  70. }
  71. if (!testmode) pclose(todpkg);
  72. if (c == 0) {
  73. fprintf(stderr, _("No packages selected\n"));
  74. return 1;
  75. }
  76. } else {
  77. sprintf(buf, "apt-get install ");
  78. for (i = 0; i < pkgcount; i++) {
  79. if (pkglist[i]->selected > 0) {
  80. /* TODO check buffer overflow; not likely, but still... */
  81. strcat(buf, pkglist[i]->name);
  82. strcat(buf, " ");
  83. c++;
  84. }
  85. }
  86. for (i = 0; i < taskcount; i++) {
  87. if (tasklist[i]->selected > 0) {
  88. int j;
  89. /* TODO check buffer overflow; not likely, but still... */
  90. if (tasklist[i]->task_pkg && ! tasklist[i]->task_pkg->pseudopackage) {
  91. strcat(buf, tasklist[i]->task_pkg->name);
  92. strcat(buf, " ");
  93. }
  94. for (j = 0; j < tasklist[i]->packagescount; j++) {
  95. strcat(buf, tasklist[i]->packages[j]);
  96. strcat(buf, " ");
  97. }
  98. c++;
  99. }
  100. }
  101. if (c > 0) {
  102. if (testmode == 1)
  103. printf("%s\n", buf);
  104. else
  105. system(buf);
  106. } else {
  107. fprintf(stderr, _("No packages selected\n"));
  108. return 1;
  109. }
  110. }
  111. return 0;
  112. }
  113. int main(int argc, char * const argv[])
  114. {
  115. int i, c, r = 0;
  116. unsigned char testmode = 0, queueinstalls = 0, installreqd = 0;
  117. unsigned char installimp = 0, installstd = 0, noninteractive = 0;
  118. unsigned char showempties = 0;
  119. struct packages_t packages;
  120. struct tasks_t tasks;
  121. struct package_t **pkglist;
  122. struct task_t **tasklist;
  123. signal(SIGWINCH, tasksel_signalhandler);
  124. setlocale(LC_ALL, "");
  125. bindtextdomain(PACKAGE, LOCALEDIR);
  126. textdomain(PACKAGE);
  127. while (1) {
  128. c = getopt(argc, argv, "tqrinsa");
  129. if (c == -1) break;
  130. switch (c) {
  131. case 't': testmode = 1; break;
  132. case 'q': queueinstalls = 1; break;
  133. case 'r': installreqd = 1; break;
  134. case 'i': installimp = 1; break;
  135. case 's': installstd = 1; break;
  136. case 'n': noninteractive = 1; break;
  137. case 'a': showempties = 1; break;
  138. default: help();
  139. }
  140. }
  141. /* Initialization */
  142. memset(&packages, 0, sizeof(struct packages_t));
  143. memset(&tasks, 0, sizeof(struct tasks_t));
  144. /* Must read packages first. */
  145. packages_readlist(&tasks, &packages);
  146. /* TODO: should probably read in all files in a directory. */
  147. taskfile_read(TASKDESC, &tasks, &packages, showempties);
  148. if (tasks.count == 0) {
  149. fprintf(stderr, _("No tasks found on this system.\nDid you update your available file?\n"));
  150. return 255;
  151. }
  152. if (noninteractive == 0) {
  153. ui_init(argc, argv, &tasks, &packages);
  154. ui_drawscreen();
  155. r = ui_eventloop();
  156. ui_shutdown();
  157. }
  158. pkglist = packages_enumerate(&packages);
  159. if (installreqd || installimp || installstd) {
  160. for (i = 0; i < packages.count; i++) {
  161. if (installreqd && pkglist[i]->priority == PRIORITY_REQUIRED)
  162. pkglist[i]->selected = 1;
  163. if (installimp && pkglist[i]->priority == PRIORITY_IMPORTANT)
  164. pkglist[i]->selected = 1;
  165. if (installstd && pkglist[i]->priority == PRIORITY_STANDARD)
  166. pkglist[i]->selected = 1;
  167. }
  168. }
  169. tasklist = tasks_enumerate(&tasks);
  170. if (noninteractive) {
  171. if (optind + 1 < argc && strcmp(argv[optind], "install") == 0) {
  172. for (optind++; optind < argc; optind++) {
  173. /* mark task argv[optind] for install, if it exists */
  174. int i;
  175. for (i = 0; i < tasks.count; i++) {
  176. if (strcmp(tasklist[i]->name, argv[optind]) == 0) break;
  177. }
  178. if (i == tasks.count) {
  179. printf("E: %s: no such task\n", argv[optind]);
  180. } else {
  181. tasklist[i]->selected = 1;
  182. }
  183. }
  184. }
  185. }
  186. if (r == 0) {
  187. r = doinstall(tasklist, tasks.count,
  188. pkglist, (installreqd || installimp || installstd
  189. ? packages.count : 0),
  190. queueinstalls, testmode);
  191. }
  192. packages_free(&tasks, &packages);
  193. return r;
  194. }