main.c 7.9 KB

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