main.cc 9.6 KB

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