main.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. /*
  2. * dpkg-deb - construction and deconstruction of *.deb archives
  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 published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This is distributed in the hope that it will be useful,
  13. * but 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 License
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. */
  20. #include <config.h>
  21. #include <compat.h>
  22. #include <sys/types.h>
  23. #include <sys/stat.h>
  24. #include <sys/wait.h>
  25. #include <assert.h>
  26. #include <limits.h>
  27. #if HAVE_LOCALE_H
  28. #include <locale.h>
  29. #endif
  30. #include <ctype.h>
  31. #include <string.h>
  32. #include <dirent.h>
  33. #include <unistd.h>
  34. #include <stdlib.h>
  35. #include <stdio.h>
  36. #include <dpkg/macros.h>
  37. #include <dpkg/i18n.h>
  38. #include <dpkg/dpkg.h>
  39. #include <dpkg/dpkg-db.h>
  40. #include <dpkg/compress.h>
  41. #include <dpkg/options.h>
  42. #include "dpkg-deb.h"
  43. const char* showformat = "${Package}\t${Version}\n";
  44. static void DPKG_ATTR_NORET
  45. printversion(const struct cmdinfo *cip, const char *value)
  46. {
  47. printf(_("Debian `%s' package archive backend version %s.\n"),
  48. BACKEND, DPKG_VERSION_ARCH);
  49. printf(_(
  50. "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. m_output(stdout, _("<standard output>"));
  53. exit(0);
  54. }
  55. static void DPKG_ATTR_NORET
  56. usage(const struct cmdinfo *cip, const char *value)
  57. {
  58. printf(_(
  59. "Usage: %s [<option> ...] <command>\n"
  60. "\n"), BACKEND);
  61. printf(_(
  62. "Commands:\n"
  63. " -b|--build <directory> [<deb>] Build an archive.\n"
  64. " -c|--contents <deb> List contents.\n"
  65. " -I|--info <deb> [<cfile> ...] Show info to stdout.\n"
  66. " -W|--show <deb> Show information on package(s)\n"
  67. " -f|--field <deb> [<cfield> ...] Show field(s) to stdout.\n"
  68. " -e|--control <deb> [<directory>] Extract control info.\n"
  69. " -x|--extract <deb> <directory> Extract files.\n"
  70. " -X|--vextract <deb> <directory> Extract & list files.\n"
  71. " -R|--raw-extract <deb> <directory>\n"
  72. " Extract control info and files.\n"
  73. " --fsys-tarfile <deb> Output filesystem tarfile.\n"
  74. "\n"));
  75. printf(_(
  76. " -h|--help Show this help message.\n"
  77. " --version Show the version.\n"
  78. "\n"));
  79. printf(_(
  80. "<deb> is the filename of a Debian format archive.\n"
  81. "<cfile> is the name of an administrative file component.\n"
  82. "<cfield> is the name of a field in the main `control' file.\n"
  83. "\n"));
  84. printf(_(
  85. "Options:\n"
  86. " --showformat=<format> Use alternative format for --show.\n"
  87. " -v, --verbose Enable verbose output.\n"
  88. " -D Enable debugging output.\n"
  89. " --old, --new Select archive format.\n"
  90. " --nocheck Suppress control file check (build bad\n"
  91. " packages).\n"
  92. " -z# Set the compression level when building.\n"
  93. " -Z<type> Set the compression type used when building.\n"
  94. " Allowed types: gzip, xz, bzip2, lzma, none.\n"
  95. "\n"));
  96. printf(_(
  97. "Format syntax:\n"
  98. " A format is a string that will be output for each package. The format\n"
  99. " can include the standard escape sequences \\n (newline), \\r (carriage\n"
  100. " return) or \\\\ (plain backslash). Package information can be included\n"
  101. " by inserting variable references to package fields using the ${var[;width]}\n"
  102. " syntax. Fields will be right-aligned unless the width is negative in which\n"
  103. " case left alignment will be used.\n"));
  104. printf(_(
  105. "\n"
  106. "Use `dpkg' to install and remove packages from your system, or\n"
  107. "`dselect' or `aptitude' for user-friendly package management. Packages\n"
  108. "unpacked using `dpkg-deb --extract' will be incorrectly installed !\n"));
  109. m_output(stdout, _("<standard output>"));
  110. exit(0);
  111. }
  112. static const char printforhelp[] =
  113. N_("Type dpkg-deb --help for help about manipulating *.deb files;\n"
  114. "Type dpkg --help for help about installing and deinstalling packages.");
  115. int debugflag=0, nocheckflag=0, oldformatflag=BUILDOLDPKGFORMAT;
  116. int opt_verbose = 0;
  117. struct compressor *compressor = &compressor_gzip;
  118. int compress_level = -1;
  119. static void
  120. set_compress_level(const struct cmdinfo *cip, const char *value)
  121. {
  122. long level;
  123. char *end;
  124. level = strtol(value, &end, 0);
  125. if (value == end || *end || level > INT_MAX)
  126. badusage(_("invalid integer for -%c: '%.250s'"), cip->oshort, value);
  127. if (level < 0 || level > 9)
  128. badusage(_("invalid compression level for -%c: %ld'"), cip->oshort, level);
  129. compress_level = level;
  130. }
  131. static void
  132. setcompresstype(const struct cmdinfo *cip, const char *value)
  133. {
  134. compressor = compressor_find_by_name(value);
  135. if (compressor == NULL)
  136. ohshit(_("unknown compression type `%s'!"), value);
  137. }
  138. static const struct cmdinfo cmdinfos[]= {
  139. ACTION("build", 'b', 0, do_build),
  140. ACTION("contents", 'c', 0, do_contents),
  141. ACTION("control", 'e', 0, do_control),
  142. ACTION("info", 'I', 0, do_info),
  143. ACTION("field", 'f', 0, do_field),
  144. ACTION("extract", 'x', 0, do_extract),
  145. ACTION("vextract", 'X', 0, do_vextract),
  146. ACTION("raw-extract", 'R', 0, do_raw_extract),
  147. ACTION("fsys-tarfile", 0, 0, do_fsystarfile),
  148. ACTION("show", 'W', 0, do_showinfo),
  149. { "new", 0, 0, &oldformatflag, NULL, NULL, 0 },
  150. { "old", 0, 0, &oldformatflag, NULL, NULL, 1 },
  151. { "debug", 'D', 0, &debugflag, NULL, NULL, 1 },
  152. { "verbose", 'v', 0, &opt_verbose, NULL, NULL, 1 },
  153. { "nocheck", 0, 0, &nocheckflag, NULL, NULL, 1 },
  154. { "compression", 'z', 1, NULL, NULL, set_compress_level },
  155. { "compress_type", 'Z', 1, NULL, NULL, setcompresstype },
  156. { "showformat", 0, 1, NULL, &showformat, NULL },
  157. { "help", 'h', 0, NULL, NULL, usage },
  158. { "version", 0, 0, NULL, NULL, printversion },
  159. { NULL, 0, 0, NULL, NULL, NULL }
  160. };
  161. int main(int argc, const char *const *argv) {
  162. int ret;
  163. setlocale(LC_NUMERIC, "POSIX");
  164. setlocale(LC_ALL, "");
  165. bindtextdomain(PACKAGE, LOCALEDIR);
  166. textdomain(PACKAGE);
  167. dpkg_set_progname(BACKEND);
  168. standard_startup();
  169. myopt(&argv, cmdinfos, printforhelp);
  170. if (!cipaction) badusage(_("need an action option"));
  171. unsetenv("GZIP");
  172. ret = cipaction->action(argv);
  173. standard_shutdown();
  174. return ret;
  175. }
  176. /* vi: sw=2
  177. */