main.cc 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539
  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 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/myopt.h>
  50. #include "dselect.h"
  51. #include "bindings.h"
  52. #include "pkglist.h"
  53. const char thisname[]= DSELECT;
  54. const char printforhelp[]= N_("Type dselect --help for help.");
  55. modstatdb_rw readwrite;
  56. const char *admindir= ADMINDIR;
  57. FILE *debug;
  58. int expertmode= 0;
  59. static keybindings packagelistbindings(packagelist_kinterps,packagelist_korgbindings);
  60. struct table_t {
  61. const char *name;
  62. const int num;
  63. };
  64. static const struct table_t colourtable[]= {
  65. {"black", COLOR_BLACK },
  66. {"red", COLOR_RED },
  67. {"green", COLOR_GREEN },
  68. {"yellow", COLOR_YELLOW },
  69. {"blue", COLOR_BLUE },
  70. {"magenta", COLOR_MAGENTA },
  71. {"cyan", COLOR_CYAN },
  72. {"white", COLOR_WHITE },
  73. {NULL, 0},
  74. };
  75. static const struct table_t attrtable[]= {
  76. {"normal", A_NORMAL },
  77. {"standout", A_STANDOUT },
  78. {"underline", A_UNDERLINE },
  79. {"reverse", A_REVERSE },
  80. {"blink", A_BLINK },
  81. {"bright", A_BLINK }, // on some terminals
  82. {"dim", A_DIM },
  83. {"bold", A_BOLD },
  84. {NULL, 0},
  85. };
  86. /* A slightly confusing mapping from dselect's internal names to
  87. * the user-visible names.*/
  88. static const struct table_t screenparttable[]= {
  89. {"list", list },
  90. {"listsel", listsel },
  91. {"title", title },
  92. {"infohead", thisstate },
  93. {"pkgstate", selstate },
  94. {"pkgstatesel", selstatesel },
  95. {"listhead", colheads },
  96. {"query", query },
  97. {"info", info },
  98. {"infodesc", info_head },
  99. {"infofoot", whatinfo },
  100. {"helpscreen", helpscreen },
  101. {NULL, 0},
  102. };
  103. /* Historical (patriotic?) colours. */
  104. struct colordata color[]= {
  105. /* fore back attr */
  106. {COLOR_WHITE, COLOR_BLACK, 0 }, // default, not used
  107. {COLOR_WHITE, COLOR_BLACK, 0 }, // list
  108. {COLOR_WHITE, COLOR_BLACK, A_REVERSE }, // listsel
  109. {COLOR_WHITE, COLOR_RED, 0 }, // title
  110. {COLOR_WHITE, COLOR_BLUE, 0 }, // thisstate
  111. {COLOR_WHITE, COLOR_BLACK, A_BOLD }, // selstate
  112. {COLOR_WHITE, COLOR_BLACK, A_REVERSE | A_BOLD }, // selstatesel
  113. {COLOR_WHITE, COLOR_BLUE, 0 }, // colheads
  114. {COLOR_WHITE, COLOR_RED, 0 }, // query
  115. {COLOR_WHITE, COLOR_BLACK, 0 }, // info
  116. {COLOR_WHITE, COLOR_BLACK, A_BOLD }, // info_head
  117. {COLOR_WHITE, COLOR_BLUE, 0 }, // whatinfo
  118. {COLOR_WHITE, COLOR_BLACK, 0 }, // help
  119. };
  120. struct menuentry {
  121. const char *command;
  122. const char *key;
  123. const char *option;
  124. const char *menuent;
  125. urqfunction *fn;
  126. };
  127. static const menuentry menuentries[]= {
  128. { "access", N_("a"), N_("[A]ccess"), N_("Choose the access method to use."), &urq_setup },
  129. { "update", N_("u"), N_("[U]pdate"), N_("Update list of available packages, if possible."), &urq_update },
  130. { "select", N_("s"), N_("[S]elect"), N_("Request which packages you want on your system."), &urq_list },
  131. { "install", N_("i"), N_("[I]nstall"),N_("Install and upgrade wanted packages."), &urq_install },
  132. { "config", N_("c"), N_("[C]onfig"), N_("Configure any packages that are unconfigured."), &urq_config },
  133. { "remove", N_("r"), N_("[R]emove"), N_("Remove unwanted software."), &urq_remove },
  134. { "quit", N_("q"), N_("[Q]uit"), N_("Quit dselect."), &urq_quit },
  135. { 0, 0, N_("menu"), 0, &urq_menu },
  136. { 0 }
  137. };
  138. static const char programdesc[]=
  139. N_("Debian `%s' package handling frontend version %s.\n");
  140. static const char copyrightstring[]= N_(
  141. "Copyright (C) 1994-1996 Ian Jackson.\n"
  142. "Copyright (C) 2000,2001 Wichert Akkerman.\n");
  143. static const char licensestring[]= N_(
  144. "This is free software; see the GNU General Public License version 2 or\n"
  145. "later for copying conditions. There is NO warranty.\n"
  146. "See %s --license for copyright and license details.\n");
  147. static void
  148. printversion(const struct cmdinfo *ci, const char *value)
  149. {
  150. printf(gettext(programdesc), DSELECT, DPKG_VERSION_ARCH);
  151. printf(gettext(copyrightstring));
  152. printf(gettext(licensestring), DSELECT);
  153. m_output(stdout, _("<standard output>"));
  154. exit(0);
  155. }
  156. static void
  157. usage(const struct cmdinfo *ci, const char *value)
  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. exit(0);
  194. }
  195. /* These are called by C code, so need to have C calling convention */
  196. extern "C" {
  197. static void setdebug(const struct cmdinfo*, const char *v) {
  198. debug= fopen(v,"a");
  199. if (!debug) ohshite(_("couldn't open debug file `%.255s'\n"),v);
  200. setvbuf(debug,0,_IONBF,0);
  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= (char *) malloc((strlen(string) + 1) * sizeof(char));
  224. char *colours, *attributes, *attrib, *colourname;
  225. int screenpart, aval;
  226. strcpy(s, 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. { "licence", 0, 0, 0, 0, showcopyright }, /* UK spelling */
  267. { "license", 0, 0, 0, 0, showcopyright }, /* US spelling */
  268. { "color", 0, 1, 0, 0, setcolor }, /* US spelling */
  269. { "colour", 0, 1, 0, 0, setcolor }, /* UK spelling */
  270. { 0, 0, 0, 0, 0, 0 }
  271. };
  272. static int cursesareon= 0;
  273. void curseson() {
  274. if (!cursesareon) {
  275. const char *cup, *smso;
  276. initscr();
  277. cup= tigetstr("cup");
  278. smso= tigetstr("smso");
  279. if (!cup || !smso) {
  280. endwin();
  281. if (!cup)
  282. fputs(_("Terminal does not appear to support cursor addressing.\n"),stderr);
  283. if (!smso)
  284. fputs(_("Terminal does not appear to support highlighting.\n"),stderr);
  285. fprintf(stderr,
  286. _("Set your TERM variable correctly, use a better terminal,\n"
  287. "or make do with the per-package management tool %s.\n"),
  288. DPKG);
  289. ohshit(_("terminal lacks necessary features, giving up"));
  290. }
  291. }
  292. cursesareon= 1;
  293. }
  294. void cursesoff() {
  295. if (cursesareon) {
  296. clear();
  297. refresh();
  298. endwin();
  299. }
  300. cursesareon=0;
  301. }
  302. extern void *operator new(size_t size) {
  303. void *p;
  304. p= m_malloc(size);
  305. assert(p);
  306. return p;
  307. }
  308. extern void operator delete(void *p) {
  309. free(p);
  310. }
  311. urqresult urq_list(void) {
  312. readwrite= modstatdb_init(admindir,msdbrw_writeifposs);
  313. curseson();
  314. packagelist *l= new packagelist(&packagelistbindings);
  315. l->resolvesuggest();
  316. l->display();
  317. delete l;
  318. modstatdb_shutdown();
  319. resetpackages();
  320. return urqr_normal;
  321. }
  322. static void
  323. dme(int i, int so)
  324. {
  325. char buf[120];
  326. const menuentry *me= &menuentries[i];
  327. sprintf(buf," %c %d. %-11.11s %-80.80s ",
  328. so ? '*' : ' ', i,
  329. gettext(me->option),
  330. gettext(me->menuent));
  331. int y,x;
  332. getmaxyx(stdscr,y,x);
  333. attrset(so ? A_REVERSE : A_NORMAL);
  334. mvaddnstr(i+2,0, buf,x-1);
  335. attrset(A_NORMAL);
  336. }
  337. static int
  338. refreshmenu(void)
  339. {
  340. char buf[2048];
  341. static int l,lockfd;
  342. static char *lockfile;
  343. curseson(); cbreak(); noecho(); nonl(); keypad(stdscr,TRUE);
  344. int y,x;
  345. getmaxyx(stdscr,y,x);
  346. clear();
  347. attrset(A_BOLD);
  348. sprintf(buf, gettext(programdesc), DSELECT, DPKG_VERSION_ARCH);
  349. mvaddnstr(0,0,buf,x-1);
  350. attrset(A_NORMAL);
  351. const struct menuentry *mep; int i;
  352. for (mep=menuentries, i=0; mep->option && mep->menuent; mep++, i++)
  353. dme(i,0);
  354. attrset(A_BOLD);
  355. addstr(_("\n\n"
  356. "Move around with ^P and ^N, cursor keys, initial letters, or digits;\n"
  357. "Press <enter> to confirm selection. ^L redraws screen.\n\n"));
  358. attrset(A_NORMAL);
  359. addstr(gettext(copyrightstring));
  360. sprintf(buf, gettext(licensestring), DSELECT);
  361. addstr(buf);
  362. l= strlen(admindir);
  363. lockfile= new char[l+sizeof(LOCKFILE)+2];
  364. strcpy(lockfile,admindir);
  365. strcpy(lockfile+l, "/" LOCKFILE);
  366. lockfd= open(lockfile, O_RDWR|O_CREAT|O_TRUNC, 0660);
  367. if (errno == EACCES || errno == EPERM)
  368. addstr(_("\n\n"
  369. "Read-only access: only preview of selections is available!"));
  370. return i;
  371. }
  372. urqresult urq_menu(void) {
  373. #define C(x) ((x)-'a'+1)
  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==C('n') || c==KEY_DOWN || c==' ' || c=='j') {
  391. dme(cursor,0); cursor++; cursor %= entries; dme(cursor,1);
  392. } else if (c==C('p') || c==KEY_UP || c==C('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. switch (menuentries[cursor].fn()) { /* FIXME: trap errors in urq_... */
  398. case urqr_quitmenu:
  399. return urqr_quitmenu;
  400. case urqr_normal:
  401. cursor++; cursor %= entries;
  402. case urqr_fail:
  403. break;
  404. default:
  405. internerr("unknown menufn");
  406. }
  407. refreshmenu(); dme(cursor,1);
  408. } else if (c==C('l')) {
  409. clearok(stdscr,TRUE); clear(); refreshmenu(); dme(cursor,1);
  410. } else if (isdigit(c)) {
  411. char buf[2]; buf[0]=c; buf[1]=0; c=atoi(buf);
  412. if (c < entries) {
  413. dme(cursor,0); cursor=c; dme(cursor,1);
  414. } else {
  415. beep();
  416. }
  417. } else if (isalpha(c)) {
  418. c= tolower(c);
  419. int i = 0;
  420. while (i < entries && gettext(menuentries[i].key)[0] != c)
  421. i++;
  422. if (i < entries) {
  423. dme(cursor,0); cursor=i; dme(cursor,1);
  424. } else {
  425. beep();
  426. }
  427. } else {
  428. beep();
  429. }
  430. }
  431. }
  432. urqresult urq_quit(void) {
  433. /* FIXME: check packages OK. */
  434. return urqr_quitmenu;
  435. }
  436. int main(int, const char *const *argv) {
  437. jmp_buf ejbuf;
  438. setlocale(LC_ALL, "");
  439. bindtextdomain(DSELECT, LOCALEDIR);
  440. textdomain(DSELECT);
  441. if (setjmp(ejbuf)) { /* expect warning about possible clobbering of argv */
  442. cursesoff();
  443. error_unwind(ehflag_bombout); exit(2);
  444. }
  445. push_error_handler(&ejbuf,print_error_fatal,0);
  446. loadcfgfile(DSELECT, cmdinfos);
  447. myopt(&argv,cmdinfos);
  448. if (*argv) {
  449. const char *a;
  450. while ((a= *argv++) != 0) {
  451. const menuentry *me = menuentries;
  452. while (me->command && strcmp(me->command, a))
  453. me++;
  454. if (!me->command) badusage(_("unknown action string `%.50s'"),a);
  455. me->fn();
  456. }
  457. } else {
  458. urq_menu();
  459. }
  460. cursesoff();
  461. standard_shutdown();
  462. return(0);
  463. }