main.cc 15 KB

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