main.cc 10 KB

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