main.cc 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  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,2001 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,2001 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*) NONRETURNING;
  99. static void helponly(const struct cmdinfo*, const char*) {
  100. usage(); exit(0);
  101. }
  102. static void versiononly(const struct cmdinfo*, const char*) NONRETURNING;
  103. static void versiononly(const struct cmdinfo*, const char*) {
  104. printversion(); exit(0);
  105. }
  106. static void setdebug(const struct cmdinfo*, const char *v) {
  107. debug= fopen(v,"a");
  108. if (!debug) ohshite(_("couldn't open debug file `%.255s'\n"),v);
  109. setvbuf(debug,0,_IONBF,0);
  110. }
  111. static void setexpert(const struct cmdinfo*, const char *v) {
  112. expertmode = 1;
  113. }
  114. } /* End of extern "C" */
  115. static const struct cmdinfo cmdinfos[]= {
  116. { "admindir", 0, 1, 0, &admindir, 0 },
  117. { "debug", 'D', 1, 0, 0, setdebug },
  118. { "expert", 'E', 0, 0, 0, setexpert },
  119. { "help", 'h', 0, 0, 0, helponly },
  120. { "version", 0, 0, 0, 0, versiononly },
  121. { "licence", 0, 0, 0, 0, showcopyright }, /* UK spelling */
  122. { "license", 0, 0, 0, 0, showcopyright }, /* US spelling */
  123. { 0, 0, 0, 0, 0, 0 }
  124. };
  125. static int cursesareon= 0;
  126. void curseson() {
  127. if (!cursesareon) {
  128. const char *cup, *smso;
  129. initscr();
  130. cup= tigetstr("cup");
  131. smso= tigetstr("smso");
  132. if (!cup || !smso) {
  133. endwin();
  134. if (!cup)
  135. fputs(_("Terminal does not appear to support cursor addressing.\n"),stderr);
  136. if (!smso)
  137. fputs(_("Terminal does not appear to support highlighting.\n"),stderr);
  138. fputs(_("Set your TERM variable correctly, use a better terminal,\n"
  139. "or make do with the per-package management tool "),stderr);
  140. fputs(DPKG ".\n",stderr);
  141. ohshit(_("terminal lacks necessary features, giving up"));
  142. }
  143. }
  144. cursesareon= 1;
  145. }
  146. void cursesoff() {
  147. if (cursesareon) {
  148. clear();
  149. refresh();
  150. endwin();
  151. }
  152. cursesareon=0;
  153. }
  154. extern void *operator new(size_t size) {
  155. void *p;
  156. p= m_malloc(size);
  157. assert(p);
  158. return p;
  159. }
  160. extern void operator delete(void *p) {
  161. free(p);
  162. }
  163. urqresult urq_list(void) {
  164. readwrite= modstatdb_init(admindir,msdbrw_writeifposs);
  165. curseson();
  166. packagelist *l= new packagelist(&packagelistbindings);
  167. l->resolvesuggest();
  168. l->display();
  169. delete l;
  170. modstatdb_shutdown();
  171. resetpackages();
  172. return urqr_normal;
  173. }
  174. void dme(int i, int so) {
  175. char buf[120];
  176. const menuentry *me= &menuentries[i];
  177. sprintf(buf," %c %d. %-11.11s %-80.80s ",
  178. so ? '*' : ' ', i,
  179. gettext(me->option),
  180. gettext(me->menuent));
  181. int y,x;
  182. getmaxyx(stdscr,y,x);
  183. attrset(so ? A_REVERSE : A_NORMAL);
  184. mvaddnstr(i+2,0, buf,x-1);
  185. attrset(A_NORMAL);
  186. }
  187. int refreshmenu(void) {
  188. char buf[2048];
  189. static int l,lockfd;
  190. static char *lockfile;
  191. curseson(); cbreak(); noecho(); nonl(); keypad(stdscr,TRUE);
  192. int y,x;
  193. getmaxyx(stdscr,y,x);
  194. clear();
  195. attrset(A_BOLD);
  196. sprintf(buf,gettext(programdesc),DSELECT);
  197. mvaddnstr(0,0,buf,x-1);
  198. attrset(A_NORMAL);
  199. const struct menuentry *mep; int i;
  200. for (mep=menuentries, i=0; mep->option && mep->menuent; mep++, i++)
  201. dme(i,0);
  202. attrset(A_BOLD);
  203. addstr(_("\n\n"
  204. "Move around with ^P and ^N, cursor keys, initial letters, or digits;\n"
  205. "Press <enter> to confirm selection. ^L redraws screen.\n\n"));
  206. attrset(A_NORMAL);
  207. sprintf(buf,gettext(copyrightstring),DPKG_VERSION_ARCH);
  208. addstr(buf);
  209. l = strlen(admindir);
  210. lockfile = new char[l+sizeof(LOCKFILE)+2];
  211. strcpy(lockfile,admindir);
  212. strcpy(lockfile+l, "/" LOCKFILE);
  213. lockfd = open(lockfile, O_RDWR|O_CREAT|O_TRUNC, 0660);
  214. if (errno == EACCES || errno == EPERM)
  215. addstr(_("\n\n"
  216. "Read-only access: only preview of selections is available!"));
  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. do
  228. c= getch();
  229. while (c == ERR && errno == EINTR);
  230. if (c==ERR) {
  231. if(errno != 0)
  232. ohshite(_("failed to getch in main menu"));
  233. else {
  234. clearok(stdscr,TRUE); clear(); refreshmenu(); dme(cursor,1);
  235. }
  236. }
  237. if (c==C('n') || c==KEY_DOWN || c==' ' || c=='j') {
  238. dme(cursor,0); cursor++; cursor %= entries; dme(cursor,1);
  239. } else if (c==C('p') || c==KEY_UP || c==C('h') ||
  240. c==KEY_BACKSPACE || c==KEY_DC || c=='k') {
  241. dme(cursor,0); cursor+= entries-1; cursor %= entries; dme(cursor,1);
  242. } else if (c=='\n' || c=='\r' || c==KEY_ENTER) {
  243. clear(); refresh();
  244. switch (menuentries[cursor].fn()) { /* fixme: trap errors in urq_... */
  245. case urqr_quitmenu:
  246. return urqr_quitmenu;
  247. case urqr_normal:
  248. cursor++; cursor %= entries;
  249. case urqr_fail:
  250. break;
  251. default:
  252. internerr("unknown menufn");
  253. }
  254. refreshmenu(); dme(cursor,1);
  255. } else if (c==C('l')) {
  256. clearok(stdscr,TRUE); clear(); refreshmenu(); dme(cursor,1);
  257. } else if (isdigit(c)) {
  258. char buf[2]; buf[0]=c; buf[1]=0; c=atoi(buf);
  259. if (c < entries) {
  260. dme(cursor,0); cursor=c; dme(cursor,1);
  261. } else {
  262. beep();
  263. }
  264. } else if (isalpha(c)) {
  265. c= tolower(c);
  266. for (i=0; i<entries && gettext(menuentries[i].key)[0] != c; i++);
  267. if (i < entries) {
  268. dme(cursor,0); cursor=i; dme(cursor,1);
  269. } else {
  270. beep();
  271. }
  272. } else {
  273. beep();
  274. }
  275. }
  276. }
  277. urqresult urq_quit(void) {
  278. return urqr_quitmenu;
  279. /* fixme: check packages OK */
  280. }
  281. int main(int, const char *const *argv) {
  282. jmp_buf ejbuf;
  283. setlocale(LC_ALL, "");
  284. bindtextdomain(PACKAGE, LOCALEDIR);
  285. textdomain(PACKAGE);
  286. if (setjmp(ejbuf)) { /* expect warning about possible clobbering of argv */
  287. cursesoff();
  288. error_unwind(ehflag_bombout); exit(2);
  289. }
  290. push_error_handler(&ejbuf,print_error_fatal,0);
  291. myfileopt(CONFIGDIR "/" DSELECT ".cfg", cmdinfos);
  292. myopt(&argv,cmdinfos);
  293. if (*argv) {
  294. const char *a;
  295. while ((a= *argv++) != 0) {
  296. const menuentry *me;
  297. for (me= menuentries; me->command && strcmp(me->command,a); me++);
  298. if (!me->command) badusage(_("unknown action string `%.50s'"),a);
  299. me->fn();
  300. }
  301. } else {
  302. urq_menu();
  303. }
  304. cursesoff();
  305. set_error_display(0,0);
  306. error_unwind(ehflag_normaltidy);
  307. return(0);
  308. }