main.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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 <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 <unistd.h>
  35. #include <stdlib.h>
  36. #include <stdio.h>
  37. #include <dpkg/macros.h>
  38. #include <dpkg/i18n.h>
  39. #include <dpkg/dpkg.h>
  40. #include <dpkg/dpkg-db.h>
  41. #include <dpkg/compress.h>
  42. #include <dpkg/myopt.h>
  43. #include "dpkg-deb.h"
  44. const char* showformat = "${Package}\t${Version}\n";
  45. static void DPKG_ATTR_NORET
  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. m_output(stdout, _("<standard output>"));
  54. exit(0);
  55. }
  56. static void DPKG_ATTR_NORET
  57. usage(const struct cmdinfo *cip, const char *value)
  58. {
  59. printf(_(
  60. "Usage: %s [<option> ...] <command>\n"
  61. "\n"), BACKEND);
  62. printf(_(
  63. "Commands:\n"
  64. " -b|--build <directory> [<deb>] Build an archive.\n"
  65. " -c|--contents <deb> List contents.\n"
  66. " -I|--info <deb> [<cfile> ...] Show info to stdout.\n"
  67. " -W|--show <deb> Show information on package(s)\n"
  68. " -f|--field <deb> [<cfield> ...] Show field(s) to stdout.\n"
  69. " -e|--control <deb> [<directory>] Extract control info.\n"
  70. " -x|--extract <deb> <directory> Extract files.\n"
  71. " -X|--vextract <deb> <directory> Extract & list files.\n"
  72. " --fsys-tarfile <deb> Output filesystem tarfile.\n"
  73. "\n"));
  74. printf(_(
  75. " -h|--help Show this help message.\n"
  76. " --version Show the version.\n"
  77. "\n"));
  78. printf(_(
  79. "<deb> is the filename of a Debian format archive.\n"
  80. "<cfile> is the name of an administrative file component.\n"
  81. "<cfield> is the name of a field in the main `control' file.\n"
  82. "\n"));
  83. printf(_(
  84. "Options:\n"
  85. " --showformat=<format> Use alternative format for --show.\n"
  86. " -D Enable debugging output.\n"
  87. " --old, --new Select archive format.\n"
  88. " --nocheck Suppress control file check (build bad\n"
  89. " packages).\n"
  90. " -z# Set the compression level when building.\n"
  91. " -Z<type> Set the compression type used when building.\n"
  92. " Allowed types: gzip, xz, bzip2, lzma, none.\n"
  93. "\n"));
  94. printf(_(
  95. "Format syntax:\n"
  96. " A format is a string that will be output for each package. The format\n"
  97. " can include the standard escape sequences \\n (newline), \\r (carriage\n"
  98. " return) or \\\\ (plain backslash). Package information can be included\n"
  99. " by inserting variable references to package fields using the ${var[;width]}\n"
  100. " syntax. Fields will be right-aligned unless the width is negative in which\n"
  101. " case left alignment will be used.\n"));
  102. printf(_(
  103. "\n"
  104. "Use `dpkg' to install and remove packages from your system, or\n"
  105. "`dselect' or `aptitude' for user-friendly package management. Packages\n"
  106. "unpacked using `dpkg-deb --extract' will be incorrectly installed !\n"));
  107. m_output(stdout, _("<standard output>"));
  108. exit(0);
  109. }
  110. const char thisname[]= BACKEND;
  111. const char printforhelp[]=
  112. N_("Type dpkg-deb --help for help about manipulating *.deb files;\n"
  113. "Type dpkg --help for help about installing and deinstalling packages.");
  114. int debugflag=0, nocheckflag=0, oldformatflag=BUILDOLDPKGFORMAT;
  115. struct compressor *compressor = &compressor_gzip;
  116. int compress_level = -1;
  117. const struct cmdinfo *cipaction = NULL;
  118. dofunction *action = NULL;
  119. static void setaction(const struct cmdinfo *cip, const char *value);
  120. static void setcompresstype(const struct cmdinfo *cip, const char *value);
  121. static void
  122. set_compress_level(const struct cmdinfo *cip, const char *value)
  123. {
  124. long level;
  125. char *end;
  126. level = strtol(value, &end, 0);
  127. if (value == end || *end || level > INT_MAX)
  128. badusage(_("invalid integer for -%c: '%.250s'"), cip->oshort, value);
  129. if (level < 0 || level > 9)
  130. badusage(_("invalid compression level for -%c: %ld'"), cip->oshort, level);
  131. compress_level = level;
  132. }
  133. static dofunction *const dofunctions[]= {
  134. do_build,
  135. do_contents,
  136. do_control,
  137. do_info,
  138. do_field,
  139. do_extract,
  140. do_vextract,
  141. do_fsystarfile,
  142. do_showinfo
  143. };
  144. /* NB: the entries using setaction must appear first and be in the
  145. * same order as dofunctions:
  146. */
  147. static const struct cmdinfo cmdinfos[]= {
  148. { "build", 'b', 0, NULL, NULL, setaction },
  149. { "contents", 'c', 0, NULL, NULL, setaction },
  150. { "control", 'e', 0, NULL, NULL, setaction },
  151. { "info", 'I', 0, NULL, NULL, setaction },
  152. { "field", 'f', 0, NULL, NULL, setaction },
  153. { "extract", 'x', 0, NULL, NULL, setaction },
  154. { "vextract", 'X', 0, NULL, NULL, setaction },
  155. { "fsys-tarfile", 0, 0, NULL, NULL, setaction },
  156. { "show", 'W', 0, NULL, NULL, setaction },
  157. { "new", 0, 0, &oldformatflag, NULL, NULL, 0 },
  158. { "old", 0, 0, &oldformatflag, NULL, NULL, 1 },
  159. { "debug", 'D', 0, &debugflag, NULL, NULL, 1 },
  160. { "nocheck", 0, 0, &nocheckflag, NULL, NULL, 1 },
  161. { "compression", 'z', 1, NULL, NULL, set_compress_level },
  162. { "compress_type", 'Z', 1, NULL, NULL, setcompresstype },
  163. { "showformat", 0, 1, NULL, &showformat, NULL },
  164. { "help", 'h', 0, NULL, NULL, usage },
  165. { "version", 0, 0, NULL, NULL, printversion },
  166. { NULL, 0, 0, NULL, NULL, NULL }
  167. };
  168. static void setaction(const struct cmdinfo *cip, const char *value) {
  169. if (cipaction)
  170. badusage(_("conflicting actions -%c (--%s) and -%c (--%s)"),
  171. cip->oshort, cip->olong, cipaction->oshort, cipaction->olong);
  172. cipaction= cip;
  173. assert((int)(cip - cmdinfos) < (int)(array_count(dofunctions)));
  174. action= dofunctions[cip-cmdinfos];
  175. }
  176. static void setcompresstype(const struct cmdinfo *cip, const char *value) {
  177. compressor = compressor_find_by_name(value);
  178. if (compressor == NULL)
  179. ohshit(_("unknown compression type `%s'!"), value);
  180. }
  181. int main(int argc, const char *const *argv) {
  182. jmp_buf ejbuf;
  183. setlocale(LC_NUMERIC, "POSIX");
  184. setlocale(LC_ALL, "");
  185. bindtextdomain(PACKAGE, LOCALEDIR);
  186. textdomain(PACKAGE);
  187. standard_startup(&ejbuf);
  188. myopt(&argv, cmdinfos);
  189. if (!cipaction) badusage(_("need an action option"));
  190. unsetenv("GZIP");
  191. action(argv);
  192. standard_shutdown();
  193. exit(0);
  194. }
  195. /* vi: sw=2
  196. */