gimpplugindebug.c 3.41 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
/* The GIMP -- an image manipulation program
 * Copyright (C) 1995 Spencer Kimball and Peter Mattis
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU 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 General Public License for more details.
 *
 * You should have received a copy of the GNU 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.
 */

#include "config.h"

#include <string.h>

#include <glib-object.h>

#include "core/core-types.h"

#include "core/gimp.h"

#include "plug-in-debug.h"


struct _GimpPlugInDebug
{
  gchar  *name;
  guint   flags;
  gchar **args;
};


static const GDebugKey gimp_debug_wrap_keys[] = {
  {"query", GIMP_DEBUG_WRAP_QUERY},
  {"init",  GIMP_DEBUG_WRAP_INIT},
  {"run",   GIMP_DEBUG_WRAP_RUN},
  {"on",    GIMP_DEBUG_WRAP_DEFAULT}
};


void
plug_in_debug_init (Gimp *gimp)
{
  GimpPlugInDebug  *dbg;
  const gchar      *wrap, *wrapper;
  gchar            *debug_string;
  gchar           **args;
  GError           *error = NULL;

  g_return_if_fail (GIMP_IS_GIMP (gimp));

  wrap = g_getenv ("GIMP_PLUGIN_DEBUG_WRAP");
  wrapper = g_getenv ("GIMP_PLUGIN_DEBUG_WRAPPER");

  if (!(wrap && wrapper))
    return;

  if (!g_shell_parse_argv (wrapper, NULL, &args, &error))
    {
      g_warning ("Unable to parse debug wrapper: \"%s\"\n%s",
                 wrapper, error->message);
      g_error_free (error);
      return;
    }

  dbg = g_new (GimpPlugInDebug, 1);

  dbg->args  = args;

  debug_string = strchr (wrap, ',');

  if (debug_string)
    {
      dbg->name = g_strndup (wrap, debug_string - wrap);
      dbg->flags = g_parse_debug_string (debug_string + 1,
                                         gimp_debug_wrap_keys,
84
                                         G_N_ELEMENTS (gimp_debug_wrap_keys));
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
    }
  else
    {
      dbg->name = g_strdup (wrap);
      dbg->flags = GIMP_DEBUG_WRAP_DEFAULT;
    }
  
  gimp->plug_in_debug = dbg;
}

void
plug_in_debug_exit (Gimp *gimp)
{
  GimpPlugInDebug *dbg;

  g_return_if_fail (GIMP_IS_GIMP (gimp));

  dbg = gimp->plug_in_debug;

  if (dbg == NULL)
    return;

  if (dbg->name)
    g_free (dbg->name);

  if (dbg->args)
    g_strfreev (dbg->args);

  g_free (dbg);

  gimp->plug_in_debug = NULL;
}

gchar **
plug_in_debug_argv (Gimp               *gimp,
		    const gchar        *name,
                    GimpDebugWrapFlag   flag,
                    gchar             **args)
{
  GimpPlugInDebug  *dbg;
  GPtrArray        *argv;
  gchar           **arg;

  g_return_val_if_fail (GIMP_IS_GIMP (gimp), NULL);
  g_return_val_if_fail (gimp->plug_in_debug != NULL, NULL);
  g_return_val_if_fail (args != NULL, NULL);

  dbg = gimp->plug_in_debug;

  if (!(dbg->flags & flag) || (strcmp (dbg->name, name) != 0))
    return NULL;

  argv = g_ptr_array_sized_new (8);

  for (arg = gimp->plug_in_debug->args; *arg != NULL; arg++)
    g_ptr_array_add (argv, *arg);

  for (arg = args; *arg != NULL; arg++)
    g_ptr_array_add (argv, *arg);

  g_ptr_array_add (argv, NULL);

  return (gchar **) g_ptr_array_free (argv, FALSE);
}