main.cc 15 KB

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