comparison mupdf-source/source/tools/mutool.c @ 3:2c135c81b16c

MERGE: upstream PyMuPDF 1.26.4 with MuPDF 1.26.7
author Franz Glasner <fzglas.hg@dom66.de>
date Mon, 15 Sep 2025 11:44:09 +0200
parents b50eed0cc0ef
children
comparison
equal deleted inserted replaced
0:6015a75abc2d 3:2c135c81b16c
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 /*
24 * mutool -- swiss army knife of pdf manipulation tools
25 */
26
27 #include "mupdf/fitz.h"
28
29 #include <string.h>
30 #include <stdio.h>
31
32 #ifdef _MSC_VER
33 #define main main_utf8
34 #endif
35
36 int muconvert_main(int argc, char *argv[]);
37 int mudraw_main(int argc, char *argv[]);
38 int mutrace_main(int argc, char *argv[]);
39 int murun_main(int argc, char *argv[]);
40
41 int pdfclean_main(int argc, char *argv[]);
42 int pdfextract_main(int argc, char *argv[]);
43 int pdfinfo_main(int argc, char *argv[]);
44 int pdfposter_main(int argc, char *argv[]);
45 int pdfshow_main(int argc, char *argv[]);
46 int pdfpages_main(int argc, char *argv[]);
47 int pdfcreate_main(int argc, char *argv[]);
48 int pdfmerge_main(int argc, char *argv[]);
49 int pdfsign_main(int argc, char *argv[]);
50 int pdfrecolor_main(int argc, char *argv[]);
51 int pdftrim_main(int argc, char *argv[]);
52 int pdfbake_main(int argc, char *argv[]);
53 int mubar_main(int argc, char *argv[]);
54
55 int cmapdump_main(int argc, char *argv[]);
56 int pdfaudit_main(int argc, char *argv[]);
57
58 static struct {
59 int (*func)(int argc, char *argv[]);
60 char *name;
61 char *desc;
62 } tools[] = {
63 #if FZ_ENABLE_JS
64 { murun_main, "run", "run javascript" },
65 #endif
66 { mudraw_main, "draw", "convert document" },
67 { muconvert_main, "convert", "convert document (with simpler options)" },
68 #if FZ_ENABLE_PDF
69 { pdfaudit_main, "audit", "produce usage stats from PDF files" },
70 { pdfbake_main, "bake", "bake PDF form into static content" },
71 { pdfclean_main, "clean", "rewrite PDF file" },
72 { pdfcreate_main, "create", "create PDF document" },
73 { pdfextract_main, "extract", "extract font and image resources" },
74 { pdfinfo_main, "info", "show information about PDF resources" },
75 { pdfmerge_main, "merge", "merge pages from multiple PDF sources into a new PDF" },
76 { pdfpages_main, "pages", "show information about PDF pages" },
77 { pdfposter_main, "poster", "split large PDF page into many tiles" },
78 { pdfrecolor_main, "recolor", "change colorspace of PDF document" },
79 { pdfshow_main, "show", "show internal PDF objects" },
80 { pdfsign_main, "sign", "manipulate PDF digital signatures" },
81 { pdftrim_main, "trim", "trim PDF page contents" },
82 #ifndef NDEBUG
83 { cmapdump_main, "cmapdump", "dump CMap resource as C source file" },
84 #endif
85 #endif
86 { mutrace_main, "trace", "trace device calls" },
87 #if FZ_ENABLE_BARCODE
88 { mubar_main, "barcode", "encode/decode barcodes" },
89 #endif
90 };
91
92 static int
93 namematch(const char *end, const char *start, const char *match)
94 {
95 size_t len = strlen(match);
96 return ((end-len >= start) && (strncmp(end-len, match, len) == 0));
97 }
98
99 #ifdef GPERF
100 #include "gperftools/profiler.h"
101
102 static int profiled_main(int argc, char **argv);
103
104 int main(int argc, char **argv)
105 {
106 int ret;
107 ProfilerStart("mutool.prof");
108 ret = profiled_main(argc, argv);
109 ProfilerStop();
110 return ret;
111 }
112
113 static int profiled_main(int argc, char **argv)
114 #else
115 int main(int argc, char **argv)
116 #endif
117 {
118 char *start, *end;
119 char buf[32];
120 int i;
121
122 if (argc == 0)
123 {
124 fprintf(stderr, "No command name found!\n");
125 return 1;
126 }
127
128 /* Check argv[0] */
129
130 if (argc > 0)
131 {
132 end = start = argv[0];
133 while (*end)
134 end++;
135 if ((end-4 >= start) && (end[-4] == '.') && (end[-3] == 'e') && (end[-2] == 'x') && (end[-1] == 'e'))
136 end = end-4;
137 for (i = 0; i < (int)nelem(tools); i++)
138 {
139 strcpy(buf, "mupdf");
140 strcat(buf, tools[i].name);
141 if (namematch(end, start, buf) || namematch(end, start, buf+2))
142 return tools[i].func(argc, argv);
143 strcpy(buf, "mu");
144 strcat(buf, tools[i].name);
145 if (namematch(end, start, buf))
146 return tools[i].func(argc, argv);
147 }
148 }
149
150 /* Check argv[1] */
151
152 if (argc > 1)
153 {
154 for (i = 0; i < (int)nelem(tools); i++)
155 if (!strcmp(tools[i].name, argv[1]))
156 return tools[i].func(argc - 1, argv + 1);
157 if (!strcmp(argv[1], "-v"))
158 {
159 fprintf(stderr, "mutool version %s\n", FZ_VERSION);
160 return 0;
161 }
162 }
163
164 /* Print usage */
165
166 fprintf(stderr, "mutool version %s\n", FZ_VERSION);
167 fprintf(stderr, "usage: mutool <command> [options]\n");
168
169 for (i = 0; i < (int)nelem(tools); i++)
170 fprintf(stderr, "\t%s\t-- %s\n", tools[i].name, tools[i].desc);
171
172 return 1;
173 }
174
175 #ifdef _MSC_VER
176 int wmain(int argc, wchar_t *wargv[])
177 {
178 char **argv = fz_argv_from_wargv(argc, wargv);
179 int ret = main(argc, argv);
180 fz_free_argv(argc, argv);
181 return ret;
182 }
183 #endif