main.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642
  1. /*
  2. * dpkg - main program for package management
  3. * main.c - main program
  4. *
  5. * Copyright (C) 1994,1995 Ian Jackson <ian@chiark.greenend.org.uk>
  6. *
  7. * This is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as
  9. * published by the Free Software Foundation; either version 2,
  10. * or (at your option) any later version.
  11. *
  12. * This is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public
  18. * License along with dpkg; if not, write to the Free Software
  19. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  20. */
  21. #include <config.h>
  22. #include <stdio.h>
  23. #include <string.h>
  24. #include <stdlib.h>
  25. #include <signal.h>
  26. #include <sys/stat.h>
  27. #include <sys/types.h>
  28. #include <sys/wait.h>
  29. #include <errno.h>
  30. #include <unistd.h>
  31. #include <dirent.h>
  32. #include <limits.h>
  33. #include <ctype.h>
  34. #include <fcntl.h>
  35. #include <dpkg.h>
  36. #include <dpkg-db.h>
  37. #include <myopt.h>
  38. #include "main.h"
  39. void
  40. printversion(void)
  41. {
  42. if (printf(_("Debian `%s' package management program version %s.\n"),
  43. DPKG, DPKG_VERSION_ARCH) < 0) werr("stdout");
  44. if (printf(_("This is free software; see the GNU General Public License version 2 or\n"
  45. "later for copying conditions. There is NO warranty.\n"
  46. "See %s --license for copyright and license details.\n"),
  47. DPKG) < 0) werr("stdout");
  48. }
  49. /*
  50. options that need fixing:
  51. dpkg --yet-to-unpack \n\
  52. */
  53. void
  54. usage(void)
  55. {
  56. if (fprintf (stdout, _(
  57. "Usage: %s [<option> ...] <command>\n"
  58. "\n"), DPKG) < 0) werr ("stdout");
  59. if (fprintf (stdout, _(
  60. "Commands:\n"
  61. " -i|--install <.deb file name> ... | -R|--recursive <directory> ...\n"
  62. " --unpack <.deb file name> ... | -R|--recursive <directory> ...\n"
  63. " -A|--record-avail <.deb file name> ... | -R|--recursive <directory> ...\n"
  64. " --configure <package> ... | -a|--pending\n"
  65. " --triggers-only <package> ... | -a|--pending\n"
  66. " -r|--remove <package> ... | -a|--pending\n"
  67. " -P|--purge <package> ... | -a|--pending\n"
  68. " --get-selections [<pattern> ...] Get list of selections to stdout.\n"
  69. " --set-selections Set package selections from stdin.\n"
  70. " --clear-selections Deselect every non-essential package.\n"
  71. " --update-avail <Packages-file> Replace available packages info.\n"
  72. " --merge-avail <Packages-file> Merge with info from file.\n"
  73. " --clear-avail Erase existing available info.\n"
  74. " --forget-old-unavail Forget uninstalled unavailable pkgs.\n"
  75. " -s|--status <package> ... Display package status details.\n"
  76. " -p|--print-avail <package> ... Display available version details.\n"
  77. " -L|--listfiles <package> ... List files `owned' by package(s).\n"
  78. " -l|--list [<pattern> ...] List packages concisely.\n"
  79. " -S|--search <pattern> ... Find package(s) owning file(s).\n"
  80. " -C|--audit Check for broken package(s).\n"
  81. " --print-architecture Print dpkg architecture.\n"
  82. " --compare-versions <a> <op> <b> Compare version numbers - see below.\n"
  83. " --force-help Show help on forcing.\n"
  84. " -Dh|--debug=help Show help on debugging.\n"
  85. "\n")) < 0) werr ("stdout");
  86. if (fprintf (stdout, _(
  87. " -h|--help Show this help message.\n"
  88. " --version Show the version.\n"
  89. " --license|--licence Show the copyright licensing terms.\n"
  90. "\n")) < 0) werr ("stdout");
  91. if (fprintf (stdout, _(
  92. "Use dpkg -b|--build|-c|--contents|-e|--control|-I|--info|-f|--field|\n"
  93. " -x|--extract|-X|--vextract|--fsys-tarfile on archives (type %s --help).\n"
  94. "\n"), BACKEND) < 0) werr ("stdout");
  95. if (fprintf (stdout, _(
  96. "For internal use: dpkg --assert-support-predepends | --predep-package |\n"
  97. " --assert-working-epoch | --assert-long-filenames | --assert-multi-conrep.\n"
  98. "\n")) < 0) werr ("stdout");
  99. if (fprintf (stdout, _(
  100. "Options:\n"
  101. " --admindir=<directory> Use <directory> instead of %s.\n"
  102. " --root=<directory> Install on a different root directory.\n"
  103. " --instdir=<directory> Change installation dir without changing admin dir.\n"
  104. " -O|--selected-only Skip packages not selected for install/upgrade.\n"
  105. " -E|--skip-same-version Skip packages whose same version is installed.\n"
  106. " -G|--refuse-downgrade Skip packages with earlier version than installed.\n"
  107. " -B|--auto-deconfigure Install even if it would break some other package.\n"
  108. " --[no-]triggers Skip or force consequential trigger processing.\n"
  109. " --no-debsig Do not try to verify package signatures.\n"
  110. " --no-act|--dry-run|--simulate\n"
  111. " Just say what we would do - don't do it.\n"
  112. " -D|--debug=<octal> Enable debugging (see -Dhelp or --debug=help).\n"
  113. " --status-fd <n> Send status change updates to file descriptor <n>.\n"
  114. " --log=<filename> Log status changes and actions to <filename>.\n"
  115. " --ignore-depends=<package>,...\n"
  116. " Ignore dependencies involving <package>.\n"
  117. " --force-... Override problems (see --force-help).\n"
  118. " --no-force-...|--refuse-...\n"
  119. " Stop when problems encountered.\n"
  120. " --abort-after <n> Abort after encountering <n> errors.\n"
  121. "\n"), ADMINDIR) < 0) werr ("stdout");
  122. if (fprintf (stdout, _(
  123. "Comparison operators for --compare-versions are:\n"
  124. " lt le eq ne ge gt (treat empty version as earlier than any version);\n"
  125. " lt-nl le-nl ge-nl gt-nl (treat empty version as later than any version);\n"
  126. " < << <= = >= >> > (only for compatibility with control file syntax).\n"
  127. "\n")) < 0) werr ("stdout");
  128. if (fprintf (stdout, _(
  129. "Use `dselect' or `aptitude' for user-friendly package management.\n")) < 0)
  130. werr ("stdout");
  131. }
  132. const char thisname[]= "dpkg";
  133. const char architecture[]= ARCHITECTURE;
  134. const char printforhelp[]= N_(
  135. "Type dpkg --help for help about installing and deinstalling packages [*];\n"
  136. "Use `dselect' or `aptitude' for user-friendly package management;\n"
  137. "Type dpkg -Dhelp for a list of dpkg debug flag values;\n"
  138. "Type dpkg --force-help for a list of forcing options;\n"
  139. "Type dpkg-deb --help for help about manipulating *.deb files;\n"
  140. "Type dpkg --license for copyright license and lack of warranty (GNU GPL) [*].\n"
  141. "\n"
  142. "Options marked [*] produce a lot of output - pipe it through `less' or `more' !");
  143. const struct cmdinfo *cipaction = NULL;
  144. int f_pending=0, f_recursive=0, f_alsoselect=1, f_skipsame=0, f_noact=0;
  145. int f_autodeconf=0, f_nodebsig=0;
  146. int f_triggers = 0;
  147. unsigned long f_debug=0;
  148. /* Change fc_overwrite to 1 to enable force-overwrite by default */
  149. int fc_downgrade=1, fc_configureany=0, fc_hold=0, fc_removereinstreq=0, fc_overwrite=0;
  150. int fc_removeessential=0, fc_conflicts=0, fc_depends=0, fc_dependsversion=0;
  151. int fc_breaks=0, fc_badpath=0, fc_overwritediverted=0, fc_architecture=0;
  152. int fc_nonroot=0, fc_overwritedir=0, fc_conff_new=0, fc_conff_miss=0;
  153. int fc_conff_old=0, fc_conff_def=0;
  154. int fc_badverify = 0;
  155. int errabort = 50;
  156. const char *admindir= ADMINDIR;
  157. const char *instdir= "";
  158. struct packageinlist *ignoredependss = NULL;
  159. static const struct forceinfo {
  160. const char *name;
  161. int *opt;
  162. } forceinfos[]= {
  163. { "downgrade", &fc_downgrade },
  164. { "configure-any", &fc_configureany },
  165. { "hold", &fc_hold },
  166. { "remove-reinstreq", &fc_removereinstreq },
  167. { "remove-essential", &fc_removeessential },
  168. { "conflicts", &fc_conflicts },
  169. { "confnew", &fc_conff_new },
  170. { "confold", &fc_conff_old },
  171. { "confdef", &fc_conff_def },
  172. { "confmiss", &fc_conff_miss },
  173. { "depends", &fc_depends },
  174. { "depends-version", &fc_dependsversion },
  175. { "breaks", &fc_breaks },
  176. { "bad-path", &fc_badpath },
  177. { "not-root", &fc_nonroot },
  178. { "overwrite", &fc_overwrite },
  179. { "overwrite-diverted", &fc_overwritediverted },
  180. { "overwrite-dir", &fc_overwritedir },
  181. { "architecture", &fc_architecture },
  182. { "bad-verify", &fc_badverify },
  183. /* FIXME: obsolete options, remove in the future. */
  184. { "auto-select", NULL },
  185. { NULL }
  186. };
  187. static void setaction(const struct cmdinfo *cip, const char *value) {
  188. if (cipaction)
  189. badusage(_("conflicting actions -%c (--%s) and -%c (--%s)"),
  190. cip->oshort, cip->olong, cipaction->oshort, cipaction->olong);
  191. cipaction= cip;
  192. }
  193. static void setobsolete(const struct cmdinfo *cip, const char *value) {
  194. fprintf(stderr, _("Warning: obsolete option `--%s'\n"),cip->olong);
  195. }
  196. static void setdebug(const struct cmdinfo *cpi, const char *value) {
  197. char *endp;
  198. if (*value == 'h') {
  199. if (printf(_(
  200. "%s debugging option, --debug=<octal> or -D<octal>:\n"
  201. "\n"
  202. " number ref. in source description\n"
  203. " 1 general Generally helpful progress information\n"
  204. " 2 scripts Invocation and status of maintainer scripts\n"
  205. " 10 eachfile Output for each file processed\n"
  206. " 100 eachfiledetail Lots of output for each file processed\n"
  207. " 20 conff Output for each configuration file\n"
  208. " 200 conffdetail Lots of output for each configuration file\n"
  209. " 40 depcon Dependencies and conflicts\n"
  210. " 400 depcondetail Lots of dependencies/conflicts output\n"
  211. " 10000 triggers Trigger activation and processing\n"
  212. " 20000 triggersdetail Lots of output regarding triggers\n"
  213. " 40000 triggersstupid Silly amounts of output regarding triggers\n"
  214. " 1000 veryverbose Lots of drivel about eg the dpkg/info directory\n"
  215. " 2000 stupidlyverbose Insane amounts of drivel\n"
  216. "\n"
  217. "Debugging options are be mixed using bitwise-or.\n"
  218. "Note that the meanings and values are subject to change.\n"),
  219. DPKG) < 0) werr("stdout");
  220. exit(0);
  221. }
  222. f_debug= strtoul(value,&endp,8);
  223. if (*endp) badusage(_("--debug requires an octal argument"));
  224. }
  225. static void setroot(const struct cmdinfo *cip, const char *value) {
  226. char *p;
  227. instdir= value;
  228. p= m_malloc(strlen(value) + sizeof(ADMINDIR));
  229. strcpy(p,value);
  230. strcat(p,ADMINDIR);
  231. admindir= p;
  232. }
  233. static void ignoredepends(const struct cmdinfo *cip, const char *value) {
  234. char *copy, *p;
  235. const char *pnerr;
  236. struct packageinlist *ni;
  237. copy= m_malloc(strlen(value)+2);
  238. strcpy(copy,value);
  239. copy[strlen(value)+1]= 0;
  240. for (p=copy; *p; p++) {
  241. if (*p != ',') continue;
  242. *p++= 0;
  243. if (!*p || *p==',' || p==copy+1)
  244. badusage(_("null package name in --ignore-depends comma-separated list `%.250s'"),
  245. value);
  246. }
  247. p= copy;
  248. while (*p) {
  249. pnerr = illegal_packagename(p, NULL);
  250. if (pnerr) ohshite(_("--ignore-depends requires a legal package name. "
  251. "`%.250s' is not; %s"), p, pnerr);
  252. ni= m_malloc(sizeof(struct packageinlist));
  253. ni->pkg= findpackage(p);
  254. ni->next= ignoredependss;
  255. ignoredependss= ni;
  256. p+= strlen(p)+1;
  257. }
  258. }
  259. static void setinteger(const struct cmdinfo *cip, const char *value) {
  260. unsigned long v;
  261. char *ep;
  262. v= strtoul(value,&ep,0);
  263. if (*ep || v > INT_MAX)
  264. badusage(_("invalid integer for --%s: `%.250s'"),cip->olong,value);
  265. *cip->iassignto= v;
  266. }
  267. static void setpipe(const struct cmdinfo *cip, const char *value) {
  268. static struct pipef **lastpipe;
  269. unsigned long v;
  270. char *ep;
  271. v= strtoul(value,&ep,0);
  272. if (*ep || v > INT_MAX)
  273. badusage(_("invalid integer for --%s: `%.250s'"),cip->olong,value);
  274. lastpipe= cip->parg;
  275. if (*lastpipe) {
  276. (*lastpipe)->next= nfmalloc(sizeof(struct pipef));
  277. *lastpipe= (*lastpipe)->next;
  278. } else {
  279. *lastpipe= nfmalloc(sizeof(struct pipef));
  280. }
  281. (*lastpipe)->fd= v;
  282. (*lastpipe)->next= NULL;
  283. }
  284. static void setforce(const struct cmdinfo *cip, const char *value) {
  285. const char *comma;
  286. size_t l;
  287. const struct forceinfo *fip;
  288. if (!strcmp(value,"help")) {
  289. if (printf(_(
  290. "%s forcing options - control behaviour when problems found:\n"
  291. " warn but continue: --force-<thing>,<thing>,...\n"
  292. " stop with error: --refuse-<thing>,<thing>,... | --no-force-<thing>,...\n"
  293. " Forcing things:\n"
  294. " all [!] Set all force options\n"
  295. " downgrade [*] Replace a package with a lower version\n"
  296. " configure-any Configure any package which may help this one\n"
  297. " hold Process incidental packages even when on hold\n"
  298. " bad-path PATH is missing important programs, problems likely\n"
  299. " not-root Try to (de)install things even when not root\n"
  300. " overwrite Overwrite a file from one package with another\n"
  301. " overwrite-diverted Overwrite a diverted file with an undiverted version\n"
  302. " bad-verify Install a package even if it fails authenticity check\n"
  303. " depends-version [!] Turn dependency version problems into warnings\n"
  304. " depends [!] Turn all dependency problems into warnings\n"
  305. " confnew [!] Always use the new config files, don't prompt\n"
  306. " confold [!] Always use the old config files, don't prompt\n"
  307. " confdef [!] Use the default option for new config files if one\n"
  308. " is available, don't prompt. If no default can be found,\n"
  309. " you will be prompted unless one of the confold or\n"
  310. " confnew options is also given\n"
  311. " confmiss [!] Always install missing config files\n"
  312. " breaks [!] Install even if it would break another package\n"
  313. " conflicts [!] Allow installation of conflicting packages\n"
  314. " architecture [!] Process even packages with wrong architecture\n"
  315. " overwrite-dir [!] Overwrite one package's directory with another's file\n"
  316. " remove-reinstreq [!] Remove packages which require installation\n"
  317. " remove-essential [!] Remove an essential package\n"
  318. "\n"
  319. "WARNING - use of options marked [!] can seriously damage your installation.\n"
  320. "Forcing options marked [*] are enabled by default.\n"),
  321. DPKG) < 0) werr("stdout");
  322. exit(0);
  323. }
  324. for (;;) {
  325. comma= strchr(value,',');
  326. l = comma ? (size_t)(comma - value) : strlen(value);
  327. for (fip=forceinfos; fip->name; fip++)
  328. if (!strncmp(fip->name,value,l) && strlen(fip->name)==l) break;
  329. if (!fip->name) {
  330. if (strncmp("all", value, l) == 0) {
  331. for (fip=forceinfos; fip->name; fip++)
  332. if (fip->opt)
  333. *fip->opt= cip->arg;
  334. } else
  335. badusage(_("unknown force/refuse option `%.*s'"), l<250 ? (int)l : 250, value);
  336. } else {
  337. if (fip->opt)
  338. *fip->opt= cip->arg;
  339. else
  340. fprintf(stderr, _("Warning: obsolete force/refuse option `%s'\n"),
  341. fip->name);
  342. }
  343. if (!comma) break;
  344. value= ++comma;
  345. }
  346. }
  347. static const char okpassshortopts[]= "D";
  348. void execbackend(const char *const *argv) NONRETURNING;
  349. void commandfd(const char *const *argv);
  350. static const struct cmdinfo cmdinfos[]= {
  351. /* This table has both the action entries in it and the normal options.
  352. * The action entries are made with the ACTION macro, as they all
  353. * have a very similar structure.
  354. */
  355. #define ACTION(longopt,shortopt,code,function) \
  356. { longopt, shortopt, 0, NULL, NULL, setaction, code, NULL, (voidfnp)function }
  357. #define OBSOLETE(longopt,shortopt) \
  358. { longopt, shortopt, 0, NULL, NULL, setobsolete, 0, NULL, NULL }
  359. #define ACTIONBACKEND(longopt,shortop, backend) \
  360. { longopt, shortop, 0, NULL, NULL, setaction, 0, (void *)backend, (voidfnp)execbackend }
  361. ACTION( "install", 'i', act_install, archivefiles ),
  362. ACTION( "unpack", 0, act_unpack, archivefiles ),
  363. ACTION( "record-avail", 'A', act_avail, archivefiles ),
  364. ACTION( "configure", 0, act_configure, packages ),
  365. ACTION( "remove", 'r', act_remove, packages ),
  366. ACTION( "purge", 'P', act_purge, packages ),
  367. ACTION( "triggers-only", 0, act_triggers, packages ),
  368. ACTIONBACKEND( "listfiles", 'L', DPKGQUERY),
  369. ACTIONBACKEND( "status", 's', DPKGQUERY),
  370. ACTION( "get-selections", 0, act_getselections, getselections ),
  371. ACTION( "set-selections", 0, act_setselections, setselections ),
  372. ACTION( "clear-selections", 0, act_clearselections, clearselections ),
  373. ACTIONBACKEND( "print-avail", 'p', DPKGQUERY),
  374. ACTION( "update-avail", 0, act_avreplace, updateavailable ),
  375. ACTION( "merge-avail", 0, act_avmerge, updateavailable ),
  376. ACTION( "clear-avail", 0, act_avclear, updateavailable ),
  377. ACTION( "forget-old-unavail", 0, act_forgetold, forgetold ),
  378. ACTION( "audit", 'C', act_audit, audit ),
  379. ACTION( "yet-to-unpack", 0, act_unpackchk, unpackchk ),
  380. ACTIONBACKEND( "list", 'l', DPKGQUERY),
  381. ACTIONBACKEND( "search", 'S', DPKGQUERY),
  382. ACTION( "assert-support-predepends", 0, act_assertpredep, assertpredep ),
  383. ACTION( "assert-working-epoch", 0, act_assertepoch, assertepoch ),
  384. ACTION( "assert-long-filenames", 0, act_assertlongfilenames, assertlongfilenames ),
  385. ACTION( "assert-multi-conrep", 0, act_assertmulticonrep, assertmulticonrep ),
  386. ACTION( "print-architecture", 0, act_printarch, printarch ),
  387. ACTION( "print-installation-architecture", 0, act_printinstarch, printarch ),
  388. ACTION( "predep-package", 0, act_predeppackage, predeppackage ),
  389. ACTION( "compare-versions", 0, act_cmpversions, cmpversions ),
  390. /*
  391. ACTION( "command-fd", 'c', act_commandfd, commandfd ),
  392. */
  393. { "status-fd", 0, 1, NULL, NULL, setpipe, 0, &status_pipes },
  394. { "log", 0, 1, NULL, &log_file, NULL, 0 },
  395. { "pending", 'a', 0, &f_pending, NULL, NULL, 1 },
  396. { "recursive", 'R', 0, &f_recursive, NULL, NULL, 1 },
  397. { "no-act", 0, 0, &f_noact, NULL, NULL, 1 },
  398. { "dry-run", 0, 0, &f_noact, NULL, NULL, 1 },
  399. { "simulate", 0, 0, &f_noact, NULL, NULL, 1 },
  400. { "no-debsig", 0, 0, &f_nodebsig, NULL, NULL, 1 },
  401. /* Alias ('G') for --refuse. */
  402. { NULL, 'G', 0, &fc_downgrade, NULL, NULL, 0 },
  403. { "selected-only", 'O', 0, &f_alsoselect, NULL, NULL, 0 },
  404. { "triggers", 0, 0, &f_triggers, NULL, NULL, 1 },
  405. { "no-triggers", 0, 0, &f_triggers, NULL, NULL, -1 },
  406. /* FIXME: Remove ('N') sometime. */
  407. { "no-also-select", 'N', 0, &f_alsoselect, NULL, NULL, 0 },
  408. { "skip-same-version", 'E', 0, &f_skipsame, NULL, NULL, 1 },
  409. { "auto-deconfigure", 'B', 0, &f_autodeconf, NULL, NULL, 1 },
  410. OBSOLETE( "largemem", 0 ),
  411. OBSOLETE( "smallmem", 0 ),
  412. { "root", 0, 1, NULL, NULL, setroot, 0 },
  413. { "abort-after", 0, 1, &errabort, NULL, setinteger, 0 },
  414. { "admindir", 0, 1, NULL, &admindir, NULL, 0 },
  415. { "instdir", 0, 1, NULL, &instdir, NULL, 0 },
  416. { "ignore-depends", 0, 1, NULL, NULL, ignoredepends, 0 },
  417. { "force", 0, 2, NULL, NULL, setforce, 1 },
  418. { "refuse", 0, 2, NULL, NULL, setforce, 0 },
  419. { "no-force", 0, 2, NULL, NULL, setforce, 0 },
  420. { "debug", 'D', 1, NULL, NULL, setdebug, 0 },
  421. { "help", 'h', 0, NULL, NULL, helponly, 0 },
  422. { "version", 0, 0, NULL, NULL, versiononly, 0 },
  423. /* UK spelling. */
  424. { "licence", 0, 0, NULL, NULL, showcopyright, 0 },
  425. /* US spelling. */
  426. { "license", 0, 0, NULL, NULL, showcopyright, 0 },
  427. ACTIONBACKEND( "build", 'b', BACKEND),
  428. ACTIONBACKEND( "contents", 'c', BACKEND),
  429. ACTIONBACKEND( "control", 'e', BACKEND),
  430. ACTIONBACKEND( "info", 'I', BACKEND),
  431. ACTIONBACKEND( "field", 'f', BACKEND),
  432. ACTIONBACKEND( "extract", 'x', BACKEND),
  433. ACTIONBACKEND( "new", 0, BACKEND),
  434. ACTIONBACKEND( "old", 0, BACKEND),
  435. ACTIONBACKEND( "vextract", 'X', BACKEND),
  436. ACTIONBACKEND( "fsys-tarfile", 0, BACKEND),
  437. { NULL, 0, 0, NULL, NULL, NULL, 0 }
  438. };
  439. void execbackend(const char *const *argv) {
  440. char **nargv; /* argv for backend command */
  441. int i = 0; /* index for nargv */
  442. int offset = 0; /* offset for copying argv strings to nargv */
  443. int argc = 1; /* for nargv */
  444. const char *const *arg = argv;
  445. int pass_admindir = 0;
  446. while (*arg != NULL) {
  447. arg++; argc++;
  448. }
  449. /*
  450. * Special case: dpkg-query takes the --admindir option, and if dpkg itself
  451. * was given a different admin directory, we need to pass it along to it.
  452. */
  453. if (strcmp(cipaction->parg, DPKGQUERY) == 0 &&
  454. strcmp(admindir, ADMINDIR) != 0) {
  455. argc++;
  456. pass_admindir = 1;
  457. }
  458. nargv = m_malloc(sizeof(char *) * (argc + 2));
  459. nargv[i] = m_strdup(cipaction->parg);
  460. i++, offset++;
  461. if (pass_admindir) {
  462. nargv[i] = m_malloc((strlen("--admindir=") + strlen(admindir) + 1));
  463. sprintf(nargv[i], "--admindir=%s", admindir);
  464. i++, offset++;
  465. }
  466. nargv[i] = m_malloc(2 + strlen(cipaction->olong) + 1);
  467. strcpy(nargv[i], "--");
  468. strcat(nargv[i], cipaction->olong);
  469. i++, offset++;
  470. /* Copy arguments from argv to nargv. */
  471. for (; i <= argc; i++)
  472. nargv[i] = m_strdup(argv[i - offset]);
  473. nargv[i] = NULL;
  474. execvp(cipaction->parg, nargv);
  475. ohshite(_("failed to exec %s"), (char *)cipaction->parg);
  476. }
  477. void commandfd(const char *const *argv) {
  478. jmp_buf ejbuf;
  479. struct varbuf linevb;
  480. const char * pipein;
  481. const char **newargs;
  482. char *ptr, *endptr;
  483. FILE *in;
  484. int c, lno, infd, i, skipchar;
  485. static void (*actionfunction)(const char *const *argv);
  486. pipein = *argv++;
  487. if (pipein == NULL)
  488. badusage(_("--command-fd takes one argument, not zero"));
  489. if (*argv)
  490. badusage(_("--command-fd only takes one argument"));
  491. if ((infd= strtol(pipein, (char **)NULL, 10)) == -1)
  492. ohshite(_("invalid number for --command-fd"));
  493. if ((in= fdopen(infd, "r")) == NULL)
  494. ohshite(_("couldn't open `%i' for stream"), infd);
  495. if (setjmp(ejbuf)) { /* expect warning about possible clobbering of argv */
  496. error_unwind(ehflag_bombout); exit(2);
  497. }
  498. varbufinit(&linevb);
  499. for (;;lno= 0) {
  500. const char **oldargs= NULL;
  501. int argc= 1, mode= 0;
  502. lno= 0;
  503. push_error_handler(&ejbuf, print_error_fatal, NULL);
  504. do { c= getc(in); if (c == '\n') lno++; } while (c != EOF && isspace(c));
  505. if (c == EOF) break;
  506. if (c == '#') {
  507. do { c= getc(in); if (c == '\n') lno++; } while (c != EOF && c != '\n');
  508. continue;
  509. }
  510. varbufreset(&linevb);
  511. do {
  512. varbufaddc(&linevb,c);
  513. c= getc(in);
  514. if (c == '\n') lno++;
  515. if (isspace(c)) argc++; /* This isn't fully accurate, but overestimating can't hurt. */
  516. } while (c != EOF && c != '\n');
  517. if (c == EOF) ohshit(_("unexpected eof before end of line %d"),lno);
  518. if (!argc) continue;
  519. varbufaddc(&linevb,0);
  520. printf("line=`%*s'\n",(int)linevb.used,linevb.buf);
  521. oldargs= newargs= realloc(oldargs,sizeof(const char *) * (argc + 1));
  522. argc= 1;
  523. ptr= linevb.buf;
  524. endptr= ptr + linevb.used;
  525. skipchar= 0;
  526. while(ptr < endptr) {
  527. if (skipchar) {
  528. skipchar= 0;
  529. } else if (*ptr == '\\') {
  530. memmove(ptr, (ptr+1), (linevb.used-(linevb.buf - ptr)-1));
  531. endptr--;
  532. skipchar= 1;
  533. continue;
  534. } else if (isspace(*ptr)) {
  535. if (mode == 1) {
  536. *ptr= 0;
  537. mode= 0;
  538. }
  539. } else {
  540. if (mode == 0) {
  541. newargs[argc]= ptr;
  542. argc++;
  543. mode= 1;
  544. }
  545. }
  546. ptr++;
  547. }
  548. *ptr= 0;
  549. newargs[argc++] = NULL;
  550. /* We strdup each argument, but never free it, because the error messages
  551. * contain references back to these strings. Freeing them, and reusing
  552. * the memory, would make those error messages confusing, to say the
  553. * least.
  554. */
  555. for(i=1;i<argc;i++)
  556. if (newargs[i])
  557. newargs[i] = m_strdup(newargs[i]);
  558. cipaction= NULL;
  559. myopt((const char *const**)&newargs,cmdinfos);
  560. if (!cipaction) badusage(_("need an action option"));
  561. actionfunction= (void (*)(const char* const*))cipaction->farg;
  562. actionfunction(newargs);
  563. set_error_display(NULL, NULL);
  564. error_unwind(ehflag_normaltidy);
  565. }
  566. }
  567. int main(int argc, const char *const *argv) {
  568. jmp_buf ejbuf;
  569. static void (*actionfunction)(const char *const *argv);
  570. standard_startup(&ejbuf, argc, &argv, DPKG, 1, cmdinfos);
  571. if (!cipaction) badusage(_("need an action option"));
  572. if (!f_triggers)
  573. f_triggers = (cipaction->arg == act_triggers && *argv) ? -1 : 1;
  574. setvbuf(stdout, NULL, _IONBF, 0);
  575. filesdbinit();
  576. actionfunction= (void (*)(const char* const*))cipaction->farg;
  577. actionfunction(argv);
  578. standard_shutdown(0);
  579. return reportbroken_retexitstatus();
  580. }