main.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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
  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 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 <errno.h>
  27. #include <limits.h>
  28. #if HAVE_LOCALE_H
  29. #include <locale.h>
  30. #endif
  31. #include <ctype.h>
  32. #include <string.h>
  33. #include <dirent.h>
  34. #include <signal.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/myopt.h>
  43. #include "dpkg-deb.h"
  44. const char* showformat = "${Package}\t${Version}\n";
  45. static void
  46. printversion(const struct cmdinfo *cip, const char *value)
  47. {
  48. printf(_("Debian `%s' package archive backend version %s.\n"),
  49. BACKEND, DPKG_VERSION_ARCH);
  50. printf(_(
  51. "This is free software; see the GNU General Public License version 2 or\n"
  52. "later for copying conditions. There is NO warranty.\n"
  53. "See %s --license for copyright and license details.\n"), BACKEND);
  54. m_output(stdout, _("<standard output>"));
  55. exit(0);
  56. }
  57. static void
  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. " --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. " --license|--licence Show the copyright licensing terms.\n"
  79. "\n"));
  80. printf(_(
  81. "<deb> is the filename of a Debian format archive.\n"
  82. "<cfile> is the name of an administrative file component.\n"
  83. "<cfield> is the name of a field in the main `control' file.\n"
  84. "\n"));
  85. printf(_(
  86. "Options:\n"
  87. " --showformat=<format> Use alternative format for --show.\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 values: gzip, 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. const char thisname[]= BACKEND;
  113. const char printforhelp[]=
  114. N_("Type dpkg-deb --help for help about manipulating *.deb files;\n"
  115. "Type dpkg --help for help about installing and deinstalling packages.");
  116. int debugflag=0, nocheckflag=0, oldformatflag=BUILDOLDPKGFORMAT;
  117. const char* compression=NULL;
  118. enum compress_type compress_type = compress_type_gzip;
  119. const struct cmdinfo *cipaction = NULL;
  120. dofunction *action = NULL;
  121. static void setaction(const struct cmdinfo *cip, const char *value);
  122. static void setcompresstype(const struct cmdinfo *cip, const char *value);
  123. static dofunction *const dofunctions[]= {
  124. do_build,
  125. do_contents,
  126. do_control,
  127. do_info,
  128. do_field,
  129. do_extract,
  130. do_vextract,
  131. do_fsystarfile,
  132. do_showinfo
  133. };
  134. /* NB: the entries using setaction must appear first and be in the
  135. * same order as dofunctions:
  136. */
  137. static const struct cmdinfo cmdinfos[]= {
  138. { "build", 'b', 0, NULL, NULL, setaction },
  139. { "contents", 'c', 0, NULL, NULL, setaction },
  140. { "control", 'e', 0, NULL, NULL, setaction },
  141. { "info", 'I', 0, NULL, NULL, setaction },
  142. { "field", 'f', 0, NULL, NULL, setaction },
  143. { "extract", 'x', 0, NULL, NULL, setaction },
  144. { "vextract", 'X', 0, NULL, NULL, setaction },
  145. { "fsys-tarfile", 0, 0, NULL, NULL, setaction },
  146. { "show", 'W', 0, NULL, NULL, setaction },
  147. { "new", 0, 0, &oldformatflag, NULL, NULL, 0 },
  148. { "old", 0, 0, &oldformatflag, NULL, NULL, 1 },
  149. { "debug", 'D', 0, &debugflag, NULL, NULL, 1 },
  150. { "nocheck", 0, 0, &nocheckflag, NULL, NULL, 1 },
  151. { "compression", 'z', 1, NULL, &compression, NULL, 1 },
  152. { "compress_type", 'Z', 1, NULL, NULL, setcompresstype },
  153. { "showformat", 0, 1, NULL, &showformat, NULL },
  154. { "help", 'h', 0, NULL, NULL, usage },
  155. { "version", 0, 0, NULL, NULL, printversion },
  156. /* UK spelling. */
  157. { "licence", 0, 0, NULL, NULL, showcopyright },
  158. /* US spelling. */
  159. { "license", 0, 0, NULL, NULL, showcopyright },
  160. { NULL, 0, 0, NULL, NULL, NULL }
  161. };
  162. static void setaction(const struct cmdinfo *cip, const char *value) {
  163. if (cipaction)
  164. badusage(_("conflicting actions -%c (--%s) and -%c (--%s)"),
  165. cip->oshort, cip->olong, cipaction->oshort, cipaction->olong);
  166. cipaction= cip;
  167. assert((int)(cip - cmdinfos) < (int)(sizeof_array(dofunctions)));
  168. action= dofunctions[cip-cmdinfos];
  169. }
  170. static void setcompresstype(const struct cmdinfo *cip, const char *value) {
  171. if (!strcmp(value, "gzip"))
  172. compress_type = compress_type_gzip;
  173. else if (!strcmp(value, "bzip2"))
  174. compress_type = compress_type_bzip2;
  175. else if (!strcmp(value, "lzma"))
  176. compress_type = compress_type_lzma;
  177. else if (!strcmp(value, "none"))
  178. compress_type = compress_type_cat;
  179. else
  180. ohshit(_("unknown compression type `%s'!"), value);
  181. }
  182. int main(int argc, const char *const *argv) {
  183. jmp_buf ejbuf;
  184. setlocale(LC_NUMERIC, "POSIX");
  185. setlocale(LC_ALL, "");
  186. bindtextdomain(PACKAGE, LOCALEDIR);
  187. textdomain(PACKAGE);
  188. standard_startup(&ejbuf);
  189. myopt(&argv, cmdinfos);
  190. if (!cipaction) badusage(_("need an action option"));
  191. unsetenv("GZIP");
  192. action(argv);
  193. standard_shutdown();
  194. exit(0);
  195. }
  196. /* vi: sw=2
  197. */