2006-05-21 16:11:05 -07:00
|
|
|
/*
|
2006-02-26 22:38:47 -08:00
|
|
|
* mooscript-context.c
|
2005-12-19 18:02:54 -08:00
|
|
|
*
|
2006-02-23 06:03:17 -08:00
|
|
|
* Copyright (C) 2004-2006 by Yevgen Muntyan <muntyan@math.tamu.edu>
|
2005-12-19 18:02:54 -08:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* See COPYING file that comes with this distribution.
|
|
|
|
*/
|
|
|
|
|
2006-08-20 12:38:20 -07:00
|
|
|
#include "mooscript-context-private.h"
|
2006-05-14 16:45:13 -07:00
|
|
|
#include "mooscript-parser.h"
|
2006-08-20 12:38:20 -07:00
|
|
|
#include "mooscript-func-private.h"
|
2006-05-24 00:02:54 -07:00
|
|
|
#include "mooutils/moomarshals.h"
|
2005-12-19 20:33:28 -08:00
|
|
|
#include <glib/gprintf.h>
|
2006-02-26 01:30:57 -08:00
|
|
|
#include <gtk/gtkwindow.h>
|
2005-12-19 18:02:54 -08:00
|
|
|
|
|
|
|
#define N_POS_VARS 20
|
|
|
|
|
2006-08-20 12:38:20 -07:00
|
|
|
typedef void (*MSPrintFunc) (const char *string,
|
|
|
|
MSContext *ctx);
|
|
|
|
|
|
|
|
struct _MSContextPrivate {
|
|
|
|
GHashTable *vars;
|
|
|
|
MSError error;
|
|
|
|
char *error_msg;
|
|
|
|
MSPrintFunc print_func;
|
|
|
|
|
|
|
|
MSValue *return_val;
|
|
|
|
guint break_set : 1;
|
|
|
|
guint continue_set : 1;
|
|
|
|
guint return_set : 1;
|
|
|
|
|
|
|
|
int argc;
|
|
|
|
char **argv;
|
|
|
|
char *name;
|
|
|
|
};
|
|
|
|
|
2006-02-26 01:30:57 -08:00
|
|
|
enum {
|
|
|
|
PROP_0,
|
2006-03-07 17:21:06 -08:00
|
|
|
PROP_WINDOW,
|
|
|
|
PROP_NAME,
|
|
|
|
PROP_ARGV
|
2006-02-26 01:30:57 -08:00
|
|
|
};
|
|
|
|
|
2006-05-24 00:02:54 -07:00
|
|
|
enum {
|
|
|
|
GET_ENV_VAR,
|
|
|
|
N_SIGNALS
|
|
|
|
};
|
|
|
|
|
|
|
|
static guint signals[N_SIGNALS];
|
|
|
|
|
2006-02-24 20:39:12 -08:00
|
|
|
G_DEFINE_TYPE (MSContext, ms_context, G_TYPE_OBJECT)
|
2005-12-19 18:02:54 -08:00
|
|
|
|
|
|
|
|
2006-05-19 22:09:09 -07:00
|
|
|
#if GLIB_CHECK_VERSION(2,10,0)
|
|
|
|
#define ms_variable_alloc() g_slice_new0 (MSVariable)
|
|
|
|
#define ms_variable_free(v) g_slice_free (MSVariable, v)
|
|
|
|
#else
|
|
|
|
#define ms_variable_alloc() g_new0 (MSVariable, 1)
|
|
|
|
#define ms_variable_free(v) g_free (v)
|
|
|
|
#endif
|
|
|
|
|
2006-02-26 01:30:57 -08:00
|
|
|
static void
|
|
|
|
ms_context_set_property (GObject *object,
|
|
|
|
guint prop_id,
|
|
|
|
const GValue *value,
|
|
|
|
GParamSpec *pspec)
|
|
|
|
{
|
|
|
|
MSContext *ctx = MS_CONTEXT (object);
|
|
|
|
|
|
|
|
switch (prop_id)
|
|
|
|
{
|
|
|
|
case PROP_WINDOW:
|
|
|
|
ctx->window = g_value_get_object (value);
|
|
|
|
g_object_notify (object, "window");
|
|
|
|
break;
|
|
|
|
|
2006-03-07 17:21:06 -08:00
|
|
|
case PROP_NAME:
|
2006-08-20 12:38:20 -07:00
|
|
|
g_free (ctx->priv->name);
|
2006-03-07 17:21:06 -08:00
|
|
|
ctx->window = g_strdup (g_value_get_string (value));
|
|
|
|
g_object_notify (object, "name");
|
|
|
|
break;
|
|
|
|
|
|
|
|
case PROP_ARGV:
|
2006-08-20 12:38:20 -07:00
|
|
|
g_strfreev (ctx->priv->argv);
|
|
|
|
ctx->priv->argv = g_strdupv (g_value_get_pointer (value));
|
|
|
|
ctx->priv->argc = ctx->priv->argv ? g_strv_length (ctx->priv->argv) : 0;
|
2006-03-07 17:21:06 -08:00
|
|
|
g_object_notify (object, "argv");
|
|
|
|
break;
|
|
|
|
|
2006-02-26 01:30:57 -08:00
|
|
|
default:
|
|
|
|
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
ms_context_get_property (GObject *object,
|
|
|
|
guint prop_id,
|
|
|
|
GValue *value,
|
|
|
|
GParamSpec *pspec)
|
|
|
|
{
|
|
|
|
MSContext *ctx = MS_CONTEXT (object);
|
|
|
|
|
|
|
|
switch (prop_id)
|
|
|
|
{
|
|
|
|
case PROP_WINDOW:
|
|
|
|
g_value_set_object (value, ctx->window);
|
|
|
|
break;
|
|
|
|
|
2006-03-07 17:21:06 -08:00
|
|
|
case PROP_NAME:
|
2006-08-20 12:38:20 -07:00
|
|
|
g_value_set_string (value, ctx->priv->name);
|
2006-03-07 17:21:06 -08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case PROP_ARGV:
|
2006-08-20 12:38:20 -07:00
|
|
|
g_value_set_pointer (value, ctx->priv->argv);
|
2006-03-07 17:21:06 -08:00
|
|
|
break;
|
|
|
|
|
2006-02-26 01:30:57 -08:00
|
|
|
default:
|
|
|
|
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-12-19 18:02:54 -08:00
|
|
|
static void
|
|
|
|
default_print_func (const char *string,
|
2006-02-24 20:39:12 -08:00
|
|
|
G_GNUC_UNUSED MSContext *ctx)
|
2005-12-19 18:02:54 -08:00
|
|
|
{
|
|
|
|
g_print ("%s", string);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-03-07 17:21:06 -08:00
|
|
|
static GObject *
|
|
|
|
ms_context_constructor (GType type,
|
|
|
|
guint n_props,
|
|
|
|
GObjectConstructParam *props)
|
|
|
|
{
|
|
|
|
GObject *obj;
|
|
|
|
MSContext *ctx;
|
|
|
|
|
|
|
|
obj = G_OBJECT_CLASS(ms_context_parent_class)->constructor (type, n_props, props);
|
|
|
|
ctx = MS_CONTEXT (obj);
|
|
|
|
|
2006-08-20 12:38:20 -07:00
|
|
|
if (!ctx->priv->name)
|
2006-03-07 17:21:06 -08:00
|
|
|
{
|
2006-08-20 12:38:20 -07:00
|
|
|
if (ctx->priv->argv && ctx->priv->argc)
|
|
|
|
ctx->priv->name = g_strdup (ctx->priv->argv[0]);
|
2006-03-07 17:21:06 -08:00
|
|
|
else
|
2006-08-20 12:38:20 -07:00
|
|
|
ctx->priv->name = g_strdup ("script");
|
2006-03-07 17:21:06 -08:00
|
|
|
}
|
|
|
|
|
2006-08-20 12:38:20 -07:00
|
|
|
if (!ctx->priv->argv || !ctx->priv->argc)
|
2006-03-07 17:21:06 -08:00
|
|
|
{
|
2006-08-20 12:38:20 -07:00
|
|
|
g_strfreev (ctx->priv->argv);
|
|
|
|
ctx->priv->argv = g_new0 (char*, 2);
|
|
|
|
ctx->priv->argv[0] = g_strdup (ctx->priv->name);
|
|
|
|
ctx->priv->argc = 1;
|
2006-03-07 17:21:06 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-12-19 18:02:54 -08:00
|
|
|
static void
|
2006-02-24 20:39:12 -08:00
|
|
|
ms_context_init (MSContext *ctx)
|
2005-12-19 18:02:54 -08:00
|
|
|
{
|
2006-08-20 12:38:20 -07:00
|
|
|
ctx->priv = G_TYPE_INSTANCE_GET_PRIVATE (ctx, MS_TYPE_CONTEXT, MSContextPrivate);
|
|
|
|
|
|
|
|
ctx->priv->vars = g_hash_table_new_full (g_str_hash, g_str_equal, g_free,
|
|
|
|
(GDestroyNotify) ms_variable_unref);
|
2005-12-19 18:02:54 -08:00
|
|
|
|
2006-08-20 12:38:20 -07:00
|
|
|
ctx->priv->print_func = default_print_func;
|
2006-02-26 22:38:47 -08:00
|
|
|
|
|
|
|
_ms_context_add_builtin (ctx);
|
2005-12-19 18:02:54 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
2006-02-24 20:39:12 -08:00
|
|
|
ms_context_finalize (GObject *object)
|
2005-12-19 18:02:54 -08:00
|
|
|
{
|
2006-02-24 20:39:12 -08:00
|
|
|
MSContext *ctx = MS_CONTEXT (object);
|
2005-12-19 18:02:54 -08:00
|
|
|
|
2006-08-20 12:38:20 -07:00
|
|
|
g_hash_table_destroy (ctx->priv->vars);
|
|
|
|
g_free (ctx->priv->error_msg);
|
2005-12-19 18:02:54 -08:00
|
|
|
|
2006-08-20 12:38:20 -07:00
|
|
|
ms_value_unref (ctx->priv->return_val);
|
2006-03-07 14:41:03 -08:00
|
|
|
|
2006-08-20 12:38:20 -07:00
|
|
|
g_free (ctx->priv->name);
|
|
|
|
g_strfreev (ctx->priv->argv);
|
2006-03-07 17:21:06 -08:00
|
|
|
|
2006-02-24 20:39:12 -08:00
|
|
|
G_OBJECT_CLASS(ms_context_parent_class)->finalize (object);
|
2005-12-19 18:02:54 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
2006-02-24 20:39:12 -08:00
|
|
|
ms_context_class_init (MSContextClass *klass)
|
2005-12-19 18:02:54 -08:00
|
|
|
{
|
2006-02-26 01:30:57 -08:00
|
|
|
GObjectClass *object_class = G_OBJECT_CLASS (klass);
|
|
|
|
|
|
|
|
object_class->finalize = ms_context_finalize;
|
|
|
|
object_class->set_property = ms_context_set_property;
|
|
|
|
object_class->get_property = ms_context_get_property;
|
2006-03-07 17:21:06 -08:00
|
|
|
object_class->constructor = ms_context_constructor;
|
2006-02-26 01:30:57 -08:00
|
|
|
|
2006-08-20 12:38:20 -07:00
|
|
|
g_type_class_add_private (klass, sizeof (MSContextPrivate));
|
|
|
|
|
2006-03-07 23:10:49 -08:00
|
|
|
ms_type_init ();
|
|
|
|
|
2006-02-26 01:30:57 -08:00
|
|
|
g_object_class_install_property (object_class,
|
|
|
|
PROP_WINDOW,
|
|
|
|
g_param_spec_object ("window",
|
|
|
|
"window",
|
|
|
|
"window",
|
|
|
|
GTK_TYPE_WINDOW,
|
|
|
|
G_PARAM_READWRITE));
|
2006-05-24 00:02:54 -07:00
|
|
|
|
|
|
|
signals[GET_ENV_VAR] =
|
|
|
|
g_signal_new ("get-env-var",
|
|
|
|
G_TYPE_FROM_CLASS (klass),
|
|
|
|
G_SIGNAL_RUN_LAST,
|
|
|
|
G_STRUCT_OFFSET (MSContextClass, get_env_var),
|
|
|
|
NULL, NULL,
|
|
|
|
_moo_marshal_BOXED__STRING,
|
|
|
|
MS_TYPE_VALUE, 1,
|
|
|
|
G_TYPE_STRING | G_SIGNAL_TYPE_STATIC_SCOPE);
|
2005-12-19 18:02:54 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-12-31 02:53:45 -08:00
|
|
|
static MSVariable *
|
|
|
|
ms_context_lookup_var (MSContext *ctx,
|
|
|
|
const char *name)
|
2005-12-19 18:02:54 -08:00
|
|
|
{
|
2006-12-31 02:53:45 -08:00
|
|
|
g_return_val_if_fail (MS_IS_CONTEXT (ctx), NULL);
|
|
|
|
g_return_val_if_fail (name != NULL, NULL);
|
|
|
|
return g_hash_table_lookup (ctx->priv->vars, name);
|
2005-12-19 18:02:54 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-02-24 20:39:12 -08:00
|
|
|
MSValue *
|
2006-02-25 12:55:58 -08:00
|
|
|
ms_context_eval_variable (MSContext *ctx,
|
|
|
|
const char *name)
|
2005-12-19 18:02:54 -08:00
|
|
|
{
|
2006-02-24 20:39:12 -08:00
|
|
|
MSVariable *var;
|
2005-12-21 05:08:40 -08:00
|
|
|
|
2006-02-24 20:39:12 -08:00
|
|
|
g_return_val_if_fail (MS_IS_CONTEXT (ctx), NULL);
|
2005-12-19 18:02:54 -08:00
|
|
|
g_return_val_if_fail (name != NULL, NULL);
|
2005-12-21 05:08:40 -08:00
|
|
|
|
2006-02-24 20:39:12 -08:00
|
|
|
var = ms_context_lookup_var (ctx, name);
|
2005-12-21 05:08:40 -08:00
|
|
|
|
|
|
|
if (!var)
|
2006-03-07 14:41:03 -08:00
|
|
|
return ms_context_format_error (ctx, MS_ERROR_NAME,
|
|
|
|
"no variable named '%s'",
|
|
|
|
name);
|
2005-12-21 05:08:40 -08:00
|
|
|
|
|
|
|
if (var->value)
|
2006-02-24 20:39:12 -08:00
|
|
|
return ms_value_ref (var->value);
|
2005-12-21 05:08:40 -08:00
|
|
|
|
2006-08-20 12:38:20 -07:00
|
|
|
return _ms_func_call (var->func, NULL, 0, ctx);
|
2005-12-19 18:02:54 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
gboolean
|
2006-02-25 12:55:58 -08:00
|
|
|
ms_context_assign_variable (MSContext *ctx,
|
|
|
|
const char *name,
|
|
|
|
MSValue *value)
|
2005-12-19 18:02:54 -08:00
|
|
|
{
|
2006-02-24 20:39:12 -08:00
|
|
|
MSVariable *var;
|
2005-12-21 05:08:40 -08:00
|
|
|
|
2006-02-24 20:39:12 -08:00
|
|
|
g_return_val_if_fail (MS_IS_CONTEXT (ctx), FALSE);
|
2005-12-21 05:08:40 -08:00
|
|
|
g_return_val_if_fail (name != NULL, FALSE);
|
|
|
|
|
2006-02-24 20:39:12 -08:00
|
|
|
var = ms_context_lookup_var (ctx, name);
|
2005-12-21 05:08:40 -08:00
|
|
|
|
|
|
|
if (value)
|
|
|
|
{
|
2005-12-21 05:09:20 -08:00
|
|
|
if (var)
|
2005-12-21 05:08:40 -08:00
|
|
|
{
|
2005-12-21 05:09:20 -08:00
|
|
|
if (var->func)
|
|
|
|
{
|
|
|
|
g_object_unref (var->func);
|
|
|
|
var->func = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (var->value != value)
|
|
|
|
{
|
2006-02-24 20:39:12 -08:00
|
|
|
ms_value_unref (var->value);
|
|
|
|
var->value = ms_value_ref (value);
|
2005-12-21 05:09:20 -08:00
|
|
|
}
|
2005-12-21 05:08:40 -08:00
|
|
|
}
|
2005-12-21 05:09:20 -08:00
|
|
|
else
|
2005-12-21 05:08:40 -08:00
|
|
|
{
|
2006-02-24 20:39:12 -08:00
|
|
|
var = ms_variable_new_value (value);
|
|
|
|
ms_context_set_var (ctx, name, var);
|
|
|
|
ms_variable_unref (var);
|
2005-12-21 05:08:40 -08:00
|
|
|
}
|
|
|
|
}
|
2005-12-21 05:09:20 -08:00
|
|
|
else if (var)
|
2005-12-21 05:08:40 -08:00
|
|
|
{
|
2006-02-24 20:39:12 -08:00
|
|
|
ms_context_set_var (ctx, name, NULL);
|
2005-12-21 05:08:40 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-04-19 11:06:05 -07:00
|
|
|
gboolean
|
|
|
|
ms_context_assign_string (MSContext *ctx,
|
|
|
|
const char *name,
|
|
|
|
const char *str_value)
|
|
|
|
{
|
|
|
|
MSValue *value = NULL;
|
|
|
|
gboolean retval;
|
|
|
|
|
|
|
|
g_return_val_if_fail (MS_IS_CONTEXT (ctx), FALSE);
|
|
|
|
g_return_val_if_fail (name != NULL, FALSE);
|
|
|
|
|
|
|
|
if (str_value)
|
|
|
|
value = ms_value_string (str_value);
|
|
|
|
|
|
|
|
retval = ms_context_assign_variable (ctx, name, value);
|
|
|
|
|
|
|
|
ms_value_unref (value);
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-02-25 12:55:58 -08:00
|
|
|
gboolean
|
|
|
|
ms_context_assign_positional (MSContext *ctx,
|
|
|
|
guint n,
|
|
|
|
MSValue *value)
|
|
|
|
{
|
|
|
|
char *name;
|
|
|
|
gboolean result;
|
|
|
|
|
|
|
|
g_return_val_if_fail (MS_IS_CONTEXT (ctx), FALSE);
|
|
|
|
|
2006-12-02 23:49:14 -08:00
|
|
|
name = g_strdup_printf ("_%u", n);
|
2006-02-25 12:55:58 -08:00
|
|
|
result = ms_context_assign_variable (ctx, name, value);
|
|
|
|
|
|
|
|
g_free (name);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-12-21 05:08:40 -08:00
|
|
|
gboolean
|
2006-02-24 20:39:12 -08:00
|
|
|
ms_context_set_var (MSContext *ctx,
|
2005-12-21 05:08:40 -08:00
|
|
|
const char *name,
|
2006-02-24 20:39:12 -08:00
|
|
|
MSVariable *var)
|
2005-12-21 05:08:40 -08:00
|
|
|
{
|
2006-02-24 20:39:12 -08:00
|
|
|
MSVariable *old;
|
2005-12-19 18:02:54 -08:00
|
|
|
|
2006-02-24 20:39:12 -08:00
|
|
|
g_return_val_if_fail (MS_IS_CONTEXT (ctx), FALSE);
|
2005-12-19 18:02:54 -08:00
|
|
|
g_return_val_if_fail (name != NULL, FALSE);
|
|
|
|
|
2006-08-20 12:38:20 -07:00
|
|
|
old = g_hash_table_lookup (ctx->priv->vars, name);
|
2005-12-19 18:02:54 -08:00
|
|
|
|
2005-12-21 05:08:40 -08:00
|
|
|
if (var != old)
|
2005-12-19 18:02:54 -08:00
|
|
|
{
|
2005-12-21 05:08:40 -08:00
|
|
|
if (var)
|
2006-08-20 12:38:20 -07:00
|
|
|
g_hash_table_insert (ctx->priv->vars,
|
|
|
|
g_strdup (name),
|
2006-02-24 20:39:12 -08:00
|
|
|
ms_variable_ref (var));
|
2005-12-19 18:02:54 -08:00
|
|
|
else
|
2006-08-20 12:38:20 -07:00
|
|
|
g_hash_table_remove (ctx->priv->vars, name);
|
2005-12-19 18:02:54 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
gboolean
|
2006-02-24 20:39:12 -08:00
|
|
|
ms_context_set_func (MSContext *ctx,
|
2005-12-19 18:02:54 -08:00
|
|
|
const char *name,
|
2006-02-24 20:39:12 -08:00
|
|
|
MSFunc *func)
|
2005-12-19 18:02:54 -08:00
|
|
|
{
|
2006-03-07 22:00:49 -08:00
|
|
|
MSValue *vfunc;
|
|
|
|
gboolean ret;
|
2005-12-19 18:02:54 -08:00
|
|
|
|
2006-02-24 20:39:12 -08:00
|
|
|
g_return_val_if_fail (MS_IS_CONTEXT (ctx), FALSE);
|
2005-12-19 18:02:54 -08:00
|
|
|
g_return_val_if_fail (name != NULL, FALSE);
|
2006-02-24 20:39:12 -08:00
|
|
|
g_return_val_if_fail (!func || MS_IS_FUNC (func), FALSE);
|
2005-12-19 18:02:54 -08:00
|
|
|
|
2006-03-07 22:00:49 -08:00
|
|
|
vfunc = ms_value_func (func);
|
|
|
|
ret = ms_context_assign_variable (ctx, name, vfunc);
|
|
|
|
ms_value_unref (vfunc);
|
2005-12-19 18:02:54 -08:00
|
|
|
|
2006-03-07 22:00:49 -08:00
|
|
|
return ret;
|
2005-12-19 18:02:54 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-02-24 20:39:12 -08:00
|
|
|
MSValue *
|
|
|
|
ms_context_set_error (MSContext *ctx,
|
|
|
|
MSError error,
|
2005-12-19 18:02:54 -08:00
|
|
|
const char *message)
|
|
|
|
{
|
2005-12-19 20:33:28 -08:00
|
|
|
const char *errname;
|
|
|
|
|
2006-02-24 20:39:12 -08:00
|
|
|
g_return_val_if_fail (MS_IS_CONTEXT (ctx), NULL);
|
2006-08-20 12:38:20 -07:00
|
|
|
g_return_val_if_fail (!ctx->priv->error && error, NULL);
|
|
|
|
g_return_val_if_fail (!ctx->priv->error_msg, NULL);
|
2005-12-19 20:33:28 -08:00
|
|
|
|
2006-08-20 12:38:20 -07:00
|
|
|
ctx->priv->error = error;
|
2006-02-24 20:39:12 -08:00
|
|
|
errname = ms_context_get_error_msg (ctx);
|
2005-12-19 20:33:28 -08:00
|
|
|
|
|
|
|
if (message && *message)
|
2006-08-20 12:38:20 -07:00
|
|
|
ctx->priv->error_msg = g_strdup_printf ("%s: %s", errname, message);
|
2005-12-19 20:33:28 -08:00
|
|
|
else
|
2006-08-20 12:38:20 -07:00
|
|
|
ctx->priv->error_msg = g_strdup (message);
|
2005-12-19 20:33:28 -08:00
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-02-24 20:39:12 -08:00
|
|
|
MSValue *
|
|
|
|
ms_context_format_error (MSContext *ctx,
|
|
|
|
MSError error,
|
2005-12-19 20:33:28 -08:00
|
|
|
const char *format,
|
|
|
|
...)
|
|
|
|
{
|
|
|
|
va_list args;
|
2006-03-14 14:27:40 -08:00
|
|
|
char *string;
|
2005-12-19 20:33:28 -08:00
|
|
|
|
2006-02-24 20:39:12 -08:00
|
|
|
g_return_val_if_fail (MS_IS_CONTEXT (ctx), NULL);
|
2006-08-20 12:38:20 -07:00
|
|
|
g_return_val_if_fail (!ctx->priv->error && error, NULL);
|
|
|
|
g_return_val_if_fail (!ctx->priv->error_msg, NULL);
|
2005-12-19 20:33:28 -08:00
|
|
|
|
|
|
|
if (!format || !format[0])
|
2006-03-14 12:59:07 -08:00
|
|
|
{
|
2006-02-24 20:39:12 -08:00
|
|
|
ms_context_set_error (ctx, error, NULL);
|
2006-03-14 12:59:07 -08:00
|
|
|
return NULL;
|
|
|
|
}
|
2005-12-19 20:33:28 -08:00
|
|
|
|
|
|
|
va_start (args, format);
|
2006-08-20 12:38:20 -07:00
|
|
|
string = _ms_vaprintf (format, args);
|
2005-12-19 20:33:28 -08:00
|
|
|
va_end (args);
|
|
|
|
|
2006-03-14 14:27:40 -08:00
|
|
|
ms_context_set_error (ctx, error, string);
|
|
|
|
g_free (string);
|
|
|
|
|
2005-12-19 18:02:54 -08:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
2006-02-24 20:39:12 -08:00
|
|
|
ms_context_clear_error (MSContext *ctx)
|
2005-12-19 18:02:54 -08:00
|
|
|
{
|
2006-02-24 20:39:12 -08:00
|
|
|
g_return_if_fail (MS_IS_CONTEXT (ctx));
|
2006-08-20 12:38:20 -07:00
|
|
|
ctx->priv->error = MS_ERROR_NONE;
|
|
|
|
g_free (ctx->priv->error_msg);
|
|
|
|
ctx->priv->error_msg = NULL;
|
2005-12-19 18:02:54 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const char *
|
2006-02-24 20:39:12 -08:00
|
|
|
ms_context_get_error_msg (MSContext *ctx)
|
2005-12-19 18:02:54 -08:00
|
|
|
{
|
2006-02-26 22:38:47 -08:00
|
|
|
static const char *msgs[MS_ERROR_LAST] = {
|
|
|
|
NULL, "Type error", "Value error", "Name error",
|
|
|
|
"Runtime error"
|
2005-12-19 18:02:54 -08:00
|
|
|
};
|
|
|
|
|
2006-02-24 20:39:12 -08:00
|
|
|
g_return_val_if_fail (MS_IS_CONTEXT (ctx), NULL);
|
2006-08-20 12:38:20 -07:00
|
|
|
g_return_val_if_fail (ctx->priv->error < MS_ERROR_LAST, NULL);
|
|
|
|
g_return_val_if_fail (ctx->priv->error != MS_ERROR_NONE, "ERROR");
|
2005-12-19 18:02:54 -08:00
|
|
|
|
2006-08-20 12:38:20 -07:00
|
|
|
if (ctx->priv->error_msg)
|
|
|
|
return ctx->priv->error_msg;
|
2005-12-19 18:02:54 -08:00
|
|
|
else
|
2006-08-20 12:38:20 -07:00
|
|
|
return msgs[ctx->priv->error];
|
2005-12-19 18:02:54 -08:00
|
|
|
}
|
2005-12-21 05:08:40 -08:00
|
|
|
|
|
|
|
|
2006-02-24 20:39:12 -08:00
|
|
|
static MSVariable *
|
|
|
|
ms_variable_new (void)
|
2005-12-21 05:08:40 -08:00
|
|
|
{
|
2006-05-19 22:09:09 -07:00
|
|
|
MSVariable *var = ms_variable_alloc ();
|
2005-12-21 05:08:40 -08:00
|
|
|
var->ref_count = 1;
|
|
|
|
return var;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-02-24 20:39:12 -08:00
|
|
|
MSVariable *
|
|
|
|
ms_variable_new_value (MSValue *value)
|
2005-12-21 05:08:40 -08:00
|
|
|
{
|
2006-02-24 20:39:12 -08:00
|
|
|
MSVariable *var;
|
2005-12-21 05:08:40 -08:00
|
|
|
|
|
|
|
g_return_val_if_fail (value != NULL, NULL);
|
|
|
|
|
2006-02-24 20:39:12 -08:00
|
|
|
var = ms_variable_new ();
|
|
|
|
var->value = ms_value_ref (value);
|
2005-12-21 05:08:40 -08:00
|
|
|
|
|
|
|
return var;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-12-31 02:53:45 -08:00
|
|
|
#if 0
|
2006-02-24 20:39:12 -08:00
|
|
|
MSVariable *
|
|
|
|
ms_variable_new_func (MSFunc *func)
|
2005-12-21 05:08:40 -08:00
|
|
|
{
|
2006-02-24 20:39:12 -08:00
|
|
|
MSVariable *var;
|
2005-12-21 05:08:40 -08:00
|
|
|
|
2006-02-24 20:39:12 -08:00
|
|
|
g_return_val_if_fail (MS_IS_FUNC (func), NULL);
|
2005-12-21 05:08:40 -08:00
|
|
|
|
2006-02-24 20:39:12 -08:00
|
|
|
var = ms_variable_new ();
|
2005-12-21 05:08:40 -08:00
|
|
|
var->func = g_object_ref (func);
|
|
|
|
|
|
|
|
return var;
|
|
|
|
}
|
2006-12-31 02:53:45 -08:00
|
|
|
#endif
|
2005-12-21 05:08:40 -08:00
|
|
|
|
|
|
|
|
2006-02-24 20:39:12 -08:00
|
|
|
MSVariable *
|
|
|
|
ms_variable_ref (MSVariable *var)
|
2005-12-21 05:08:40 -08:00
|
|
|
{
|
|
|
|
g_return_val_if_fail (var != NULL, NULL);
|
|
|
|
var->ref_count++;
|
|
|
|
return var;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
2006-02-24 20:39:12 -08:00
|
|
|
ms_variable_unref (MSVariable *var)
|
2005-12-21 05:08:40 -08:00
|
|
|
{
|
|
|
|
g_return_if_fail (var != NULL);
|
|
|
|
|
|
|
|
if (!--var->ref_count)
|
|
|
|
{
|
2006-03-07 23:10:49 -08:00
|
|
|
ms_value_unref (var->value);
|
2005-12-21 05:08:40 -08:00
|
|
|
if (var->func)
|
|
|
|
g_object_unref (var->func);
|
2006-05-19 22:09:09 -07:00
|
|
|
ms_variable_free (var);
|
2005-12-21 05:08:40 -08:00
|
|
|
}
|
|
|
|
}
|
2006-03-07 14:41:03 -08:00
|
|
|
|
|
|
|
|
|
|
|
void
|
2006-08-20 12:38:20 -07:00
|
|
|
_ms_context_set_return (MSContext *ctx,
|
|
|
|
MSValue *val)
|
2006-03-07 14:41:03 -08:00
|
|
|
{
|
2006-08-20 12:38:20 -07:00
|
|
|
g_assert (MS_IS_CONTEXT (ctx));
|
|
|
|
g_return_if_fail (!ctx->priv->return_set);
|
|
|
|
ctx->priv->return_set = TRUE;
|
|
|
|
ctx->priv->return_val = val ? ms_value_ref (val) : ms_value_none ();
|
|
|
|
}
|
|
|
|
|
|
|
|
MSValue *
|
|
|
|
_ms_context_get_return (MSContext *ctx)
|
|
|
|
{
|
|
|
|
g_assert (MS_IS_CONTEXT (ctx));
|
|
|
|
g_return_val_if_fail (ctx->priv->return_set, NULL);
|
|
|
|
return ms_value_ref (ctx->priv->return_val);
|
2006-03-07 14:41:03 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
2006-08-20 12:38:20 -07:00
|
|
|
_ms_context_set_break (MSContext *ctx)
|
2006-03-07 14:41:03 -08:00
|
|
|
{
|
2006-08-20 12:38:20 -07:00
|
|
|
g_assert (MS_IS_CONTEXT (ctx));
|
|
|
|
g_return_if_fail (!ctx->priv->break_set);
|
|
|
|
ctx->priv->break_set = TRUE;
|
2006-03-07 14:41:03 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
2006-08-20 12:38:20 -07:00
|
|
|
_ms_context_set_continue (MSContext *ctx)
|
2006-03-07 14:41:03 -08:00
|
|
|
{
|
2006-08-20 12:38:20 -07:00
|
|
|
g_assert (MS_IS_CONTEXT (ctx));
|
|
|
|
g_return_if_fail (!ctx->priv->continue_set);
|
|
|
|
ctx->priv->continue_set = TRUE;
|
2006-03-07 14:41:03 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
2006-08-20 12:38:20 -07:00
|
|
|
_ms_context_unset_return (MSContext *ctx)
|
2006-03-07 14:41:03 -08:00
|
|
|
{
|
2006-08-20 12:38:20 -07:00
|
|
|
g_assert (MS_IS_CONTEXT (ctx));
|
|
|
|
g_return_if_fail (ctx->priv->return_set);
|
|
|
|
ctx->priv->return_set = FALSE;
|
|
|
|
ms_value_unref (ctx->priv->return_val);
|
|
|
|
ctx->priv->return_val = NULL;
|
2006-03-07 14:41:03 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
2006-08-20 12:38:20 -07:00
|
|
|
_ms_context_unset_break (MSContext *ctx)
|
2006-03-07 14:41:03 -08:00
|
|
|
{
|
2006-08-20 12:38:20 -07:00
|
|
|
g_assert (MS_IS_CONTEXT (ctx));
|
|
|
|
g_return_if_fail (ctx->priv->break_set);
|
|
|
|
ctx->priv->break_set = FALSE;
|
2006-03-07 14:41:03 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
2006-08-20 12:38:20 -07:00
|
|
|
_ms_context_unset_continue (MSContext *ctx)
|
2006-03-07 14:41:03 -08:00
|
|
|
{
|
2006-08-20 12:38:20 -07:00
|
|
|
g_assert (MS_IS_CONTEXT (ctx));
|
|
|
|
g_return_if_fail (ctx->priv->continue_set);
|
|
|
|
ctx->priv->continue_set = FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
gboolean
|
|
|
|
_ms_context_return_set (MSContext *ctx)
|
|
|
|
{
|
|
|
|
g_assert (MS_IS_CONTEXT (ctx));
|
|
|
|
return ctx->priv->return_set;
|
|
|
|
}
|
|
|
|
|
|
|
|
gboolean
|
|
|
|
_ms_context_break_set (MSContext *ctx)
|
|
|
|
{
|
|
|
|
g_assert (MS_IS_CONTEXT (ctx));
|
|
|
|
return ctx->priv->break_set;
|
|
|
|
}
|
|
|
|
|
|
|
|
gboolean
|
|
|
|
_ms_context_continue_set (MSContext *ctx)
|
|
|
|
{
|
|
|
|
g_assert (MS_IS_CONTEXT (ctx));
|
|
|
|
return ctx->priv->continue_set;
|
|
|
|
}
|
|
|
|
|
|
|
|
gboolean
|
|
|
|
_ms_context_error_set (MSContext *ctx)
|
|
|
|
{
|
|
|
|
g_assert (MS_IS_CONTEXT (ctx));
|
|
|
|
return ctx->priv->error != 0;
|
2006-03-07 14:41:03 -08:00
|
|
|
}
|
2006-03-14 12:59:07 -08:00
|
|
|
|
|
|
|
|
2006-12-31 02:53:45 -08:00
|
|
|
#if 0
|
2006-05-14 16:45:13 -07:00
|
|
|
MSValue *
|
|
|
|
ms_context_run_script (MSContext *ctx,
|
|
|
|
const char *script)
|
|
|
|
{
|
|
|
|
MSNode *node;
|
|
|
|
MSValue *result;
|
|
|
|
|
|
|
|
g_return_val_if_fail (MS_IS_CONTEXT (ctx), NULL);
|
|
|
|
g_return_val_if_fail (script != NULL, NULL);
|
|
|
|
|
|
|
|
node = ms_script_parse (script);
|
|
|
|
g_return_val_if_fail (node != NULL, NULL);
|
|
|
|
|
|
|
|
result = ms_top_node_eval (node, ctx);
|
|
|
|
|
|
|
|
ms_node_unref (node);
|
|
|
|
return result;
|
|
|
|
}
|
2006-12-31 02:53:45 -08:00
|
|
|
#endif
|
2006-05-14 16:45:13 -07:00
|
|
|
|
|
|
|
|
2006-05-24 00:02:54 -07:00
|
|
|
MSValue *
|
|
|
|
ms_context_get_env_variable (MSContext *ctx,
|
|
|
|
const char *name)
|
|
|
|
{
|
|
|
|
MSValue *val = NULL;
|
|
|
|
|
|
|
|
g_return_val_if_fail (MS_IS_CONTEXT (ctx), NULL);
|
|
|
|
g_return_val_if_fail (name != NULL, NULL);
|
|
|
|
|
|
|
|
g_signal_emit (ctx, signals[GET_ENV_VAR], 0, name, &val);
|
|
|
|
|
|
|
|
return val;
|
|
|
|
}
|
2006-08-20 12:38:20 -07:00
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
_ms_context_print (MSContext *ctx,
|
|
|
|
const char *string)
|
|
|
|
{
|
|
|
|
g_assert (MS_IS_CONTEXT (ctx));
|
|
|
|
ctx->priv->print_func (string, ctx);
|
|
|
|
}
|