main.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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. " -S<strategy> Set the compression strategy when building.\n"
  96. " Allowed values: extreme (xz).\n"
  97. "\n"));
  98. printf(_(
  99. "Format syntax:\n"
  100. " A format is a string that will be output for each package. The format\n"
  101. " can include the standard escape sequences \\n (newline), \\r (carriage\n"
  102. " return) or \\\\ (plain backslash). Package information can be included\n"
  103. " by inserting variable references to package fields using the ${var[;width]}\n"
  104. " syntax. Fields will be right-aligned unless the width is negative in which\n"
  105. " case left alignment will be used.\n"));
  106. printf(_(
  107. "\n"
  108. "Use `dpkg' to install and remove packages from your system, or\n"
  109. "`dselect' or `aptitude' for user-friendly package management. Packages\n"
  110. "unpacked using `dpkg-deb --extract' will be incorrectly installed !\n"));
  111. m_output(stdout, _("<standard output>"));
  112. exit(0);
  113. }
  114. static const char printforhelp[] =
  115. N_("Type dpkg-deb --help for help about manipulating *.deb files;\n"
  116. "Type dpkg --help for help about installing and deinstalling packages.");
  117. int debugflag=0, nocheckflag=0, oldformatflag=BUILDOLDPKGFORMAT;
  118. int opt_verbose = 0;
  119. struct compress_params compress_params = {
  120. .type = compressor_type_gzip,
  121. .level = -1,
  122. .strategy = NULL,
  123. };
  124. static void
  125. set_compress_level(const struct cmdinfo *cip, const char *value)
  126. {
  127. long level;
  128. char *end;
  129. level = strtol(value, &end, 0);
  130. if (value == end || *end || level > INT_MAX)
  131. badusage(_("invalid integer for -%c: '%.250s'"), cip->oshort, value);
  132. if (level < 0 || level > 9)
  133. badusage(_("invalid compression level for -%c: %ld'"), cip->oshort, level);
  134. compress_params.level = level;
  135. }
  136. static void
  137. setcompresstype(const struct cmdinfo *cip, const char *value)
  138. {
  139. compress_params.type = compressor_find_by_name(value);
  140. if (compress_params.type == compressor_type_unknown)
  141. ohshit(_("unknown compression type `%s'!"), value);
  142. }
  143. static const struct cmdinfo cmdinfos[]= {
  144. ACTION("build", 'b', 0, do_build),
  145. ACTION("contents", 'c', 0, do_contents),
  146. ACTION("control", 'e', 0, do_control),
  147. ACTION("info", 'I', 0, do_info),
  148. ACTION("field", 'f', 0, do_field),
  149. ACTION("extract", 'x', 0, do_extract),
  150. ACTION("vextract", 'X', 0, do_vextract),
  151. ACTION("raw-extract", 'R', 0, do_raw_extract),
  152. ACTION("fsys-tarfile", 0, 0, do_fsystarfile),
  153. ACTION("show", 'W', 0, do_showinfo),
  154. { "new", 0, 0, &oldformatflag, NULL, NULL, 0 },
  155. { "old", 0, 0, &oldformatflag, NULL, NULL, 1 },
  156. { "debug", 'D', 0, &debugflag, NULL, NULL, 1 },
  157. { "verbose", 'v', 0, &opt_verbose, NULL, NULL, 1 },
  158. { "nocheck", 0, 0, &nocheckflag, NULL, NULL, 1 },
  159. { NULL, 'z', 1, NULL, NULL, set_compress_level },
  160. { NULL, 'Z', 1, NULL, NULL, setcompresstype },
  161. { NULL, 'S', 1, NULL, &compress_params.strategy, NULL },
  162. { "showformat", 0, 1, NULL, &showformat, NULL },
  163. { "help", 'h', 0, NULL, NULL, usage },
  164. { "version", 0, 0, NULL, NULL, printversion },
  165. { NULL, 0, 0, NULL, NULL, NULL }
  166. };
  167. int main(int argc, const char *const *argv) {
  168. struct dpkg_error err;
  169. int ret;
  170. setlocale(LC_NUMERIC, "POSIX");
  171. setlocale(LC_ALL, "");
  172. bindtextdomain(PACKAGE, LOCALEDIR);
  173. textdomain(PACKAGE);
  174. dpkg_set_progname(BACKEND);
  175. standard_startup();
  176. myopt(&argv, cmdinfos, printforhelp);
  177. if (!cipaction) badusage(_("need an action option"));
  178. if (!compressor_check_params(&compress_params, &err))
  179. badusage(_("invalid compressor parameters: %s"), err.str);
  180. unsetenv("GZIP");
  181. ret = cipaction->action(argv);
  182. standard_shutdown();
  183. return ret;
  184. }
  185. /* vi: sw=2
  186. */