main.cc 9.6 KB

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