main.cc 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540
  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 <sys/types.h>
  25. #include <sys/stat.h>
  26. #include <sys/wait.h>
  27. #include <assert.h>
  28. #include <errno.h>
  29. #include <limits.h>
  30. #if HAVE_LOCALE_H
  31. #include <locale.h>
  32. #endif
  33. #include <ctype.h>
  34. #include <string.h>
  35. #include <fcntl.h>
  36. #include <dirent.h>
  37. #include <unistd.h>
  38. #include <stdlib.h>
  39. #include <stdio.h>
  40. #if defined(HAVE_NCURSESW_TERM_H)
  41. #include <ncursesw/term.h>
  42. #elif defined(HAVE_NCURSES_TERM_H)
  43. #include <ncurses/term.h>
  44. #else
  45. #include <term.h>
  46. #endif
  47. #include <dpkg/i18n.h>
  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. static void
  149. printversion(const struct cmdinfo *ci, const char *value)
  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. exit(0);
  156. }
  157. static void
  158. usage(const struct cmdinfo *ci, const char *value)
  159. {
  160. int i;
  161. printf(_(
  162. "Usage: %s [<option> ...] [<action> ...]\n"
  163. "\n"), DSELECT);
  164. printf(_(
  165. "Options:\n"
  166. " --admindir <directory> Use <directory> instead of %s.\n"
  167. " --expert Turn on expert mode.\n"
  168. " --debug <file> | -D<file> Turn on debugging, sending output to <file>.\n"
  169. " --colour | --color screenpart:[foreground],[background][:attr[+attr+..]]\n"
  170. " Configure screen colours.\n"
  171. "\n"), ADMINDIR);
  172. printf(_(
  173. " --help Show this help message.\n"
  174. " --version Show the version.\n"
  175. " --license | --licence Show the license.\n"
  176. "\n"));
  177. printf(_(
  178. "Actions:\n"
  179. " access update select install config remove quit\n"
  180. "\n"));
  181. printf(_("Screenparts:\n"));
  182. for (i=0; screenparttable[i].name; i++)
  183. printf(" %s", screenparttable[i].name);
  184. fputs("\n\n", stdout);
  185. printf(_("Colours:\n"));
  186. for (i=0; colourtable[i].name; i++)
  187. printf(" %s", colourtable[i].name);
  188. fputs("\n\n", stdout);
  189. printf(_("Attributes:\n"));
  190. for (i=0; attrtable[i].name; i++)
  191. printf(" %s", attrtable[i].name);
  192. fputs("\n\n", stdout);
  193. m_output(stdout, _("<standard output>"));
  194. exit(0);
  195. }
  196. /* These are called by C code, so need to have C calling convention */
  197. extern "C" {
  198. static void setdebug(const struct cmdinfo*, const char *v) {
  199. debug= fopen(v,"a");
  200. if (!debug) ohshite(_("couldn't open debug file `%.255s'\n"),v);
  201. setvbuf(debug,0,_IONBF,0);
  202. }
  203. static void setexpert(const struct cmdinfo*, const char *v) {
  204. expertmode= 1;
  205. }
  206. static int
  207. findintable(const struct table_t *table, const char *item, const char *tablename)
  208. {
  209. int i;
  210. for (i= 0; item && (table[i].name!=NULL); i++)
  211. if (strcasecmp(item, table[i].name) == 0)
  212. return table[i].num;
  213. ohshit(_("Invalid %s `%s'\n"), tablename, item);
  214. }
  215. /*
  216. * The string's format is:
  217. * screenpart:[forecolor][,backcolor][:[<attr>, ...]
  218. * Examples: --color title:black,cyan:bright+underline
  219. * --color list:red,yellow
  220. * --color colheads:,green:bright
  221. * --color selstate::reverse // doesn't work FIXME
  222. */
  223. static void setcolor(const struct cmdinfo*, const char *string) {
  224. char *s= (char *) malloc((strlen(string) + 1) * sizeof(char));
  225. char *colours, *attributes, *attrib, *colourname;
  226. int screenpart, aval;
  227. strcpy(s, string); // strtok modifies strings, keep string const
  228. screenpart= findintable(screenparttable, strtok(s, ":"), _("screen part"));
  229. colours= strtok(NULL, ":");
  230. attributes= strtok(NULL, ":");
  231. if ((colours == NULL || ! strlen(colours)) &&
  232. (attributes == NULL || ! strlen(attributes))) {
  233. ohshit(_("Null colour specification\n"));
  234. }
  235. if (colours != NULL && strlen(colours)) {
  236. colourname= strtok(colours, ",");
  237. if (colourname != NULL && strlen(colourname)) {
  238. // normalize attributes to prevent confusion
  239. color[screenpart].attr= A_NORMAL;
  240. color[screenpart].fore=findintable(colourtable, colourname, _("colour"));
  241. }
  242. colourname= strtok(NULL, ",");
  243. if (colourname != NULL && strlen(colourname)) {
  244. color[screenpart].attr= A_NORMAL;
  245. color[screenpart].back=findintable(colourtable, colourname, _("colour"));
  246. }
  247. }
  248. if (attributes != NULL && strlen(attributes)) {
  249. for (attrib= strtok(attributes, "+");
  250. attrib != NULL && strlen(attrib);
  251. attrib= strtok(NULL, "+")) {
  252. aval=findintable(attrtable, attrib, _("colour attribute"));
  253. if (aval == A_NORMAL) // set to normal
  254. color[screenpart].attr= aval;
  255. else // add to existing attribs
  256. color[screenpart].attr= color[screenpart].attr | aval;
  257. }
  258. }
  259. }
  260. } /* End of extern "C" */
  261. static const struct cmdinfo cmdinfos[]= {
  262. { "admindir", 0, 1, 0, &admindir, 0 },
  263. { "debug", 'D', 1, 0, 0, setdebug },
  264. { "expert", 'E', 0, 0, 0, setexpert },
  265. { "help", 'h', 0, 0, 0, usage },
  266. { "version", 0, 0, 0, 0, printversion },
  267. { "licence", 0, 0, 0, 0, showcopyright }, /* UK spelling */
  268. { "license", 0, 0, 0, 0, showcopyright }, /* US spelling */
  269. { "color", 0, 1, 0, 0, setcolor }, /* US spelling */
  270. { "colour", 0, 1, 0, 0, setcolor }, /* UK spelling */
  271. { 0, 0, 0, 0, 0, 0 }
  272. };
  273. static int cursesareon= 0;
  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= 1;
  294. }
  295. void cursesoff() {
  296. if (cursesareon) {
  297. clear();
  298. refresh();
  299. endwin();
  300. }
  301. cursesareon=0;
  302. }
  303. extern void *operator new(size_t size) {
  304. void *p;
  305. p= m_malloc(size);
  306. assert(p);
  307. return p;
  308. }
  309. extern void operator delete(void *p) {
  310. free(p);
  311. }
  312. urqresult urq_list(void) {
  313. readwrite= modstatdb_init(admindir,msdbrw_writeifposs);
  314. curseson();
  315. packagelist *l= new packagelist(&packagelistbindings);
  316. l->resolvesuggest();
  317. l->display();
  318. delete l;
  319. modstatdb_shutdown();
  320. resetpackages();
  321. return urqr_normal;
  322. }
  323. static void
  324. dme(int i, int so)
  325. {
  326. char buf[120];
  327. const menuentry *me= &menuentries[i];
  328. sprintf(buf," %c %d. %-11.11s %-80.80s ",
  329. so ? '*' : ' ', i,
  330. gettext(me->option),
  331. gettext(me->menuent));
  332. int y,x;
  333. getmaxyx(stdscr,y,x);
  334. attrset(so ? A_REVERSE : A_NORMAL);
  335. mvaddnstr(i+2,0, buf,x-1);
  336. attrset(A_NORMAL);
  337. }
  338. static int
  339. refreshmenu(void)
  340. {
  341. char buf[2048];
  342. static int l,lockfd;
  343. static char *lockfile;
  344. curseson(); cbreak(); noecho(); nonl(); keypad(stdscr,TRUE);
  345. int y,x;
  346. getmaxyx(stdscr,y,x);
  347. clear();
  348. attrset(A_BOLD);
  349. sprintf(buf, gettext(programdesc), DSELECT, DPKG_VERSION_ARCH);
  350. mvaddnstr(0,0,buf,x-1);
  351. attrset(A_NORMAL);
  352. const struct menuentry *mep; int i;
  353. for (mep=menuentries, i=0; mep->option && mep->menuent; mep++, i++)
  354. dme(i,0);
  355. attrset(A_BOLD);
  356. addstr(_("\n\n"
  357. "Move around with ^P and ^N, cursor keys, initial letters, or digits;\n"
  358. "Press <enter> to confirm selection. ^L redraws screen.\n\n"));
  359. attrset(A_NORMAL);
  360. addstr(gettext(copyrightstring));
  361. sprintf(buf, gettext(licensestring), DSELECT);
  362. addstr(buf);
  363. l= strlen(admindir);
  364. lockfile= new char[l+sizeof(LOCKFILE)+2];
  365. strcpy(lockfile,admindir);
  366. strcpy(lockfile+l, "/" LOCKFILE);
  367. lockfd= open(lockfile, O_RDWR|O_CREAT|O_TRUNC, 0660);
  368. if (errno == EACCES || errno == EPERM)
  369. addstr(_("\n\n"
  370. "Read-only access: only preview of selections is available!"));
  371. return i;
  372. }
  373. urqresult urq_menu(void) {
  374. #define C(x) ((x)-'a'+1)
  375. int entries, c;
  376. entries= refreshmenu();
  377. int cursor=0;
  378. dme(0,1);
  379. for (;;) {
  380. refresh();
  381. do
  382. c= getch();
  383. while (c == ERR && errno == EINTR);
  384. if (c==ERR) {
  385. if(errno != 0)
  386. ohshite(_("failed to getch in main menu"));
  387. else {
  388. clearok(stdscr,TRUE); clear(); refreshmenu(); dme(cursor,1);
  389. }
  390. }
  391. if (c==C('n') || c==KEY_DOWN || c==' ' || c=='j') {
  392. dme(cursor,0); cursor++; cursor %= entries; dme(cursor,1);
  393. } else if (c==C('p') || c==KEY_UP || c==C('h') ||
  394. c==KEY_BACKSPACE || c==KEY_DC || c=='k') {
  395. dme(cursor,0); cursor+= entries-1; cursor %= entries; dme(cursor,1);
  396. } else if (c=='\n' || c=='\r' || c==KEY_ENTER) {
  397. clear(); refresh();
  398. switch (menuentries[cursor].fn()) { /* FIXME: trap errors in urq_... */
  399. case urqr_quitmenu:
  400. return urqr_quitmenu;
  401. case urqr_normal:
  402. cursor++; cursor %= entries;
  403. case urqr_fail:
  404. break;
  405. default:
  406. internerr("unknown menufn");
  407. }
  408. refreshmenu(); dme(cursor,1);
  409. } else if (c==C('l')) {
  410. clearok(stdscr,TRUE); clear(); refreshmenu(); dme(cursor,1);
  411. } else if (isdigit(c)) {
  412. char buf[2]; buf[0]=c; buf[1]=0; c=atoi(buf);
  413. if (c < entries) {
  414. dme(cursor,0); cursor=c; dme(cursor,1);
  415. } else {
  416. beep();
  417. }
  418. } else if (isalpha(c)) {
  419. c= tolower(c);
  420. int i = 0;
  421. while (i < entries && gettext(menuentries[i].key)[0] != c)
  422. i++;
  423. if (i < entries) {
  424. dme(cursor,0); cursor=i; dme(cursor,1);
  425. } else {
  426. beep();
  427. }
  428. } else {
  429. beep();
  430. }
  431. }
  432. }
  433. urqresult urq_quit(void) {
  434. /* FIXME: check packages OK. */
  435. return urqr_quitmenu;
  436. }
  437. int main(int, const char *const *argv) {
  438. jmp_buf ejbuf;
  439. setlocale(LC_ALL, "");
  440. bindtextdomain(DSELECT, LOCALEDIR);
  441. textdomain(DSELECT);
  442. if (setjmp(ejbuf)) { /* expect warning about possible clobbering of argv */
  443. cursesoff();
  444. error_unwind(ehflag_bombout); exit(2);
  445. }
  446. push_error_handler(&ejbuf,print_error_fatal,0);
  447. loadcfgfile(DSELECT, cmdinfos);
  448. myopt(&argv,cmdinfos);
  449. if (*argv) {
  450. const char *a;
  451. while ((a= *argv++) != 0) {
  452. const menuentry *me = menuentries;
  453. while (me->command && strcmp(me->command, a))
  454. me++;
  455. if (!me->command) badusage(_("unknown action string `%.50s'"),a);
  456. me->fn();
  457. }
  458. } else {
  459. urq_menu();
  460. }
  461. cursesoff();
  462. standard_shutdown();
  463. return(0);
  464. }