main.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. /*
  2. * dpkg-deb - construction and deconstruction of *.deb archives
  3. * main.c - main program
  4. *
  5. * Copyright (C) 1994,1995 Ian Jackson <iwj10@cus.cam.ac.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 <assert.h>
  34. #include <config.h>
  35. #include <dpkg.h>
  36. #include <version.h>
  37. #include <myopt.h>
  38. #include "dpkg-deb.h"
  39. const char* showformat = "${pkg:Package}\t${pkg:Version}\n";
  40. static void printversion(void) {
  41. if (fputs(_("Debian `"), stdout) < 0) werr("stdout");
  42. if (fputs(BACKEND, stdout) < 0) werr("stdout");
  43. if (fputs(_("' package archive backend version "), stdout) < 0) werr("stdout");
  44. if (fputs(DPKG_VERSION_ARCH ".\n", stdout) < 0) werr("stdout");
  45. if (fputs(_("This is free software; see the GNU General Public Licence version 2 or\n"
  46. "later for copying conditions. There is NO warranty.\n"
  47. "See dpkg-deb --licence for details.\n"),
  48. stdout) < 0) werr("stdout");
  49. }
  50. static void usage(void) {
  51. if (fputs(_("\
  52. Command:\n\
  53. -b|--build <directory> [<deb>] build an archive.\n\
  54. -c|--contents <deb> list contents.\n\
  55. -I|--info <deb> [<cfile>...] show info to stdout.\n\
  56. -W|--show <deb> Show information on package(s)\n\
  57. -f|--field <deb> [<cfield>...] show field(s) to stdout.\n\
  58. -e|--control <deb> [<directory>] extract control info.\n\
  59. -x|--extract <deb> <directory> extract files.\n\
  60. -X|--vextract <deb> <directory> extract & list files.\n\
  61. --fsys-tarfile <deb> output filesystem tarfile.\n\
  62. -h|--help display this message.\n\
  63. --version | --licence show version/licence.\n\
  64. \n\
  65. <deb> is the filename of a Debian format archive.\n\
  66. <cfile> is the name of an administrative file component.\n\
  67. <cfield> is the name of a field in the main `control' file.\n\
  68. \n\
  69. Options:\n\
  70. --showformat=<format> Use alternative format for --show\n\
  71. -D Enable debugging output\n\
  72. --old, --new select archive format\n\
  73. --nocheck suppress control file check (build bad package).\n\
  74. -z# to set the compression when building\n\
  75. \n\
  76. Format syntax:\n\
  77. A format is a string that will be output for each package. The format\n\
  78. can include the standard escape sequences \\n (newline), \\r (carriage\n\
  79. return) or \\\\ (plain backslash). Package information can be included\n\
  80. by inserting variable references to package fields using the ${var[;width]}\n\
  81. syntax. Fields will be right-aligned unless the width is negative in which\n\
  82. case left aligenment will be used. \n\
  83. \n\
  84. Use `dpkg' to install and remove packages from your system, or\n\
  85. `dselect' for user-friendly package management. Packages unpacked\n\
  86. using `dpkg-deb --extract' will be incorrectly installed !\n"),
  87. stdout) < 0) werr("stdout");
  88. }
  89. const char thisname[]= BACKEND;
  90. const char printforhelp[]=
  91. N_("Type dpkg-deb --help for help about manipulating *.deb files;\n"
  92. "Type dpkg --help for help about installing and deinstalling packages.");
  93. int debugflag=0, nocheckflag=0, oldformatflag=BUILDOLDPKGFORMAT;
  94. const char* compression=NULL;
  95. const struct cmdinfo *cipaction=0;
  96. dofunction *action=0;
  97. static void helponly(const struct cmdinfo *cip, const char *value) NONRETURNING;
  98. static void helponly(const struct cmdinfo *cip, const char *value) {
  99. usage(); exit(0);
  100. }
  101. static void versiononly(const struct cmdinfo *cip, const char *value) NONRETURNING;
  102. static void versiononly(const struct cmdinfo *cip, const char *value) {
  103. printversion(); exit(0);
  104. }
  105. static void setaction(const struct cmdinfo *cip, const char *value);
  106. static dofunction *const dofunctions[]= {
  107. do_build,
  108. do_contents,
  109. do_control,
  110. do_info,
  111. do_field,
  112. do_extract,
  113. do_vextract,
  114. do_fsystarfile,
  115. do_showinfo
  116. };
  117. /* NB: the entries using setaction must appear first and be in the
  118. * same order as dofunctions:
  119. */
  120. static const struct cmdinfo cmdinfos[]= {
  121. { "build", 'b', 0, 0, 0, setaction },
  122. { "contents", 'c', 0, 0, 0, setaction },
  123. { "control", 'e', 0, 0, 0, setaction },
  124. { "info", 'I', 0, 0, 0, setaction },
  125. { "field", 'f', 0, 0, 0, setaction },
  126. { "extract", 'x', 0, 0, 0, setaction },
  127. { "vextract", 'X', 0, 0, 0, setaction },
  128. { "fsys-tarfile", 0, 0, 0, 0, setaction },
  129. { "show", 'W', 0, 0, 0, setaction },
  130. { "new", 0, 0, &oldformatflag, 0, 0, 0 },
  131. { "old", 0, 0, &oldformatflag, 0, 0, 1 },
  132. { "debug", 'D', 0, &debugflag, 0, 0, 1 },
  133. { "nocheck", 0, 0, &nocheckflag, 0, 0, 1 },
  134. { "compression", 'z', 1, 0, &compression, 0, 1 },
  135. { "showformat", 0, 1, 0, &showformat, 0 },
  136. { "help", 'h', 0, 0, 0, helponly },
  137. { "version", 0, 0, 0, 0, versiononly },
  138. { "licence", 0, 0, 0, 0, showcopyright }, /* UK spelling */
  139. { "license", 0, 0, 0, 0, showcopyright }, /* US spelling */
  140. { 0, 0 }
  141. };
  142. static void setaction(const struct cmdinfo *cip, const char *value) {
  143. if (cipaction)
  144. badusage(_("conflicting actions --%s and --%s"),cip->olong,cipaction->olong);
  145. cipaction= cip;
  146. assert((int)(cip-cmdinfos) < (int)(sizeof(dofunctions)*sizeof(dofunction*)));
  147. action= dofunctions[cip-cmdinfos];
  148. }
  149. int main(int argc, const char *const *argv) NONRETURNING;
  150. int main(int argc, const char *const *argv) {
  151. jmp_buf ejbuf;
  152. setlocale(LC_ALL, "");
  153. setlocale(LC_NUMERIC, "POSIX");
  154. bindtextdomain(PACKAGE, LOCALEDIR);
  155. textdomain(PACKAGE);
  156. if (setjmp(ejbuf)) { /* expect warning about possible clobbering of argv */
  157. error_unwind(ehflag_bombout); exit(2);
  158. }
  159. push_error_handler(&ejbuf,print_error_fatal,0);
  160. myopt(&argv,cmdinfos);
  161. if (!cipaction) badusage(_("need an action option"));
  162. unsetenv("GZIP");
  163. action(argv);
  164. set_error_display(0,0);
  165. error_unwind(ehflag_normaltidy);
  166. exit(0);
  167. }
  168. /* vi: sw=2
  169. */