/* 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 /*** 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; }