yaubil.c
author Myhailo Danylenko <isbear@ukrpost.net>
Fri, 17 Aug 2012 19:10:07 +0300
changeset 31 d7b9a5a84f96
parent 20 d6b68c5fb1c1
permissions -rw-r--r--
fix readme

/*
 * yaubil.c             -- Yet Another Useless Built-In Language
 *
 * Copyrigth (C) 2009      Myhailo Danylenko <isbear@ukrpost.net>
 *
 * 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 <glib.h>
#include <string.h>
#include <stdlib.h>

#include <mcabber/commands.h>
#include <mcabber/compl.h>
#include <mcabber/logprint.h>
#include <mcabber/settings.h>
#include <mcabber/modules.h>

#include "config.h"

void yaubil_init   (void);
void yaubil_uninit (void);

#define DESCRIPTION ( "Yet Another Useless Built-In Language\nProvides commands /multi, /if, /then, /else, /eval, /let" )

module_info_t info_yaubil = {
	.branch      = MCABBER_BRANCH,
	.api         = MCABBER_API_VERSION,
	.version     = PROJECT_VERSION,
	.description = DESCRIPTION,
	.requires    = NULL,
	.init        = yaubil_init,
	.uninit      = yaubil_uninit,
	.next        = NULL,
};

#ifdef MCABBER_API_HAVE_CMD_ID
static gpointer yaubil_multi_cmid = NULL;
static gpointer yaubil_if_cmid    = NULL;
static gpointer yaubil_then_cmid  = NULL;
static gpointer yaubil_else_cmid  = NULL;
static gpointer yaubil_eval_cmid  = NULL;
static gpointer yaubil_let_cmid   = NULL;
static gboolean yaubil_multi_set_safe = FALSE;
static gboolean yaubil_if_set_safe    = FALSE;
static gboolean yaubil_then_set_safe  = FALSE;
static gboolean yaubil_else_set_safe  = FALSE;
static gboolean yaubil_eval_set_safe  = FALSE;
static gboolean yaubil_let_set_safe   = FALSE;
#endif

static gboolean ifresult = TRUE;

#define MSGPREFIX "yaubil: "

#define TYPE_UNDEF ( 0 )
#define TYPE_STR   ( 1 )
#define TYPE_INT   ( 2 )

#define STATE_LVALUE ( 1 )
#define STATE_OP     ( 2 )
#define STATE_RVALUE ( 3 )

typedef struct {
	int   type;
	int   int_value;
	char *str_value;
} value_t;

typedef struct {
	char op;
	gboolean (*handler) (value_t *l, value_t *r);
} op_t;

static int check_value_type (const char *value)
{
	if (value) {
		const char *e;
		gboolean    integer = (*value == '-') ? TRUE : (g_ascii_isdigit (*value) ? TRUE : FALSE);

		if (integer) {
			for (e = value + 1; *e; ++e) {
				if (g_ascii_isdigit (*e))
					integer = TRUE;
				else {
					integer = FALSE;
					break;
				}
			}
		}

		if (integer)
			return TYPE_INT;
		else
			return TYPE_STR;
	} else
		return TYPE_UNDEF;
}

static gboolean op_concat (value_t *l, value_t *r)
{
	GString *res = g_string_new (NULL);

	if (l->type == TYPE_INT)
		g_string_append_printf (res, "%d", l->int_value);
	else if (l->type == TYPE_STR)
		g_string_append (res, l->str_value);

	if (r->type == TYPE_INT)
		g_string_append_printf (res, "%d", r->int_value);
	else if (r->type == TYPE_STR)
		g_string_append (res, r->str_value);
	
	l->type = TYPE_STR;
	g_free (l->str_value);
	l->str_value = g_string_free (res, FALSE);

	return TRUE;
}

static gboolean op_head (value_t *l, value_t *r)
{
	if (l->type == TYPE_UNDEF) {
		l->type      = TYPE_INT;
		l->int_value = 0;
	}
	if (r->type == TYPE_UNDEF) {
		r->type      = TYPE_INT;
		r->int_value = 0;
	}

	if (l -> type == TYPE_INT) {
		l -> type = TYPE_STR;
		if (!l -> str_value)
			l -> str_value = g_strdup_printf ("%d", l -> int_value);
	}
	if (r->type == TYPE_STR && check_value_type (r->str_value) == TYPE_INT) {
		r->type      = TYPE_INT;
		r->int_value = atoi (r->str_value);
		g_free (r->str_value);
		r->str_value = NULL;
	}

	if (l->type == TYPE_STR && r->type == TYPE_INT) {
		gchar *res = NULL;
		int    len = l -> str_value ? strlen (l -> str_value) : 0; // XXX can str_value be NULL?
		int    off = r -> int_value;
		if (off < 0 && len + off > 0)
			res = g_strndup (l -> str_value, len + off);
		else if (off > 0 && len > off)
			res = g_strndup (l -> str_value, off);
		else
			res = g_strdup (""); // XXX leave NULL here?
		g_free (l->str_value);
		l->str_value = res;
	} else
		return FALSE;
	
	return TRUE;
}

static gboolean op_tail (value_t *l, value_t *r)
{
	if (l->type == TYPE_UNDEF) {
		l->type      = TYPE_INT;
		l->int_value = 0;
	}
	if (r->type == TYPE_UNDEF) {
		r->type      = TYPE_INT;
		r->int_value = 0;
	}

	if (l -> type == TYPE_INT) {
		l -> type = TYPE_STR;
		if (!l -> str_value)
			l -> str_value = g_strdup_printf ("%d", l -> int_value);
	}
	if (r->type == TYPE_STR && check_value_type (r->str_value) == TYPE_INT) {
		r->type      = TYPE_INT;
		r->int_value = atoi (r->str_value);
		g_free (r->str_value);
		r->str_value = NULL;
	}

	if (l->type == TYPE_STR && r->type == TYPE_INT) {
		gchar *res = NULL;
		int    len = l -> str_value ? strlen (l -> str_value) : 0; // XXX can str_value be NULL?
		int    off = r -> int_value;
		if (off < 0 && len + off > 0)
			res = g_strdup (l -> str_value + (-off));
		else if (off > 0 && len > off)
			res = g_strdup (l -> str_value + (len - off));
		else
			res = g_strdup (""); // XXX leave NULL here?
		g_free (l->str_value);
		l->str_value = res;
	} else
		return FALSE;
	
	return TRUE;
}

static gboolean op_plus (value_t *l, value_t *r)
{
	if (l->type == TYPE_UNDEF) {
		l->type      = TYPE_INT;
		l->int_value = 0;
	}
	if (r->type == TYPE_UNDEF) {
		r->type      = TYPE_INT;
		r->int_value = 0;
	}
	
	if (l->type == TYPE_STR && check_value_type (l->str_value) == TYPE_INT) {
		l->type      = TYPE_INT;
		l->int_value = atoi (l->str_value);
		// not free value here
	}
	if (r->type == TYPE_STR && check_value_type (r->str_value) == TYPE_INT) {
		r->type      = TYPE_INT;
		r->int_value = atoi (r->str_value);
		// not free value here
	}

	if (l->type == TYPE_INT && r->type == TYPE_INT) {
		// integer
		l->int_value += r->int_value;
		g_free (l->str_value);
		l->str_value = NULL;
		g_free (r->str_value);
		r->str_value = NULL;
	} else {
		// convert both to strings
		if (l->type == TYPE_INT) {
			l->type = TYPE_STR;
			if (!l->str_value)
				l->str_value = g_strdup_printf ("%d", l->int_value);
		}
		if (r->type == TYPE_INT) {
			r->type = TYPE_STR;
			if (!r->str_value)
				r->str_value = g_strdup_printf ("%d", r->int_value);
		}

		{
			char *tmp = l->str_value;
			l->str_value = g_strdup_printf ("%s%s", tmp, r->str_value);
			g_free (tmp);
		}
	}

	return TRUE;
}

static gboolean op_minus (value_t *l, value_t *r)
{
	if (l->type == TYPE_UNDEF) {
		l->type      = TYPE_INT;
		l->int_value = 0;
	}
	if (r->type == TYPE_UNDEF) {
		r->type      = TYPE_INT;
		r->int_value = 0;
	}
	
	if (l->type == TYPE_STR && check_value_type (l->str_value) == TYPE_INT) {
		l->type      = TYPE_INT;
		l->int_value = atoi (l->str_value);
		g_free (l->str_value);
		l->str_value = NULL;
	}
	if (r->type == TYPE_STR && check_value_type (r->str_value) == TYPE_INT) {
		r->type      = TYPE_INT;
		r->int_value = atoi (r->str_value);
		g_free (r->str_value);
		r->str_value = NULL;
	}

	if (l->type == TYPE_INT && r->type == TYPE_INT)
		l->int_value -= r->int_value;
	else
		return FALSE;

	return TRUE;
}

static gboolean op_multiply (value_t *l, value_t *r)
{
	if (l->type == TYPE_UNDEF) {
		l->type      = TYPE_INT;
		l->int_value = 0;
	}
	if (r->type == TYPE_UNDEF) {
		r->type      = TYPE_INT;
		r->int_value = 0;
	}
	
	if (l->type == TYPE_STR && check_value_type (l->str_value) == TYPE_INT) {
		l->type      = TYPE_INT;
		l->int_value = atoi (l->str_value);
		// not free value here
	}
	if (r->type == TYPE_STR && check_value_type (r->str_value) == TYPE_INT) {
		r->type      = TYPE_INT;
		r->int_value = atoi (r->str_value);
		g_free (r->str_value);
		r->str_value = NULL;
	}

	if (l->type == TYPE_INT && r->type == TYPE_INT) {
		l->int_value *= r->int_value;
		g_free (l->str_value);
		l->str_value = NULL;
	} else if (r->type == TYPE_INT) {
		GString *res = g_string_new (NULL);
		int      i;
		for (i = r->int_value; i; --i)
			g_string_append (res, l->str_value);
		g_free (l->str_value);
		l->str_value = g_string_free (res, FALSE);
	} else
		return FALSE;

	return TRUE;
}

static gboolean op_divide (value_t *l, value_t *r)
{
	if (l->type == TYPE_UNDEF) {
		l->type      = TYPE_INT;
		l->int_value = 0;
	}
	if (r->type == TYPE_UNDEF) {
		r->type      = TYPE_INT;
		r->int_value = 0;
	}
	
	if (l->type == TYPE_STR && check_value_type (l->str_value) == TYPE_INT) {
		l->type      = TYPE_INT;
		l->int_value = atoi (l->str_value);
		g_free (l->str_value);
		l->str_value = NULL;
	}
	if (r->type == TYPE_STR && check_value_type (r->str_value) == TYPE_INT) {
		r->type      = TYPE_INT;
		r->int_value = atoi (r->str_value);
		g_free (r->str_value);
		r->str_value = NULL;
	}

	if (l->type == TYPE_INT && r->type == TYPE_INT) {
		if (r->int_value == 0) {
			scr_log_print (LPRINT_NORMAL, MSGPREFIX "/: Error: division by zero.");
			return FALSE;
		}
		l->int_value /= r->int_value;
	} else
		return FALSE;

	return TRUE;
}

static gboolean op_remain (value_t *l, value_t *r)
{
	if (l->type == TYPE_UNDEF) {
		l->type      = TYPE_INT;
		l->int_value = 0;
	}
	if (r->type == TYPE_UNDEF) {
		r->type      = TYPE_INT;
		r->int_value = 0;
	}
	
	if (l->type == TYPE_STR && check_value_type (l->str_value) == TYPE_INT) {
		l->type      = TYPE_INT;
		l->int_value = atoi (l->str_value);
		g_free (l->str_value);
		l->str_value = NULL;
	}
	if (r->type == TYPE_STR && check_value_type (r->str_value) == TYPE_INT) {
		r->type      = TYPE_INT;
		r->int_value = atoi (r->str_value);
		g_free (r->str_value);
		r->str_value = NULL;
	}

	if (l->type == TYPE_INT && r->type == TYPE_INT) {
		if (r->int_value == 0) {
			scr_log_print (LPRINT_NORMAL, MSGPREFIX "%%: Error: division by zero.");
			return FALSE;
		}
		l->int_value %= r->int_value;
	} else
		return FALSE;

	return TRUE;
}

static gboolean op_equal (value_t *l, value_t *r)
{
	if (l->type == TYPE_UNDEF) {
		l->type      = TYPE_INT;
		l->int_value = 0;
	}
	if (r->type == TYPE_UNDEF) {
		r->type      = TYPE_INT;
		r->int_value = 0;
	}
	
	if (l->type == TYPE_STR && check_value_type (l->str_value) == TYPE_INT) {
		l->type      = TYPE_INT;
		l->int_value = atoi (l->str_value);
		// not free value here
	}
	if (r->type == TYPE_STR && check_value_type (r->str_value) == TYPE_INT) {
		r->type      = TYPE_INT;
		r->int_value = atoi (r->str_value);
		// not free value here
	}

	if (l->type == TYPE_INT && r->type == TYPE_INT) {
		// integer
		l->int_value = (l->int_value == r->int_value) ? 1 : 0;
		g_free (l->str_value);
		l->str_value = NULL;
		g_free (r->str_value);
		r->str_value = NULL;
	} else {
		// convert both to strings
		if (l->type == TYPE_INT) {
			l->type = TYPE_STR;
			if (!l->str_value)
				l->str_value = g_strdup_printf ("%d", l->int_value);
		}
		if (r->type == TYPE_INT) {
			r->type = TYPE_STR;
			if (!r->str_value)
				r->str_value = g_strdup_printf ("%d", r->int_value);
		}

		{
			char *tmp = l->str_value;
			l->type      = TYPE_INT;
			l->int_value = (g_strcmp0 (l->str_value, r->str_value) == 0) ? 1 : 0;
			g_free (tmp);
		}
	}

	return TRUE;
}

static gboolean op_lt (value_t *l, value_t *r)
{
	if (l->type == TYPE_UNDEF) {
		l->type      = TYPE_INT;
		l->int_value = 0;
	}
	if (r->type == TYPE_UNDEF) {
		r->type      = TYPE_INT;
		r->int_value = 0;
	}
	
	if (l->type == TYPE_STR && check_value_type (l->str_value) == TYPE_INT) {
		l->type      = TYPE_INT;
		l->int_value = atoi (l->str_value);
		// not free value here
	}
	if (r->type == TYPE_STR && check_value_type (r->str_value) == TYPE_INT) {
		r->type      = TYPE_INT;
		r->int_value = atoi (r->str_value);
		// not free value here
	}

	if (l->type == TYPE_INT && r->type == TYPE_INT) {
		// integer
		l->int_value = (l->int_value < r->int_value) ? 1 : 0;
		g_free (l->str_value);
		l->str_value = NULL;
		g_free (r->str_value);
		r->str_value = NULL;
	} else {
		// convert both to strings
		if (l->type == TYPE_INT) {
			l->type = TYPE_STR;
			if (!l->str_value)
				l->str_value = g_strdup_printf ("%d", l->int_value);
		}
		if (r->type == TYPE_INT) {
			r->type = TYPE_STR;
			if (!r->str_value)
				r->str_value = g_strdup_printf ("%d", r->int_value);
		}

		{
			char *tmp = l->str_value;
			l->type      = TYPE_INT;
			l->int_value = (g_strcmp0 (l->str_value, r->str_value) < 0) ? 1 : 0;
			g_free (tmp);
		}
	}

	return TRUE;
}

static gboolean op_gt (value_t *l, value_t *r)
{
	if (l->type == TYPE_UNDEF) {
		l->type      = TYPE_INT;
		l->int_value = 0;
	}
	if (r->type == TYPE_UNDEF) {
		r->type      = TYPE_INT;
		r->int_value = 0;
	}
	
	if (l->type == TYPE_STR && check_value_type (l->str_value) == TYPE_INT) {
		l->type      = TYPE_INT;
		l->int_value = atoi (l->str_value);
		// not free value here
	}
	if (r->type == TYPE_STR && check_value_type (r->str_value) == TYPE_INT) {
		r->type      = TYPE_INT;
		r->int_value = atoi (r->str_value);
		// not free value here
	}

	if (l->type == TYPE_INT && r->type == TYPE_INT) {
		// integer
		l->int_value = (l->int_value > r->int_value) ? 1 : 0;
		g_free (l->str_value);
		l->str_value = NULL;
		g_free (r->str_value);
		r->str_value = NULL;
	} else {
		// convert both to strings
		if (l->type == TYPE_INT) {
			l->type = TYPE_STR;
			if (!l->str_value)
				l->str_value = g_strdup_printf ("%d", l->int_value);
		}
		if (r->type == TYPE_INT) {
			r->type = TYPE_STR;
			if (!r->str_value)
				r->str_value = g_strdup_printf ("%d", r->int_value);
		}

		{
			char *tmp = l->str_value;
			l->type      = TYPE_INT;
			l->int_value = (g_strcmp0 (l->str_value, r->str_value) > 0) ? 1 : 0;
			g_free (tmp);
		}
	}

	return TRUE;
}

static op_t operators[] = {
	{ '.', op_concat   },
	{ ':', op_head     },
	{ '^', op_tail     },
	{ '+', op_plus     },
	{ '-', op_minus    },
	{ '*', op_multiply },
	{ '/', op_divide   },
	{ '%', op_remain   },
	{ '=', op_equal    },
	{ '<', op_lt       },
	{ '>', op_gt       },
	{ 0,   NULL        },
};

static void destroy_value (value_t *value)
{
	if (value->str_value)
		g_free (value->str_value);
	g_slice_free (value_t, value);
}

static value_t *process_expression (const char *str, gsize *len)
{
	const char *strend = str + *len;
	const char *p;
	op_t       *op     = NULL;
	int         state  = STATE_LVALUE;
	value_t     val    = {
		.type      = TYPE_UNDEF,
		.int_value = 0,
		.str_value = NULL,
	};

	for (p = str; *p && p < strend && *p != ')'; ++p) {
		switch (state) {
		case STATE_LVALUE:
		case STATE_RVALUE:

			if (*p == ' ')
				break;

			{
				value_t rval = {
					.type      = TYPE_UNDEF,
					.int_value = 0,
					.str_value = NULL,
				};

				if (g_ascii_isdigit (*p) || *p == '-') { // integer // XXX: no unary operators for now...

					const char *e;

					rval.type = TYPE_INT;

					for (e = p + 1; g_ascii_isdigit (*e) && e < strend; ++e); // TODO: does atoi handle 0x etc? then also allow this?
					
					{
						char *v = g_strndup (p, e - p);
						rval.int_value = atoi (v);
						g_free (v);
					}

					p = e - 1;

				} else if (*p == '"') { // string

					const char *e;

					rval.type = TYPE_STR;

					{
						gboolean  finished = FALSE;
						gboolean  escape   = FALSE;
						GString  *v        = g_string_new (NULL);

						for (e = p + 1; *e && e < strend; ++e) {
							switch (*e) {
							case '\\':
								if (!escape)
									escape = TRUE;
								else
									escape = FALSE;
								break;
							case '"':
								if (!escape) {
									finished = TRUE;
									break;
								} else
									escape = FALSE;
								break;
							default:
								escape = FALSE;
								break;
							}

							if (finished)
								break;

							if (!escape)
								g_string_append_c (v, *e);
						};

						if (!finished) {
							scr_log_print (LPRINT_NORMAL, MSGPREFIX "Error: Unmatched quote.");
							g_string_free (v, TRUE);
							g_free (val.str_value);
							return NULL;
						}

						rval.str_value = g_string_free (v, FALSE);
					}

					p = e;

				} else if (g_ascii_isalpha (*p)) { // variable (MUST start from alpha)

					const char *e;

					for (e = p + 1; (g_ascii_isalnum (*e) || *e == '-' || *e == '_') && e < strend; ++e);

					char       *name = g_strndup (p, e - p);
					const char *value = settings_opt_get (name);
					g_free (name);

					rval.type = check_value_type (value);
					if (rval.type == TYPE_INT)
						rval.int_value = atoi (value);
					else if (rval.type == TYPE_STR)
						rval.str_value = g_strdup (value);

					p = e - 1;

				} else if (*p == '(') {
					
					gsize    len = strend - p - 1;
					value_t *n   = process_expression (p + 1, &len);
					
					if (!n) {
						scr_log_print (LPRINT_NORMAL, MSGPREFIX "Error: Error in subexpression.");
						g_free (val.str_value);
						return NULL;
					}

					rval.type      = n->type;
					rval.int_value = n->int_value;
					rval.str_value = g_strdup (n->str_value);
					destroy_value (n);

					p += len + 1;

				} else {
					scr_log_print (LPRINT_NORMAL, MSGPREFIX "Error: unrecognized symbols.");
					g_free (val.str_value);
					return NULL;
				}

				if (state == STATE_RVALUE) {
					if (op->handler) {
						if (!op->handler (&val, &rval)) {
							scr_log_print (LPRINT_NORMAL, MSGPREFIX "Error: operand argument types mismatch.");
							g_free (val.str_value);
							g_free (rval.str_value);
							return NULL;
						}
					}
					g_free (rval.str_value);
				} else {
					val.type      = rval.type;
					val.int_value = rval.int_value;
					val.str_value = rval.str_value;
				}
				
				state = STATE_OP;
			}

			break;

		case STATE_OP:

			if (*p == ' ')
				break;

			{
				op_t     *operator;
				gboolean  found    = FALSE;

				for (operator = operators; operator->op; ++operator) {
					if (operator->op == *p) {
						op    = operator;
						found = TRUE;
						break;
					}
				}

				if (!found) {
					scr_log_print (LPRINT_NORMAL, MSGPREFIX "Error: unknown operator.");
					g_free (val.str_value);
				}
			}

			state = STATE_RVALUE;

			break;
		default:
			break;
		}
	}

	{ // return value
		*len = p - str;
		value_t *rval = g_slice_new (value_t);

		rval->type      = val.type;
		rval->int_value = val.int_value;
		rval->str_value = val.str_value;

		return rval;
	}
}

static void do_eval (char *arg)
{
	gsize    len = strlen (arg);
	value_t *val = process_expression (arg, &len);

	if (!val) {
		scr_log_print (LPRINT_NORMAL, "eval: Evaluation error.");
		return;
	}

	if (val->type == TYPE_STR)
		process_command (val->str_value, TRUE);
	else
		scr_log_print (LPRINT_NORMAL, "eval: Expression does not result in string.");

	destroy_value (val);
}

static void do_let (char *arg)
{
	value_t  *value;
	int       namelen;
	char     *val     = strchr (arg, '=');

	if (!val) {
		scr_log_print (LPRINT_NORMAL, "let: Syntax error: no equal sign in line.");
		return;
	}
	
	{
		char *p = val;

		for (p = val - 1; p >= arg && *p == ' '; --p);
		if (p < arg) {
			scr_log_print (LPRINT_NORMAL, "let: Syntax error: no destination variable name specified.");
			return;
		}

		namelen = p + 1 - arg;
	}

	{ // evaluate expression
		gsize len = strlen (val + 1);

		value = process_expression (val + 1, &len);
	}

	if (!value) {
		scr_log_print (LPRINT_NORMAL, "let: Evaluation error.");
		return;
	}

	if (value->type == TYPE_INT) {
		value->type      = TYPE_STR;
		value->str_value = g_strdup_printf ("%d", value->int_value);
	}

	{ // assign value
		char *varname = g_strndup (arg, namelen);

		if (value->str_value)
			settings_set (SETTINGS_TYPE_OPTION, varname, value->str_value);
		else
			settings_del (SETTINGS_TYPE_OPTION, varname);

		g_free (varname);
	}

	destroy_value (value);
}

static void do_if (char *arg)
{
	gsize    len = strlen (arg);
	value_t *val = process_expression (arg, &len);

	if (!val) {
		scr_log_print (LPRINT_NORMAL, "if: Evaluation error.");
		return;
	}

	if (val->type == TYPE_UNDEF) {
		val->type = TYPE_INT;
		val->int_value = 0;
	}
	if (val->type == TYPE_STR && check_value_type (val->str_value) == TYPE_INT) {
		val->type = TYPE_INT;
		val->int_value = atoi (val->str_value);
		g_free (val->str_value);
		val->str_value = NULL;
	}

	if (val->type == TYPE_INT)
		ifresult = val->int_value ? TRUE : FALSE;
	else if (val->str_value)
		ifresult = TRUE;
	else
		ifresult = FALSE;

	destroy_value (val);
}

static void do_then (char *arg)
{
	if (ifresult)
		process_command (arg, TRUE);
}

static void do_else (char *arg)
{
	if (!ifresult)
		process_command (arg, TRUE);
}

static void do_multi (char *arg)
{
	char *end;
	char *start = arg;
	
	for (end = strchr (start, ';'); end; end = strchr (start, ';')) {

		// execute command
		char *command = g_strndup (start, end - start);
		process_command (command, TRUE);
		g_free (command);

		// skip leading spaces
		for (start = end + 1; *start == ' '; ++start);
	}

	if (*start)
		process_command (start, TRUE);
}

void yaubil_init (void)
{
#ifndef MCABBER_API_HAVE_CMD_ID
	cmd_add ("multi", "", COMPL_CMD, COMPL_CMD, do_multi, NULL);
	cmd_add ("if", "", 0, 0, do_if, NULL);
	cmd_add ("then", "", COMPL_CMD, COMPL_CMD, do_then, NULL);
	cmd_add ("else", "", COMPL_CMD, COMPL_CMD, do_else, NULL);
	cmd_add ("eval", "", 0, 0, do_eval, NULL);
	cmd_add ("let", "", 0, 0, do_let, NULL);
#else
	yaubil_multi_cmid = cmd_add ("multi", "", COMPL_CMD, COMPL_CMD, do_multi, NULL);
	yaubil_if_cmid    = cmd_add ("if", "", 0, 0, do_if, NULL);
	yaubil_then_cmid  = cmd_add ("then", "", COMPL_CMD, COMPL_CMD, do_then, NULL);
	yaubil_else_cmid  = cmd_add ("else", "", COMPL_CMD, COMPL_CMD, do_else, NULL);
	yaubil_eval_cmid  = cmd_add ("eval", "", 0, 0, do_eval, NULL);
	yaubil_let_cmid   = cmd_add ("let", "", 0, 0, do_let, NULL);
	yaubil_multi_set_safe = cmd_set_safe ("multi", TRUE);
	yaubil_if_set_safe    = cmd_set_safe ("if", TRUE);
	yaubil_then_set_safe  = cmd_set_safe ("then", TRUE);
	yaubil_else_set_safe  = cmd_set_safe ("else", TRUE);
	yaubil_eval_set_safe  = cmd_set_safe ("eval", TRUE);
	yaubil_let_set_safe   = cmd_set_safe ("let", TRUE);
#endif
}

void yaubil_uninit (void)
{
#ifndef MCABBER_API_HAVE_CMD_ID
	cmd_del ("multi");
	cmd_del ("if");
	cmd_del ("then");
	cmd_del ("else");
	cmd_del ("eval");
	cmd_del ("let");
#else
	if (yaubil_multi_cmid)
		cmd_del (yaubil_multi_cmid);
	if (yaubil_if_cmid)
		cmd_del (yaubil_if_cmid);
	if (yaubil_then_cmid)
		cmd_del (yaubil_then_cmid);
	if (yaubil_else_cmid)
		cmd_del (yaubil_else_cmid);
	if (yaubil_eval_cmid)
		cmd_del (yaubil_eval_cmid);
	if (yaubil_let_cmid)
		cmd_del (yaubil_let_cmid);
	if (yaubil_multi_set_safe)
		cmd_set_safe ("multi", FALSE);
	if (yaubil_if_set_safe)
		cmd_set_safe ("if", FALSE);
	if (yaubil_then_set_safe)
		cmd_set_safe ("then", FALSE);
	if (yaubil_else_set_safe)
		cmd_set_safe ("else", FALSE);
	if (yaubil_eval_set_safe)
		cmd_set_safe ("eval", FALSE);
	if (yaubil_let_set_safe)
		cmd_set_safe ("let", FALSE);
#endif
}

/* The End */