LICENSE at the root; the 103 files new in ext/src/mcde (none was in the CDE import) carry the CNCL line instead of the LGPL notice. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
191 lines
4.2 KiB
C
191 lines
4.2 KiB
C
/*
|
|
* Profiles.c -- MCDE: terminal profiles for dtterm
|
|
*
|
|
* License: CNCL 1.0 (Could Not Care Less), see LICENSE at the top of the tree.
|
|
*
|
|
* A profile names a font, colours and a command; a new tab can be opened
|
|
* with one (Window > New Tab with Profile, dtterm -profile NAME). This file
|
|
* only reads them -- C99 and POSIX, no X (Profiles.h).
|
|
*/
|
|
|
|
#include <ctype.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
|
|
#include "Profiles.h"
|
|
|
|
static char *
|
|
Trim(char *s)
|
|
{
|
|
char *e;
|
|
|
|
while (isspace((unsigned char) *s))
|
|
s++;
|
|
e = s + strlen(s);
|
|
while (e > s && isspace((unsigned char) e[-1]))
|
|
*--e = '\0';
|
|
return s;
|
|
}
|
|
|
|
static int
|
|
Copy(char *dst, size_t len, const char *src)
|
|
{
|
|
if (strlen(src) >= len)
|
|
return 0;
|
|
strcpy(dst, src);
|
|
return 1;
|
|
}
|
|
|
|
/* "Hack 12", "DejaVu Sans Mono 11", "Hack": family and size */
|
|
static int
|
|
ParseFont(McdeProfile *p, char *v)
|
|
{
|
|
char *sp = strrchr(v, ' '), *end;
|
|
long size = 0;
|
|
|
|
if (sp != NULL) {
|
|
size = strtol(sp + 1, &end, 10);
|
|
if (*end == '\0' && end != sp + 1) {
|
|
if (size < 4 || size > 96)
|
|
return 0;
|
|
*sp = '\0';
|
|
v = Trim(v);
|
|
} else
|
|
size = 0;
|
|
}
|
|
if (!Copy(p->font, sizeof p->font, v))
|
|
return 0;
|
|
p->fontSize = (int) size;
|
|
return 1;
|
|
}
|
|
|
|
int
|
|
McdeProfilesParse(FILE *fp, McdeProfile *out, int max, char *err,
|
|
size_t errlen)
|
|
{
|
|
char line[1024], *s, *key, *val;
|
|
int n = 0, lineno = 0, ok;
|
|
McdeProfile *cur = NULL;
|
|
|
|
while (fgets(line, sizeof line, fp) != NULL) {
|
|
lineno++;
|
|
if (strchr(line, '\n') == NULL && !feof(fp)) {
|
|
snprintf(err, errlen, "%d: line too long", lineno);
|
|
return -1;
|
|
}
|
|
s = Trim(line);
|
|
if (*s == '\0' || *s == '#')
|
|
continue;
|
|
key = s;
|
|
while (*s != '\0' && !isspace((unsigned char) *s))
|
|
s++;
|
|
if (*s != '\0')
|
|
*s++ = '\0';
|
|
val = Trim(s);
|
|
|
|
if (strcmp(key, "profile") == 0) {
|
|
if (*val == '\0') {
|
|
snprintf(err, errlen, "%d: profile without a name",
|
|
lineno);
|
|
return -1;
|
|
}
|
|
if (McdeProfileFind(out, n, val) != NULL) {
|
|
snprintf(err, errlen, "%d: profile \"%s\" twice",
|
|
lineno, val);
|
|
return -1;
|
|
}
|
|
if (n >= max) {
|
|
snprintf(err, errlen, "%d: more than %d profiles",
|
|
lineno, max);
|
|
return -1;
|
|
}
|
|
cur = &out[n++];
|
|
memset(cur, 0, sizeof *cur);
|
|
if (!Copy(cur->name, sizeof cur->name, val)) {
|
|
snprintf(err, errlen, "%d: name too long", lineno);
|
|
return -1;
|
|
}
|
|
continue;
|
|
}
|
|
if (cur == NULL) {
|
|
snprintf(err, errlen, "%d: \"%s\" before the first profile",
|
|
lineno, key);
|
|
return -1;
|
|
}
|
|
if (strcmp(key, "font") == 0)
|
|
ok = ParseFont(cur, val);
|
|
else if (strcmp(key, "foreground") == 0)
|
|
ok = Copy(cur->foreground, sizeof cur->foreground, val);
|
|
else if (strcmp(key, "background") == 0)
|
|
ok = Copy(cur->background, sizeof cur->background, val);
|
|
else if (strcmp(key, "command") == 0)
|
|
ok = Copy(cur->command, sizeof cur->command, val);
|
|
else {
|
|
snprintf(err, errlen, "%d: unknown key \"%s\"", lineno, key);
|
|
return -1;
|
|
}
|
|
if (!ok) {
|
|
snprintf(err, errlen, "%d: bad value for \"%s\"", lineno, key);
|
|
return -1;
|
|
}
|
|
}
|
|
return n;
|
|
}
|
|
|
|
int
|
|
McdeProfilesLoad(const char *home, const char *const *sysfiles,
|
|
McdeProfile *out, int max)
|
|
{
|
|
char path[1024], err[256];
|
|
const char *name = NULL;
|
|
FILE *fp = NULL;
|
|
int n;
|
|
|
|
if (home != NULL && *home != '\0' &&
|
|
(size_t) snprintf(path, sizeof path, "%s/.dt/dtterm.profiles",
|
|
home) < sizeof path && (fp = fopen(path, "r")) != NULL)
|
|
name = path;
|
|
for (; fp == NULL && sysfiles != NULL && *sysfiles != NULL; sysfiles++)
|
|
if ((fp = fopen(*sysfiles, "r")) != NULL)
|
|
name = *sysfiles;
|
|
if (fp == NULL)
|
|
return 0;
|
|
n = McdeProfilesParse(fp, out, max, err, sizeof err);
|
|
fclose(fp);
|
|
if (n < 0) {
|
|
fprintf(stderr, "dtterm: %s:%s\n", name, err);
|
|
return 0;
|
|
}
|
|
return n;
|
|
}
|
|
|
|
const McdeProfile *
|
|
McdeProfileFind(const McdeProfile *p, int n, const char *name)
|
|
{
|
|
int i;
|
|
|
|
if (name == NULL)
|
|
return NULL;
|
|
for (i = 0; i < n; i++)
|
|
if (strcmp(p[i].name, name) == 0)
|
|
return &p[i];
|
|
return NULL;
|
|
}
|
|
|
|
int
|
|
McdeProfileWidgetName(const char *name, char *buf, size_t len)
|
|
{
|
|
static const char prefix[] = "profile_";
|
|
size_t i, n = strlen(prefix);
|
|
|
|
if (n + strlen(name) + 1 > len)
|
|
return 0;
|
|
memcpy(buf, prefix, n);
|
|
for (i = 0; name[i] != '\0'; i++)
|
|
buf[n + i] = (isalnum((unsigned char) name[i]) ||
|
|
name[i] == '_') ? name[i] : '_';
|
|
buf[n + i] = '\0';
|
|
return 1;
|
|
}
|