main.cc 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  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 `" DSELECT "' package handling frontend.");
  70. static const char copyrightstring[]= N_(
  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",gettext(programdesc),gettext(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. const char* option = gettext(me->option);
  185. sprintf(buf," %c %d. [%c]%-10.10s %-80.80s ",
  186. so ? '*' : ' ', i,
  187. toupper(option[0]), option+1,
  188. gettext(me->menuent));
  189. int y,x;
  190. getmaxyx(stdscr,y,x);
  191. attrset(so ? A_REVERSE : A_NORMAL);
  192. mvaddnstr(i+2,0, buf,x-1);
  193. attrset(A_NORMAL);
  194. }
  195. int refreshmenu(void) {
  196. curseson(); cbreak(); noecho(); nonl(); keypad(stdscr,TRUE);
  197. int y,x;
  198. getmaxyx(stdscr,y,x);
  199. clear();
  200. attrset(A_BOLD);
  201. mvaddnstr(0,0, gettext(programdesc),x-1);
  202. attrset(A_NORMAL);
  203. const struct menuentry *mep; int i;
  204. for (mep=menuentries, i=0; mep->option && mep->menuent; mep++, i++)
  205. dme(i,0);
  206. attrset(A_BOLD);
  207. addstr(_("\n\n"
  208. "Use ^P and ^N, cursor keys, initial letters, or digits to select;\n"
  209. "Press ENTER to confirm selection. ^L to redraw screen.\n\n"));
  210. attrset(A_NORMAL);
  211. addstr(gettext(copyrightstring));
  212. return i;
  213. }
  214. urqresult urq_menu(void) {
  215. #define C(x) ((x)-'a'+1)
  216. int entries, c, i;
  217. entries= refreshmenu();
  218. int cursor=0;
  219. dme(0,1);
  220. for (;;) {
  221. refresh();
  222. c= getch(); if (c==ERR) ohshite(_("failed to getch in main menu"));
  223. if (c==C('n') || c==KEY_DOWN || c==' ') {
  224. dme(cursor,0); cursor++; cursor %= entries; dme(cursor,1);
  225. } else if (c==C('p') || c==KEY_UP || c==C('h') ||
  226. c==KEY_BACKSPACE || c==KEY_DC) {
  227. dme(cursor,0); cursor+= entries-1; cursor %= entries; dme(cursor,1);
  228. } else if (c=='\n' || c=='\r' || c==KEY_ENTER) {
  229. clear(); refresh();
  230. switch (menuentries[cursor].fn()) { /* fixme: trap errors in urq_... */
  231. case urqr_quitmenu:
  232. return urqr_quitmenu;
  233. case urqr_normal:
  234. cursor++; cursor %= entries;
  235. case urqr_fail:
  236. break;
  237. default:
  238. internerr(_("unknown menufn"));
  239. }
  240. refreshmenu(); dme(cursor,1);
  241. } else if (c==C('l')) {
  242. clearok(stdscr,TRUE); clear(); refreshmenu(); dme(cursor,1);
  243. } else if (isdigit(c)) {
  244. char buf[2]; buf[0]=c; buf[1]=0; c=atoi(buf);
  245. if (c < entries) {
  246. dme(cursor,0); cursor=c; dme(cursor,1);
  247. } else {
  248. beep();
  249. }
  250. } else if (isalpha(c)) {
  251. c= tolower(c);
  252. for (i=0; i<entries && gettext(menuentries[i].option)[0] != c; i++);
  253. if (i < entries) {
  254. dme(cursor,0); cursor=i; dme(cursor,1);
  255. } else {
  256. beep();
  257. }
  258. } else {
  259. beep();
  260. }
  261. }
  262. }
  263. urqresult urq_quit(void) {
  264. return urqr_quitmenu;
  265. /* fixme: check packages OK */
  266. }
  267. int main(int, const char *const *argv) {
  268. jmp_buf ejbuf;
  269. setlocale(LC_ALL, "");
  270. bindtextdomain(PACKAGE, LOCALEDIR);
  271. textdomain(PACKAGE);
  272. #if CAN_RESIZE
  273. (void) signal(SIGWINCH, adjust); /* arrange interrupts to resize */
  274. #endif
  275. if (setjmp(ejbuf)) { /* expect warning about possible clobbering of argv */
  276. cursesoff();
  277. error_unwind(ehflag_bombout); exit(2);
  278. }
  279. push_error_handler(&ejbuf,print_error_fatal,0);
  280. myopt(&argv,cmdinfos);
  281. if (*argv) {
  282. const char *a;
  283. while ((a= *argv++) != 0) {
  284. const menuentry *me;
  285. for (me= menuentries; me->option && strcmp(me->option,a); me++);
  286. if (!me->option) badusage(_("unknown action string `%.50s'"),a);
  287. me->fn();
  288. }
  289. } else {
  290. urq_menu();
  291. }
  292. cursesoff();
  293. set_error_display(0,0);
  294. error_unwind(ehflag_normaltidy);
  295. return(0);
  296. }