main.cc 15 KB

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