main.cc 9.8 KB

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