main.cc 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. /*
  2. * dselect - Debian GNU/Linux package maintenance user interface
  3. * main.cc - main program
  4. *
  5. * Copyright (C) 1994,1995 Ian Jackson <iwj10@cus.cam.ac.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 <stdio.h>
  22. #include <string.h>
  23. #include <stdlib.h>
  24. #include <signal.h>
  25. #include <sys/stat.h>
  26. #include <sys/types.h>
  27. #include <sys/wait.h>
  28. #include <errno.h>
  29. #include <unistd.h>
  30. #include <dirent.h>
  31. #include <limits.h>
  32. #include <ctype.h>
  33. #include <assert.h>
  34. #include <ncurses.h>
  35. #include <term.h>
  36. extern "C" {
  37. #include "config.h"
  38. #include "dpkg.h"
  39. #include "dpkg-db.h"
  40. #include "version.h"
  41. #include "myopt.h"
  42. }
  43. #include "dselect.h"
  44. #include "bindings.h"
  45. #include "pkglist.h"
  46. const char thisname[]= DSELECT;
  47. const char printforhelp[]= "Type " DPKG " --help for help.";
  48. modstatdb_rw readwrite;
  49. const char *admindir= ADMINDIR;
  50. FILE *debug;
  51. static keybindings packagelistbindings(packagelist_kinterps,packagelist_korgbindings);
  52. struct menuentry {
  53. const char *option;
  54. const char *menuent;
  55. urqfunction *fn;
  56. };
  57. static const menuentry menuentries[]= {
  58. { "access", "Choose the access method to use.", &urq_setup },
  59. { "update", "Update list of available packages, if possible.", &urq_update },
  60. { "select", "Request which packages you want on your system.", &urq_list },
  61. { "install", "Install and upgrade wanted packages.", &urq_install },
  62. { "config", "Configure any packages that are unconfigured.", &urq_config },
  63. { "remove", "Remove unwanted software.", &urq_remove },
  64. { "quit", "Quit dselect.", &urq_quit },
  65. { "menu", 0, &urq_menu },
  66. { 0 }
  67. };
  68. static const char programdesc[]=
  69. "Debian Linux `" DSELECT "' package handling frontend.";
  70. static const char copyrightstring[]=
  71. "Version " DPKG_VERSION_ARCH ". Copyright (C) 1994,1995 Ian Jackson. This is\n"
  72. "free software; see the GNU General Public Licence version 2 or later for\n"
  73. "copying conditions. There is NO warranty. See dselect --licence for details.\n";
  74. static void printversion(void) {
  75. if (fprintf(stdout,"%s\n%s",programdesc,copyrightstring) == EOF) werr("stdout");
  76. }
  77. static void usage(void) {
  78. if (!fputs
  79. ("Usage: dselect [options]\n"
  80. " dselect [options] action ...\n"
  81. "Options: --admindir <directory> (default is /var/lib/dpkg)\n"
  82. " --help --version --licence --debug <file> | -D<file> | -D\n"
  83. "Actions: access update select install config remove quit menu\n",
  84. stdout)) werr("stdout");
  85. }
  86. /* These are called by C code, so need to have C calling convention */
  87. extern "C" {
  88. static void helponly(const struct cmdinfo*, const char*) {
  89. usage(); exit(0);
  90. }
  91. static void versiononly(const struct cmdinfo*, const char*) {
  92. printversion(); exit(0);
  93. }
  94. static void setdebug(const struct cmdinfo*, const char *v) {
  95. debug= fopen(v,"a");
  96. if (!debug) ohshite("couldn't open debug file `%.255s'\n",v);
  97. setvbuf(debug,0,_IONBF,0);
  98. }
  99. } /* End of extern "C" */
  100. static const struct cmdinfo cmdinfos[]= {
  101. { "admindir", 0, 1, 0, &admindir, 0 },
  102. { "debug", 'D', 1, 0, 0, setdebug },
  103. { "help", 'h', 0, 0, 0, helponly },
  104. { "version", 0, 0, 0, 0, versiononly },
  105. { "licence", 0, 0, 0, 0, showcopyright }, /* UK spelling */
  106. { "license", 0, 0, 0, 0, showcopyright }, /* US spelling */
  107. { 0, 0 }
  108. };
  109. static int cursesareon= 0;
  110. void curseson() {
  111. if (!cursesareon) {
  112. const char *cup, *smso;
  113. initscr();
  114. cup= tigetstr("cup");
  115. smso= tigetstr("smso");
  116. if (!cup || !smso) {
  117. endwin();
  118. if (!cup)
  119. fputs("Terminal does not appear to support cursor addressing.\n",stderr);
  120. if (!smso)
  121. fputs("Terminal does not appear to support highlighting.\n",stderr);
  122. fputs("Set your TERM variable correctly, use a better terminal,\n"
  123. "or make do with the per-package management tool " DPKG ".\n",stderr);
  124. ohshit("terminal lacks necessary features, giving up");
  125. }
  126. }
  127. cursesareon= 1;
  128. }
  129. void cursesoff() {
  130. if (cursesareon) {
  131. clear();
  132. refresh();
  133. endwin();
  134. }
  135. cursesareon=0;
  136. }
  137. extern void *operator new(size_t size) {
  138. void *p;
  139. p= m_malloc(size);
  140. assert(p);
  141. return p;
  142. }
  143. extern void operator delete(void *p) {
  144. free(p);
  145. }
  146. urqresult urq_list(void) {
  147. readwrite= modstatdb_init(admindir,msdbrw_writeifposs);
  148. curseson();
  149. packagelist *l= new packagelist(&packagelistbindings);
  150. l->resolvesuggest();
  151. l->display();
  152. delete l;
  153. modstatdb_shutdown();
  154. resetpackages();
  155. return urqr_normal;
  156. }
  157. void dme(int i, int so) {
  158. char buf[120];
  159. const menuentry *me= &menuentries[i];
  160. sprintf(buf," %c %d. [%c]%-10.10s %-80.80s ",
  161. so ? '*' : ' ', i,
  162. toupper(me->option[0]), me->option+1,
  163. me->menuent);
  164. int y,x;
  165. getmaxyx(stdscr,y,x);
  166. attrset(so ? A_REVERSE : A_NORMAL);
  167. mvaddnstr(i+2,0, buf,x-1);
  168. attrset(A_NORMAL);
  169. }
  170. int refreshmenu(void) {
  171. curseson(); cbreak(); noecho(); nonl(); keypad(stdscr,TRUE);
  172. int y,x;
  173. getmaxyx(stdscr,y,x);
  174. clear();
  175. attrset(A_BOLD);
  176. mvaddnstr(0,0, programdesc,x-1);
  177. attrset(A_NORMAL);
  178. const struct menuentry *mep; int i;
  179. for (mep=menuentries, i=0; mep->option && mep->menuent; mep++, i++)
  180. dme(i,0);
  181. attrset(A_BOLD);
  182. addstr("\n\n"
  183. "Use ^P and ^N, cursor keys, initial letters, or digits to select;\n"
  184. "Press ENTER to confirm selection. ^L to redraw screen.\n\n");
  185. attrset(A_NORMAL);
  186. addstr(copyrightstring);
  187. return i;
  188. }
  189. urqresult urq_menu(void) {
  190. #define C(x) ((x)-'a'+1)
  191. int entries, c, i;
  192. entries= refreshmenu();
  193. int cursor=0;
  194. dme(0,1);
  195. for (;;) {
  196. refresh();
  197. c= getch(); if (c==ERR) ohshite("failed to getch in main menu");
  198. if (c==C('n') || c==KEY_DOWN || c==' ') {
  199. dme(cursor,0); cursor++; cursor %= entries; dme(cursor,1);
  200. } else if (c==C('p') || c==KEY_UP || c==C('h') ||
  201. c==KEY_BACKSPACE || c==KEY_DC) {
  202. dme(cursor,0); cursor+= entries-1; cursor %= entries; dme(cursor,1);
  203. } else if (c=='\n' || c=='\r' || c==KEY_ENTER) {
  204. clear(); refresh();
  205. switch (menuentries[cursor].fn()) { /* fixme: trap errors in urq_... */
  206. case urqr_quitmenu:
  207. return urqr_quitmenu;
  208. case urqr_normal:
  209. cursor++; cursor %= entries;
  210. case urqr_fail:
  211. break;
  212. default:
  213. internerr("unknown menufn");
  214. }
  215. refreshmenu(); dme(cursor,1);
  216. } else if (c==C('l')) {
  217. clearok(stdscr,TRUE); clear(); refreshmenu(); dme(cursor,1);
  218. } else if (isdigit(c)) {
  219. char buf[2]; buf[0]=c; buf[1]=0; c=atoi(buf);
  220. if (c < entries) {
  221. dme(cursor,0); cursor=c; dme(cursor,1);
  222. } else {
  223. beep();
  224. }
  225. } else if (isalpha(c)) {
  226. c= tolower(c);
  227. for (i=0; i<entries && menuentries[i].option[0] != c; i++);
  228. if (i < entries) {
  229. dme(cursor,0); cursor=i; dme(cursor,1);
  230. } else {
  231. beep();
  232. }
  233. } else {
  234. beep();
  235. }
  236. }
  237. }
  238. urqresult urq_quit(void) {
  239. return urqr_quitmenu;
  240. /* fixme: check packages OK */
  241. }
  242. int main(int, const char *const *argv) {
  243. jmp_buf ejbuf;
  244. if (setjmp(ejbuf)) { /* expect warning about possible clobbering of argv */
  245. cursesoff();
  246. error_unwind(ehflag_bombout); exit(2);
  247. }
  248. push_error_handler(&ejbuf,print_error_fatal,0);
  249. myopt(&argv,cmdinfos);
  250. if (*argv) {
  251. const char *a;
  252. while ((a= *argv++) != 0) {
  253. const menuentry *me;
  254. for (me= menuentries; me->option && strcmp(me->option,a); me++);
  255. if (!me->option) badusage("unknown action string `%.50s'",a);
  256. me->fn();
  257. }
  258. } else {
  259. urq_menu();
  260. }
  261. cursesoff();
  262. set_error_display(0,0);
  263. error_unwind(ehflag_normaltidy);
  264. return(0);
  265. }