main.c 26 KB

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