summaryrefslogtreecommitdiff
path: root/lib/mu-script.cc
blob: afcfd8e4a982ff075567f84382e2fd0fa40e5d7c (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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
/*
** Copyright (C) 2012-2021 Dirk-Jan C. Binnema <djcb@djcbsoftware.nl>
**
** 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, 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
**
*/

#include "config.h"

#ifdef BUILD_GUILE

#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wredundant-decls"
#include <libguile.h>
#pragma GCC diagnostic pop
#endif /*BUILD_GUILE*/

#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <dirent.h>
#include <errno.h>
#include <unistd.h>

#include "utils/mu-str.h"
#include "mu-script.hh"
#include "utils/mu-util.h"

/**
 * Structure with information about a certain script.
 * the values will be *freed* when MuScriptInfo is freed
 */
struct MuScriptInfo {
	char *_name;    /* filename-sans-extension */
	char *_path;    /* full path to script */
	char *_oneline; /* one-line description */
	char *_descr;   /* longer description */
};


/* create a new MuScriptInfo* object*/
static MuScriptInfo*
script_info_new (void)
{
	return g_slice_new0 (MuScriptInfo);
}

/* destroy a MuScriptInfo* object */
static void
script_info_destroy (MuScriptInfo *msi)
{
	if (!msi)
		return;

	g_free (msi->_name);
	g_free (msi->_path);
	g_free (msi->_oneline);
	g_free (msi->_descr);

	g_slice_free (MuScriptInfo, msi);
}

/* compare two MuScripInfo* objects (for sorting) */
static int
script_info_cmp (MuScriptInfo *msi1, MuScriptInfo *msi2)
{
	return strcmp (msi1->_name, msi2->_name);

}

const char*
mu_script_info_name (MuScriptInfo *msi)
{
	g_return_val_if_fail (msi, NULL);
	return msi->_name;
}

const char*
mu_script_info_path (MuScriptInfo *msi)
{
	g_return_val_if_fail (msi, NULL);
	return msi->_path;
}

const char*
mu_script_info_one_line (MuScriptInfo *msi)
{
	g_return_val_if_fail (msi, NULL);
	return msi->_oneline;
}

const char*
mu_script_info_description (MuScriptInfo *msi)
{
	g_return_val_if_fail (msi, NULL);
	return msi->_descr;
}


gboolean
mu_script_info_matches_regex (MuScriptInfo *msi, const char *rxstr,
			      GError **err)
{
	GRegex *rx;
	gboolean match;

	g_return_val_if_fail (msi, FALSE);
	g_return_val_if_fail (rxstr, FALSE);

	rx = g_regex_new (rxstr,
                          (GRegexCompileFlags)(G_REGEX_CASELESS|G_REGEX_OPTIMIZE),
                          (GRegexMatchFlags)0, err);
	if (!rx)
		return FALSE;

	match = FALSE;
	if (msi->_name)
		match = g_regex_match (rx, msi->_name, (GRegexMatchFlags)0, NULL);
	if (!match && msi->_oneline)
		match = g_regex_match (rx, msi->_oneline,(GRegexMatchFlags)0, NULL);

	return match;
}

void
mu_script_info_list_destroy (GSList *lst)
{
	g_slist_free_full(lst, (GDestroyNotify)script_info_destroy);
}


static GIOChannel *
open_channel (const char *path)
{
	GError *err;
	GIOChannel *io_chan;

	err = NULL;

	io_chan = g_io_channel_new_file (path, "r", &err);
	if (!io_chan) {
		g_warning ("failed to open '%s': %s", path,
			   err ? err->message : "something went wrong");
		g_clear_error (&err);
		return NULL;
	}

	return io_chan;
}

static void
end_channel (GIOChannel *io_chan)
{
	GIOStatus status;
	GError *err;

	err    = NULL;
	status = g_io_channel_shutdown (io_chan, FALSE,
					&err);
	if (status != G_IO_STATUS_NORMAL) {
		g_warning ("failed to shutdown io-channel: %s",
			   err ? err->message : "something went wrong");
		g_clear_error (&err);
	}

	g_io_channel_unref (io_chan);
}



static gboolean
get_descriptions (MuScriptInfo *msi, const char *prefix)
{
	GIOStatus  io_status;
	GIOChannel *script_io;
	GError *err;

	char *line, *descr, *oneline;

	if (!prefix)
		return TRUE; /* not an error */

	if (!(script_io = open_channel (msi->_path)))
		return FALSE;

	err  = NULL;
	line = descr = oneline = NULL;

	do {
		g_free (line);
		io_status = g_io_channel_read_line (script_io, &line,
						    NULL, NULL, &err);
		if (io_status != G_IO_STATUS_NORMAL)
			break;

		if (!g_str_has_prefix (line, prefix))
			continue;

		if (!oneline)
			oneline = g_strdup (line + strlen (prefix));
		else {
			char *tmp;
			tmp = descr;
			descr = g_strdup_printf
				("%s%s", descr ? descr : "",
				 line + strlen(prefix));
				g_free (tmp);
		}

	} while (TRUE);

	if (io_status != G_IO_STATUS_EOF) {
		g_warning ("error reading %s: %s", msi->_path,
			   err ? err->message : "something went wrong");
		g_clear_error (&err);
	}

	end_channel (script_io);
	msi->_oneline = oneline;
	msi->_descr   = descr;

	return TRUE;
}



GSList*
mu_script_get_script_info_list (const char *path, const char *ext,
				const char *descprefix, GError **err)
{
	DIR *dir;
	GSList *lst;
	struct dirent *dentry;

	g_return_val_if_fail (path, NULL);

	dir = opendir (path);
	if (!dir) {
		mu_util_g_set_error (err, MU_ERROR_FILE_CANNOT_OPEN,
				     "failed to open '%s': %s",
				     path, g_strerror(errno));
		return NULL;
	}

	/* create a list of names, paths */
	lst = NULL;
	while ((dentry = readdir (dir))) {
		MuScriptInfo *msi;
		/* only consider files with certain extensions,
		 * if ext != NULL */
		if (ext && !g_str_has_suffix (dentry->d_name, ext))
			continue;
		msi = script_info_new ();
		msi->_name = g_strdup (dentry->d_name);
		if (ext) /* strip the extension */
			msi->_name[strlen(msi->_name) - strlen(ext)] = '\0';
		msi->_path = g_strdup_printf ("%s%c%s", path, G_DIR_SEPARATOR,
					      dentry->d_name);
		/* set the one-line and long description */
		get_descriptions (msi, descprefix);
		lst = g_slist_prepend (lst, msi);
	}

	closedir (dir); /* ignore error checking... */

	return g_slist_sort (lst, (GCompareFunc)script_info_cmp);
}


MuScriptInfo*
mu_script_find_script_with_name (GSList *lst, const char *name)
{
	GSList *cur;

	g_return_val_if_fail (name, NULL);

	for (cur = lst; cur; cur = g_slist_next (cur)) {

		MuScriptInfo *msi;
		msi = (MuScriptInfo*)cur->data;

		if (g_strcmp0 (name, mu_script_info_name (msi)) == 0)
			return msi;
	}

	return NULL;
}

#ifdef BUILD_GUILE
static void
guile_shell (void *closure, int argc, char **argv)
{
	scm_shell (argc, argv);
}

gboolean
mu_script_guile_run (MuScriptInfo *msi, const char *muhome,
		     const char **args, GError **err)
{
	const char	 *s;
	char		 *mainargs, *expr;
	char		**argv;

	g_return_val_if_fail (msi, FALSE);
	g_return_val_if_fail (muhome, FALSE);

	argv = g_new0 (char*, 6);
	argv[0] = g_strdup(GUILE_BINARY);
	argv[1] = g_strdup("-l");

	if (access (mu_script_info_path (msi), R_OK) != 0) {
		mu_util_g_set_error (err, MU_ERROR_FILE_CANNOT_READ,
				     "failed to read script: %s",
				     g_strerror(errno));
		 return FALSE;
	}

	s	= mu_script_info_path (msi);
	argv[2] = g_strdup (s ? s : "");

	mainargs = mu_str_quoted_from_strv (args);
	expr = g_strdup_printf (
	"(main '(\"%s\" \"--muhome=%s\" %s))",
		mu_script_info_name (msi),
		muhome,
		mainargs ? mainargs : "");

	g_free (mainargs);
	argv[3] = g_strdup("-c");
	argv[4] = expr;

	scm_boot_guile (5, argv, guile_shell, NULL);

	/* never reached but let's be correct(TM)*/
	g_strfreev (argv);
	return TRUE;
}
#else /*!BUILD_GUILE*/
gboolean
mu_script_guile_run (MuScriptInfo *msi, const char *muhome,
		     const char **args, GError **err)
{
	mu_util_g_set_error (err, MU_ERROR_INTERNAL,
			     "this mu does not have guile support");
	return FALSE;
}
#endif /*!BUILD_GUILE*/