main.cc 9.9 KB

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