2009-08-16 17:00:08 +02:00
|
|
|
/* im_system(): run a command on an image
|
|
|
|
*
|
|
|
|
* 7/3/00 JC
|
|
|
|
* - hacked it in
|
|
|
|
* 21/10/02 JC
|
|
|
|
* - use mktemp() if mkstemp() is not available
|
|
|
|
* 10/3/03 JC
|
|
|
|
* - out can be NULL
|
|
|
|
* 23/12/04
|
|
|
|
* - use g_mkstemp()
|
2009-09-10 12:44:10 +02:00
|
|
|
* 8/9/09
|
|
|
|
* - add .v suffix (thanks Roland)
|
|
|
|
* - use vipsbuf
|
|
|
|
* - rewrite to make it simpler
|
2010-02-02 15:32:17 +01:00
|
|
|
* 2/2/10
|
|
|
|
* - gtkdoc
|
2009-08-16 17:00:08 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
|
|
|
This file is part of VIPS.
|
|
|
|
|
|
|
|
VIPS is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU Lesser General Public License as published by
|
|
|
|
the Free Software Foundation; either version 2 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU Lesser General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Lesser General Public License
|
|
|
|
along with this program; if not, write to the Free Software
|
|
|
|
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
|
|
|
These files are distributed with VIPS - http://www.vips.ecs.soton.ac.uk
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include <config.h>
|
|
|
|
#endif /*HAVE_CONFIG_H*/
|
|
|
|
#include <vips/intl.h>
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
#ifdef HAVE_UNISTD_H
|
|
|
|
#include <unistd.h>
|
|
|
|
#endif /*HAVE_UNISTD_H*/
|
|
|
|
#include <stdarg.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
|
|
|
|
|
|
|
#include <vips/vips.h>
|
|
|
|
|
|
|
|
#define IM_MAX_STRSIZE (4096)
|
|
|
|
|
2010-02-02 15:32:17 +01:00
|
|
|
/**
|
|
|
|
* im_system:
|
|
|
|
* @im: image to run command on
|
|
|
|
* @cmd: command to run
|
|
|
|
* @out: stdout of command is returned here
|
|
|
|
*
|
|
|
|
* im_system() runs a command on an image, returning the command's output as a
|
2010-02-02 22:55:29 +01:00
|
|
|
* string. The command is executed with popen(), the first '%%s' in the
|
2010-02-02 15:32:17 +01:00
|
|
|
* command being substituted for a filename.
|
|
|
|
*
|
|
|
|
* If the IMAGE is a file on disc, then the filename will be the name of the
|
|
|
|
* real file. If the image is in memory, or the result of a computation,
|
|
|
|
* then a new file is created in the temporary area called something like
|
|
|
|
* "vips_XXXXXX.v", and that filename given to the command. The file is
|
|
|
|
* deleted when the command finishes.
|
|
|
|
*
|
2010-07-29 12:18:44 +02:00
|
|
|
* See im_system_image() for details on how VIPS selects a temporary
|
|
|
|
* directory.
|
2010-02-02 15:32:17 +01:00
|
|
|
*
|
|
|
|
* In all cases, @log must be freed with im_free().
|
|
|
|
*
|
|
|
|
* See also: im_system_image().
|
|
|
|
*
|
|
|
|
* Returns: 0 on success, -1 on error
|
2009-08-16 17:00:08 +02:00
|
|
|
*/
|
|
|
|
int
|
|
|
|
im_system( IMAGE *im, const char *cmd, char **out )
|
|
|
|
{
|
|
|
|
FILE *fp;
|
|
|
|
|
|
|
|
if( !im_isfile( im ) ) {
|
|
|
|
IMAGE *disc;
|
|
|
|
|
2010-01-10 22:28:38 +01:00
|
|
|
if( !(disc = im__open_temp( "%s.v" )) )
|
2009-08-16 17:00:08 +02:00
|
|
|
return( -1 );
|
2009-09-10 12:44:10 +02:00
|
|
|
if( im_copy( im, disc ) ||
|
|
|
|
im_system( disc, cmd, out ) ) {
|
2009-08-16 17:00:08 +02:00
|
|
|
im_close( disc );
|
|
|
|
return( -1 );
|
|
|
|
}
|
|
|
|
im_close( disc );
|
|
|
|
}
|
2010-01-08 17:58:44 +01:00
|
|
|
else if( (fp = im_popenf( cmd, "r", im->filename )) ) {
|
2009-08-16 17:00:08 +02:00
|
|
|
char line[IM_MAX_STRSIZE];
|
2010-01-08 17:58:44 +01:00
|
|
|
char txt[IM_MAX_STRSIZE];
|
|
|
|
VipsBuf buf = VIPS_BUF_STATIC( txt );
|
2009-08-16 17:00:08 +02:00
|
|
|
|
|
|
|
while( fgets( line, IM_MAX_STRSIZE, fp ) )
|
2009-09-10 12:44:10 +02:00
|
|
|
if( !vips_buf_appends( &buf, line ) )
|
2009-08-16 17:00:08 +02:00
|
|
|
break;
|
|
|
|
pclose( fp );
|
|
|
|
|
|
|
|
if( out )
|
2009-09-10 12:44:10 +02:00
|
|
|
*out = im_strdup( NULL, vips_buf_all( &buf ) );
|
2009-08-16 17:00:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return( 0 );
|
|
|
|
}
|