File: mu-script.cc

package info (click to toggle)
maildir-utils 1.12.14-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 4,996 kB
  • sloc: cpp: 56,429; lisp: 11,218; sh: 895; ansic: 889; makefile: 126; pascal: 12; python: 4
file content (162 lines) | stat: -rw-r--r-- 3,696 bytes parent folder | download | duplicates (2)
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
/*
** Copyright (C) 2022-2023 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"

#include "mu-script.hh"
#include "mu/mu-options.hh"
#include "utils/mu-utils.hh"
#include "utils/mu-option.hh"

#include <fstream>
#include <iostream>

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

using namespace Mu;

static std::string
get_name(const std::string& path)
{
	auto pos = path.find_last_of("/");
	if (pos == std::string::npos)
		return path;

	auto name = path.substr(pos + 1);

	pos = name.find_last_of(".");
	if (pos == std::string::npos)
		return name;

	return name.substr(0, pos);
}


static Mu::Option<Mu::ScriptInfo>
get_info(std::string&& path, const std::string& prefix)
{
	std::ifstream file{path};
	if (!file.is_open()) {
		mu_warning ("failed to open {}", path);
		return Nothing;
	}

	Mu::ScriptInfo info{};
	info.path = path;
	info.name = get_name(path);

	std::string line;
	while (std::getline(file, line)) {

		if (line.find(prefix) != 0)
			continue;

		line = line.substr(prefix.length());

		if (info.oneline.empty())
			info.oneline = line;
		else
			info.description += line;
	}

	// std::cerr << "ONELINE: " << info.oneline << '\n';
	// std::cerr << "DESCR  : " << info.description << '\n';

	return info;
}



static void
script_infos_in_dir(const std::string& scriptdir, Mu::ScriptInfos& infos)
{
	DIR *dir = opendir(scriptdir.c_str());
	if (!dir) {
		mu_debug("failed to open '{}': {}", scriptdir,
			g_strerror(errno));
		return;
	}

	const std::string ext{".scm"};

	struct dirent *dentry;
	while ((dentry = readdir(dir))) {

		if (!g_str_has_suffix(dentry->d_name, ext.c_str()))
			continue;

		auto&& info = get_info(scriptdir + "/" + dentry->d_name, ";; INFO: ");
		if (!info)
			continue;

		infos.emplace_back(std::move(*info));
	}

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


Mu::ScriptInfos
Mu::script_infos(const Mu::ScriptPaths& paths)
{
	/* create a list of names, paths */
	ScriptInfos infos;
	for (auto&& dir: paths) {
		script_infos_in_dir(dir, infos);
	}

	std::sort(infos.begin(), infos.end(), [](auto&& i1, auto&& i2) {
		return i1.name < i2.name;
	});

	return infos;
}

Result<void>
Mu::run_script(const std::string& path,
	       const std::vector<std::string>& args)
{
#ifndef BUILD_GUILE
	return Err(Error::Code::Script,
		   "guile script support is not available");
#else
	std::string mainargs;
	for (auto&& arg: args)
		mainargs += mu_format("{}\"{}\"", mainargs.empty() ? "" : " ", arg);
	auto expr = mu_format("(main '(\"{}\" {}))", get_name(path), mainargs);

	std::vector<const char*> argv = {
		GUILE_BINARY,
		"-l", path.c_str(),
		"-c", expr.c_str(),
	};

	/* does not return */
	scm_boot_guile(argv.size(), const_cast<char**>(argv.data()),
		       [](void *closure, int argc, char **argv) {
			       scm_shell(argc, argv);
		       }, NULL);

	return Ok();
#endif /*BUILD_GUILE*/
}