main.cc 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535
  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 published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This is distributed in the hope that it will be useful,
  14. * but 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. static void DPKG_ATTR_NORET
  147. printversion(const struct cmdinfo *ci, const char *value)
  148. {
  149. printf(gettext(programdesc), DSELECT, DPKG_VERSION_ARCH);
  150. printf(gettext(copyrightstring));
  151. printf(gettext(licensestring), DSELECT);
  152. m_output(stdout, _("<standard output>"));
  153. exit(0);
  154. }
  155. static void DPKG_ATTR_NORET
  156. usage(const struct cmdinfo *ci, const char *value)
  157. {
  158. int i;
  159. printf(_(
  160. "Usage: %s [<option> ...] [<action> ...]\n"
  161. "\n"), DSELECT);
  162. printf(_(
  163. "Options:\n"
  164. " --admindir <directory> Use <directory> instead of %s.\n"
  165. " --expert Turn on expert mode.\n"
  166. " --debug <file> | -D<file> Turn on debugging, sending output to <file>.\n"
  167. " --colour | --color screenpart:[foreground],[background][:attr[+attr+..]]\n"
  168. " Configure screen colours.\n"
  169. "\n"), ADMINDIR);
  170. printf(_(
  171. " --help Show this help message.\n"
  172. " --version Show the version.\n"
  173. "\n"));
  174. printf(_(
  175. "Actions:\n"
  176. " access update select install config remove quit\n"
  177. "\n"));
  178. printf(_("Screenparts:\n"));
  179. for (i=0; screenparttable[i].name; i++)
  180. printf(" %s", screenparttable[i].name);
  181. fputs("\n\n", stdout);
  182. printf(_("Colours:\n"));
  183. for (i=0; colourtable[i].name; i++)
  184. printf(" %s", colourtable[i].name);
  185. fputs("\n\n", stdout);
  186. printf(_("Attributes:\n"));
  187. for (i=0; attrtable[i].name; i++)
  188. printf(" %s", attrtable[i].name);
  189. fputs("\n\n", stdout);
  190. m_output(stdout, _("<standard output>"));
  191. exit(0);
  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;
  222. char *colours, *attributes, *attrib, *colourname;
  223. int screenpart, aval;
  224. s = m_strdup(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, usage },
  263. { "version", 0, 0, 0, 0, printversion },
  264. { "color", 0, 1, 0, 0, setcolor }, /* US spelling */
  265. { "colour", 0, 1, 0, 0, setcolor }, /* UK spelling */
  266. { 0, 0, 0, 0, 0, 0 }
  267. };
  268. static int cursesareon= 0;
  269. void curseson() {
  270. if (!cursesareon) {
  271. const char *cup, *smso;
  272. initscr();
  273. cup= tigetstr("cup");
  274. smso= tigetstr("smso");
  275. if (!cup || !smso) {
  276. endwin();
  277. if (!cup)
  278. fputs(_("Terminal does not appear to support cursor addressing.\n"),stderr);
  279. if (!smso)
  280. fputs(_("Terminal does not appear to support highlighting.\n"),stderr);
  281. fprintf(stderr,
  282. _("Set your TERM variable correctly, use a better terminal,\n"
  283. "or make do with the per-package management tool %s.\n"),
  284. DPKG);
  285. ohshit(_("terminal lacks necessary features, giving up"));
  286. }
  287. }
  288. cursesareon= 1;
  289. }
  290. void cursesoff() {
  291. if (cursesareon) {
  292. clear();
  293. refresh();
  294. endwin();
  295. }
  296. cursesareon=0;
  297. }
  298. extern void *operator new(size_t size) {
  299. void *p;
  300. p= m_malloc(size);
  301. assert(p);
  302. return p;
  303. }
  304. extern void operator delete(void *p) {
  305. free(p);
  306. }
  307. urqresult urq_list(void) {
  308. readwrite= modstatdb_init(admindir,msdbrw_writeifposs);
  309. curseson();
  310. packagelist *l= new packagelist(&packagelistbindings);
  311. l->resolvesuggest();
  312. l->display();
  313. delete l;
  314. modstatdb_shutdown();
  315. resetpackages();
  316. return urqr_normal;
  317. }
  318. static void
  319. dme(int i, int so)
  320. {
  321. char buf[120];
  322. const menuentry *me= &menuentries[i];
  323. sprintf(buf," %c %d. %-11.11s %-80.80s ",
  324. so ? '*' : ' ', i,
  325. gettext(me->option),
  326. gettext(me->menuent));
  327. int y,x;
  328. getmaxyx(stdscr,y,x);
  329. attrset(so ? A_REVERSE : A_NORMAL);
  330. mvaddnstr(i+2,0, buf,x-1);
  331. attrset(A_NORMAL);
  332. }
  333. static int
  334. refreshmenu(void)
  335. {
  336. char buf[2048];
  337. static int l,lockfd;
  338. static char *lockfile;
  339. curseson(); cbreak(); noecho(); nonl(); keypad(stdscr,TRUE);
  340. int y,x;
  341. getmaxyx(stdscr,y,x);
  342. clear();
  343. attrset(A_BOLD);
  344. sprintf(buf, gettext(programdesc), DSELECT, DPKG_VERSION_ARCH);
  345. mvaddnstr(0,0,buf,x-1);
  346. attrset(A_NORMAL);
  347. const struct menuentry *mep; int i;
  348. for (mep=menuentries, i=0; mep->option && mep->menuent; mep++, i++)
  349. dme(i,0);
  350. attrset(A_BOLD);
  351. addstr(_("\n\n"
  352. "Move around with ^P and ^N, cursor keys, initial letters, or digits;\n"
  353. "Press <enter> to confirm selection. ^L redraws screen.\n\n"));
  354. attrset(A_NORMAL);
  355. addstr(gettext(copyrightstring));
  356. sprintf(buf, gettext(licensestring), DSELECT);
  357. addstr(buf);
  358. l= strlen(admindir);
  359. lockfile= new char[l+sizeof(LOCKFILE)+2];
  360. strcpy(lockfile,admindir);
  361. strcpy(lockfile+l, "/" LOCKFILE);
  362. lockfd= open(lockfile, O_RDWR|O_CREAT|O_TRUNC, 0660);
  363. if (errno == EACCES || errno == EPERM)
  364. addstr(_("\n\n"
  365. "Read-only access: only preview of selections is available!"));
  366. return i;
  367. }
  368. urqresult urq_menu(void) {
  369. #define C(x) ((x)-'a'+1)
  370. int entries, c;
  371. entries= refreshmenu();
  372. int cursor=0;
  373. dme(0,1);
  374. for (;;) {
  375. refresh();
  376. do
  377. c= getch();
  378. while (c == ERR && errno == EINTR);
  379. if (c==ERR) {
  380. if(errno != 0)
  381. ohshite(_("failed to getch in main menu"));
  382. else {
  383. clearok(stdscr,TRUE); clear(); refreshmenu(); dme(cursor,1);
  384. }
  385. }
  386. if (c==C('n') || c==KEY_DOWN || c==' ' || c=='j') {
  387. dme(cursor,0); cursor++; cursor %= entries; dme(cursor,1);
  388. } else if (c==C('p') || c==KEY_UP || c==C('h') ||
  389. c==KEY_BACKSPACE || c==KEY_DC || c=='k') {
  390. dme(cursor,0); cursor+= entries-1; cursor %= entries; dme(cursor,1);
  391. } else if (c=='\n' || c=='\r' || c==KEY_ENTER) {
  392. clear(); refresh();
  393. switch (menuentries[cursor].fn()) { /* FIXME: trap errors in urq_... */
  394. case urqr_quitmenu:
  395. return urqr_quitmenu;
  396. case urqr_normal:
  397. cursor++; cursor %= entries;
  398. case urqr_fail:
  399. break;
  400. default:
  401. internerr("unknown menufn");
  402. }
  403. refreshmenu(); dme(cursor,1);
  404. } else if (c==C('l')) {
  405. clearok(stdscr,TRUE); clear(); refreshmenu(); dme(cursor,1);
  406. } else if (isdigit(c)) {
  407. char buf[2]; buf[0]=c; buf[1]=0; c=atoi(buf);
  408. if (c < entries) {
  409. dme(cursor,0); cursor=c; dme(cursor,1);
  410. } else {
  411. beep();
  412. }
  413. } else if (isalpha(c)) {
  414. c= tolower(c);
  415. int i = 0;
  416. while (i < entries && gettext(menuentries[i].key)[0] != c)
  417. i++;
  418. if (i < entries) {
  419. dme(cursor,0); cursor=i; dme(cursor,1);
  420. } else {
  421. beep();
  422. }
  423. } else {
  424. beep();
  425. }
  426. }
  427. }
  428. urqresult urq_quit(void) {
  429. /* FIXME: check packages OK. */
  430. return urqr_quitmenu;
  431. }
  432. int main(int, const char *const *argv) {
  433. jmp_buf ejbuf;
  434. setlocale(LC_ALL, "");
  435. bindtextdomain(DSELECT, LOCALEDIR);
  436. textdomain(DSELECT);
  437. if (setjmp(ejbuf)) { /* expect warning about possible clobbering of argv */
  438. cursesoff();
  439. error_unwind(ehflag_bombout); exit(2);
  440. }
  441. push_error_handler(&ejbuf,print_error_fatal,0);
  442. loadcfgfile(DSELECT, cmdinfos);
  443. myopt(&argv,cmdinfos);
  444. if (*argv) {
  445. const char *a;
  446. while ((a= *argv++) != 0) {
  447. const menuentry *me = menuentries;
  448. while (me->command && strcmp(me->command, a))
  449. me++;
  450. if (!me->command) badusage(_("unknown action string `%.50s'"),a);
  451. me->fn();
  452. }
  453. } else {
  454. urq_menu();
  455. }
  456. cursesoff();
  457. standard_shutdown();
  458. return(0);
  459. }