main.cc 9.6 KB

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