Files
lotherkandClaude Sonnet 5.5 048e842776 MCDE dtlaunch: the panes of System Settings are results; dtsystemsettings -list-panes (t0185, first part)
dtsystemsettings -list-panes prints the panes (ID, name, category, keywords; a
PANE_LATER pane is left out) before any X setup.  dtlaunch asks for them once
(pipe and exec, no shell, a 3 s poll limit; no waitpid, SIGCHLD is ignored
there) and model.c parses and matches them: the name at full score, a keyword
at half, the ID checked because it ends up in dtsystemsettings -pane ID.  A
result reads '[settings] Displays (Hardware)' and starts through the same
Parental Controls as a program (run_args).  dtlaunch -text STRING fills the
field of the first opening, for tests and tools/mcde-shot.sh.

Measured: make mcde-linux 'check programs/dtlaunch programs/dtsystemsettings'
exit=0, test_model PASS (test_panes: parse incl. unsafe IDs, matching incl. a
German keyword); -list-panes: 24 lines, exit 0 with DISPLAY unset, no dock;
Spotlight-mcde.png: typing 'disp' lists '[settings] Displays (Hardware)'
second; Return on 'displays' starts 'dtsystemsettings -pane displays' and a
window 'Displays' appears (Xvfb).  Not done: the grouped two-column list,
documents/mail/PDF/web results, the dtbar magnifier, the clipboard without the
1 s poll (t0185, t0080).

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-14 22:28:06 +00:00

461 lines
8.5 KiB
C

/*
* model.c -- dtlaunch without X: matching, the calculator, snippets, programs
*
* License: CNCL 1.0 (Could Not Care Less), see LICENSE at the top of the tree.
*/
#include <ctype.h>
#include <dirent.h>
#include <errno.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <unistd.h>
#include "model.h"
/* ---- matching ---------------------------------------------------------- */
int
mx_match(const char *query, const char *text)
{
const char *t = text;
int score = 0, run = 0, first = 1;
if (*query == '\0')
return 1;
for (; *query != '\0'; query++) {
int q = tolower((unsigned char)*query);
int adjacent = 1;
if (q == ' ')
continue;
while (*t != '\0' && tolower((unsigned char)*t) != q) {
t++;
adjacent = 0;
}
if (*t == '\0')
return 0;
if (first && t == text)
score += 20; /* the very start */
else if (t > text && !isalnum((unsigned char)t[-1]))
score += 8; /* the start of a word */
run = adjacent && !first ? run + 1 : 0;
score += 1 + 3 * run; /* letters in a row */
first = 0;
t++;
}
/* a shorter text is the better match for the same letters */
score += 10 - (int)(strlen(text) > 10 ? 10 : strlen(text));
return score > 0 ? score : 1;
}
/* ---- the calculator: recursive descent --------------------------------- */
typedef struct {
const char *p;
int error;
} Calc;
static double expr(Calc *c);
static void
skip(Calc *c)
{
while (*c->p == ' ' || *c->p == '\t')
c->p++;
}
static double
primary(Calc *c)
{
double v;
char *end;
skip(c);
if (*c->p == '(') {
c->p++;
v = expr(c);
skip(c);
if (*c->p != ')')
c->error = 1;
else
c->p++;
return v;
}
if (*c->p == '-') {
c->p++;
return -primary(c);
}
if (*c->p == '+') {
c->p++;
return primary(c);
}
if (!isdigit((unsigned char)*c->p) && *c->p != '.') {
c->error = 1;
return 0;
}
errno = 0;
v = strtod(c->p, &end);
if (end == c->p || errno != 0)
c->error = 1;
c->p = end;
return v;
}
static double
power(Calc *c)
{
double b = primary(c);
skip(c);
if (*c->p == '^') {
c->p++;
return pow(b, power(c)); /* right associative */
}
return b;
}
static double
term(Calc *c)
{
double v = power(c), r;
for (;;) {
skip(c);
if (*c->p == '*' || *c->p == '/' || *c->p == '%') {
char op = *c->p++;
r = power(c);
if (op == '*')
v *= r;
else if (r == 0)
c->error = 1;
else
v = op == '/' ? v / r : fmod(v, r);
} else
return v;
}
}
static double
expr(Calc *c)
{
double v = term(c);
for (;;) {
skip(c);
if (*c->p == '+') {
c->p++;
v += term(c);
} else if (*c->p == '-') {
c->p++;
v -= term(c);
} else
return v;
}
}
int
mx_calc(const char *s, double *out)
{
Calc c;
double v;
int ops = 0;
const char *q;
while (*s == ' ')
s++;
if (*s == '=')
s++;
else {
/* without '=', only text that is clearly a calculation counts:
* digits and at least one operator, nothing else */
for (q = s; *q != '\0'; q++) {
if (strchr("+-*/%^", *q) != NULL && q != s)
ops++;
else if (!isdigit((unsigned char)*q) &&
strchr(" .()+-", *q) == NULL)
return -1;
}
if (ops == 0)
return -1;
}
c.p = s;
c.error = 0;
v = expr(&c);
skip(&c);
if (c.error || *c.p != '\0' || isnan(v) || isinf(v))
return -1;
*out = v;
return 0;
}
void
mx_calc_format(double v, char *buf, size_t len)
{
char *p;
(void)snprintf(buf, len, "%.10f", v);
if (strchr(buf, '.') == NULL)
return;
p = buf + strlen(buf) - 1;
while (p > buf && *p == '0')
*p-- = '\0';
if (*p == '.')
*p = '\0';
if (strcmp(buf, "-0") == 0)
(void)snprintf(buf, len, "0");
}
/* ---- snippets ---------------------------------------------------------- */
char *
mx_unescape(const char *s)
{
char *r = malloc(strlen(s) + 1), *d = r;
if (r == NULL)
return NULL;
for (; *s != '\0'; s++) {
if (*s == '\\' && s[1] != '\0') {
s++;
*d++ = *s == 'n' ? '\n' : *s == 't' ? '\t' : *s;
} else
*d++ = *s;
}
*d = '\0';
return r;
}
int
mx_snippets_load(const char *path, MxSnippet **out, size_t *n)
{
FILE *f;
char line[4096], *tab;
MxSnippet *v = NULL, *nv;
size_t cnt = 0;
*out = NULL;
*n = 0;
if ((f = fopen(path, "r")) == NULL)
return errno == ENOENT ? 0 : -1;
while (fgets(line, sizeof line, f) != NULL) {
line[strcspn(line, "\r\n")] = '\0';
if (line[0] == '#' || (tab = strchr(line, '\t')) == NULL ||
tab == line)
continue;
*tab = '\0';
if ((nv = realloc(v, (cnt + 1) * sizeof *v)) == NULL)
break;
v = nv;
v[cnt].key = strdup(line);
v[cnt].text = mx_unescape(tab + 1);
if (v[cnt].key == NULL || v[cnt].text == NULL) {
free(v[cnt].key);
free(v[cnt].text);
break;
}
cnt++;
}
(void)fclose(f);
*out = v;
*n = cnt;
return 0;
}
void
mx_snippets_free(MxSnippet *s, size_t n)
{
size_t i;
for (i = 0; i < n; i++) {
free(s[i].key);
free(s[i].text);
}
free(s);
}
/* ---- programs in $PATH ------------------------------------------------- */
static int
cmpstr(const void *a, const void *b)
{
return strcmp(*(char *const *)a, *(char *const *)b);
}
int
mx_path_programs(const char *path, char ***out, size_t *n)
{
char *copy, *dir, *save = NULL, file[4096];
char **v = NULL, **nv;
size_t cnt = 0, cap = 0, i, j;
struct dirent *de;
struct stat st;
DIR *d;
*out = NULL;
*n = 0;
if (path == NULL || (copy = strdup(path)) == NULL)
return 0;
for (dir = strtok_r(copy, ":", &save); dir != NULL;
dir = strtok_r(NULL, ":", &save)) {
if ((d = opendir(dir)) == NULL)
continue;
while ((de = readdir(d)) != NULL) {
if (de->d_name[0] == '.')
continue;
(void)snprintf(file, sizeof file, "%s/%s", dir,
de->d_name);
if (stat(file, &st) != 0 || !S_ISREG(st.st_mode) ||
access(file, X_OK) != 0)
continue;
if (cnt == cap) {
cap = cap ? cap * 2 : 256;
if ((nv = realloc(v, cap * sizeof *v)) == NULL)
break;
v = nv;
}
if ((v[cnt] = strdup(de->d_name)) != NULL)
cnt++;
}
(void)closedir(d);
}
free(copy);
if (cnt > 1)
qsort(v, cnt, sizeof *v, cmpstr);
/* duplicates: the first one in $PATH wins, the name is the same */
for (i = j = 0; i < cnt; i++) {
if (j > 0 && strcmp(v[i], v[j - 1]) == 0)
free(v[i]);
else
v[j++] = v[i];
}
*out = v;
*n = j;
return 0;
}
void
mx_strv_free(char **v, size_t n)
{
size_t i;
for (i = 0; i < n; i++)
free(v[i]);
free(v);
}
/* ---- the panes of System Settings -------------------------------------- */
static int
pane_id_ok(const char *id)
{
size_t i, len = strlen(id);
if (len == 0 || len > 40)
return 0;
for (i = 0; i < len; i++)
if (!isalnum((unsigned char)id[i]) && id[i] != '-' &&
id[i] != '_')
return 0;
return 1;
}
int
mx_panes_parse(const char *text, MxPane **out, size_t *n)
{
char *copy, *line, *next;
MxPane *v = NULL, *nv;
size_t cnt = 0;
*out = NULL;
*n = 0;
if (text == NULL || (copy = strdup(text)) == NULL)
return text == NULL ? 0 : -1;
for (line = copy; line != NULL && *line != '\0'; line = next) {
char *f[4] = { NULL, NULL, NULL, NULL };
int nf, k;
if ((next = strchr(line, '\n')) != NULL)
*next++ = '\0';
/* the fields: ID, name, category, keywords */
for (nf = 0, f[0] = line; nf < 3; nf++) {
char *tab = strchr(f[nf], '\t');
if (tab == NULL)
break;
*tab = '\0';
f[nf + 1] = tab + 1;
}
if (nf < 2)
continue; /* fewer than three fields */
k = nf == 3 ? 1 : 0;
if (!pane_id_ok(f[0]) || f[1][0] == '\0' || f[2][0] == '\0')
continue;
if ((nv = realloc(v, (cnt + 1) * sizeof *v)) == NULL)
break;
v = nv;
v[cnt].id = strdup(f[0]);
v[cnt].name = strdup(f[1]);
v[cnt].category = strdup(f[2]);
v[cnt].keywords = strdup(k ? f[3] : "");
if (v[cnt].id == NULL || v[cnt].name == NULL ||
v[cnt].category == NULL || v[cnt].keywords == NULL) {
free(v[cnt].id);
free(v[cnt].name);
free(v[cnt].category);
free(v[cnt].keywords);
break;
}
cnt++;
}
free(copy);
*out = v;
*n = cnt;
return 0;
}
void
mx_panes_free(MxPane *p, size_t n)
{
size_t i;
for (i = 0; i < n; i++) {
free(p[i].id);
free(p[i].name);
free(p[i].category);
free(p[i].keywords);
}
free(p);
}
int
mx_pane_match(const char *query, const MxPane *p)
{
int best = mx_match(query, p->name);
char *kw, *w, *save;
if (*query == '\0' || p->keywords[0] == '\0')
return best;
if ((kw = strdup(p->keywords)) == NULL)
return best;
for (w = kw; w != NULL && *w != '\0'; w = save) {
int s;
while (*w == ' ')
w++;
if (*w == '\0')
break;
if ((save = strchr(w, ' ')) != NULL)
*save++ = '\0';
s = mx_match(query, w) / 2;
if (s > best)
best = s;
}
free(kw);
return best;
}