script.cpp revision 5cacbbec22b42a0480f396eb94bcfb5f746ac68a
/** \file
* Code for handling extensions (i.e.\ scripts).
*/
/*
* Authors:
* Bryce Harrington <bryce@osdl.org>
* Ted Gould <ted@gould.cx>
* Jon A. Cruz <jon@joncruz.org>
* Abhishek Sharma
*
* Copyright (C) 2002-2005,2007 Authors
*
* Released under GNU GPL, read the file 'COPYING' for more information
*/
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <unistd.h>
#include <errno.h>
#include <glib.h>
#include <gtkmm/scrolledwindow.h>
#include <gtkmm/textview.h>
#include "desktop-handles.h"
#include "desktop.h"
#include "selection.h"
#include "sp-namedview.h"
#include "preferences.h"
#include "../system.h"
#include "script.h"
#include "dialogs/dialog-events.h"
#include "inkscape.h"
#include "xml/attribute-record.h"
#include "util/glib-list-iterators.h"
#include "path-prefix.h"
#ifdef WIN32
#include <windows.h>
#include "registrytool.h"
#endif
/** This is the command buffer that gets allocated from the stack */
#define BUFSIZE (255)
/* Namespaces */
namespace Inkscape {
namespace Extension {
namespace Implementation {
/** \brief Make GTK+ events continue to come through a little bit
This just keeps coming the events through so that we'll make the GUI
update and look pretty.
*/
void Script::pump_events (void) {
}
return;
}
/** \brief A table of what interpreters to call for a given language
This table is used to keep track of all the programs to execute a
given script. It also tracks the preference to use to overwrite
the given interpreter to a custom one per user.
*/
{"perl", "perl-interpreter", "perl" },
#ifdef WIN32
{"python", "python-interpreter", "pythonw" },
#else
{"python", "python-interpreter", "python" },
#endif
{"ruby", "ruby-interpreter", "ruby" },
{"shell", "shell-interpreter", "sh" },
};
/** \brief Look up an interpreter name, and translate to something that
is executable
\param interpNameArg The name of the interpreter that we're looking
for, should be an entry in interpreterTab
*/
{
interpreter_t const *interp = 0;
bool foundInterp = false;
foundInterp = true;
break;
}
}
// Do we have a supported interpreter type?
if (!foundInterp) {
return "";
}
// 1. Check preferences for an override.
// Note: this must be an absolute path.
if (!prefInterp.empty()) {
}
// 2. Search the path.
// Do this on all systems, for consistency.
// PATH is set up to contain the Python and Perl binary directories
// on Windows, so no extra code is necessary.
}
return interpreter_path;
}
/** \brief This function creates a script object and sets up the
variables.
\return A script object
This function just sets the command to NULL. It should get built
officially in the load function. This allows for less allocation
of memory in the unloaded state.
*/
_canceled(false)
{
}
/**
* brief Destructor
*/
{
}
/**
\return A string with the complete string with the relative directory expanded
\brief This function takes in a Repr that contains a reldir entry
and returns that data with the relative directory expanded.
Mostly it is here so that relative directories all get used
the same way.
\param reprin The Inkscape::XML::Node with the reldir in it.
Basically this function looks at an attribute of the Repr, and makes
a decision based on that. Currently, it is only working with the
'extensions' relative directory, but there will be more of them.
One thing to notice is that this function always returns an allocated
string. This means that the caller of this function can always
free what they are given (and should do it too!).
*/
// right now the only recognized relative directory is "extensions"
return str;
}
for (unsigned int i=0;
i++) {
NULL);
//printf("Filename: %s\n", filename.c_str());
}
}
return "";
}
/**
\return Whether the command given exists, including in the path
\brief This function is used to find out if something exists for
the check command. It can look in the path if required.
\param command The command or file that should be looked for
The first thing that this function does is check to see if the
incoming file name has a directory delimiter in it. This would
mean that it wants to control the directories, and should be
used directly.
If not, the path is used. Each entry in the path is stepped through,
attached to the string, and then tested. If the file is found
then a TRUE is returned. If we get all the way through the path
then a FALSE is returned, the command could not be found.
*/
{
// Check the simple case first
return false;
}
//Don't search when it is an absolute path. */
return true;
} else {
return false;
}
}
// First search in the current directory
// And then in the PATH environment variable.
} else {
}
//printf("### %s\n", localPath.c_str());
Glib::FILE_TEST_EXISTS)) {
return true;
}
}
return false;
}
/**
\return none
\brief This function 'loads' an extention, basically it determines
the full command for the extention and stores that.
\param module The extention to be loaded.
The most difficult part about this function is finding the actual
command through all of the Reprs. Basically it is hidden down a
couple of layers, and so the code has to move down too. When
the command is actually found, it has its relative directory
solved.
At that point all of the loops are exited, and there is an
if statement to make sure they didn't exit because of not finding
the command. If that's the case, the extention doesn't get loaded
and should error out at a higher level.
*/
{
return true;
}
helper_extension = "";
/* This should probably check to find the executable... */
while (child_repr != NULL) {
while (child_repr != NULL) {
if (interpretstr != NULL) {
}
}
}
}
break;
}
}
//g_return_val_if_fail(command.length() > 0, false);
return true;
}
/**
\return None.
\brief Unload this puppy!
\param module Extension to be unloaded.
This function just sets the module to unloaded. It free's the
command if it has been allocated.
*/
{
helper_extension = "";
}
/**
\return Whether the check passed or not
\brief Check every dependency that was given to make sure we should keep this extension
\param module The Extension in question
*/
{
int script_count = 0;
while (child_repr != NULL) {
script_count++;
while (child_repr != NULL) {
if (!command_text.empty()) {
/* I've got the command */
if (!existance) {
return false;
}
} else {
return false;
}
}
return false;
}
}
}
break;
}
}
if (script_count == 0) {
return false;
}
return true;
}
class ScriptDocCache : public ImplementationDocumentCache {
friend class Script;
protected:
int _tempfd;
public:
~ScriptDocCache ( );
};
_filename(""),
_tempfd(0)
{
try {
} catch (...) {
/// \todo Popup dialog here
return;
}
view->doc(), _filename.c_str(), false, false, false, Inkscape::Extension::FILE_SAVE_METHOD_TEMPORARY);
return;
}
ScriptDocCache::~ScriptDocCache ( )
{
}
ImplementationDocumentCache *Script::newDocCache( Inkscape::Extension::Extension * /*ext*/, Inkscape::UI::View::View * view ) {
return new ScriptDocCache(view);
}
/**
\return A dialog for preferences
\brief A stub funtion right now
\param module Module who's preferences need getting
\param filename Hey, the file you're getting might be important
This function should really do something, right now it doesn't.
*/
const gchar */*filename*/)
{
}
/**
\return A dialog for preferences
\brief A stub funtion right now
\param module Module whose preferences need getting
This function should really do something, right now it doesn't.
*/
{
}
/**
\return A new document that has been opened
\brief This function uses a filename that is put in, and calls
the extension's command to create an SVG file which is
returned.
\param module Extension to use.
\param filename File to open.
First things first, this function needs a temporary file name. To
create on of those the function g_file_open_tmp is used with
the header of ink_ext_.
The extension is then executed using the 'execute' function
with the filname coming in, and the temporary filename. After
That executing, the SVG should be in the temporary file.
Finally, the temporary file is opened using the SVG input module and
a document is returned. That document has its filename set to
the incoming filename (so that it's not the temporary filename).
That document is then returned from this function.
*/
const gchar *filenameArg)
{
int tempfd_out = 0;
try {
} catch (...) {
/// \todo Popup dialog here
return NULL;
}
if (data_read > 10) {
if (helper_extension.size()==0) {
} else {
}
} // data_read
}
// make sure we don't leak file descriptors from g_file_open_tmp
return mydoc;
} // open
/**
\return none
\brief This function uses an extention to save a document. It first
creates an SVG file of the document, and then runs it through
the script.
\param module Extention to be used
\param doc Document to be saved
\param filename The name to save the final file as
\return false in case of any failure writing the file, otherwise true
Well, at some point people need to save - it is really what makes
the entire application useful. And, it is possible that someone
would want to use an extetion for this, so we need a function to
do that eh?
First things first, the document is saved to a temporary file that
is an SVG file. To get the temporary filename g_file_open_tmp is used with
ink_ext_ as a prefix. Don't worry, this file gets deleted at the
end of the function.
After we have the SVG file, then extention_execute is called with
the temporary file name and the final output filename. This should
put the output of the script into the final output file. We then
delete the temporary file.
*/
const gchar *filenameArg)
{
int tempfd_in = 0;
try {
} catch (...) {
/// \todo Popup dialog here
}
if (helper_extension.size() == 0) {
} else {
}
bool success = false;
if (data_read > 0) {
}
// make sure we don't leak file descriptors from g_file_open_tmp
// FIXME: convert to utf8 (from "filename encoding") and unlink_utf8name
if (success == false) {
}
return;
}
/**
\return none
\brief This function uses an extention as a effect on a document.
\param module Extention to effect with.
\param doc Document to run through the effect.
This function is a little bit trickier than the previous two. It
needs two temporary files to get it's work done. Both of these
files have random names created for them using the g_file_open_temp function
with the ink_ext_ prefix in the temporary directory. Like the other
functions, the temporary files are deleted at the end.
modules for SVG load and save are used. They are both used through
the module system function by passing their keys into the functions.
The command itself is built a little bit differently than in other
functions because the effect support selections. So on the command
line a list of all the ids that are selected is included. Currently,
this only works for a single selected object, but there will be more.
The command string is filled with the data, and then after the execution
it is freed.
The execute function is used at the core of this function
to execute the Script on the two SVG documents (actually only one
exists at the time, the other is created by that script). At that
point both should be full, and the second one is loaded.
*/
{
}
printf("TOO BAD TO LIVE!!!");
exit(1);
}
// this is a no-doc extension, e.g. a Help menu command;
// just run the command without any files, ignoring errors
return;
}
int tempfd_out = 0;
try {
} catch (...) {
/// \todo Popup dialog here
return;
}
selected_id += "--id=";
++selected;
}
}
pump_events();
if (data_read > 10) {
} // data_read
pump_events();
// make sure we don't leak file descriptors from g_file_open_tmp
/* Do something with mydoc.... */
if (mydoc) {
// Getting the named view from the document generated by the extension
//Check if it has a default layer set up
//If so, get that layer
}
//If that layer exists,
if (layer) {
//set the current layer
}
}
return;
}
/**
\brief A function to take all the svg elements from one document
and put them in another.
\param oldroot The root node of the document to be replaced
\param newroot The root node of the document to replace it with
This function first deletes all of the data in the old document. It
does this by creating a list of what needs to be deleted, and then
goes through the list. This two pass approach removes issues with
the list being change while parsing through it. Lots of nasty bugs.
Then, it goes through the new document, duplicating all of the
elements and putting them into the old document. The copy
is then complete.
*/
{
}
} else {
}
}
for (unsigned int i = 0; i < delete_list.size(); i++) {
}
if (oldroot_namedview != NULL) {
}
}
} else {
}
}
{
// Make a list of all attributes of the old root node.
}
// Delete the attributes of the old root nodes.
}
// Set the new attributes.
}
}
/** \todo Restore correct layer */
/** \todo Restore correct selection */
}
/** \brief This function checks the stderr file, and if it has data,
shows it in a warning dialog to the user
\param filename Filename of the stderr file
*/
{
warning.set_resizable(true);
/* Gtk::TextView * textview = new Gtk::TextView(Gtk::TextBuffer::create()); */
textview->set_editable(false);
// Remove the last character
errormsg -= 1;
*errormsg = '\0';
scrollwindow->show();
return;
}
bool Script::cancelProcessing (void) {
_canceled = true;
_main_loop->quit();
return true;
}
/** \brief This is the core of the extension file as it actually does
the execution of the extension.
\param in_command The command to be executed
\param filein Filename coming in
\param fileout Filename of the out file
\return Number of bytes that were read into the output file.
The first thing that this function does is build the command to be
executed. This consists of the first string (in_command) and then
the filename for input (filein). This file is put on the command
line.
The next thing is that this function does is open a pipe to the
command and get the file handle in the ppipe variable. It then
opens the output file with the output file handle. Both of these
operations are checked extensively for errors.
After both are opened, then the data is copied from the output
of the pipe into the file out using fread and fwrite. These two
functions are used because of their primitive nature they make
no assumptions about the data. A buffer is used in the transfer,
but the output of fread is stored so the exact number of bytes
is handled gracefully.
At the very end (after the data has been copied) both of the files
are closed, and we return to what we were doing.
*/
{
// printf("Executing\n");
// Use Glib::find_program_in_path instead of the equivalent
// Glib::spawn_* functionality, because _wspawnp is broken on Windows:
// it doesn't work when PATH contains Unicode directories
}
if (interpreted) {
// On Windows, Python garbles Unicode command line parameters
// in an useless way. This means extensions fail when Inkscape
// is run from an Unicode directory.
// As a workaround, we set the working directory to the one
// containing the script.
#ifdef G_OS_WIN32
// ANNOYING: glibmm does not wrap g_win32_locale_filename_from_utf8
#endif
}
// assemble the rest of argv
}
int stdout_pipe, stderr_pipe;
try {
argv, // arg v
&_pid, // Pid
NULL, // STDIN
&stdout_pipe, // STDOUT
&stderr_pipe); // STDERR
return 0;
}
_canceled = false;
_main_loop->run();
// Ensure all the data is out of the pipe
}
}
if (_canceled) {
// std::cout << "Script Canceled" << std::endl;
return 0;
}
if (stderr_data.length() != 0 &&
) {
_("Inkscape has received additional data from the script executed. "
"The script did not return an error, but this may indicate the results will not be as expected."));
}
if (stdout_data.length() == 0) {
return 0;
}
// std::cout << "Finishing Execution." << std::endl;
return stdout_data.length();
}
} // namespace Implementation
} // namespace Extension
} // namespace Inkscape
/*
Local Variables:
mode:c++
c-file-style:"stroustrup"
c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
indent-tabs-mode:nil
fill-column:99
End:
*/
// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :