main.cc 15 KB

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