summaryrefslogtreecommitdiff
path: root/plugins/libedit/libedit-main.c
blob: d1b83eae798ce3b8dbd30cbb4b86483d7973455c (plain)
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
84
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/* 
 * libedit-main.c
 * Copyright (C) 2006-2010 Akira TAGOH
 * 
 * Authors:
 *   Akira TAGOH  <akira@tagoh.org>
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 * 
 * This library 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
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#ifndef USE_LIBEDIT
#error Please install libedit and/or the development package of it and run configure again.  This plugin is for libedit only.
#endif

#include <readline.h>
#define PLUGIN
#include "hgdict.h"
#include "hgplugin.h"
#include "hgoperator.h"
#include "hgstack.h"
#include "hgvm.h"


static gchar    *_libedit_get_line    (hg_lineedit_t  *lineedit,
                                       const gchar    *prompt,
                                       gpointer        user_data);
static void      _libedit_add_history (hg_lineedit_t  *lineedit,
                                       const gchar    *history,
                                       gpointer        user_data);
static gboolean  _libedit_load_history(hg_lineedit_t  *lineedit,
                                       const gchar    *historyfile,
                                       gpointer        user_data);
static gboolean  _libedit_save_history(hg_lineedit_t  *lineedit,
                                       const gchar    *historyfile,
                                       gpointer        user_data);

PROTO_PLUGIN (libedit);

typedef enum {
	HG_libedit_enc_loadhistory = 0,
	HG_libedit_enc_savehistory,
	HG_libedit_enc_END
} hg_libedit_encoding_t;
static hg_lineedit_vtable_t __libedit_lineedit_vtable = {
	.get_line = _libedit_get_line,
	.add_history = _libedit_add_history,
	.load_history = _libedit_load_history,
	.save_history = _libedit_save_history,
};
static hg_quark_t __libedit_enc_list[HG_libedit_enc_END];

/*< private >*/
static gchar *
_libedit_get_line(hg_lineedit_t *lineedit,
		  const gchar   *prompt,
		  gpointer       user_data)
{
	gchar *retval;

	if (prompt == NULL)
		retval = readline("");
	else
		retval = readline(prompt);

	return retval;
}

static void
_libedit_add_history(hg_lineedit_t *lineedit,
		     const gchar   *history,
		     gpointer       user_data)
{
	hg_return_if_fail (history != NULL);

	if (history[0] == 0 ||
	    history[0] == '\n' ||
	    history[0] == '\r' ||
	    (history[0] == '\r' && history[1] == '\n'))
		return;

	add_history(history);
}

static gboolean
_libedit_load_history(hg_lineedit_t *lineedit,
		      const gchar   *historyfile,
		      gpointer       user_data)
{
	hg_return_val_if_fail (historyfile != NULL, FALSE);

	read_history(historyfile);

	return TRUE;
}

static gboolean
_libedit_save_history(hg_lineedit_t *lineedit,
		      const gchar   *historyfile,
		      gpointer       user_data)
{
	hg_return_val_if_fail (historyfile != NULL, FALSE);

	write_history(historyfile);

	return TRUE;
}

/* -filename- .loadhistory - */
DEFUNC_OPER (private_loadhistory)
G_STMT_START {
	hg_quark_t arg0;
	hg_string_t *s;
	gchar *cstr, *filename, *histfile;
	const gchar *env;

	CHECK_STACK (ostack, 1);

	arg0 = hg_stack_index(ostack, 0, error);
	if (!HG_IS_QSTRING (arg0)) {
		hg_vm_set_error(vm, qself, HG_VM_e_typecheck);
		return FALSE;
	}
	s = HG_VM_LOCK (vm, arg0, error);
	if (s == NULL) {
		hg_vm_set_error(vm, qself, HG_VM_e_VMerror);
		return FALSE;
	}
	cstr = hg_string_get_cstr(s);
	HG_VM_UNLOCK (vm, arg0);

	filename = g_path_get_basename(cstr);
	env = g_getenv("HOME");
	if (env != NULL) {
		histfile = g_build_filename(env, filename, NULL);
	} else {
		histfile = g_strdup(filename);
	}
	retval = hg_lineedit_load_history(vm->lineedit, histfile);

	g_free(histfile);
	g_free(filename);
	g_free(cstr);

	hg_stack_drop(ostack, error);
} G_STMT_END;
VALIDATE_STACK_SIZE (-1, 0, 0);
DEFUNC_OPER_END

/* -filename- .savehistory - */
DEFUNC_OPER (private_savehistory)
G_STMT_START {
	hg_quark_t arg0;
	hg_string_t *s;
	gchar *cstr, *filename, *histfile;
	const gchar *env;

	CHECK_STACK (ostack, 1);

	arg0 = hg_stack_index(ostack, 0, error);
	if (!HG_IS_QSTRING (arg0)) {
		hg_vm_set_error(vm, qself, HG_VM_e_typecheck);
		return FALSE;
	}
	s = HG_VM_LOCK (vm, arg0, error);
	if (s == NULL) {
		hg_vm_set_error(vm, qself, HG_VM_e_VMerror);
		return FALSE;
	}
	cstr = hg_string_get_cstr(s);
	HG_VM_UNLOCK (vm, arg0);

	filename = g_path_get_basename(cstr);
	env = g_getenv("HOME");
	if (env != NULL) {
		histfile = g_build_filename(env, filename, NULL);
	} else {
		histfile = g_strdup(filename);
	}
	retval = hg_lineedit_save_history(vm->lineedit, histfile);

	g_free(histfile);
	g_free(filename);
	g_free(cstr);

	hg_stack_drop(ostack, error);
} G_STMT_END;
VALIDATE_STACK_SIZE (-1, 0, 0);
DEFUNC_OPER_END

static gboolean
_libedit_init(void)
{
	gint i;

	for (i = 0; i < HG_libedit_enc_END; i++) {
		__libedit_enc_list[i] = Qnil;
	}

	return TRUE;
}

static gboolean
_libedit_finalize(void)
{
	return TRUE;
}

static gboolean
_libedit_load(hg_plugin_t  *plugin,
	      gpointer      vm_,
	      GError      **error)
{
	hg_vm_t *vm = vm_;
	hg_dict_t *dict;
	gboolean is_global;
	gint i;

	if (plugin->user_data != NULL) {
		g_warning("plugin is already loaded.");
		return TRUE;
	}
	plugin->user_data = vm->lineedit;

	is_global = hg_vm_is_global_mem_used(vm);
	dict = HG_VM_LOCK (vm, vm->qsystemdict, error);
	if (dict == NULL)
		return FALSE;
	hg_vm_use_global_mem(vm, TRUE);

	vm->lineedit = hg_lineedit_new(hg_vm_get_mem(vm),
				       &__libedit_lineedit_vtable,
				       NULL, NULL);

	__libedit_enc_list[HG_libedit_enc_loadhistory] = REG_ENC (vm->name, .loadhistory, private_loadhistory);
	__libedit_enc_list[HG_libedit_enc_savehistory] = REG_ENC (vm->name, .savehistory, private_savehistory);

	for (i = 0; i < HG_libedit_enc_END; i++) {
		if (__libedit_enc_list[i] == Qnil)
			return FALSE;
		REG_OPER (dict, __libedit_enc_list[i]);
	}

	hg_vm_use_global_mem(vm, is_global);

	return TRUE;
}

static gboolean
_libedit_unload(hg_plugin_t  *plugin,
		gpointer      vm_,
		GError      **error)
{
	hg_vm_t *vm = vm_;

	if (plugin->user_data == NULL) {
		g_warning("plugin not loaded.");
		return FALSE;
	}
	hg_lineedit_destroy(vm->lineedit);
	vm->lineedit = plugin->user_data;
	plugin->user_data = NULL;

	return TRUE;
}

/*< public >*/
hg_plugin_t *
plugin_new(hg_mem_t  *mem,
	   GError   **error)
{
	hg_return_val_with_gerror_if_fail (mem != NULL, NULL, error, HG_VM_e_VMerror);

	return hg_plugin_new(mem,
			     &__libedit_plugin_info);
}