main.cc 15 KB

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