main.cc 15 KB

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