summaryrefslogtreecommitdiff
path: root/kon/konrmplugin.c
blob: 175afc1e68d4401c2c5b6f3038a8e15965858a77 (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
/* SPDX-License-Identifier: MIT */
/*
 * konrmplugin.c
 *
 * Copyright (c) 2026 KonAki
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

#include "konrmplugin.h"

#include <string.h>

/*** table ***/

void kon_rmPluginsInit(kon_rmPlugins_t *plugins, const kon_rmPluginEntry_t *entries, int count, void *user)
{
	if (!plugins) return;
	memset(plugins, 0, sizeof *plugins);
	plugins->entries = entries;
	plugins->count = entries && count > 0 ? count : 0;
	plugins->user = user;
}

static void miss(kon_rmPlugins_t *plugins, const char *name)
{
	int i;

	plugins->missedTotal++;
	for (i = 0; i < plugins->missedCount; i++) {
		if (!strcmp(plugins->missed[i].name, name)) {
			plugins->missed[i].count++;
			return;
		}
	}
	if (plugins->missedCount >= KON_RM_PLUGIN_MISSED) return;
	strncpy(plugins->missed[plugins->missedCount].name, name, sizeof plugins->missed[0].name - 1);
	plugins->missed[plugins->missedCount].count = 1;
	plugins->missedCount++;
}

/*** commands ***/

int kon_rmPluginHost(kon_rmPlugins_t *plugins, int code, const char *text)
{
	char buffer[1024];
	const char *argv[KON_RM_PLUGIN_ARGS];
	char sep = code == 357 ? '\t' : ' ';
	int argc = 0, i;
	char *s;

	if (!plugins || (code != 356 && code != 357)) return -1;
	if (!text) text = "";
	strncpy(buffer, text, sizeof buffer - 1);
	buffer[sizeof buffer - 1] = '\0';

	/* MZ gives the plugin and then the command, they become plugin.command. the rest is split in place */
	if (code == 357 && (s = strchr(buffer, '\t')) != NULL) *s = '.';
	s = buffer;
	while (argc < KON_RM_PLUGIN_ARGS) {
		argv[argc++] = s;
		while (*s && *s != sep) s++;
		if (!*s) break;
		*s++ = '\0';
	}
	for (i = 0; i < plugins->count; i++) {
		const kon_rmPluginEntry_t *e = &plugins->entries[i];

		if (!e->name || strcmp(e->name, argv[0])) continue;
		return e->fn ? e->fn(argc, argv, plugins->user) : 0;
	}
	miss(plugins, argv[0]);
	return 0;
}

int kon_rmPluginMissed(const kon_rmPlugins_t *plugins, int index, const char **name, int *count)
{
	if (!plugins || index < 0 || index >= plugins->missedCount) return 0;
	if (name) *name = plugins->missed[index].name;
	if (count) *count = plugins->missed[index].count;
	return 1;
}