/* skk.c generated by valac 0.20.0.2-fd7ed, the Vala compiler
 * generated from skk.vala, do not modify */

/*
 * Copyright (C) 2011-2012 Daiki Ueno <ueno@unixuser.org>
 * Copyright (C) 2011-2012 Red Hat, Inc.
 *
 * 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 3 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, see <http://www.gnu.org/licenses/>.
 */

#include <glib.h>
#include <glib-object.h>
#include <stdlib.h>
#include <string.h>
#include <locale.h>
#include <glib/gi18n-lib.h>
#include <config.h>
#include <stdio.h>
#include <libskk/libskk.h>
#include <gee.h>

#define _g_option_context_free0(var) ((var == NULL) ? NULL : (var = (g_option_context_free (var), NULL)))
#define _g_error_free0(var) ((var == NULL) ? NULL : (var = (g_error_free (var), NULL)))
#define _g_object_unref0(var) ((var == NULL) ? NULL : (var = (g_object_unref (var), NULL)))
#define _g_free0(var) (var = (g_free (var), NULL))

#define TYPE_REPL (repl_get_type ())
#define REPL(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), TYPE_REPL, Repl))
#define REPL_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), TYPE_REPL, ReplClass))
#define IS_REPL(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), TYPE_REPL))
#define IS_REPL_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), TYPE_REPL))
#define REPL_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), TYPE_REPL, ReplClass))

typedef struct _Repl Repl;
typedef struct _ReplClass ReplClass;
typedef struct _ReplPrivate ReplPrivate;
#define _g_string_free0(var) ((var == NULL) ? NULL : (var = (g_string_free (var, TRUE), NULL)))
#define _g_regex_unref0(var) ((var == NULL) ? NULL : (var = (g_regex_unref (var), NULL)))

struct _Repl {
	GObject parent_instance;
	ReplPrivate * priv;
};

struct _ReplClass {
	GObjectClass parent_class;
};

struct _ReplPrivate {
	SkkContext* context;
};


extern gchar* opt_file_dict;
gchar* opt_file_dict = NULL;
extern gchar* opt_user_dict;
gchar* opt_user_dict = NULL;
extern gchar* opt_skkserv;
gchar* opt_skkserv = NULL;
extern gchar* opt_typing_rule;
gchar* opt_typing_rule = NULL;
extern gboolean opt_list_typing_rules;
gboolean opt_list_typing_rules = FALSE;
static gpointer repl_parent_class = NULL;

gint _vala_main (gchar** args, int args_length1);
static void _vala_SkkRuleMetadata_array_free (SkkRuleMetadata* array, gint array_length);
Repl* repl_new (SkkContext* context);
Repl* repl_construct (GType object_type, SkkContext* context);
GType repl_get_type (void) G_GNUC_CONST;
gboolean repl_run (Repl* self);
#define REPL_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), TYPE_REPL, ReplPrivate))
enum  {
	REPL_DUMMY_PROPERTY
};
static void repl_finalize (GObject* obj);
static void _vala_array_destroy (gpointer array, gint array_length, GDestroyNotify destroy_func);
static void _vala_array_free (gpointer array, gint array_length, GDestroyNotify destroy_func);

const GOptionEntry options[6] = {{"file-dict", 'f', 0, G_OPTION_ARG_STRING, &opt_file_dict, "Path to a file dictionary", NULL}, {"user-dict", 'u', 0, G_OPTION_ARG_STRING, &opt_user_dict, "Path to a user dictionary", NULL}, {"skkserv", 's', 0, G_OPTION_ARG_STRING, &opt_skkserv, "Host and port running skkserv (HOST:PORT)", NULL}, {"rule", 'r', 0, G_OPTION_ARG_STRING, &opt_typing_rule, "Typing rule (default: \"default\")", NULL}, {"list-rules", 'l', 0, G_OPTION_ARG_NONE, &opt_list_typing_rules, "List typing rules", NULL}, {NULL}};

static void _vala_SkkRuleMetadata_array_free (SkkRuleMetadata* array, gint array_length) {
	if (array != NULL) {
		int i;
		for (i = 0; i < array_length; i = i + 1) {
			skk_rule_metadata_destroy (&array[i]);
		}
	}
	g_free (array);
}


static gint string_last_index_of (const gchar* self, const gchar* needle, gint start_index) {
	gint result = 0;
	gint _tmp0_;
	const gchar* _tmp1_;
	gchar* _tmp2_ = NULL;
	gchar* _result_;
	gchar* _tmp3_;
	g_return_val_if_fail (self != NULL, 0);
	g_return_val_if_fail (needle != NULL, 0);
	_tmp0_ = start_index;
	_tmp1_ = needle;
	_tmp2_ = g_strrstr (((gchar*) self) + _tmp0_, (gchar*) _tmp1_);
	_result_ = _tmp2_;
	_tmp3_ = _result_;
	if (_tmp3_ != NULL) {
		gchar* _tmp4_;
		_tmp4_ = _result_;
		result = (gint) (_tmp4_ - ((gchar*) self));
		return result;
	} else {
		result = -1;
		return result;
	}
}


static gchar* string_slice (const gchar* self, glong start, glong end) {
	gchar* result = NULL;
	gint _tmp0_;
	gint _tmp1_;
	glong string_length;
	glong _tmp2_;
	glong _tmp5_;
	gboolean _tmp8_ = FALSE;
	glong _tmp9_;
	gboolean _tmp12_;
	gboolean _tmp13_ = FALSE;
	glong _tmp14_;
	gboolean _tmp17_;
	glong _tmp18_;
	glong _tmp19_;
	glong _tmp20_;
	glong _tmp21_;
	glong _tmp22_;
	gchar* _tmp23_ = NULL;
	g_return_val_if_fail (self != NULL, NULL);
	_tmp0_ = strlen (self);
	_tmp1_ = _tmp0_;
	string_length = (glong) _tmp1_;
	_tmp2_ = start;
	if (_tmp2_ < ((glong) 0)) {
		glong _tmp3_;
		glong _tmp4_;
		_tmp3_ = string_length;
		_tmp4_ = start;
		start = _tmp3_ + _tmp4_;
	}
	_tmp5_ = end;
	if (_tmp5_ < ((glong) 0)) {
		glong _tmp6_;
		glong _tmp7_;
		_tmp6_ = string_length;
		_tmp7_ = end;
		end = _tmp6_ + _tmp7_;
	}
	_tmp9_ = start;
	if (_tmp9_ >= ((glong) 0)) {
		glong _tmp10_;
		glong _tmp11_;
		_tmp10_ = start;
		_tmp11_ = string_length;
		_tmp8_ = _tmp10_ <= _tmp11_;
	} else {
		_tmp8_ = FALSE;
	}
	_tmp12_ = _tmp8_;
	g_return_val_if_fail (_tmp12_, NULL);
	_tmp14_ = end;
	if (_tmp14_ >= ((glong) 0)) {
		glong _tmp15_;
		glong _tmp16_;
		_tmp15_ = end;
		_tmp16_ = string_length;
		_tmp13_ = _tmp15_ <= _tmp16_;
	} else {
		_tmp13_ = FALSE;
	}
	_tmp17_ = _tmp13_;
	g_return_val_if_fail (_tmp17_, NULL);
	_tmp18_ = start;
	_tmp19_ = end;
	g_return_val_if_fail (_tmp18_ <= _tmp19_, NULL);
	_tmp20_ = start;
	_tmp21_ = end;
	_tmp22_ = start;
	_tmp23_ = g_strndup (((gchar*) self) + _tmp20_, (gsize) (_tmp21_ - _tmp22_));
	result = _tmp23_;
	return result;
}


gint _vala_main (gchar** args, int args_length1) {
	gint result = 0;
	const gchar* _tmp0_ = NULL;
	GOptionContext* _tmp1_;
	GOptionContext* option_context;
	GOptionContext* _tmp2_;
	gboolean _tmp7_;
	GeeArrayList* _tmp19_;
	GeeArrayList* dictionaries;
	const gchar* _tmp20_;
	const gchar* _tmp30_;
	const gchar* _tmp32_;
	gboolean _tmp33_ = FALSE;
	const gchar* _tmp52_;
	GeeArrayList* _tmp80_;
	gint _tmp81_ = 0;
	gpointer* _tmp82_ = NULL;
	SkkDict** _tmp83_;
	gint _tmp83__length1;
	SkkContext* _tmp84_;
	SkkContext* _tmp85_;
	SkkContext* context;
	const gchar* _tmp86_;
	SkkContext* _tmp96_;
	Repl* _tmp97_;
	Repl* repl;
	Repl* _tmp98_;
	gboolean _tmp99_ = FALSE;
	GError * _inner_error_ = NULL;
	setlocale (LC_ALL, "");
	bindtextdomain (GETTEXT_PACKAGE, LOCALEDIR);
	bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
	textdomain (GETTEXT_PACKAGE);
	_tmp0_ = _ ("- emulate SKK input method on the command line");
	_tmp1_ = g_option_context_new (_tmp0_);
	option_context = _tmp1_;
	_tmp2_ = option_context;
	g_option_context_add_main_entries (_tmp2_, options, "libskk");
	{
		GOptionContext* _tmp3_;
		_tmp3_ = option_context;
		g_option_context_parse (_tmp3_, &args_length1, &args, &_inner_error_);
		if (_inner_error_ != NULL) {
			if (_inner_error_->domain == G_OPTION_ERROR) {
				goto __catch0_g_option_error;
			}
			_g_option_context_free0 (option_context);
			g_critical ("file %s: line %d: unexpected error: %s (%s, %d)", __FILE__, __LINE__, _inner_error_->message, g_quark_to_string (_inner_error_->domain), _inner_error_->code);
			g_clear_error (&_inner_error_);
			return 0;
		}
	}
	goto __finally0;
	__catch0_g_option_error:
	{
		GError* e = NULL;
		FILE* _tmp4_;
		GError* _tmp5_;
		const gchar* _tmp6_;
		e = _inner_error_;
		_inner_error_ = NULL;
		_tmp4_ = stderr;
		_tmp5_ = e;
		_tmp6_ = _tmp5_->message;
		fprintf (_tmp4_, "%s\n", _tmp6_);
		result = 1;
		_g_error_free0 (e);
		_g_option_context_free0 (option_context);
		return result;
	}
	__finally0:
	if (_inner_error_ != NULL) {
		_g_option_context_free0 (option_context);
		g_critical ("file %s: line %d: uncaught error: %s (%s, %d)", __FILE__, __LINE__, _inner_error_->message, g_quark_to_string (_inner_error_->domain), _inner_error_->code);
		g_clear_error (&_inner_error_);
		return 0;
	}
	skk_init ();
	_tmp7_ = opt_list_typing_rules;
	if (_tmp7_) {
		gint _tmp8_ = 0;
		SkkRuleMetadata* _tmp9_ = NULL;
		SkkRuleMetadata* rules;
		gint rules_length1;
		gint _rules_size_;
		SkkRuleMetadata* _tmp10_;
		gint _tmp10__length1;
		_tmp9_ = skk_rule_list (&_tmp8_);
		rules = _tmp9_;
		rules_length1 = _tmp8_;
		_rules_size_ = rules_length1;
		_tmp10_ = rules;
		_tmp10__length1 = rules_length1;
		{
			SkkRuleMetadata* rule_collection = NULL;
			gint rule_collection_length1 = 0;
			gint _rule_collection_size_ = 0;
			gint rule_it = 0;
			rule_collection = _tmp10_;
			rule_collection_length1 = _tmp10__length1;
			for (rule_it = 0; rule_it < _tmp10__length1; rule_it = rule_it + 1) {
				SkkRuleMetadata _tmp11_ = {0};
				SkkRuleMetadata rule = {0};
				skk_rule_metadata_copy (&rule_collection[rule_it], &_tmp11_);
				rule = _tmp11_;
				{
					FILE* _tmp12_;
					SkkRuleMetadata _tmp13_;
					const gchar* _tmp14_;
					SkkRuleMetadata _tmp15_;
					const gchar* _tmp16_;
					SkkRuleMetadata _tmp17_;
					const gchar* _tmp18_;
					_tmp12_ = stdout;
					_tmp13_ = rule;
					_tmp14_ = _tmp13_.name;
					_tmp15_ = rule;
					_tmp16_ = _tmp15_.label;
					_tmp17_ = rule;
					_tmp18_ = _tmp17_.description;
					fprintf (_tmp12_, "%s - %s: %s\n", _tmp14_, _tmp16_, _tmp18_);
					skk_rule_metadata_destroy (&rule);
				}
			}
		}
		result = 0;
		rules = (_vala_SkkRuleMetadata_array_free (rules, rules_length1), NULL);
		_g_option_context_free0 (option_context);
		return result;
	}
	_tmp19_ = gee_array_list_new (SKK_TYPE_DICT, (GBoxedCopyFunc) g_object_ref, g_object_unref, NULL);
	dictionaries = _tmp19_;
	_tmp20_ = opt_user_dict;
	if (_tmp20_ != NULL) {
		{
			const gchar* _tmp21_;
			SkkUserDict* _tmp22_;
			SkkUserDict* _tmp23_;
			GeeArrayList* _tmp24_;
			SkkUserDict* _tmp25_;
			_tmp21_ = opt_user_dict;
			_tmp22_ = skk_user_dict_new (_tmp21_, "UTF-8", &_inner_error_);
			_tmp23_ = _tmp22_;
			if (_inner_error_ != NULL) {
				goto __catch1_g_error;
			}
			_tmp24_ = dictionaries;
			_tmp25_ = _tmp23_;
			gee_abstract_collection_add ((GeeAbstractCollection*) _tmp24_, (SkkDict*) _tmp25_);
			_g_object_unref0 (_tmp25_);
		}
		goto __finally1;
		__catch1_g_error:
		{
			GError* e = NULL;
			FILE* _tmp26_;
			const gchar* _tmp27_;
			GError* _tmp28_;
			const gchar* _tmp29_;
			e = _inner_error_;
			_inner_error_ = NULL;
			_tmp26_ = stderr;
			_tmp27_ = opt_user_dict;
			_tmp28_ = e;
			_tmp29_ = _tmp28_->message;
			fprintf (_tmp26_, "can't open user dict %s: %s", _tmp27_, _tmp29_);
			result = 1;
			_g_error_free0 (e);
			_g_object_unref0 (dictionaries);
			_g_option_context_free0 (option_context);
			return result;
		}
		__finally1:
		if (_inner_error_ != NULL) {
			_g_object_unref0 (dictionaries);
			_g_option_context_free0 (option_context);
			g_critical ("file %s: line %d: uncaught error: %s (%s, %d)", __FILE__, __LINE__, _inner_error_->message, g_quark_to_string (_inner_error_->domain), _inner_error_->code);
			g_clear_error (&_inner_error_);
			return 0;
		}
	}
	_tmp30_ = opt_file_dict;
	if (_tmp30_ == NULL) {
		gchar* _tmp31_ = NULL;
		_tmp31_ = g_build_filename (DATADIR, "skk", "SKK-JISYO.L", NULL);
		_g_free0 (opt_file_dict);
		opt_file_dict = _tmp31_;
	}
	_tmp32_ = opt_file_dict;
	_tmp33_ = g_str_has_suffix (_tmp32_, ".cdb");
	if (_tmp33_) {
		{
			const gchar* _tmp34_;
			SkkCdbDict* _tmp35_;
			SkkCdbDict* _tmp36_;
			GeeArrayList* _tmp37_;
			SkkCdbDict* _tmp38_;
			_tmp34_ = opt_file_dict;
			_tmp35_ = skk_cdb_dict_new (_tmp34_, "EUC-JP", &_inner_error_);
			_tmp36_ = _tmp35_;
			if (_inner_error_ != NULL) {
				goto __catch2_g_error;
			}
			_tmp37_ = dictionaries;
			_tmp38_ = _tmp36_;
			gee_abstract_collection_add ((GeeAbstractCollection*) _tmp37_, (SkkDict*) _tmp38_);
			_g_object_unref0 (_tmp38_);
		}
		goto __finally2;
		__catch2_g_error:
		{
			GError* e = NULL;
			FILE* _tmp39_;
			const gchar* _tmp40_;
			GError* _tmp41_;
			const gchar* _tmp42_;
			e = _inner_error_;
			_inner_error_ = NULL;
			_tmp39_ = stderr;
			_tmp40_ = opt_file_dict;
			_tmp41_ = e;
			_tmp42_ = _tmp41_->message;
			fprintf (_tmp39_, "can't open CDB dict %s: %s", _tmp40_, _tmp42_);
			result = 1;
			_g_error_free0 (e);
			_g_object_unref0 (dictionaries);
			_g_option_context_free0 (option_context);
			return result;
		}
		__finally2:
		if (_inner_error_ != NULL) {
			_g_object_unref0 (dictionaries);
			_g_option_context_free0 (option_context);
			g_critical ("file %s: line %d: uncaught error: %s (%s, %d)", __FILE__, __LINE__, _inner_error_->message, g_quark_to_string (_inner_error_->domain), _inner_error_->code);
			g_clear_error (&_inner_error_);
			return 0;
		}
	} else {
		{
			const gchar* _tmp43_;
			SkkFileDict* _tmp44_;
			SkkFileDict* _tmp45_;
			GeeArrayList* _tmp46_;
			SkkFileDict* _tmp47_;
			_tmp43_ = opt_file_dict;
			_tmp44_ = skk_file_dict_new (_tmp43_, "EUC-JP", &_inner_error_);
			_tmp45_ = _tmp44_;
			if (_inner_error_ != NULL) {
				goto __catch3_g_error;
			}
			_tmp46_ = dictionaries;
			_tmp47_ = _tmp45_;
			gee_abstract_collection_add ((GeeAbstractCollection*) _tmp46_, (SkkDict*) _tmp47_);
			_g_object_unref0 (_tmp47_);
		}
		goto __finally3;
		__catch3_g_error:
		{
			GError* e = NULL;
			FILE* _tmp48_;
			const gchar* _tmp49_;
			GError* _tmp50_;
			const gchar* _tmp51_;
			e = _inner_error_;
			_inner_error_ = NULL;
			_tmp48_ = stderr;
			_tmp49_ = opt_file_dict;
			_tmp50_ = e;
			_tmp51_ = _tmp50_->message;
			fprintf (_tmp48_, "can't open file dict %s: %s", _tmp49_, _tmp51_);
			result = 1;
			_g_error_free0 (e);
			_g_object_unref0 (dictionaries);
			_g_option_context_free0 (option_context);
			return result;
		}
		__finally3:
		if (_inner_error_ != NULL) {
			_g_object_unref0 (dictionaries);
			_g_option_context_free0 (option_context);
			g_critical ("file %s: line %d: uncaught error: %s (%s, %d)", __FILE__, __LINE__, _inner_error_->message, g_quark_to_string (_inner_error_->domain), _inner_error_->code);
			g_clear_error (&_inner_error_);
			return 0;
		}
	}
	_tmp52_ = opt_skkserv;
	if (_tmp52_ != NULL) {
		const gchar* _tmp53_;
		gint _tmp54_ = 0;
		gint index;
		gchar* host = NULL;
		guint16 port = 0U;
		gint _tmp55_;
		_tmp53_ = opt_skkserv;
		_tmp54_ = string_last_index_of (_tmp53_, ":", 0);
		index = _tmp54_;
		_tmp55_ = index;
		if (_tmp55_ < 0) {
			const gchar* _tmp56_;
			gchar* _tmp57_;
			_tmp56_ = opt_skkserv;
			_tmp57_ = g_strdup (_tmp56_);
			_g_free0 (host);
			host = _tmp57_;
			port = (guint16) 1178;
		} else {
			const gchar* _tmp58_;
			gint _tmp59_;
			gchar* _tmp60_ = NULL;
			const gchar* _tmp61_;
			gint _tmp62_;
			const gchar* _tmp63_;
			gint _tmp64_;
			gint _tmp65_;
			gchar* _tmp66_ = NULL;
			gchar* _tmp67_;
			gint _tmp68_ = 0;
			_tmp58_ = opt_skkserv;
			_tmp59_ = index;
			_tmp60_ = string_slice (_tmp58_, (glong) 0, (glong) _tmp59_);
			_g_free0 (host);
			host = _tmp60_;
			_tmp61_ = opt_skkserv;
			_tmp62_ = index;
			_tmp63_ = opt_skkserv;
			_tmp64_ = strlen (_tmp63_);
			_tmp65_ = _tmp64_;
			_tmp66_ = string_slice (_tmp61_, (glong) (_tmp62_ + 1), (glong) _tmp65_);
			_tmp67_ = _tmp66_;
			_tmp68_ = atoi (_tmp67_);
			port = (guint16) _tmp68_;
			_g_free0 (_tmp67_);
		}
		{
			const gchar* _tmp69_;
			guint16 _tmp70_;
			SkkSkkServ* _tmp71_;
			SkkSkkServ* _tmp72_;
			GeeArrayList* _tmp73_;
			SkkSkkServ* _tmp74_;
			_tmp69_ = host;
			_tmp70_ = port;
			_tmp71_ = skk_skk_serv_new (_tmp69_, _tmp70_, "EUC-JP", &_inner_error_);
			_tmp72_ = _tmp71_;
			if (_inner_error_ != NULL) {
				goto __catch4_g_error;
			}
			_tmp73_ = dictionaries;
			_tmp74_ = _tmp72_;
			gee_abstract_collection_add ((GeeAbstractCollection*) _tmp73_, (SkkDict*) _tmp74_);
			_g_object_unref0 (_tmp74_);
		}
		goto __finally4;
		__catch4_g_error:
		{
			GError* e = NULL;
			FILE* _tmp75_;
			const gchar* _tmp76_;
			guint16 _tmp77_;
			GError* _tmp78_;
			const gchar* _tmp79_;
			e = _inner_error_;
			_inner_error_ = NULL;
			_tmp75_ = stderr;
			_tmp76_ = host;
			_tmp77_ = port;
			_tmp78_ = e;
			_tmp79_ = _tmp78_->message;
			fprintf (_tmp75_, "can't connect to skkserv at %s:%d: %s", _tmp76_, (gint) _tmp77_, _tmp79_);
			result = 1;
			_g_error_free0 (e);
			_g_free0 (host);
			_g_object_unref0 (dictionaries);
			_g_option_context_free0 (option_context);
			return result;
		}
		__finally4:
		if (_inner_error_ != NULL) {
			_g_free0 (host);
			_g_object_unref0 (dictionaries);
			_g_option_context_free0 (option_context);
			g_critical ("file %s: line %d: uncaught error: %s (%s, %d)", __FILE__, __LINE__, _inner_error_->message, g_quark_to_string (_inner_error_->domain), _inner_error_->code);
			g_clear_error (&_inner_error_);
			return 0;
		}
		_g_free0 (host);
	}
	_tmp80_ = dictionaries;
	_tmp82_ = gee_abstract_collection_to_array ((GeeAbstractCollection*) _tmp80_, &_tmp81_);
	_tmp83_ = _tmp82_;
	_tmp83__length1 = _tmp81_;
	_tmp84_ = skk_context_new (_tmp83_, _tmp81_);
	_tmp85_ = _tmp84_;
	_tmp83_ = (_vala_array_free (_tmp83_, _tmp83__length1, (GDestroyNotify) g_object_unref), NULL);
	context = _tmp85_;
	_tmp86_ = opt_typing_rule;
	if (_tmp86_ != NULL) {
		{
			const gchar* _tmp87_;
			SkkRule* _tmp88_;
			SkkRule* _tmp89_;
			SkkContext* _tmp90_;
			SkkRule* _tmp91_;
			_tmp87_ = opt_typing_rule;
			_tmp88_ = skk_rule_new (_tmp87_, &_inner_error_);
			_tmp89_ = _tmp88_;
			if (_inner_error_ != NULL) {
				if (_inner_error_->domain == SKK_RULE_PARSE_ERROR) {
					goto __catch5_skk_rule_parse_error;
				}
				_g_object_unref0 (context);
				_g_object_unref0 (dictionaries);
				_g_option_context_free0 (option_context);
				g_critical ("file %s: line %d: unexpected error: %s (%s, %d)", __FILE__, __LINE__, _inner_error_->message, g_quark_to_string (_inner_error_->domain), _inner_error_->code);
				g_clear_error (&_inner_error_);
				return 0;
			}
			_tmp90_ = context;
			_tmp91_ = _tmp89_;
			skk_context_set_typing_rule (_tmp90_, _tmp91_);
			_g_object_unref0 (_tmp91_);
		}
		goto __finally5;
		__catch5_skk_rule_parse_error:
		{
			GError* e = NULL;
			FILE* _tmp92_;
			const gchar* _tmp93_;
			GError* _tmp94_;
			const gchar* _tmp95_;
			e = _inner_error_;
			_inner_error_ = NULL;
			_tmp92_ = stderr;
			_tmp93_ = opt_typing_rule;
			_tmp94_ = e;
			_tmp95_ = _tmp94_->message;
			fprintf (_tmp92_, "can't load rule \"%s\": %s\n", _tmp93_, _tmp95_);
			result = 1;
			_g_error_free0 (e);
			_g_object_unref0 (context);
			_g_object_unref0 (dictionaries);
			_g_option_context_free0 (option_context);
			return result;
		}
		__finally5:
		if (_inner_error_ != NULL) {
			_g_object_unref0 (context);
			_g_object_unref0 (dictionaries);
			_g_option_context_free0 (option_context);
			g_critical ("file %s: line %d: uncaught error: %s (%s, %d)", __FILE__, __LINE__, _inner_error_->message, g_quark_to_string (_inner_error_->domain), _inner_error_->code);
			g_clear_error (&_inner_error_);
			return 0;
		}
	}
	_tmp96_ = context;
	_tmp97_ = repl_new (_tmp96_);
	repl = _tmp97_;
	_tmp98_ = repl;
	_tmp99_ = repl_run (_tmp98_);
	if (!_tmp99_) {
		result = 1;
		_g_object_unref0 (repl);
		_g_object_unref0 (context);
		_g_object_unref0 (dictionaries);
		_g_option_context_free0 (option_context);
		return result;
	}
	result = 0;
	_g_object_unref0 (repl);
	_g_object_unref0 (context);
	_g_object_unref0 (dictionaries);
	_g_option_context_free0 (option_context);
	return result;
}


int main (int argc, char ** argv) {
	g_type_init ();
	return _vala_main (argv, argc);
}


static gchar* g_file_stream_read_line (FILE* self) {
	gchar* result = NULL;
	gint c = 0;
	GString* ret;
	GString* _tmp7_;
	g_return_val_if_fail (self != NULL, NULL);
	ret = NULL;
	while (TRUE) {
		gint _tmp0_ = 0;
		gint _tmp1_;
		GString* _tmp2_;
		gint _tmp4_;
		GString* _tmp5_;
		gint _tmp6_;
		_tmp0_ = fgetc (self);
		c = _tmp0_;
		_tmp1_ = c;
		if (!(_tmp1_ != EOF)) {
			break;
		}
		_tmp2_ = ret;
		if (_tmp2_ == NULL) {
			GString* _tmp3_;
			_tmp3_ = g_string_new ("");
			_g_string_free0 (ret);
			ret = _tmp3_;
		}
		_tmp4_ = c;
		if (_tmp4_ == ((gint) '\n')) {
			break;
		}
		_tmp5_ = ret;
		_tmp6_ = c;
		g_string_append_c ((GString*) _tmp5_, (gchar) _tmp6_);
	}
	_tmp7_ = ret;
	if (_tmp7_ == NULL) {
		result = NULL;
		_g_string_free0 (ret);
		return result;
	} else {
		GString* _tmp8_;
		const gchar* _tmp9_;
		gchar* _tmp10_;
		_tmp8_ = ret;
		_tmp9_ = ((GString*) _tmp8_)->str;
		_tmp10_ = g_strdup (_tmp9_);
		result = _tmp10_;
		_g_string_free0 (ret);
		return result;
	}
	_g_string_free0 (ret);
}


static gchar* string_replace (const gchar* self, const gchar* old, const gchar* replacement) {
	gchar* result = NULL;
	GError * _inner_error_ = NULL;
	g_return_val_if_fail (self != NULL, NULL);
	g_return_val_if_fail (old != NULL, NULL);
	g_return_val_if_fail (replacement != NULL, NULL);
	{
		const gchar* _tmp0_;
		gchar* _tmp1_ = NULL;
		gchar* _tmp2_;
		GRegex* _tmp3_;
		GRegex* _tmp4_;
		GRegex* regex;
		GRegex* _tmp5_;
		const gchar* _tmp6_;
		gchar* _tmp7_ = NULL;
		gchar* _tmp8_;
		_tmp0_ = old;
		_tmp1_ = g_regex_escape_string (_tmp0_, -1);
		_tmp2_ = _tmp1_;
		_tmp3_ = g_regex_new (_tmp2_, 0, 0, &_inner_error_);
		_tmp4_ = _tmp3_;
		_g_free0 (_tmp2_);
		regex = _tmp4_;
		if (_inner_error_ != NULL) {
			if (_inner_error_->domain == G_REGEX_ERROR) {
				goto __catch6_g_regex_error;
			}
			g_critical ("file %s: line %d: unexpected error: %s (%s, %d)", __FILE__, __LINE__, _inner_error_->message, g_quark_to_string (_inner_error_->domain), _inner_error_->code);
			g_clear_error (&_inner_error_);
			return NULL;
		}
		_tmp5_ = regex;
		_tmp6_ = replacement;
		_tmp7_ = g_regex_replace_literal (_tmp5_, self, (gssize) (-1), 0, _tmp6_, 0, &_inner_error_);
		_tmp8_ = _tmp7_;
		if (_inner_error_ != NULL) {
			_g_regex_unref0 (regex);
			if (_inner_error_->domain == G_REGEX_ERROR) {
				goto __catch6_g_regex_error;
			}
			_g_regex_unref0 (regex);
			g_critical ("file %s: line %d: unexpected error: %s (%s, %d)", __FILE__, __LINE__, _inner_error_->message, g_quark_to_string (_inner_error_->domain), _inner_error_->code);
			g_clear_error (&_inner_error_);
			return NULL;
		}
		result = _tmp8_;
		_g_regex_unref0 (regex);
		return result;
	}
	goto __finally6;
	__catch6_g_regex_error:
	{
		GError* e = NULL;
		e = _inner_error_;
		_inner_error_ = NULL;
		g_assert_not_reached ();
		_g_error_free0 (e);
	}
	__finally6:
	if (_inner_error_ != NULL) {
		g_critical ("file %s: line %d: uncaught error: %s (%s, %d)", __FILE__, __LINE__, _inner_error_->message, g_quark_to_string (_inner_error_->domain), _inner_error_->code);
		g_clear_error (&_inner_error_);
		return NULL;
	}
}


gboolean repl_run (Repl* self) {
	gboolean result = FALSE;
	gchar* line = NULL;
	g_return_val_if_fail (self != NULL, FALSE);
	while (TRUE) {
		FILE* _tmp0_;
		gchar* _tmp1_ = NULL;
		const gchar* _tmp2_;
		SkkContext* _tmp3_;
		const gchar* _tmp4_;
		SkkContext* _tmp5_;
		gchar* _tmp6_ = NULL;
		gchar* output;
		SkkContext* _tmp7_;
		const gchar* _tmp8_;
		const gchar* _tmp9_;
		gchar* _tmp10_;
		gchar* preedit;
		FILE* _tmp11_;
		const gchar* _tmp12_;
		gchar* _tmp13_ = NULL;
		gchar* _tmp14_;
		const gchar* _tmp15_;
		gchar* _tmp16_ = NULL;
		gchar* _tmp17_;
		const gchar* _tmp18_;
		gchar* _tmp19_ = NULL;
		gchar* _tmp20_;
		SkkContext* _tmp21_;
		SkkContext* _tmp22_;
		_tmp0_ = stdin;
		_tmp1_ = g_file_stream_read_line (_tmp0_);
		_g_free0 (line);
		line = _tmp1_;
		_tmp2_ = line;
		if (!(_tmp2_ != NULL)) {
			break;
		}
		_tmp3_ = self->priv->context;
		_tmp4_ = line;
		skk_context_process_key_events (_tmp3_, _tmp4_);
		_tmp5_ = self->priv->context;
		_tmp6_ = skk_context_poll_output (_tmp5_);
		output = _tmp6_;
		_tmp7_ = self->priv->context;
		_tmp8_ = skk_context_get_preedit (_tmp7_);
		_tmp9_ = _tmp8_;
		_tmp10_ = g_strdup (_tmp9_);
		preedit = _tmp10_;
		_tmp11_ = stdout;
		_tmp12_ = line;
		_tmp13_ = string_replace (_tmp12_, "\"", "\\\"");
		_tmp14_ = _tmp13_;
		_tmp15_ = output;
		_tmp16_ = string_replace (_tmp15_, "\"", "\\\"");
		_tmp17_ = _tmp16_;
		_tmp18_ = preedit;
		_tmp19_ = string_replace (_tmp18_, "\"", "\\\"");
		_tmp20_ = _tmp19_;
		fprintf (_tmp11_, "{ \"input\": \"%s\", " "\"output\": \"%s\", " "\"preedit\": \"%s\" }\n", _tmp14_, _tmp17_, _tmp20_);
		_g_free0 (_tmp20_);
		_g_free0 (_tmp17_);
		_g_free0 (_tmp14_);
		_tmp21_ = self->priv->context;
		skk_context_reset (_tmp21_);
		_tmp22_ = self->priv->context;
		skk_context_clear_output (_tmp22_);
		_g_free0 (preedit);
		_g_free0 (output);
	}
	result = TRUE;
	_g_free0 (line);
	return result;
}


static gpointer _g_object_ref0 (gpointer self) {
	return self ? g_object_ref (self) : NULL;
}


Repl* repl_construct (GType object_type, SkkContext* context) {
	Repl * self = NULL;
	SkkContext* _tmp0_;
	SkkContext* _tmp1_;
	g_return_val_if_fail (context != NULL, NULL);
	self = (Repl*) g_object_new (object_type, NULL);
	_tmp0_ = context;
	_tmp1_ = _g_object_ref0 (_tmp0_);
	_g_object_unref0 (self->priv->context);
	self->priv->context = _tmp1_;
	return self;
}


Repl* repl_new (SkkContext* context) {
	return repl_construct (TYPE_REPL, context);
}


static void repl_class_init (ReplClass * klass) {
	repl_parent_class = g_type_class_peek_parent (klass);
	g_type_class_add_private (klass, sizeof (ReplPrivate));
	G_OBJECT_CLASS (klass)->finalize = repl_finalize;
}


static void repl_instance_init (Repl * self) {
	self->priv = REPL_GET_PRIVATE (self);
}


static void repl_finalize (GObject* obj) {
	Repl * self;
	self = G_TYPE_CHECK_INSTANCE_CAST (obj, TYPE_REPL, Repl);
	_g_object_unref0 (self->priv->context);
	G_OBJECT_CLASS (repl_parent_class)->finalize (obj);
}


GType repl_get_type (void) {
	static volatile gsize repl_type_id__volatile = 0;
	if (g_once_init_enter (&repl_type_id__volatile)) {
		static const GTypeInfo g_define_type_info = { sizeof (ReplClass), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) repl_class_init, (GClassFinalizeFunc) NULL, NULL, sizeof (Repl), 0, (GInstanceInitFunc) repl_instance_init, NULL };
		GType repl_type_id;
		repl_type_id = g_type_register_static (G_TYPE_OBJECT, "Repl", &g_define_type_info, 0);
		g_once_init_leave (&repl_type_id__volatile, repl_type_id);
	}
	return repl_type_id__volatile;
}


static void _vala_array_destroy (gpointer array, gint array_length, GDestroyNotify destroy_func) {
	if ((array != NULL) && (destroy_func != NULL)) {
		int i;
		for (i = 0; i < array_length; i = i + 1) {
			if (((gpointer*) array)[i] != NULL) {
				destroy_func (((gpointer*) array)[i]);
			}
		}
	}
}


static void _vala_array_free (gpointer array, gint array_length, GDestroyNotify destroy_func) {
	_vala_array_destroy (array, array_length, destroy_func);
	g_free (array);
}



