main.cc 15 KB

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