main.c 27 KB

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