Files
splinter/src/i18n.c
T
Konrad f3174c0296 splinter: kid mode, and Portuguese (Brazil)
Kid mode plugin: a parent approves every action that changes something, a
whitelist lets the harmless ones through, and the model is told to stay
age-appropriate on every request (not once at the start, where it gets buried).

The core grew one hook for it: a gate. A plugin's gate sees tool, class and
arguments before the operator prompt and before the authorization class, and
answers allow, deny with a reason, or no opinion.

  allowed  -> the call runs, the operator is not asked
  denied   -> the model gets 'denied: <reason>' and cannot retry around it
  silence  -> the normal rules apply

	A whitelist entry is '<tool> <glob>' in ~/.splinter/kids/allow:
	run ls*
	read *

	Parents are reached through SPLINTER_KIDS_APPROVE_CMD, which gets the request
	as its argument and answers with its exit status. No channel means no
	mutating work at all - it fails closed, like the operator prompt it replaces.
	A timeout is a no.

	Measured: 'run' was denied with 'ein Erwachsener hat nicht zugestimmt
	(abgelehnt)', the parent channel recorded the request, whitelisted 'ls'
	went through.

	Daily budget: SPLINTER_KIDS_DAILY_MINUTES, counted per step, stored per day in
	~/.splinter/kids/usage, reset at midnight. When it is gone the gate refuses
	every tool including reads.

	pt_BR: a full catalogue (51 keys, 3 languages). Language detection now keeps
	the region - pt_BR and pt are different catalogues and a Brazilian child
	should not be handed European Portuguese - and falls back to the bare
	language before English.
2026-11-08 15:56:20 +00:00

297 lines
5.9 KiB
C

/*
* splinter -- tiny message catalogues.
*
* Copyright (c) 2026 Konrad Lother
* SPDX-License-Identifier: BSD-3-Clause
*/
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "splinter.h"
#include "i18n.h"
#define MAX_KEYS 512
#define MAX_DIRS 8
struct entry {
char key[96];
char text[1024];
};
static struct entry main_cat[MAX_KEYS];
static size_t main_n;
static struct entry fallback[MAX_KEYS];
static size_t fallback_n;
static char lang[16] = "en";
static char dirs[MAX_DIRS][256];
static size_t ndirs;
void
sp_i18n_add_dir(const char *dir)
{
if (dir == NULL || ndirs == MAX_DIRS)
return;
snprintf(dirs[ndirs++], sizeof(dirs[0]), "%s", dir);
}
const char *
sp_i18n_lang(void)
{
return lang;
}
static char *
trim(char *s)
{
char *end;
while (*s == ' ' || *s == '\t')
s++;
end = s + strlen(s);
while (end > s && (end[-1] == '\n' || end[-1] == '\r' ||
end[-1] == ' ' || end[-1] == '\t'))
*--end = '\0';
return s;
}
/* Detect the language from the environment, ignoring the regional part. */
static void
detect_language(const char *override_lang)
{
const char *cand = override_lang;
if (cand == NULL || *cand == '\0')
cand = getenv("SPLINTER_LOCALE");
if (cand == NULL || *cand == '\0')
cand = getenv("LC_ALL");
if (cand == NULL || *cand == '\0')
cand = getenv("LC_MESSAGES");
if (cand == NULL || *cand == '\0')
cand = getenv("LANG");
memset(lang, 0, sizeof(lang));
if (cand == NULL || *cand == '\0') {
snprintf(lang, sizeof(lang), "en");
return;
}
{
/*
* Keep the region: pt_BR and pt are different catalogues and a
* Brazilian child should not be handed European Portuguese. The
* loader tries the full tag first and falls back to the language.
*/
size_t i = 0;
while (cand[i] != '\0' && cand[i] != '.' && cand[i] != '@' &&
i < sizeof(lang) - 1) {
char c = cand[i];
lang[i] = (c == '-') ? '_' : (char)tolower((unsigned char)c);
i++;
}
lang[i] = '\0';
if (i == 0 || (i == 1 && lang[0] == 'c'))
snprintf(lang, sizeof(lang), "en");
}
}
static size_t
parse_file(const char *path, struct entry *cat, size_t max)
{
FILE *f;
char line[1400];
size_t n = 0;
f = fopen(path, "r");
if (f == NULL)
return 0;
while (n < max && fgets(line, sizeof(line), f) != NULL) {
char *eq, *key, *val;
key = trim(line);
if (*key == '\0' || *key == '#')
continue;
eq = strchr(key, '=');
if (eq == NULL)
continue; /* a catalogue is data, not config:
* a bad line is skipped, not fatal */
*eq = '\0';
val = trim(eq + 1);
key = trim(key);
if (*key == '\0')
continue;
snprintf(cat[n].key, sizeof(cat[n].key), "%s", key);
snprintf(cat[n].text, sizeof(cat[n].text), "%s", val);
n++;
}
fclose(f);
return n;
}
int
sp_i18n_load(struct sp_buf *err, const char *want)
{
size_t i, tried = 0;
char path[512];
main_n = 0;
if (want == NULL || *want == '\0')
want = lang;
for (i = 0; i < ndirs; i++) {
snprintf(path, sizeof(path), "%s/%s.msg", dirs[i], want);
main_n = parse_file(path, main_cat, MAX_KEYS);
tried++;
if (main_n > 0)
break;
}
/* English is the fallback for every key, always loaded */
for (i = 0; i < ndirs; i++) {
if (strcmp(want, "en") == 0 && main_n > 0)
break;
snprintf(path, sizeof(path), "%s/en.msg", dirs[i]);
fallback_n = parse_file(path, fallback, MAX_KEYS);
if (fallback_n > 0)
break;
}
if (main_n == 0 && fallback_n == 0) {
if (err != NULL)
sp_buf_printf(err, "i18n: no catalogue for '%s' in %zu "
"directories", want, tried);
return -1;
}
snprintf(lang, sizeof(lang), "%s", want);
return 0;
}
static const char *
lookup(struct entry *cat, size_t n, const char *key)
{
size_t i;
for (i = 0; i < n; i++)
if (strcmp(cat[i].key, key) == 0)
return cat[i].text;
return NULL;
}
const char *
sp_t(const char *key)
{
const char *v;
if (key == NULL)
return "";
v = lookup(main_cat, main_n, key);
if (v != NULL)
return v;
v = lookup(fallback, fallback_n, key);
if (v != NULL)
return v;
return key; /* untranslated keys are visible */
}
size_t
sp_i18n_count(void)
{
return main_n;
}
const char *
sp_i18n_key(size_t index)
{
return index < main_n ? main_cat[index].key : NULL;
}
int
sp_i18n_fallback_count(void)
{
return (int)fallback_n;
}
const char *
sp_i18n_fallback_key(size_t index)
{
return index < fallback_n ? fallback[index].key : NULL;
}
const char *
sp_td(const char *key, const char *dflt)
{
const char *v;
if (key == NULL)
return dflt != NULL ? dflt : "";
v = lookup(main_cat, main_n, key);
if (v != NULL)
return v;
v = lookup(fallback, fallback_n, key);
if (v != NULL)
return v;
return dflt != NULL ? dflt : key;
}
const char *
sp_i18n_init(const char *override_lang)
{
struct sp_buf err;
detect_language(override_lang);
/*
* Where catalogues may live. The executable's own directory comes first,
* so `splinter` works from any working directory; then the explicit
* override, then the source tree, then an installed prefix.
*/
{
const char *env = getenv("SPLINTER_LOCALE_DIR");
if (env != NULL && *env != '\0')
sp_i18n_add_dir(env);
}
{
char exe[512];
ssize_t n = readlink("/proc/self/exe", exe, sizeof(exe) - 1);
if (n > 0) {
char dir[600];
char *slash;
exe[n] = '\0';
slash = strrchr(exe, '/');
if (slash != NULL) {
*slash = '\0';
snprintf(dir, sizeof(dir), "%s/share/locale", exe);
sp_i18n_add_dir(dir);
}
}
}
sp_i18n_add_dir("share/locale");
sp_i18n_add_dir("/usr/local/share/splinter/locale");
if (sp_buf_init(&err) != 0)
return lang;
/*
* pt_BR first, then pt: a region we have no catalogue for is not a reason
* to fall all the way back to English.
*/
if (sp_i18n_load(&err, lang) != 0 || main_n == 0) {
char base[16];
size_t i = 0;
while (lang[i] != '\0' && lang[i] != '_' && i < sizeof(base) - 1) {
base[i] = lang[i];
i++;
}
base[i] = '\0';
if (strcmp(base, lang) != 0)
(void)sp_i18n_load(&err, base);
}
sp_buf_free(&err);
return lang;
}