main.cc 9.7 KB

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