main.cc 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  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 <curses.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-1996 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. #if CAN_RESIZE
  87. /*
  88. * This uses functions that are "unsafe", but it seems to work on SunOS and
  89. * Linux. The 'wrefresh(curscr)' is needed to force the refresh to start from
  90. * the top of the screen -- some xterms mangle the bitmap while resizing.
  91. *
  92. * Borrowed from the ncurses example view.c
  93. */
  94. static RETSIGTYPE adjust(int sig)
  95. {
  96. if (waiting || sig == 0) {
  97. struct winsize size;
  98. if (ioctl(fileno(stdout), TIOCGWINSZ, &size) == 0) {
  99. resizeterm(size.ws_row, size.ws_col);
  100. wrefresh(curscr); /* Linux needs this */
  101. show_all();
  102. }
  103. interrupted = FALSE;
  104. } else {
  105. interrupted = TRUE;
  106. }
  107. (void) signal(SIGWINCH, adjust); /* some systems need this */
  108. }
  109. #endif /* CAN_RESIZE */
  110. /* These are called by C code, so need to have C calling convention */
  111. extern "C" {
  112. static void helponly(const struct cmdinfo*, const char*) {
  113. usage(); exit(0);
  114. }
  115. static void versiononly(const struct cmdinfo*, const char*) {
  116. printversion(); exit(0);
  117. }
  118. static void setdebug(const struct cmdinfo*, const char *v) {
  119. debug= fopen(v,"a");
  120. if (!debug) ohshite("couldn't open debug file `%.255s'\n",v);
  121. setvbuf(debug,0,_IONBF,0);
  122. }
  123. } /* End of extern "C" */
  124. static const struct cmdinfo cmdinfos[]= {
  125. { "admindir", 0, 1, 0, &admindir, 0 },
  126. { "debug", 'D', 1, 0, 0, setdebug },
  127. { "help", 'h', 0, 0, 0, helponly },
  128. { "version", 0, 0, 0, 0, versiononly },
  129. { "licence", 0, 0, 0, 0, showcopyright }, /* UK spelling */
  130. { "license", 0, 0, 0, 0, showcopyright }, /* US spelling */
  131. { 0, 0 }
  132. };
  133. static int cursesareon= 0;
  134. void curseson() {
  135. if (!cursesareon) {
  136. const char *cup, *smso;
  137. initscr();
  138. cup= tigetstr("cup");
  139. smso= tigetstr("smso");
  140. if (!cup || !smso) {
  141. endwin();
  142. if (!cup)
  143. fputs("Terminal does not appear to support cursor addressing.\n",stderr);
  144. if (!smso)
  145. fputs("Terminal does not appear to support highlighting.\n",stderr);
  146. fputs("Set your TERM variable correctly, use a better terminal,\n"
  147. "or make do with the per-package management tool " DPKG ".\n",stderr);
  148. ohshit("terminal lacks necessary features, giving up");
  149. }
  150. }
  151. cursesareon= 1;
  152. }
  153. void cursesoff() {
  154. if (cursesareon) {
  155. clear();
  156. refresh();
  157. endwin();
  158. }
  159. cursesareon=0;
  160. }
  161. extern void *operator new(size_t size) {
  162. void *p;
  163. p= m_malloc(size);
  164. assert(p);
  165. return p;
  166. }
  167. extern void operator delete(void *p) {
  168. free(p);
  169. }
  170. urqresult urq_list(void) {
  171. readwrite= modstatdb_init(admindir,msdbrw_writeifposs);
  172. curseson();
  173. packagelist *l= new packagelist(&packagelistbindings);
  174. l->resolvesuggest();
  175. l->display();
  176. delete l;
  177. modstatdb_shutdown();
  178. resetpackages();
  179. return urqr_normal;
  180. }
  181. void dme(int i, int so) {
  182. char buf[120];
  183. const menuentry *me= &menuentries[i];
  184. sprintf(buf," %c %d. [%c]%-10.10s %-80.80s ",
  185. so ? '*' : ' ', i,
  186. toupper(me->option[0]), me->option+1,
  187. me->menuent);
  188. int y,x;
  189. getmaxyx(stdscr,y,x);
  190. attrset(so ? A_REVERSE : A_NORMAL);
  191. mvaddnstr(i+2,0, buf,x-1);
  192. attrset(A_NORMAL);
  193. }
  194. int refreshmenu(void) {
  195. curseson(); cbreak(); noecho(); nonl(); keypad(stdscr,TRUE);
  196. int y,x;
  197. getmaxyx(stdscr,y,x);
  198. clear();
  199. attrset(A_BOLD);
  200. mvaddnstr(0,0, programdesc,x-1);
  201. attrset(A_NORMAL);
  202. const struct menuentry *mep; int i;
  203. for (mep=menuentries, i=0; mep->option && mep->menuent; mep++, i++)
  204. dme(i,0);
  205. attrset(A_BOLD);
  206. addstr("\n\n"
  207. "Use ^P and ^N, cursor keys, initial letters, or digits to select;\n"
  208. "Press ENTER to confirm selection. ^L to redraw screen.\n\n");
  209. attrset(A_NORMAL);
  210. addstr(copyrightstring);
  211. return i;
  212. }
  213. urqresult urq_menu(void) {
  214. #define C(x) ((x)-'a'+1)
  215. int entries, c, i;
  216. entries= refreshmenu();
  217. int cursor=0;
  218. dme(0,1);
  219. for (;;) {
  220. refresh();
  221. c= getch(); if (c==ERR) ohshite("failed to getch in main menu");
  222. if (c==C('n') || c==KEY_DOWN || c==' ') {
  223. dme(cursor,0); cursor++; cursor %= entries; dme(cursor,1);
  224. } else if (c==C('p') || c==KEY_UP || c==C('h') ||
  225. c==KEY_BACKSPACE || c==KEY_DC) {
  226. dme(cursor,0); cursor+= entries-1; cursor %= entries; dme(cursor,1);
  227. } else if (c=='\n' || c=='\r' || c==KEY_ENTER) {
  228. clear(); refresh();
  229. switch (menuentries[cursor].fn()) { /* fixme: trap errors in urq_... */
  230. case urqr_quitmenu:
  231. return urqr_quitmenu;
  232. case urqr_normal:
  233. cursor++; cursor %= entries;
  234. case urqr_fail:
  235. break;
  236. default:
  237. internerr("unknown menufn");
  238. }
  239. refreshmenu(); dme(cursor,1);
  240. } else if (c==C('l')) {
  241. clearok(stdscr,TRUE); clear(); refreshmenu(); dme(cursor,1);
  242. } else if (isdigit(c)) {
  243. char buf[2]; buf[0]=c; buf[1]=0; c=atoi(buf);
  244. if (c < entries) {
  245. dme(cursor,0); cursor=c; dme(cursor,1);
  246. } else {
  247. beep();
  248. }
  249. } else if (isalpha(c)) {
  250. c= tolower(c);
  251. for (i=0; i<entries && menuentries[i].option[0] != c; i++);
  252. if (i < entries) {
  253. dme(cursor,0); cursor=i; dme(cursor,1);
  254. } else {
  255. beep();
  256. }
  257. } else {
  258. beep();
  259. }
  260. }
  261. }
  262. urqresult urq_quit(void) {
  263. return urqr_quitmenu;
  264. /* fixme: check packages OK */
  265. }
  266. int main(int, const char *const *argv) {
  267. jmp_buf ejbuf;
  268. #if CAN_RESIZE
  269. (void) signal(SIGWINCH, adjust); /* arrange interrupts to resize */
  270. #endif
  271. if (setjmp(ejbuf)) { /* expect warning about possible clobbering of argv */
  272. cursesoff();
  273. error_unwind(ehflag_bombout); exit(2);
  274. }
  275. push_error_handler(&ejbuf,print_error_fatal,0);
  276. myopt(&argv,cmdinfos);
  277. if (*argv) {
  278. const char *a;
  279. while ((a= *argv++) != 0) {
  280. const menuentry *me;
  281. for (me= menuentries; me->option && strcmp(me->option,a); me++);
  282. if (!me->option) badusage("unknown action string `%.50s'",a);
  283. me->fn();
  284. }
  285. } else {
  286. urq_menu();
  287. }
  288. cursesoff();
  289. set_error_display(0,0);
  290. error_unwind(ehflag_normaltidy);
  291. return(0);
  292. }