mutool.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. // Copyright (C) 2004-2025 Artifex Software, Inc.
  2. //
  3. // This file is part of MuPDF.
  4. //
  5. // MuPDF is free software: you can redistribute it and/or modify it under the
  6. // terms of the GNU Affero General Public License as published by the Free
  7. // Software Foundation, either version 3 of the License, or (at your option)
  8. // any later version.
  9. //
  10. // MuPDF is distributed in the hope that it will be useful, but WITHOUT ANY
  11. // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  12. // FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
  13. // details.
  14. //
  15. // You should have received a copy of the GNU Affero General Public License
  16. // along with MuPDF. If not, see <https://www.gnu.org/licenses/agpl-3.0.en.html>
  17. //
  18. // Alternative licensing terms are available from the licensor.
  19. // For commercial licensing, see <https://www.artifex.com/> or contact
  20. // Artifex Software, Inc., 39 Mesa Street, Suite 108A, San Francisco,
  21. // CA 94129, USA, for further information.
  22. /*
  23. * mutool -- swiss army knife of pdf manipulation tools
  24. */
  25. #include "mupdf/fitz.h"
  26. #include <string.h>
  27. #include <stdio.h>
  28. #ifdef _MSC_VER
  29. #define main main_utf8
  30. #endif
  31. int muconvert_main(int argc, char *argv[]);
  32. int mudraw_main(int argc, char *argv[]);
  33. int mutrace_main(int argc, char *argv[]);
  34. int murun_main(int argc, char *argv[]);
  35. int pdfclean_main(int argc, char *argv[]);
  36. int pdfextract_main(int argc, char *argv[]);
  37. int pdfinfo_main(int argc, char *argv[]);
  38. int pdfposter_main(int argc, char *argv[]);
  39. int pdfshow_main(int argc, char *argv[]);
  40. int pdfpages_main(int argc, char *argv[]);
  41. int pdfcreate_main(int argc, char *argv[]);
  42. int pdfmerge_main(int argc, char *argv[]);
  43. int pdfsign_main(int argc, char *argv[]);
  44. int pdfrecolor_main(int argc, char *argv[]);
  45. int pdftrim_main(int argc, char *argv[]);
  46. int pdfbake_main(int argc, char *argv[]);
  47. int mubar_main(int argc, char *argv[]);
  48. int cmapdump_main(int argc, char *argv[]);
  49. int pdfaudit_main(int argc, char *argv[]);
  50. static struct {
  51. int (*func)(int argc, char *argv[]);
  52. char *name;
  53. char *desc;
  54. } tools[] = {
  55. #if FZ_ENABLE_PDF
  56. { pdfclean_main, "clean", "rewrite PDF file" },
  57. #endif
  58. { muconvert_main, "convert", "convert document" },
  59. #if FZ_ENABLE_PDF
  60. { pdfcreate_main, "create", "create PDF document" },
  61. #endif
  62. { mudraw_main, "draw", "convert document" },
  63. { mutrace_main, "trace", "trace device calls" },
  64. #if FZ_ENABLE_PDF
  65. { pdfextract_main, "extract", "extract font and image resources" },
  66. { pdfinfo_main, "info", "show information about PDF resources" },
  67. { pdfmerge_main, "merge", "merge pages from multiple PDF sources into a new PDF" },
  68. { pdfpages_main, "pages", "show information about PDF pages" },
  69. { pdfposter_main, "poster", "split large page into many tiles" },
  70. { pdfrecolor_main, "recolor", "change colorspace of PDF document" },
  71. { pdfsign_main, "sign", "manipulate PDF digital signatures" },
  72. { pdftrim_main, "trim", "trim PDF page contents" },
  73. { pdfbake_main, "bake", "bake PDF form into static content" },
  74. #endif
  75. #if FZ_ENABLE_JS
  76. { murun_main, "run", "run javascript" },
  77. #endif
  78. #if FZ_ENABLE_PDF
  79. { pdfshow_main, "show", "show internal PDF objects" },
  80. #ifndef NDEBUG
  81. { cmapdump_main, "cmapdump", "dump CMap resource as C source file" },
  82. #endif
  83. { pdfaudit_main, "audit", "produce usage stats from PDF files" },
  84. #endif
  85. #if FZ_ENABLE_BARCODE
  86. { mubar_main, "barcode", "encode/decode barcodes" },
  87. #endif
  88. };
  89. static int
  90. namematch(const char *end, const char *start, const char *match)
  91. {
  92. size_t len = strlen(match);
  93. return ((end-len >= start) && (strncmp(end-len, match, len) == 0));
  94. }
  95. #ifdef GPERF
  96. #include "gperftools/profiler.h"
  97. static int profiled_main(int argc, char **argv);
  98. int main(int argc, char **argv)
  99. {
  100. int ret;
  101. ProfilerStart("mutool.prof");
  102. ret = profiled_main(argc, argv);
  103. ProfilerStop();
  104. return ret;
  105. }
  106. static int profiled_main(int argc, char **argv)
  107. #else
  108. int main(int argc, char **argv)
  109. #endif
  110. {
  111. char *start, *end;
  112. char buf[32];
  113. int i;
  114. if (argc == 0)
  115. {
  116. fprintf(stderr, "No command name found!\n");
  117. return 1;
  118. }
  119. /* Check argv[0] */
  120. if (argc > 0)
  121. {
  122. end = start = argv[0];
  123. while (*end)
  124. end++;
  125. if ((end-4 >= start) && (end[-4] == '.') && (end[-3] == 'e') && (end[-2] == 'x') && (end[-1] == 'e'))
  126. end = end-4;
  127. for (i = 0; i < (int)nelem(tools); i++)
  128. {
  129. strcpy(buf, "mupdf");
  130. strcat(buf, tools[i].name);
  131. if (namematch(end, start, buf) || namematch(end, start, buf+2))
  132. return tools[i].func(argc, argv);
  133. strcpy(buf, "mu");
  134. strcat(buf, tools[i].name);
  135. if (namematch(end, start, buf))
  136. return tools[i].func(argc, argv);
  137. }
  138. }
  139. /* Check argv[1] */
  140. if (argc > 1)
  141. {
  142. for (i = 0; i < (int)nelem(tools); i++)
  143. if (!strcmp(tools[i].name, argv[1]))
  144. return tools[i].func(argc - 1, argv + 1);
  145. if (!strcmp(argv[1], "-v"))
  146. {
  147. fprintf(stderr, "mutool version %s\n", FZ_VERSION);
  148. return 0;
  149. }
  150. }
  151. /* Print usage */
  152. fprintf(stderr, "mutool version %s\n", FZ_VERSION);
  153. fprintf(stderr, "usage: mutool <command> [options]\n");
  154. for (i = 0; i < (int)nelem(tools); i++)
  155. fprintf(stderr, "\t%s\t-- %s\n", tools[i].name, tools[i].desc);
  156. return 1;
  157. }
  158. #ifdef _MSC_VER
  159. int wmain(int argc, wchar_t *wargv[])
  160. {
  161. char **argv = fz_argv_from_wargv(argc, wargv);
  162. int ret = main(argc, argv);
  163. fz_free_argv(argc, argv);
  164. return ret;
  165. }
  166. #endif