main.cc 15 KB

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