main.cc 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. /*
  2. * dselect - Debian package maintenance user interface
  3. * main.cc - main program
  4. *
  5. * Copyright (C) 1994,1995 Ian Jackson <iwj10@cus.cam.ac.uk>
  6. * Copyright (C) 2000 Wichert Akkerman <wakkerma@debian.org>
  7. *
  8. * This is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as
  10. * published by the Free Software Foundation; either version 2,
  11. * or (at your option) any later version.
  12. *
  13. * This is distributed in the hope that it will be useful, but
  14. * WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public
  19. * License along with dpkg; if not, write to the Free Software
  20. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  21. */
  22. #include <stdio.h>
  23. #include <string.h>
  24. #include <stdlib.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 <term.h>
  35. extern "C" {
  36. #include <config.h>
  37. #include <dpkg.h>
  38. #include <dpkg-db.h>
  39. #include <version.h>
  40. #include <myopt.h>
  41. }
  42. #include "dselect.h"
  43. #include "bindings.h"
  44. #include "pkglist.h"
  45. #include <sys/types.h>
  46. #include <sys/stat.h>
  47. #include <fcntl.h>
  48. const char thisname[]= DSELECT;
  49. const char printforhelp[]= N_("Type dselect --help for help.");
  50. modstatdb_rw readwrite;
  51. const char *admindir= ADMINDIR;
  52. FILE *debug;
  53. int expertmode = 0;
  54. static keybindings packagelistbindings(packagelist_kinterps,packagelist_korgbindings);
  55. struct menuentry {
  56. const char *command;
  57. const char *key;
  58. const char *option;
  59. const char *menuent;
  60. urqfunction *fn;
  61. };
  62. static const menuentry menuentries[]= {
  63. { "access", N_("a"), N_("[A]ccess"), N_("Choose the access method to use."), &urq_setup },
  64. { "update", N_("u"), N_("[U]pdate"), N_("Update list of available packages, if possible."), &urq_update },
  65. { "select", N_("s"), N_("[S]elect"), N_("Request which packages you want on your system."), &urq_list },
  66. { "install", N_("i"), N_("[I]nstall"),N_("Install and upgrade wanted packages."), &urq_install },
  67. { "config", N_("c"), N_("[C]onfig"), N_("Configure any packages that are unconfigured."), &urq_config },
  68. { "remove", N_("r"), N_("[R]emove"), N_("Remove unwanted software."), &urq_remove },
  69. { "quit", N_("q"), N_("[Q]uit"), N_("Quit dselect."), &urq_quit },
  70. { 0, 0, N_("menu"), 0, &urq_menu },
  71. { 0 }
  72. };
  73. static const char programdesc[]=
  74. N_("Debian `%s' package handling frontend.");
  75. static const char copyrightstring[]= N_(
  76. "Version %s.\n"
  77. "Copyright (C) 1994-1996 Ian Jackson.\n"
  78. "Copyright (C) 2000 Wichert Akkerman.\n"
  79. "This is free software; see the GNU General Public Licence version 2\n"
  80. "or later for copying conditions. There is NO warranty. See\n"
  81. "dselect --licence for details.\n");
  82. static void printversion(void) {
  83. if (fprintf(stdout,gettext(programdesc),DSELECT) == EOF) werr("stdout");
  84. if (fprintf(stdout,"\n") == EOF) werr("stdout");
  85. if (fprintf(stdout,gettext(copyrightstring), DPKG_VERSION_ARCH) == EOF) werr("stdout");
  86. }
  87. static void usage(void) {
  88. if (!fputs(
  89. _("Usage: dselect [options]\n"
  90. " dselect [options] action ...\n"
  91. "Options: --admindir <directory> (default is /var/lib/dpkg)\n"
  92. " --help --version --licence --expert --debug <file> | -D<file>\n"
  93. "Actions: access update select install config remove quit menu\n"),
  94. stdout)) werr("stdout");
  95. }
  96. /* These are called by C code, so need to have C calling convention */
  97. extern "C" {
  98. static void helponly(const struct cmdinfo*, const char*) {
  99. usage(); exit(0);
  100. }
  101. static void versiononly(const struct cmdinfo*, const char*) {
  102. printversion(); exit(0);
  103. }
  104. static void setdebug(const struct cmdinfo*, const char *v) {
  105. debug= fopen(v,"a");
  106. if (!debug) ohshite(_("couldn't open debug file `%.255s'\n"),v);
  107. setvbuf(debug,0,_IONBF,0);
  108. }
  109. static void setexpert(const struct cmdinfo*, const char *v) {
  110. expertmode = 1;
  111. }
  112. } /* End of extern "C" */
  113. static const struct cmdinfo cmdinfos[]= {
  114. { "admindir", 0, 1, 0, &admindir, 0 },
  115. { "debug", 'D', 1, 0, 0, setdebug },
  116. { "expert", 'E', 0, 0, 0, setexpert },
  117. { "help", 'h', 0, 0, 0, helponly },
  118. { "version", 0, 0, 0, 0, versiononly },
  119. { "licence", 0, 0, 0, 0, showcopyright }, /* UK spelling */
  120. { "license", 0, 0, 0, 0, showcopyright }, /* US spelling */
  121. { 0, 0, 0, 0, 0, 0 }
  122. };
  123. static int cursesareon= 0;
  124. void curseson() {
  125. if (!cursesareon) {
  126. const char *cup, *smso;
  127. initscr();
  128. cup= tigetstr("cup");
  129. smso= tigetstr("smso");
  130. if (!cup || !smso) {
  131. endwin();
  132. if (!cup)
  133. fputs(_("Terminal does not appear to support cursor addressing.\n"),stderr);
  134. if (!smso)
  135. fputs(_("Terminal does not appear to support highlighting.\n"),stderr);
  136. fputs(_("Set your TERM variable correctly, use a better terminal,\n"
  137. "or make do with the per-package management tool "),stderr);
  138. fputs(DPKG ".\n",stderr);
  139. ohshit(_("terminal lacks necessary features, giving up"));
  140. }
  141. }
  142. cursesareon= 1;
  143. }
  144. void cursesoff() {
  145. if (cursesareon) {
  146. clear();
  147. refresh();
  148. endwin();
  149. }
  150. cursesareon=0;
  151. }
  152. extern void *operator new(size_t size) {
  153. void *p;
  154. p= m_malloc(size);
  155. assert(p);
  156. return p;
  157. }
  158. extern void operator delete(void *p) {
  159. free(p);
  160. }
  161. urqresult urq_list(void) {
  162. readwrite= modstatdb_init(admindir,msdbrw_writeifposs);
  163. curseson();
  164. packagelist *l= new packagelist(&packagelistbindings);
  165. l->resolvesuggest();
  166. l->display();
  167. delete l;
  168. modstatdb_shutdown();
  169. resetpackages();
  170. return urqr_normal;
  171. }
  172. void dme(int i, int so) {
  173. char buf[120];
  174. const menuentry *me= &menuentries[i];
  175. sprintf(buf," %c %d. %-11.11s %-80.80s ",
  176. so ? '*' : ' ', i,
  177. me->option,
  178. gettext(me->menuent));
  179. int y,x;
  180. getmaxyx(stdscr,y,x);
  181. attrset(so ? A_REVERSE : A_NORMAL);
  182. mvaddnstr(i+2,0, buf,x-1);
  183. attrset(A_NORMAL);
  184. }
  185. int refreshmenu(void) {
  186. char buf[2048];
  187. static int l,lockfd;
  188. static char *lockfile;
  189. curseson(); cbreak(); noecho(); nonl(); keypad(stdscr,TRUE);
  190. int y,x;
  191. getmaxyx(stdscr,y,x);
  192. clear();
  193. attrset(A_BOLD);
  194. sprintf(buf,gettext(programdesc),DSELECT);
  195. mvaddnstr(0,0,buf,x-1);
  196. attrset(A_NORMAL);
  197. const struct menuentry *mep; int i;
  198. for (mep=menuentries, i=0; mep->option && mep->menuent; mep++, i++)
  199. dme(i,0);
  200. attrset(A_BOLD);
  201. addstr(_("\n\n"
  202. "Move around with ^P and ^N, cursor keys, initial letters, or digits;\n"
  203. "Press <enter> to confirm selection. ^L redraws screen.\n\n"));
  204. attrset(A_NORMAL);
  205. sprintf(buf,gettext(copyrightstring),DPKG_VERSION_ARCH);
  206. addstr(buf);
  207. l = strlen(admindir);
  208. lockfile = new char[l+sizeof(LOCKFILE)+2];
  209. strcpy(lockfile,admindir);
  210. strcpy(lockfile+l, "/" LOCKFILE);
  211. lockfd = open(lockfile, O_RDWR|O_CREAT|O_TRUNC, 0660);
  212. if (errno == EACCES || errno == EPERM)
  213. addstr(_("\n\n"
  214. "Read-only access: only preview of selections is available!"));
  215. return i;
  216. }
  217. urqresult urq_menu(void) {
  218. #define C(x) ((x)-'a'+1)
  219. int entries, c, i;
  220. entries= refreshmenu();
  221. int cursor=0;
  222. dme(0,1);
  223. for (;;) {
  224. refresh();
  225. c= getch(); if (c==ERR) ohshite(_("failed to getch in main menu"));
  226. if (c==C('n') || c==KEY_DOWN || c==' ') {
  227. dme(cursor,0); cursor++; cursor %= entries; dme(cursor,1);
  228. } else if (c==C('p') || c==KEY_UP || c==C('h') ||
  229. c==KEY_BACKSPACE || c==KEY_DC) {
  230. dme(cursor,0); cursor+= entries-1; cursor %= entries; dme(cursor,1);
  231. } else if (c=='\n' || c=='\r' || c==KEY_ENTER) {
  232. clear(); refresh();
  233. switch (menuentries[cursor].fn()) { /* fixme: trap errors in urq_... */
  234. case urqr_quitmenu:
  235. return urqr_quitmenu;
  236. case urqr_normal:
  237. cursor++; cursor %= entries;
  238. case urqr_fail:
  239. break;
  240. default:
  241. internerr("unknown menufn");
  242. }
  243. refreshmenu(); dme(cursor,1);
  244. } else if (c==C('l')) {
  245. clearok(stdscr,TRUE); clear(); refreshmenu(); dme(cursor,1);
  246. } else if (isdigit(c)) {
  247. char buf[2]; buf[0]=c; buf[1]=0; c=atoi(buf);
  248. if (c < entries) {
  249. dme(cursor,0); cursor=c; dme(cursor,1);
  250. } else {
  251. beep();
  252. }
  253. } else if (isalpha(c)) {
  254. c= tolower(c);
  255. for (i=0; i<entries && gettext(menuentries[i].key)[0] != c; i++);
  256. if (i < entries) {
  257. dme(cursor,0); cursor=i; dme(cursor,1);
  258. } else {
  259. beep();
  260. }
  261. } else {
  262. beep();
  263. }
  264. }
  265. }
  266. urqresult urq_quit(void) {
  267. return urqr_quitmenu;
  268. /* fixme: check packages OK */
  269. }
  270. int main(int, const char *const *argv) {
  271. jmp_buf ejbuf;
  272. setlocale(LC_ALL, "");
  273. bindtextdomain(PACKAGE, LOCALEDIR);
  274. textdomain(PACKAGE);
  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->command && strcmp(me->command,a); me++);
  286. if (!me->command) 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. }