/src/libvips/libvips/iofuncs/system.c
Line | Count | Source |
1 | | /* vips_system(): run a command on an image |
2 | | * |
3 | | * 7/3/00 JC |
4 | | * - hacked it in |
5 | | * 21/10/02 JC |
6 | | * - use mktemp() if mkstemp() is not available |
7 | | * 10/3/03 JC |
8 | | * - out can be NULL |
9 | | * 23/12/04 |
10 | | * - use g_mkstemp() |
11 | | * 8/9/09 |
12 | | * - add .v suffix (thanks Roland) |
13 | | * - use vipsbuf |
14 | | * - rewrite to make it simpler |
15 | | * 2/2/10 |
16 | | * - gtkdoc |
17 | | * 4/6/13 |
18 | | * - redo as a class |
19 | | * - input and output images are now optional |
20 | | * 3/5/14 |
21 | | * - switch to g_spawn_command_line_sync() from popen() ... helps stop |
22 | | * stray command-windows on Windows |
23 | | * 27/3/16 |
24 | | * - allow [options] in out_format |
25 | | * 20/8/25 |
26 | | * - add "cache" argument |
27 | | */ |
28 | | |
29 | | /* |
30 | | |
31 | | This file is part of VIPS. |
32 | | |
33 | | VIPS is free software; you can redistribute it and/or modify |
34 | | it under the terms of the GNU Lesser General Public License as published by |
35 | | the Free Software Foundation; either version 2 of the License, or |
36 | | (at your option) any later version. |
37 | | |
38 | | This program is distributed in the hope that it will be useful, |
39 | | but WITHOUT ANY WARRANTY; without even the implied warranty of |
40 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
41 | | GNU Lesser General Public License for more details. |
42 | | |
43 | | You should have received a copy of the GNU Lesser General Public License |
44 | | along with this program; if not, write to the Free Software |
45 | | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
46 | | 02110-1301 USA |
47 | | |
48 | | */ |
49 | | |
50 | | /* |
51 | | |
52 | | These files are distributed with VIPS - http://www.vips.ecs.soton.ac.uk |
53 | | |
54 | | */ |
55 | | |
56 | | /* |
57 | | #define DEBUG |
58 | | */ |
59 | | |
60 | | #ifdef HAVE_CONFIG_H |
61 | | #include <config.h> |
62 | | #endif /*HAVE_CONFIG_H*/ |
63 | | #include <glib/gi18n-lib.h> |
64 | | |
65 | | #include <stdio.h> |
66 | | #include <stdlib.h> |
67 | | |
68 | | #ifdef HAVE_UNISTD_H |
69 | | #include <unistd.h> |
70 | | #endif /*HAVE_UNISTD_H*/ |
71 | | #include <stdarg.h> |
72 | | #include <string.h> |
73 | | #include <errno.h> |
74 | | |
75 | | #include <vips/vips.h> |
76 | | #include <vips/internal.h> |
77 | | |
78 | | typedef struct _VipsSystem { |
79 | | VipsOperation parent_instance; |
80 | | |
81 | | VipsArrayImage *in; |
82 | | VipsImage *out; |
83 | | char *cmd_format; |
84 | | char *in_format; |
85 | | char *out_format; |
86 | | char *log; |
87 | | gboolean cache; |
88 | | |
89 | | /* Array of names we wrote the input images to. |
90 | | */ |
91 | | char **in_name; |
92 | | |
93 | | /* Output name without any options, so /tmp/vips-weifh.svg, for |
94 | | * example. |
95 | | */ |
96 | | char *out_name; |
97 | | |
98 | | /* Output name with any options, so /tmp/vips-weifh.svg[scale=2], for |
99 | | * example. |
100 | | */ |
101 | | char *out_name_options; |
102 | | |
103 | | } VipsSystem; |
104 | | |
105 | | typedef VipsOperationClass VipsSystemClass; |
106 | | |
107 | 0 | G_DEFINE_TYPE(VipsSystem, vips_system, VIPS_TYPE_OPERATION); |
108 | 0 |
|
109 | 0 | static void |
110 | 0 | vips_system_dispose(GObject *gobject) |
111 | 0 | { |
112 | 0 | VipsSystem *system = (VipsSystem *) gobject; |
113 | |
|
114 | 0 | if (system->in_name) { |
115 | 0 | int i; |
116 | |
|
117 | 0 | for (i = 0; i < VIPS_AREA(system->in)->n; i++) { |
118 | 0 | g_unlink(system->in_name[i]); |
119 | 0 | VIPS_FREE(system->in_name[i]); |
120 | 0 | } |
121 | 0 | } |
122 | |
|
123 | 0 | VIPS_FREE(system->out_name); |
124 | 0 | VIPS_FREE(system->out_name_options); |
125 | |
|
126 | 0 | G_OBJECT_CLASS(vips_system_parent_class)->dispose(gobject); |
127 | 0 | } |
128 | | |
129 | | static int |
130 | | vips_system_build(VipsObject *object) |
131 | 0 | { |
132 | 0 | VipsObjectClass *class = VIPS_OBJECT_GET_CLASS(object); |
133 | 0 | VipsSystem *system = (VipsSystem *) object; |
134 | |
|
135 | 0 | int i; |
136 | |
|
137 | 0 | char cmd[VIPS_PATH_MAX]; |
138 | |
|
139 | 0 | char *p; |
140 | 0 | char *std_output; |
141 | 0 | char *std_error; |
142 | 0 | int result; |
143 | 0 | GError *error = NULL; |
144 | |
|
145 | 0 | if (VIPS_OBJECT_CLASS(vips_system_parent_class)->build(object)) |
146 | 0 | return -1; |
147 | | |
148 | | /* Write the input images to files. We must always make copies of the |
149 | | * files, even if this image is a disc file already, in case the |
150 | | * command needs a different format. |
151 | | */ |
152 | 0 | if (system->in) { |
153 | 0 | char *in_format = system->in_format ? system->in_format : "%s.tif"; |
154 | 0 | int n; |
155 | 0 | VipsImage **in = vips_array_image_get(system->in, &n); |
156 | |
|
157 | 0 | if (!(system->in_name = VIPS_ARRAY(object, n, char *))) |
158 | 0 | return -1; |
159 | 0 | memset(system->in_name, 0, n * sizeof(char *)); |
160 | 0 | for (i = 0; i < n; i++) { |
161 | 0 | if (!(system->in_name[i] = vips__temp_name(in_format))) |
162 | 0 | return -1; |
163 | 0 | if (vips_image_write_to_file(in[i], system->in_name[i], NULL)) |
164 | 0 | return -1; |
165 | 0 | } |
166 | 0 | } |
167 | | |
168 | | /* Make the output filename. |
169 | | */ |
170 | 0 | if (system->out_format) { |
171 | 0 | char filename[VIPS_PATH_MAX]; |
172 | 0 | char option_string[VIPS_PATH_MAX]; |
173 | |
|
174 | 0 | vips__filename_split8(system->out_format, filename, option_string); |
175 | 0 | if (!(system->out_name = vips__temp_name(filename))) |
176 | 0 | return -1; |
177 | 0 | system->out_name_options = |
178 | 0 | g_strconcat(system->out_name, option_string, NULL); |
179 | 0 | } |
180 | | |
181 | 0 | g_strlcpy(cmd, system->cmd_format, VIPS_PATH_MAX); |
182 | 0 | if (system->in) |
183 | 0 | for (i = 0; i < VIPS_AREA(system->in)->n; i++) |
184 | 0 | if (vips__substitute(cmd, VIPS_PATH_MAX, system->in_name[i])) { |
185 | 0 | vips_error(class->nickname, "%s", |
186 | 0 | _("unable to substitute input filename")); |
187 | 0 | return -1; |
188 | 0 | } |
189 | 0 | if (system->out_name && |
190 | 0 | vips__substitute(cmd, VIPS_PATH_MAX, system->out_name)) { |
191 | 0 | vips_error(class->nickname, "%s", |
192 | 0 | _("unable to substitute output filename")); |
193 | 0 | return -1; |
194 | 0 | } |
195 | | |
196 | | /* Swap all "%%" in the string for a single "%". We need this for |
197 | | * compatibility with older printf-based vips_system()s which |
198 | | * needed a double %%. |
199 | | */ |
200 | 0 | for (p = cmd; *p; p++) |
201 | 0 | if (p[0] == '%' && |
202 | 0 | p[1] == '%') |
203 | 0 | memmove(p, p + 1, strlen(p)); |
204 | |
|
205 | | #ifdef DEBUG |
206 | | printf("vips_system_build: spawn <<%s>> ...\n", cmd); |
207 | | #endif /*DEBUG*/ |
208 | 0 | if (!g_spawn_command_line_sync(cmd, |
209 | 0 | &std_output, &std_error, &result, &error) || |
210 | 0 | result) { |
211 | | #ifdef DEBUG |
212 | | printf("\t... failed\n"); |
213 | | #endif /*DEBUG*/ |
214 | |
|
215 | 0 | if (error) { |
216 | 0 | vips_error(class->nickname, "%s", error->message); |
217 | 0 | g_error_free(error); |
218 | 0 | } |
219 | 0 | if (std_error) { |
220 | 0 | g_strchomp(std_error); |
221 | 0 | if (strcmp(std_error, "") != 0) |
222 | 0 | vips_error(class->nickname, "error output: %s", std_error); |
223 | 0 | VIPS_FREE(std_error); |
224 | 0 | } |
225 | 0 | if (std_output) { |
226 | 0 | g_strchomp(std_output); |
227 | 0 | if (strcmp(std_output, "") != 0) |
228 | 0 | vips_error(class->nickname, "output: %s", std_output); |
229 | 0 | VIPS_FREE(std_output); |
230 | 0 | } |
231 | 0 | vips_error_system(result, class->nickname, |
232 | 0 | _("command \"%s\" failed"), cmd); |
233 | |
|
234 | 0 | return -1; |
235 | 0 | } |
236 | | |
237 | | #ifdef DEBUG |
238 | | printf("\t... success\n"); |
239 | | #endif /*DEBUG*/ |
240 | | |
241 | 0 | if (std_error) { |
242 | 0 | g_strchomp(std_error); |
243 | 0 | if (strcmp(std_error, "") != 0) |
244 | 0 | g_warning("stderr output: %s", std_error); |
245 | 0 | } |
246 | 0 | if (std_output) { |
247 | 0 | g_strchomp(std_output); |
248 | 0 | g_object_set(system, "log", std_output, NULL); |
249 | 0 | } |
250 | |
|
251 | 0 | VIPS_FREE(std_output); |
252 | 0 | VIPS_FREE(std_error); |
253 | |
|
254 | 0 | if (system->out_name_options) { |
255 | 0 | VipsImage *out; |
256 | |
|
257 | 0 | if (!(out = vips_image_new_from_file(system->out_name_options, NULL))) |
258 | 0 | return -1; |
259 | 0 | vips_image_set_delete_on_close(out, TRUE); |
260 | 0 | g_object_set(system, "out", out, NULL); |
261 | 0 | } |
262 | | |
263 | 0 | return 0; |
264 | 0 | } |
265 | | |
266 | | static VipsOperationFlags |
267 | | vips_system_get_flags(VipsOperation *operation) |
268 | 0 | { |
269 | 0 | VipsOperationFlags flags = |
270 | 0 | VIPS_OPERATION_CLASS(vips_system_parent_class)->get_flags(operation); |
271 | 0 | VipsSystem *system = (VipsSystem *) operation; |
272 | |
|
273 | 0 | if (system->cache) |
274 | 0 | flags &= ~VIPS_OPERATION_NOCACHE; |
275 | |
|
276 | 0 | return flags; |
277 | 0 | } |
278 | | |
279 | | static void |
280 | | vips_system_class_init(VipsSystemClass *class) |
281 | 0 | { |
282 | 0 | GObjectClass *gobject_class = G_OBJECT_CLASS(class); |
283 | 0 | VipsObjectClass *vobject_class = VIPS_OBJECT_CLASS(class); |
284 | 0 | VipsOperationClass *operation_class = VIPS_OPERATION_CLASS(class); |
285 | |
|
286 | 0 | gobject_class->dispose = vips_system_dispose; |
287 | 0 | gobject_class->set_property = vips_object_set_property; |
288 | 0 | gobject_class->get_property = vips_object_get_property; |
289 | |
|
290 | 0 | vobject_class->nickname = "system"; |
291 | 0 | vobject_class->description = _("run an external command"); |
292 | 0 | vobject_class->build = vips_system_build; |
293 | |
|
294 | 0 | operation_class->get_flags = vips_system_get_flags; |
295 | 0 | operation_class->flags = VIPS_OPERATION_NOCACHE; |
296 | |
|
297 | 0 | VIPS_ARG_BOXED(class, "in", 0, |
298 | 0 | _("Input"), |
299 | 0 | _("Array of input images"), |
300 | 0 | VIPS_ARGUMENT_OPTIONAL_INPUT, |
301 | 0 | G_STRUCT_OFFSET(VipsSystem, in), |
302 | 0 | VIPS_TYPE_ARRAY_IMAGE); |
303 | |
|
304 | 0 | VIPS_ARG_IMAGE(class, "out", 1, |
305 | 0 | _("Output"), |
306 | 0 | _("Output image"), |
307 | 0 | VIPS_ARGUMENT_OPTIONAL_OUTPUT, |
308 | 0 | G_STRUCT_OFFSET(VipsSystem, out)); |
309 | |
|
310 | 0 | VIPS_ARG_STRING(class, "cmd_format", 2, |
311 | 0 | _("Command"), |
312 | 0 | _("Command to run"), |
313 | 0 | VIPS_ARGUMENT_REQUIRED_INPUT, |
314 | 0 | G_STRUCT_OFFSET(VipsSystem, cmd_format), |
315 | 0 | NULL); |
316 | |
|
317 | 0 | VIPS_ARG_STRING(class, "in_format", 3, |
318 | 0 | _("Input format"), |
319 | 0 | _("Format for input filename"), |
320 | 0 | VIPS_ARGUMENT_OPTIONAL_INPUT, |
321 | 0 | G_STRUCT_OFFSET(VipsSystem, in_format), |
322 | 0 | NULL); |
323 | |
|
324 | 0 | VIPS_ARG_STRING(class, "out_format", 4, |
325 | 0 | _("Output format"), |
326 | 0 | _("Format for output filename"), |
327 | 0 | VIPS_ARGUMENT_OPTIONAL_INPUT, |
328 | 0 | G_STRUCT_OFFSET(VipsSystem, out_format), |
329 | 0 | NULL); |
330 | |
|
331 | 0 | VIPS_ARG_BOOL(class, "cache", 5, |
332 | 0 | _("Cache"), |
333 | 0 | _("Cache this call"), |
334 | 0 | VIPS_ARGUMENT_OPTIONAL_INPUT, |
335 | 0 | G_STRUCT_OFFSET(VipsSystem, cache), |
336 | 0 | FALSE); |
337 | |
|
338 | 0 | VIPS_ARG_STRING(class, "log", 6, |
339 | 0 | _("Log"), |
340 | 0 | _("Command log"), |
341 | 0 | VIPS_ARGUMENT_OPTIONAL_OUTPUT, |
342 | 0 | G_STRUCT_OFFSET(VipsSystem, log), |
343 | 0 | NULL); |
344 | |
|
345 | 0 | } |
346 | | |
347 | | static void |
348 | | vips_system_init(VipsSystem *system) |
349 | 0 | { |
350 | 0 | } |
351 | | |
352 | | /** |
353 | | * vips_system: |
354 | | * @cmd_format: command to run |
355 | | * @...: `NULL`-terminated list of optional named arguments |
356 | | * |
357 | | * [ctor@Image.system] runs a command, optionally passing a set of images in and |
358 | | * optionally getting an image back. The command's stdout is returned in @log. |
359 | | * |
360 | | * First, if @in is set, the array of images are written to files. See |
361 | | * [ctor@Image.new_temp_file] to see how temporary files are created. |
362 | | * If @in_format is something like `%s.png`, the file will be written in PNG |
363 | | * format. By default, @in_format is `%s.tif`. |
364 | | * |
365 | | * If @out_format is set, an output filename is formed in the same way. Any |
366 | | * trailing `[options]` are stripped from @out_format. |
367 | | * |
368 | | * The command string to run is made by substituting the first set of `%s` |
369 | | * in @cmd_format for the names of the input files, if @in is set, and then |
370 | | * the next `%s` for the output filename, if @out_format is set. |
371 | | * You can put a number between the `%` and the `s` to change the order |
372 | | * in which the substitution occurs. |
373 | | * |
374 | | * The command is executed with [`popen()`](man:popen(3)) and the output |
375 | | * captured in @log. |
376 | | * |
377 | | * After the command finishes, if @out_format is set, the output image is |
378 | | * opened and returned in @out. You can append `[options]` to @out_format to |
379 | | * control how this open happens. |
380 | | * |
381 | | * Closing @out image will automatically delete the output file. |
382 | | * |
383 | | * Finally the input images are deleted. |
384 | | * |
385 | | * If @cache is set, this call will be added to the libvips operation cache |
386 | | * and reused if possible. |
387 | | * |
388 | | * For example, this call will run the ImageMagick convert program on an |
389 | | * image, using JPEG files to pass images into and out of the convert command. |
390 | | * |
391 | | * ```c |
392 | | * VipsArrayImage *in; |
393 | | * VipsImage *out; |
394 | | * char *log; |
395 | | * |
396 | | * if (vips_system("convert %s -swirl 45 %s", |
397 | | * "in", in, |
398 | | * "out", &out, |
399 | | * "in_format", "%s.jpg", |
400 | | * "out_format", "%s.jpg", |
401 | | * "cache", TRUE, |
402 | | * "log", &log, |
403 | | * NULL)) |
404 | | * error ... |
405 | | * ``` |
406 | | * |
407 | | * ::: tip "Optional arguments" |
408 | | * * @in: [struct@ArrayImage], array of input images |
409 | | * * @out: [class@Image], output, image |
410 | | * * @in_format: `gchararray`, write input files like this |
411 | | * * @out_format: `gchararray`, write output filename like this |
412 | | * * @cache: `gboolean`, cache this call |
413 | | * * @log: `gchararray`, output, stdout of command is returned here |
414 | | * |
415 | | * Returns: 0 on success, -1 on failure. |
416 | | */ |
417 | | int |
418 | | vips_system(const char *cmd_format, ...) |
419 | 0 | { |
420 | 0 | va_list ap; |
421 | 0 | int result; |
422 | |
|
423 | 0 | va_start(ap, cmd_format); |
424 | 0 | result = vips_call_split("system", ap, cmd_format); |
425 | 0 | va_end(ap); |
426 | |
|
427 | 0 | return result; |
428 | 0 | } |