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.
 
 
 

242 lines
6.8 KiB

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