main.cc 15 KB

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