main.c 24 KB

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