mu/lib/mu-script.cc

375 lines
7.9 KiB
C++
Raw Normal View History

2012-10-21 14:52:34 +02:00
/*
** Copyright (C) 2012-2021 Dirk-Jan C. Binnema <djcb@djcbsoftware.nl>
2012-10-21 14:52:34 +02: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 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
2020-02-17 20:45:21 +01:00
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wredundant-decls"
2012-10-21 14:52:34 +02:00
#include <libguile.h>
2020-02-17 20:45:21 +01:00
#pragma GCC diagnostic pop
2012-10-21 14:52:34 +02:00
#endif /*BUILD_GUILE*/
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <dirent.h>
#include <errno.h>
#include <unistd.h>
#include "mu-script.hh"
#include "utils/mu-util.h"
2012-10-21 14:52:34 +02:00
/**
* 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 */
2012-10-21 14:52:34 +02:00
};
/* create a new MuScriptInfo* object*/
static MuScriptInfo*
script_info_new(void)
2012-10-21 14:52:34 +02:00
{
return g_slice_new0(MuScriptInfo);
2012-10-21 14:52:34 +02:00
}
/* destroy a MuScriptInfo* object */
static void
script_info_destroy(MuScriptInfo* msi)
2012-10-21 14:52:34 +02:00
{
if (!msi)
return;
g_free(msi->_name);
g_free(msi->_path);
g_free(msi->_oneline);
g_free(msi->_descr);
g_slice_free(MuScriptInfo, msi);
2012-10-21 14:52:34 +02:00
}
/* compare two MuScripInfo* objects (for sorting) */
static int
script_info_cmp(MuScriptInfo* msi1, MuScriptInfo* msi2)
2012-10-21 14:52:34 +02:00
{
return strcmp(msi1->_name, msi2->_name);
2012-10-21 14:52:34 +02:00
}
const char*
mu_script_info_name(MuScriptInfo* msi)
2012-10-21 14:52:34 +02:00
{
g_return_val_if_fail(msi, NULL);
2012-10-21 14:52:34 +02:00
return msi->_name;
}
const char*
mu_script_info_path(MuScriptInfo* msi)
2012-10-21 14:52:34 +02:00
{
g_return_val_if_fail(msi, NULL);
2012-10-21 14:52:34 +02:00
return msi->_path;
}
2012-10-22 22:16:01 +02:00
const char*
mu_script_info_one_line(MuScriptInfo* msi)
2012-10-22 22:16:01 +02:00
{
g_return_val_if_fail(msi, NULL);
2012-10-22 22:16:01 +02:00
return msi->_oneline;
}
2012-10-21 14:52:34 +02:00
const char*
mu_script_info_description(MuScriptInfo* msi)
2012-10-21 14:52:34 +02:00
{
g_return_val_if_fail(msi, NULL);
2012-10-21 14:52:34 +02:00
return msi->_descr;
}
2012-10-22 22:16:01 +02:00
gboolean
mu_script_info_matches_regex(MuScriptInfo* msi, const char* rxstr, GError** err)
2012-10-22 22:16:01 +02:00
{
GRegex* rx;
2012-10-22 22:16:01 +02:00
gboolean match;
g_return_val_if_fail(msi, FALSE);
g_return_val_if_fail(rxstr, FALSE);
2012-10-22 22:16:01 +02:00
rx = g_regex_new(rxstr,
(GRegexCompileFlags)(G_REGEX_CASELESS | G_REGEX_OPTIMIZE),
(GRegexMatchFlags)0,
err);
2012-10-22 22:16:01 +02:00
if (!rx)
return FALSE;
match = FALSE;
if (msi->_name)
match = g_regex_match(rx, msi->_name, (GRegexMatchFlags)0, NULL);
2012-10-22 22:16:01 +02:00
if (!match && msi->_oneline)
match = g_regex_match(rx, msi->_oneline, (GRegexMatchFlags)0, NULL);
2012-10-22 22:16:01 +02:00
return match;
}
2012-10-21 14:52:34 +02:00
void
mu_script_info_list_destroy(GSList* lst)
2012-10-21 14:52:34 +02:00
{
g_slist_free_full(lst, (GDestroyNotify)script_info_destroy);
2012-10-21 14:52:34 +02:00
}
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);
}
2012-10-22 22:16:01 +02:00
static gboolean
get_descriptions(MuScriptInfo* msi, const char* prefix)
{
GIOStatus io_status;
GIOChannel* script_io;
GError* err;
2012-10-22 22:16:01 +02:00
char *line, *descr, *oneline;
if (!prefix)
2012-10-22 22:16:01 +02:00
return TRUE; /* not an error */
if (!(script_io = open_channel(msi->_path)))
2012-10-22 22:16:01 +02:00
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;
2012-10-22 22:16:01 +02:00
if (!g_str_has_prefix(line, prefix))
2012-10-22 22:16:01 +02:00
continue;
if (!oneline)
oneline = g_strdup(line + strlen(prefix));
2012-10-22 22:16:01 +02:00
else {
char* tmp;
tmp = descr;
descr = g_strdup_printf("%s%s", descr ? descr : "", line + strlen(prefix));
g_free(tmp);
2012-10-22 22:16:01 +02:00
}
} 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);
2012-10-22 22:16:01 +02:00
msi->_oneline = oneline;
msi->_descr = descr;
return TRUE;
}
2012-10-21 14:52:34 +02:00
GSList*
mu_script_get_script_info_list(const char* path,
const char* ext,
const char* descprefix,
GError** err)
2012-10-21 14:52:34 +02:00
{
DIR* dir;
GSList* lst;
struct dirent* dentry;
2012-10-21 14:52:34 +02:00
g_return_val_if_fail(path, NULL);
2012-10-21 14:52:34 +02:00
dir = opendir(path);
2012-10-21 14:52:34 +02:00
if (!dir) {
mu_util_g_set_error(err,
MU_ERROR_FILE_CANNOT_OPEN,
"failed to open '%s': %s",
path,
g_strerror(errno));
2012-10-21 14:52:34 +02:00
return NULL;
}
/* create a list of names, paths */
lst = NULL;
while ((dentry = readdir(dir))) {
MuScriptInfo* msi;
2012-10-21 14:52:34 +02:00
/* only consider files with certain extensions,
* if ext != NULL */
if (ext && !g_str_has_suffix(dentry->d_name, ext))
2012-10-21 14:52:34 +02:00
continue;
msi = script_info_new();
msi->_name = g_strdup(dentry->d_name);
2012-10-21 14:52:34 +02:00
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);
2012-10-22 22:16:01 +02:00
/* set the one-line and long description */
get_descriptions(msi, descprefix);
lst = g_slist_prepend(lst, msi);
2012-10-21 14:52:34 +02:00
}
closedir(dir); /* ignore error checking... */
2012-10-21 14:52:34 +02:00
return g_slist_sort(lst, (GCompareFunc)script_info_cmp);
2012-10-21 14:52:34 +02:00
}
MuScriptInfo*
mu_script_find_script_with_name(GSList* lst, const char* name)
2012-10-21 14:52:34 +02:00
{
GSList* cur;
2012-10-21 14:52:34 +02:00
g_return_val_if_fail(name, NULL);
2012-10-21 14:52:34 +02:00
for (cur = lst; cur; cur = g_slist_next(cur)) {
MuScriptInfo* msi;
2012-10-21 14:52:34 +02:00
msi = (MuScriptInfo*)cur->data;
if (g_strcmp0(name, mu_script_info_name(msi)) == 0)
2012-10-21 14:52:34 +02:00
return msi;
}
return NULL;
}
static char*
quoted_from_strv (const gchar **params)
{
GString *str;
int i;
g_return_val_if_fail (params, NULL);
if (!params[0])
return g_strdup ("");
str = g_string_sized_new (64); /* just a guess */
for (i = 0; params[i]; ++i) {
if (i > 0)
g_string_append_c (str, ' ');
g_string_append_c (str, '"');
g_string_append (str, params[i]);
g_string_append_c (str, '"');
}
return g_string_free (str, FALSE);
}
2012-10-21 14:52:34 +02:00
#ifdef BUILD_GUILE
static void
guile_shell(void* closure, int argc, char** argv)
2012-10-21 14:52:34 +02:00
{
scm_shell(argc, argv);
2012-10-21 14:52:34 +02:00
}
gboolean
mu_script_guile_run(MuScriptInfo* msi, const char* muhome, const char** args, GError** err)
2012-10-24 14:48:07 +02:00
{
const char* s;
char * mainargs, *expr;
char** argv;
2012-10-22 22:16:01 +02:00
g_return_val_if_fail(msi, FALSE);
g_return_val_if_fail(muhome, FALSE);
2012-10-22 22:16:01 +02:00
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;
2017-11-05 12:12:41 +01:00
}
2022-02-07 16:36:34 +01:00
argv = g_new0(char*, 6);
argv[0] = g_strdup(GUILE_BINARY);
argv[1] = g_strdup("-l");
s = mu_script_info_path(msi);
argv[2] = g_strdup(s ? s : "");
2012-10-22 22:16:01 +02:00
mainargs = quoted_from_strv(args);
expr = g_strdup_printf("(main '(\"%s\" \"--muhome=%s\" %s))",
mu_script_info_name(msi),
muhome,
mainargs ? mainargs : "");
2012-10-22 22:16:01 +02:00
g_free(mainargs);
2017-11-05 12:12:41 +01:00
argv[3] = g_strdup("-c");
2012-10-24 14:48:07 +02:00
argv[4] = expr;
2012-10-22 22:16:01 +02:00
scm_boot_guile(5, argv, guile_shell, NULL);
2012-10-21 14:52:34 +02:00
/* never reached but let's be correct(TM)*/
g_strfreev(argv);
2012-10-21 14:52:34 +02:00
return TRUE;
}
#else /*!BUILD_GUILE*/
2012-10-21 14:52:34 +02:00
gboolean
mu_script_guile_run(MuScriptInfo* msi, const char* muhome, const char** args, GError** err)
2012-10-21 14:52:34 +02:00
{
mu_util_g_set_error(err, MU_ERROR_INTERNAL, "this mu does not have guile support");
2012-10-21 14:52:34 +02:00
return FALSE;
}
#endif /*!BUILD_GUILE*/