main.cc 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537
  1. /*
  2. * dselect - Debian package maintenance user interface
  3. * main.cc - main program
  4. *
  5. * Copyright © 1994,1995 Ian Jackson <ian@chiark.greenend.org.uk>
  6. * Copyright © 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 <config.h>
  23. #include <compat.h>
  24. #include <dpkg/i18n.h>
  25. #include <stdio.h>
  26. #include <string.h>
  27. #include <stdlib.h>
  28. #include <sys/stat.h>
  29. #include <sys/types.h>
  30. #include <sys/wait.h>
  31. #include <fcntl.h>
  32. #include <errno.h>
  33. #include <unistd.h>
  34. #include <dirent.h>
  35. #include <limits.h>
  36. #include <ctype.h>
  37. #include <assert.h>
  38. #if HAVE_LOCALE_H
  39. #include <locale.h>
  40. #endif
  41. #if defined(HAVE_NCURSESW_TERM_H)
  42. #include <ncursesw/term.h>
  43. #elif defined(HAVE_NCURSES_TERM_H)
  44. #include <ncurses/term.h>
  45. #else
  46. #include <term.h>
  47. #endif
  48. #include <dpkg/dpkg.h>
  49. #include <dpkg/dpkg-db.h>
  50. #include <dpkg/myopt.h>
  51. #include "dselect.h"
  52. #include "bindings.h"
  53. #include "pkglist.h"
  54. const char thisname[]= DSELECT;
  55. const char printforhelp[]= N_("Type dselect --help for help.");
  56. modstatdb_rw readwrite;
  57. const char *admindir= ADMINDIR;
  58. FILE *debug;
  59. int expertmode= 0;
  60. static keybindings packagelistbindings(packagelist_kinterps,packagelist_korgbindings);
  61. struct table_t {
  62. const char *name;
  63. const int num;
  64. };
  65. static const struct table_t colourtable[]= {
  66. {"black", COLOR_BLACK },
  67. {"red", COLOR_RED },
  68. {"green", COLOR_GREEN },
  69. {"yellow", COLOR_YELLOW },
  70. {"blue", COLOR_BLUE },
  71. {"magenta", COLOR_MAGENTA },
  72. {"cyan", COLOR_CYAN },
  73. {"white", COLOR_WHITE },
  74. {NULL, 0},
  75. };
  76. static const struct table_t attrtable[]= {
  77. {"normal", A_NORMAL },
  78. {"standout", A_STANDOUT },
  79. {"underline", A_UNDERLINE },
  80. {"reverse", A_REVERSE },
  81. {"blink", A_BLINK },
  82. {"bright", A_BLINK }, // on some terminals
  83. {"dim", A_DIM },
  84. {"bold", A_BOLD },
  85. {NULL, 0},
  86. };
  87. /* A slightly confusing mapping from dselect's internal names to
  88. * the user-visible names.*/
  89. static const struct table_t screenparttable[]= {
  90. {"list", list },
  91. {"listsel", listsel },
  92. {"title", title },
  93. {"infohead", thisstate },
  94. {"pkgstate", selstate },
  95. {"pkgstatesel", selstatesel },
  96. {"listhead", colheads },
  97. {"query", query },
  98. {"info", info },
  99. {"infodesc", info_head },
  100. {"infofoot", whatinfo },
  101. {"helpscreen", helpscreen },
  102. {NULL, 0},
  103. };
  104. /* Historical (patriotic?) colours. */
  105. struct colordata color[]= {
  106. /* fore back attr */
  107. {COLOR_WHITE, COLOR_BLACK, 0 }, // default, not used
  108. {COLOR_WHITE, COLOR_BLACK, 0 }, // list
  109. {COLOR_WHITE, COLOR_BLACK, A_REVERSE }, // listsel
  110. {COLOR_WHITE, COLOR_RED, 0 }, // title
  111. {COLOR_WHITE, COLOR_BLUE, 0 }, // thisstate
  112. {COLOR_WHITE, COLOR_BLACK, A_BOLD }, // selstate
  113. {COLOR_WHITE, COLOR_BLACK, A_REVERSE | A_BOLD }, // selstatesel
  114. {COLOR_WHITE, COLOR_BLUE, 0 }, // colheads
  115. {COLOR_WHITE, COLOR_RED, 0 }, // query
  116. {COLOR_WHITE, COLOR_BLACK, 0 }, // info
  117. {COLOR_WHITE, COLOR_BLACK, A_BOLD }, // info_head
  118. {COLOR_WHITE, COLOR_BLUE, 0 }, // whatinfo
  119. {COLOR_WHITE, COLOR_BLACK, 0 }, // help
  120. };
  121. struct menuentry {
  122. const char *command;
  123. const char *key;
  124. const char *option;
  125. const char *menuent;
  126. urqfunction *fn;
  127. };
  128. static const menuentry menuentries[]= {
  129. { "access", N_("a"), N_("[A]ccess"), N_("Choose the access method to use."), &urq_setup },
  130. { "update", N_("u"), N_("[U]pdate"), N_("Update list of available packages, if possible."), &urq_update },
  131. { "select", N_("s"), N_("[S]elect"), N_("Request which packages you want on your system."), &urq_list },
  132. { "install", N_("i"), N_("[I]nstall"),N_("Install and upgrade wanted packages."), &urq_install },
  133. { "config", N_("c"), N_("[C]onfig"), N_("Configure any packages that are unconfigured."), &urq_config },
  134. { "remove", N_("r"), N_("[R]emove"), N_("Remove unwanted software."), &urq_remove },
  135. { "quit", N_("q"), N_("[Q]uit"), N_("Quit dselect."), &urq_quit },
  136. { 0, 0, N_("menu"), 0, &urq_menu },
  137. { 0 }
  138. };
  139. static const char programdesc[]=
  140. N_("Debian `%s' package handling frontend version %s.\n");
  141. static const char copyrightstring[]= N_(
  142. "Copyright (C) 1994-1996 Ian Jackson.\n"
  143. "Copyright (C) 2000,2001 Wichert Akkerman.\n");
  144. static const char licensestring[]= N_(
  145. "This is free software; see the GNU General Public License version 2 or\n"
  146. "later for copying conditions. There is NO warranty.\n"
  147. "See %s --license for copyright and license details.\n");
  148. void
  149. printversion(void)
  150. {
  151. printf(gettext(programdesc), DSELECT, DPKG_VERSION_ARCH);
  152. printf(gettext(copyrightstring));
  153. printf(gettext(licensestring), DSELECT);
  154. m_output(stdout, _("<standard output>"));
  155. }
  156. void
  157. usage(void)
  158. {
  159. int i;
  160. printf(_(
  161. "Usage: %s [<option> ...] [<action> ...]\n"
  162. "\n"), DSELECT);
  163. printf(_(
  164. "Options:\n"
  165. " --admindir <directory> Use <directory> instead of %s.\n"
  166. " --expert Turn on expert mode.\n"
  167. " --debug <file> | -D<file> Turn on debugging, sending output to <file>.\n"
  168. " --colour | --color screenpart:[foreground],[background][:attr[+attr+..]]\n"
  169. " Configure screen colours.\n"
  170. "\n"), ADMINDIR);
  171. printf(_(
  172. " --help Show this help message.\n"
  173. " --version Show the version.\n"
  174. " --license | --licence Show the license.\n"
  175. "\n"));
  176. printf(_(
  177. "Actions:\n"
  178. " access update select install config remove quit\n"
  179. "\n"));
  180. printf(_("Screenparts:\n"));
  181. for (i=0; screenparttable[i].name; i++)
  182. printf(" %s", screenparttable[i].name);
  183. fputs("\n\n", stdout);
  184. printf(_("Colours:\n"));
  185. for (i=0; colourtable[i].name; i++)
  186. printf(" %s", colourtable[i].name);
  187. fputs("\n\n", stdout);
  188. printf(_("Attributes:\n"));
  189. for (i=0; attrtable[i].name; i++)
  190. printf(" %s", attrtable[i].name);
  191. fputs("\n\n", stdout);
  192. m_output(stdout, _("<standard output>"));
  193. }
  194. /* These are called by C code, so need to have C calling convention */
  195. extern "C" {
  196. static void setdebug(const struct cmdinfo*, const char *v) {
  197. debug= fopen(v,"a");
  198. if (!debug) ohshite(_("couldn't open debug file `%.255s'\n"),v);
  199. setvbuf(debug,0,_IONBF,0);
  200. }
  201. static void setexpert(const struct cmdinfo*, const char *v) {
  202. expertmode= 1;
  203. }
  204. static int
  205. findintable(const struct table_t *table, const char *item, const char *tablename)
  206. {
  207. int i;
  208. for (i= 0; item && (table[i].name!=NULL); i++)
  209. if (strcasecmp(item, table[i].name) == 0)
  210. return table[i].num;
  211. ohshit(_("Invalid %s `%s'\n"), tablename, item);
  212. }
  213. /*
  214. * The string's format is:
  215. * screenpart:[forecolor][,backcolor][:[<attr>, ...]
  216. * Examples: --color title:black,cyan:bright+underline
  217. * --color list:red,yellow
  218. * --color colheads:,green:bright
  219. * --color selstate::reverse // doesn't work FIXME
  220. */
  221. static void setcolor(const struct cmdinfo*, const char *string) {
  222. char *s= (char *) malloc((strlen(string) + 1) * sizeof(char));
  223. char *colours, *attributes, *attrib, *colourname;
  224. int screenpart, aval;
  225. strcpy(s, string); // strtok modifies strings, keep string const
  226. screenpart= findintable(screenparttable, strtok(s, ":"), _("screen part"));
  227. colours= strtok(NULL, ":");
  228. attributes= strtok(NULL, ":");
  229. if ((colours == NULL || ! strlen(colours)) &&
  230. (attributes == NULL || ! strlen(attributes))) {
  231. ohshit(_("Null colour specification\n"));
  232. }
  233. if (colours != NULL && strlen(colours)) {
  234. colourname= strtok(colours, ",");
  235. if (colourname != NULL && strlen(colourname)) {
  236. // normalize attributes to prevent confusion
  237. color[screenpart].attr= A_NORMAL;
  238. color[screenpart].fore=findintable(colourtable, colourname, _("colour"));
  239. }
  240. colourname= strtok(NULL, ",");
  241. if (colourname != NULL && strlen(colourname)) {
  242. color[screenpart].attr= A_NORMAL;
  243. color[screenpart].back=findintable(colourtable, colourname, _("colour"));
  244. }
  245. }
  246. if (attributes != NULL && strlen(attributes)) {
  247. for (attrib= strtok(attributes, "+");
  248. attrib != NULL && strlen(attrib);
  249. attrib= strtok(NULL, "+")) {
  250. aval=findintable(attrtable, attrib, _("colour attribute"));
  251. if (aval == A_NORMAL) // set to normal
  252. color[screenpart].attr= aval;
  253. else // add to existing attribs
  254. color[screenpart].attr= color[screenpart].attr | aval;
  255. }
  256. }
  257. }
  258. } /* End of extern "C" */
  259. static const struct cmdinfo cmdinfos[]= {
  260. { "admindir", 0, 1, 0, &admindir, 0 },
  261. { "debug", 'D', 1, 0, 0, setdebug },
  262. { "expert", 'E', 0, 0, 0, setexpert },
  263. { "help", 'h', 0, 0, 0, helponly },
  264. { "version", 0, 0, 0, 0, versiononly },
  265. { "licence", 0, 0, 0, 0, showcopyright }, /* UK spelling */
  266. { "license", 0, 0, 0, 0, showcopyright }, /* US spelling */
  267. { "color", 0, 1, 0, 0, setcolor }, /* US spelling */
  268. { "colour", 0, 1, 0, 0, setcolor }, /* UK spelling */
  269. { 0, 0, 0, 0, 0, 0 }
  270. };
  271. static int cursesareon= 0;
  272. void curseson() {
  273. if (!cursesareon) {
  274. const char *cup, *smso;
  275. initscr();
  276. cup= tigetstr("cup");
  277. smso= tigetstr("smso");
  278. if (!cup || !smso) {
  279. endwin();
  280. if (!cup)
  281. fputs(_("Terminal does not appear to support cursor addressing.\n"),stderr);
  282. if (!smso)
  283. fputs(_("Terminal does not appear to support highlighting.\n"),stderr);
  284. fprintf(stderr,
  285. _("Set your TERM variable correctly, use a better terminal,\n"
  286. "or make do with the per-package management tool %s.\n"),
  287. DPKG);
  288. ohshit(_("terminal lacks necessary features, giving up"));
  289. }
  290. }
  291. cursesareon= 1;
  292. }
  293. void cursesoff() {
  294. if (cursesareon) {
  295. clear();
  296. refresh();
  297. endwin();
  298. }
  299. cursesareon=0;
  300. }
  301. extern void *operator new(size_t size) {
  302. void *p;
  303. p= m_malloc(size);
  304. assert(p);
  305. return p;
  306. }
  307. extern void operator delete(void *p) {
  308. free(p);
  309. }
  310. urqresult urq_list(void) {
  311. readwrite= modstatdb_init(admindir,msdbrw_writeifposs);
  312. curseson();
  313. packagelist *l= new packagelist(&packagelistbindings);
  314. l->resolvesuggest();
  315. l->display();
  316. delete l;
  317. modstatdb_shutdown();
  318. resetpackages();
  319. return urqr_normal;
  320. }
  321. static void
  322. dme(int i, int so)
  323. {
  324. char buf[120];
  325. const menuentry *me= &menuentries[i];
  326. sprintf(buf," %c %d. %-11.11s %-80.80s ",
  327. so ? '*' : ' ', i,
  328. gettext(me->option),
  329. gettext(me->menuent));
  330. int y,x;
  331. getmaxyx(stdscr,y,x);
  332. attrset(so ? A_REVERSE : A_NORMAL);
  333. mvaddnstr(i+2,0, buf,x-1);
  334. attrset(A_NORMAL);
  335. }
  336. static int
  337. refreshmenu(void)
  338. {
  339. char buf[2048];
  340. static int l,lockfd;
  341. static char *lockfile;
  342. curseson(); cbreak(); noecho(); nonl(); keypad(stdscr,TRUE);
  343. int y,x;
  344. getmaxyx(stdscr,y,x);
  345. clear();
  346. attrset(A_BOLD);
  347. sprintf(buf, gettext(programdesc), DSELECT, DPKG_VERSION_ARCH);
  348. mvaddnstr(0,0,buf,x-1);
  349. attrset(A_NORMAL);
  350. const struct menuentry *mep; int i;
  351. for (mep=menuentries, i=0; mep->option && mep->menuent; mep++, i++)
  352. dme(i,0);
  353. attrset(A_BOLD);
  354. addstr(_("\n\n"
  355. "Move around with ^P and ^N, cursor keys, initial letters, or digits;\n"
  356. "Press <enter> to confirm selection. ^L redraws screen.\n\n"));
  357. attrset(A_NORMAL);
  358. addstr(gettext(copyrightstring));
  359. sprintf(buf, gettext(licensestring), DSELECT);
  360. addstr(buf);
  361. l= strlen(admindir);
  362. lockfile= new char[l+sizeof(LOCKFILE)+2];
  363. strcpy(lockfile,admindir);
  364. strcpy(lockfile+l, "/" LOCKFILE);
  365. lockfd= open(lockfile, O_RDWR|O_CREAT|O_TRUNC, 0660);
  366. if (errno == EACCES || errno == EPERM)
  367. addstr(_("\n\n"
  368. "Read-only access: only preview of selections is available!"));
  369. return i;
  370. }
  371. urqresult urq_menu(void) {
  372. #define C(x) ((x)-'a'+1)
  373. int entries, c;
  374. entries= refreshmenu();
  375. int cursor=0;
  376. dme(0,1);
  377. for (;;) {
  378. refresh();
  379. do
  380. c= getch();
  381. while (c == ERR && errno == EINTR);
  382. if (c==ERR) {
  383. if(errno != 0)
  384. ohshite(_("failed to getch in main menu"));
  385. else {
  386. clearok(stdscr,TRUE); clear(); refreshmenu(); dme(cursor,1);
  387. }
  388. }
  389. if (c==C('n') || c==KEY_DOWN || c==' ' || c=='j') {
  390. dme(cursor,0); cursor++; cursor %= entries; dme(cursor,1);
  391. } else if (c==C('p') || c==KEY_UP || c==C('h') ||
  392. c==KEY_BACKSPACE || c==KEY_DC || c=='k') {
  393. dme(cursor,0); cursor+= entries-1; cursor %= entries; dme(cursor,1);
  394. } else if (c=='\n' || c=='\r' || c==KEY_ENTER) {
  395. clear(); refresh();
  396. switch (menuentries[cursor].fn()) { /* FIXME: trap errors in urq_... */
  397. case urqr_quitmenu:
  398. return urqr_quitmenu;
  399. case urqr_normal:
  400. cursor++; cursor %= entries;
  401. case urqr_fail:
  402. break;
  403. default:
  404. internerr("unknown menufn");
  405. }
  406. refreshmenu(); dme(cursor,1);
  407. } else if (c==C('l')) {
  408. clearok(stdscr,TRUE); clear(); refreshmenu(); dme(cursor,1);
  409. } else if (isdigit(c)) {
  410. char buf[2]; buf[0]=c; buf[1]=0; c=atoi(buf);
  411. if (c < entries) {
  412. dme(cursor,0); cursor=c; dme(cursor,1);
  413. } else {
  414. beep();
  415. }
  416. } else if (isalpha(c)) {
  417. c= tolower(c);
  418. int i = 0;
  419. while (i < entries && gettext(menuentries[i].key)[0] != c)
  420. i++;
  421. if (i < entries) {
  422. dme(cursor,0); cursor=i; dme(cursor,1);
  423. } else {
  424. beep();
  425. }
  426. } else {
  427. beep();
  428. }
  429. }
  430. }
  431. urqresult urq_quit(void) {
  432. /* FIXME: check packages OK. */
  433. return urqr_quitmenu;
  434. }
  435. int main(int, const char *const *argv) {
  436. jmp_buf ejbuf;
  437. setlocale(LC_ALL, "");
  438. bindtextdomain(DSELECT, LOCALEDIR);
  439. textdomain(DSELECT);
  440. if (setjmp(ejbuf)) { /* expect warning about possible clobbering of argv */
  441. cursesoff();
  442. error_unwind(ehflag_bombout); exit(2);
  443. }
  444. push_error_handler(&ejbuf,print_error_fatal,0);
  445. loadcfgfile(DSELECT, cmdinfos);
  446. myopt(&argv,cmdinfos);
  447. if (*argv) {
  448. const char *a;
  449. while ((a= *argv++) != 0) {
  450. const menuentry *me = menuentries;
  451. while (me->command && strcmp(me->command, a))
  452. me++;
  453. if (!me->command) badusage(_("unknown action string `%.50s'"),a);
  454. me->fn();
  455. }
  456. } else {
  457. urq_menu();
  458. }
  459. cursesoff();
  460. standard_shutdown();
  461. return(0);
  462. }