main.cc 9.5 KB

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