main.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. /*
  2. * dpkg-deb - construction and deconstruction of *.deb archives
  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 <assert.h>
  35. #include <dpkg.h>
  36. #include <dpkg-db.h>
  37. #include <myopt.h>
  38. #include "dpkg-deb.h"
  39. const char* showformat = "${Package}\t${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. " -Z<type> set the compression type to use when building.\n"
  76. " allowed values: gzip, bzip2, none\n"
  77. "\n"
  78. "Format syntax:\n"
  79. " A format is a string that will be output for each package. The format\n"
  80. " can include the standard escape sequences \\n (newline), \\r (carriage\n"
  81. " return) or \\\\ (plain backslash). Package information can be included\n"
  82. " by inserting variable references to package fields using the ${var[;width]}\n"
  83. " syntax. Fields will be right-aligned unless the width is negative in which\n"
  84. " case left alignment will be used. \n"
  85. "\n"
  86. "Use `dpkg' to install and remove packages from your system, or\n"
  87. "`dselect' for user-friendly package management. Packages unpacked\n"
  88. "using `dpkg-deb --extract' will be incorrectly installed !\n"),
  89. stdout) < 0) werr("stdout");
  90. }
  91. const char thisname[]= BACKEND;
  92. const char printforhelp[]=
  93. N_("Type dpkg-deb --help for help about manipulating *.deb files;\n"
  94. "Type dpkg --help for help about installing and deinstalling packages.");
  95. int debugflag=0, nocheckflag=0, oldformatflag=BUILDOLDPKGFORMAT;
  96. const char* compression=NULL;
  97. enum compression_type compress_type = GZ;
  98. const struct cmdinfo *cipaction=0;
  99. dofunction *action=0;
  100. static void helponly(const struct cmdinfo *cip, const char *value) NONRETURNING;
  101. static void helponly(const struct cmdinfo *cip, const char *value) {
  102. usage(); exit(0);
  103. }
  104. static void versiononly(const struct cmdinfo *cip, const char *value) NONRETURNING;
  105. static void versiononly(const struct cmdinfo *cip, const char *value) {
  106. printversion(); exit(0);
  107. }
  108. static void setaction(const struct cmdinfo *cip, const char *value);
  109. static void setcompresstype(const struct cmdinfo *cip, const char *value);
  110. static dofunction *const dofunctions[]= {
  111. do_build,
  112. do_contents,
  113. do_control,
  114. do_info,
  115. do_field,
  116. do_extract,
  117. do_vextract,
  118. do_fsystarfile,
  119. do_showinfo
  120. };
  121. /* NB: the entries using setaction must appear first and be in the
  122. * same order as dofunctions:
  123. */
  124. static const struct cmdinfo cmdinfos[]= {
  125. { "build", 'b', 0, 0, 0, setaction },
  126. { "contents", 'c', 0, 0, 0, setaction },
  127. { "control", 'e', 0, 0, 0, setaction },
  128. { "info", 'I', 0, 0, 0, setaction },
  129. { "field", 'f', 0, 0, 0, setaction },
  130. { "extract", 'x', 0, 0, 0, setaction },
  131. { "vextract", 'X', 0, 0, 0, setaction },
  132. { "fsys-tarfile", 0, 0, 0, 0, setaction },
  133. { "show", 'W', 0, 0, 0, setaction },
  134. { "new", 0, 0, &oldformatflag, 0, 0, 0 },
  135. { "old", 0, 0, &oldformatflag, 0, 0, 1 },
  136. { "debug", 'D', 0, &debugflag, 0, 0, 1 },
  137. { "nocheck", 0, 0, &nocheckflag, 0, 0, 1 },
  138. { "compression", 'z', 1, 0, &compression, 0, 1 },
  139. { "compress_type",'Z', 1, 0, 0, setcompresstype },
  140. { "showformat", 0, 1, 0, &showformat, 0 },
  141. { "help", 'h', 0, 0, 0, helponly },
  142. { "version", 0, 0, 0, 0, versiononly },
  143. { "licence", 0, 0, 0, 0, showcopyright }, /* UK spelling */
  144. { "license", 0, 0, 0, 0, showcopyright }, /* US spelling */
  145. { 0, 0 }
  146. };
  147. static void setaction(const struct cmdinfo *cip, const char *value) {
  148. if (cipaction)
  149. badusage(_("conflicting actions --%s and --%s"),cip->olong,cipaction->olong);
  150. cipaction= cip;
  151. assert((int)(cip-cmdinfos) < (int)(sizeof(dofunctions)*sizeof(dofunction*)));
  152. action= dofunctions[cip-cmdinfos];
  153. }
  154. static void setcompresstype(const struct cmdinfo *cip, const char *value) {
  155. if (!strcmp(value, "gzip"))
  156. compress_type= GZ;
  157. else if (!strcmp(value, "bzip2"))
  158. compress_type= BZ2;
  159. else if (!strcmp(value, "none"))
  160. compress_type= CAT;
  161. else
  162. ohshit(_("unknown compression type `%s'!"), value);
  163. }
  164. int main(int argc, const char *const *argv) {
  165. jmp_buf ejbuf;
  166. setlocale(LC_NUMERIC, "POSIX");
  167. standard_startup(&ejbuf, argc, &argv, NULL, 0, cmdinfos);
  168. if (!cipaction) badusage(_("need an action option"));
  169. unsetenv("GZIP");
  170. action(argv);
  171. standard_shutdown(0);
  172. exit(0);
  173. }
  174. /* vi: sw=2
  175. */