main.cc 15 KB

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