Files
lotherkandClaude Sonnet 5.5 d53ea094e7 licence: CNCL for what Makronix wrote itself
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>
2026-11-14 11:48:02 +00:00

865 lines
25 KiB
C

/*
* dtappscan -- MCDE: the applications of this computer, by category
*
* License: CNCL 1.0 (Could Not Care Less), see LICENSE at the top of the tree.
*
* CDE's application manager shows a fixed list (programs/localized/
* templates/appmgr.am): Firefox, Skype and GIMP are there whether they are
* installed or not. dtappscan shows what is there. It reads
*
* - the desktop entries of the installed packages (the freedesktop.org
* "Desktop Entry Specification": *.desktop in <dir>/applications of
* $XDG_DATA_HOME, $XDG_DATA_DIRS, /usr/pkg/share, /usr/local/share and
* /usr/share; docs/reference/desktop-entry.txt in the Makronix tree),
* - and a table of the desktop's own programs (MCDE and CDE),
*
* keeps a program only when its command is on the PATH, and writes the
* CDE way:
*
* ~/.dt/types/mcde-apps.dt one ACTION per desktop entry
* ~/.dt/icons/McdeApp_<id>.[lmt].pm its icon, from PNG (48, 32, 16)
* ~/.dt/Applications/<Category>/<Action>
* an executable file named as the action,
* which the file manager shows and runs as
* that action (CDE's "action files")
*
* The file manager's "Applications" and the front panel open
* ~/.dt/Applications. Run at login by Xsession.d/0077.dtappscan, before the
* session loads the action database; "dtappscan -v" says what it found.
* MCDE_APPSCAN_DIRS (dirs separated by ':') replaces the search for tests.
*/
#define _POSIX_C_SOURCE 200809L
#include <ctype.h>
#include <dirent.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <png.h>
#define MAX_APPS 512
#define LINE 4096
static int verbose;
static char home[1024];
/* the data directories the entries came from (<root>/applications); their
* icons are in <root>/icons and <root>/pixmaps */
static char roots[16][1024];
static int nroots;
/* ---- categories -------------------------------------------------- */
/* the folders, in the order of the freedesktop main categories that lead
* to them (the first category of an entry that is listed here wins) */
static const struct {
const char *category; /* freedesktop name */
const char *folder;
} categories[] = {
{ "Game", "Games" },
{ "Education", "Education" },
{ "Science", "Education" },
{ "Development", "Development" },
{ "Office", "Office" },
{ "Graphics", "Graphics" },
{ "AudioVideo", "Sound & Video" },
{ "Audio", "Sound & Video" },
{ "Video", "Sound & Video" },
{ "Network", "Internet" },
{ "Settings", "System" },
{ "System", "System" },
{ "Utility", "Accessories" },
};
#define NCATEGORIES (sizeof categories / sizeof categories[0])
#define OTHER "Other"
/* the desktop's own programs: action (types/*.dt), folder, the program that
* must be there. MCDE: "System" is for settings, "Utilities" for the
* system's tools (the reference's Utilities folder: Terminal, Disk Utility,
* ...); the X tools sit in Utilities/X11 and the command-line tools in
* Utilities/Command Line, so that the tools MCDE has are what one sees. */
static const struct {
const char *action, *folder, *program;
} builtin[] = {
{ "Dtcalc", "Accessories", "dtcalc" },
{ "Dtpad", "Accessories", "dtpad" },
{ "Dtcm", "Office", "dtcm" },
{ "Dtmail", "Internet", "dtmail" },
{ "McdeNetwork", "Internet", "dtfile" },
{ "McdeRemoteLogin", "Internet", "ssh" },
{ "Dticon", "Graphics", "dticon" },
{ "Dtappbuild", "Development", "dtbuilder" },
{ "Dtcreate", "Development", "dtcreate" },
/* the settings, and the desktop's own window */
{ "Dtfile", "System", "dtfile" },
{ "McdeSystemSettings", "System", "dtsystemsettings" },
{ "McdeSharing", "System", "dtsystemsettings" },
{ "Dtstyle", "System", "dtstyle" },
{ "DtPrintManager", "System", "dtprintinfo" },
{ "Dthelpview", "Help", "dthelpview" },
{ "Dtmanpageview", "Help", "dthelpview" },
{ "OpenDtIntro", "Help", "dthelpview" },
{ "DtLoadInfoLib", "Help", "dtinfo" },
/* the system's tools MCDE has: Terminal and Disk Utility, as the
* reference's Utilities folder has them */
{ "Dtterm", "Utilities", "dtterm" },
{ "McdeDiskUtility", "Utilities", "dtdiskutility" },
/* CDE's X tools (appmanager/C/Desktop_Tools) one level below */
{ "Xterm", "Utilities/X11", "xterm" },
{ "XclockDig", "Utilities/X11", "xclock" },
{ "Xload", "Utilities/X11", "xload" },
{ "Xwd", "Utilities/X11", "xwd" },
{ "Xwud", "Utilities/X11", "xwud" },
{ "Xdpyinfo", "Utilities/X11", "xdpyinfo" },
{ "Xlsfonts", "Utilities/X11", "xlsfonts" },
{ "Xfd", "Utilities/X11", "xfd" },
{ "Xprop", "Utilities/X11", "xprop" },
{ "Xwininfo", "Utilities/X11", "xwininfo" },
{ "Xclipboard", "Utilities/X11", "xclipboard" },
{ "Bitmap", "Utilities/X11", "bitmap" },
/* and the command-line tools in a folder of their own */
{ "Compress", "Utilities/Command Line", "compress" },
{ "Uncompress", "Utilities/Command Line", "uncompress" },
{ "Tar", "Utilities/Command Line", "tar" },
{ "TarList", "Utilities/Command Line", "tar" },
{ "TarUnpack", "Utilities/Command Line", "tar" },
{ "Df", "Utilities/Command Line", "df" },
{ "DuSort", "Utilities/Command Line", "du" },
{ "Grep", "Utilities/Command Line", "grep" },
{ "Diff", "Utilities/Command Line", "diff" },
{ "Wc", "Utilities/Command Line", "wc" },
{ "File", "Utilities/Command Line", "file" },
{ "Vi", "Utilities/Command Line", "vi" },
{ "ExecuteCmd", "Utilities/Command Line", "sh" },
{ "ReloadActions", "Utilities/Command Line", "dtaction" },
{ "ReloadResources", "Utilities/Command Line", "dtaction" },
{ "RestorePanel", "Utilities/Command Line", "dtaction" },
{ "Dttypes", "Utilities/Command Line", "dttypes" },
};
#define NBUILTIN (sizeof builtin / sizeof builtin[0])
/* ---- small things ------------------------------------------------ */
static void
Trim(char *s)
{
size_t n = strlen(s);
while (n > 0 && isspace((unsigned char) s[n - 1]))
s[--n] = '\0';
}
static int
IsExecutable(const char *path)
{
struct stat st;
return stat(path, &st) == 0 && S_ISREG(st.st_mode) &&
access(path, X_OK) == 0;
}
/* is PROG (a name or a path) a program of this computer */
static int
OnPath(const char *prog)
{
const char *path = getenv("PATH");
const char *extra = "/usr/pkg/bin:/usr/pkg/sbin:/usr/local/bin:"
"/usr/X11R7/bin:/usr/bin:/bin:/usr/sbin:/sbin";
char buf[2048], dirs[4096], *d, *last = NULL;
if (prog == NULL || prog[0] == '\0')
return 0;
if (strchr(prog, '/') != NULL)
return IsExecutable(prog);
snprintf(dirs, sizeof dirs, "%s:%s", path != NULL ? path : "", extra);
for (d = strtok_r(dirs, ":", &last); d != NULL;
d = strtok_r(NULL, ":", &last)) {
snprintf(buf, sizeof buf, "%s/%s", d, prog);
if (IsExecutable(buf))
return 1;
}
return 0;
}
static int
MakeDirs(const char *path)
{
char buf[2048], *p;
snprintf(buf, sizeof buf, "%s", path);
for (p = buf + 1; *p != '\0'; p++)
if (*p == '/') {
*p = '\0';
if (mkdir(buf, 0755) != 0 && errno != EEXIST)
return -1;
*p = '/';
}
return mkdir(buf, 0755) != 0 && errno != EEXIST ? -1 : 0;
}
/* remove the tree below DIR (our own ~/.dt/Applications) */
static void
RemoveTree(const char *dir)
{
DIR *d = opendir(dir);
struct dirent *e;
char buf[2048];
struct stat st;
if (d == NULL)
return;
while ((e = readdir(d)) != NULL) {
if (strcmp(e->d_name, ".") == 0 || strcmp(e->d_name, "..") == 0)
continue;
snprintf(buf, sizeof buf, "%s/%s", dir, e->d_name);
if (lstat(buf, &st) == 0 && S_ISDIR(st.st_mode))
RemoveTree(buf);
else
(void) unlink(buf);
}
closedir(d);
(void) rmdir(dir);
}
/* ---- desktop entries --------------------------------------------- */
typedef struct {
char id[128]; /* the file name without .desktop */
char name[256];
char comment[256];
char exec[1024];
char tryexec[512];
char icon[512];
char categories[512];
int terminal, hidden, takesFiles;
char action[160]; /* McdeApp_<id> */
const char *folder;
} App;
static App apps[MAX_APPS];
static int napps;
/* every desktop file id met, kept or not: the first directory that has an
* id decides, also when its entry is Hidden ("strictly equivalent to the
* .desktop file not existing at all", the specification's Hidden) */
static char seen[MAX_APPS * 2][128];
static int nseen;
/* the language's value wins over the plain one: Name[de_DE], Name[de],
* Name (Desktop Entry Specification, "Localized values for keys") */
static int
LangRank(const char *key, const char *base, const char *lang)
{
size_t n = strlen(base);
const char *b;
if (strncmp(key, base, n) != 0)
return -1;
if (key[n] == '\0')
return 0;
if (key[n] != '[' || lang == NULL || (b = strchr(key, ']')) == NULL)
return -1;
if (strncmp(key + n + 1, lang, (size_t) (b - key - n - 1)) != 0)
return -1;
return (size_t) (b - key - n - 1) == strcspn(lang, ".@") ? 2 : 1;
}
/* the first word of an Exec line, after "env NAME=VALUE ..." */
static void
Program(const char *exec, char *buf, size_t len)
{
const char *p = exec;
size_t n;
for (;;) {
while (*p == ' ')
p++;
if (*p == '"') {
const char *e = strchr(p + 1, '"');
n = e != NULL ? (size_t) (e - p - 1) : strlen(p + 1);
snprintf(buf, len, "%.*s", (int) n, p + 1);
} else {
n = strcspn(p, " ");
snprintf(buf, len, "%.*s", (int) n, p);
}
if (strcmp(buf, "env") == 0 || strchr(buf, '=') != NULL) {
p += n + (*p == '"' ? 2 : 0);
continue;
}
return;
}
}
/* Exec without its field codes (%f %F %u %U ... ; %% is a %); whether it
* takes files. A CDE action puts in $NAME itself, so a '$' is left out. */
static void
CleanExec(App *a)
{
char out[1024];
const char *p;
size_t o = 0;
a->takesFiles = 0;
for (p = a->exec; *p != '\0' && o + 2 < sizeof out; p++) {
if (*p == '%') {
p++;
if (*p == '%')
out[o++] = '%';
else if (strchr("fFuU", *p) != NULL && *p != '\0')
a->takesFiles = 1;
if (*p == '\0')
break;
continue;
}
if (*p == '$')
continue;
out[o++] = *p;
}
out[o] = '\0';
Trim(out);
snprintf(a->exec, sizeof a->exec, "%s", out);
}
static const char *
FolderOf(const char *cats)
{
size_t i;
char buf[512], *c, *last = NULL;
for (i = 0; i < NCATEGORIES; i++) {
snprintf(buf, sizeof buf, "%s", cats);
for (c = strtok_r(buf, ";", &last); c != NULL;
c = strtok_r(NULL, ";", &last))
if (strcmp(c, categories[i].category) == 0)
return categories[i].folder;
}
return OTHER;
}
/* the Desktop Entry group of PATH into A; 0 if it is no application of
* this desktop */
static int
ReadEntry(const char *path, const char *id, App *a)
{
FILE *f = fopen(path, "r");
char line[LINE], *eq;
int inGroup = 0, nameRank = -1, commentRank = -1, isApp = 0, r;
const char *lang = getenv("LC_ALL");
char prog[512];
size_t i;
if (lang == NULL || *lang == '\0')
lang = getenv("LC_MESSAGES");
if (lang == NULL || *lang == '\0')
lang = getenv("LANG");
if (f == NULL)
return 0;
memset(a, 0, sizeof *a);
snprintf(a->id, sizeof a->id, "%s", id);
while (fgets(line, sizeof line, f) != NULL) {
Trim(line);
if (line[0] == '[') {
inGroup = strcmp(line, "[Desktop Entry]") == 0;
continue;
}
if (!inGroup || line[0] == '#' || (eq = strchr(line, '=')) == NULL)
continue;
*eq++ = '\0';
Trim(line);
while (*eq == ' ')
eq++;
if (strcmp(line, "Type") == 0)
isApp = strcmp(eq, "Application") == 0;
else if ((r = LangRank(line, "Name", lang)) > nameRank) {
nameRank = r;
snprintf(a->name, sizeof a->name, "%s", eq);
} else if ((r = LangRank(line, "Comment", lang)) > commentRank) {
commentRank = r;
snprintf(a->comment, sizeof a->comment, "%s", eq);
} else if (strcmp(line, "Exec") == 0)
snprintf(a->exec, sizeof a->exec, "%s", eq);
else if (strcmp(line, "TryExec") == 0)
snprintf(a->tryexec, sizeof a->tryexec, "%s", eq);
else if (strcmp(line, "Icon") == 0)
snprintf(a->icon, sizeof a->icon, "%s", eq);
else if (strcmp(line, "Categories") == 0)
snprintf(a->categories, sizeof a->categories, "%s", eq);
else if (strcmp(line, "Terminal") == 0)
a->terminal = strcmp(eq, "true") == 0;
else if ((strcmp(line, "NoDisplay") == 0 ||
strcmp(line, "Hidden") == 0) && strcmp(eq, "true") == 0)
a->hidden = 1;
else if (strcmp(line, "OnlyShowIn") == 0 &&
strstr(eq, "MCDE") == NULL)
a->hidden = 1;
else if (strcmp(line, "NotShowIn") == 0 &&
strstr(eq, "MCDE") != NULL)
a->hidden = 1;
}
fclose(f);
if (!isApp || a->hidden || a->name[0] == '\0' || a->exec[0] == '\0')
return 0;
Program(a->exec, prog, sizeof prog);
if (!OnPath(a->tryexec[0] != '\0' ? a->tryexec : prog)) {
if (verbose)
fprintf(stderr, "dtappscan: %s: %s is not there\n", id,
a->tryexec[0] != '\0' ? a->tryexec : prog);
return 0;
}
CleanExec(a);
a->folder = FolderOf(a->categories);
/* an action name: letters, digits and _ (CDE's database names) */
snprintf(a->action, sizeof a->action, "McdeApp_");
for (i = 0; id[i] != '\0' && strlen(a->action) + 2 < sizeof a->action;
i++) {
size_t n = strlen(a->action);
a->action[n] = isalnum((unsigned char) id[i]) ? id[i] : '_';
a->action[n + 1] = '\0';
}
return 1;
}
static void
ScanDir(const char *dir)
{
DIR *d = opendir(dir);
struct dirent *e;
char path[2048], id[128];
int i;
if (d == NULL)
return;
while ((e = readdir(d)) != NULL && napps < MAX_APPS) {
size_t n = strlen(e->d_name);
if (n < 9 || strcmp(e->d_name + n - 8, ".desktop") != 0)
continue;
snprintf(id, sizeof id, "%.*s", (int) (n - 8), e->d_name);
/* the first directory that has an id wins ("the first one in
* the $XDG_DATA_DIRS precedence order is used") */
for (i = 0; i < nseen; i++)
if (strcmp(seen[i], id) == 0)
break;
if (i < nseen)
continue;
if (nseen < MAX_APPS * 2)
snprintf(seen[nseen++], sizeof seen[0], "%s", id);
snprintf(path, sizeof path, "%s/%s", dir, e->d_name);
if (ReadEntry(path, id, &apps[napps]))
napps++;
}
closedir(d);
}
/* ---- icons: PNG to XPM ------------------------------------------- */
typedef struct {
int w, h;
unsigned char *rgba;
} Image;
static int
LoadPng(const char *path, Image *img)
{
FILE *f = fopen(path, "rb");
png_structp png;
png_infop info;
png_bytep *rows = NULL;
int y;
img->rgba = NULL;
if (f == NULL)
return -1;
png = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
info = png != NULL ? png_create_info_struct(png) : NULL;
if (info == NULL || setjmp(png_jmpbuf(png))) {
png_destroy_read_struct(&png, &info, NULL);
free(rows);
free(img->rgba);
img->rgba = NULL;
fclose(f);
return -1;
}
png_init_io(png, f);
png_read_info(png, info);
/* everything to 8-bit RGBA (libpng manual, "Transformations") */
png_set_expand(png);
png_set_strip_16(png);
png_set_gray_to_rgb(png);
png_set_add_alpha(png, 0xff, PNG_FILLER_AFTER);
(void) png_set_interlace_handling(png);
png_read_update_info(png, info);
img->w = (int) png_get_image_width(png, info);
img->h = (int) png_get_image_height(png, info);
if (img->w <= 0 || img->h <= 0 || img->w > 4096 || img->h > 4096)
longjmp(png_jmpbuf(png), 1);
img->rgba = malloc((size_t) img->w * img->h * 4);
rows = malloc(sizeof *rows * (size_t) img->h);
if (img->rgba == NULL || rows == NULL)
longjmp(png_jmpbuf(png), 1);
for (y = 0; y < img->h; y++)
rows[y] = img->rgba + (size_t) y * img->w * 4;
png_read_image(png, rows);
png_destroy_read_struct(&png, &info, NULL);
free(rows);
fclose(f);
return 0;
}
/* SRC scaled into a SIZE x SIZE square, kept in proportion, by averaging
* the source pixels each target pixel covers (alpha-weighted colour) */
static void
Scale(const Image *src, int size, unsigned char *dst)
{
int s = src->w > src->h ? src->w : src->h;
int ox = (s - src->w) / 2, oy = (s - src->h) / 2;
int tx, ty, x, y;
for (ty = 0; ty < size; ty++)
for (tx = 0; tx < size; tx++) {
int x0 = tx * s / size, x1 = (tx + 1) * s / size;
int y0 = ty * s / size, y1 = (ty + 1) * s / size;
unsigned long r = 0, g = 0, b = 0, a = 0, n = 0;
unsigned char *o = dst + ((size_t) ty * size + tx) * 4;
if (x1 <= x0)
x1 = x0 + 1;
if (y1 <= y0)
y1 = y0 + 1;
for (y = y0; y < y1; y++)
for (x = x0; x < x1; x++) {
int sx = x - ox, sy = y - oy;
const unsigned char *p;
n++;
if (sx < 0 || sy < 0 || sx >= src->w ||
sy >= src->h)
continue;
p = src->rgba + ((size_t) sy * src->w +
sx) * 4;
r += (unsigned long) p[0] * p[3];
g += (unsigned long) p[1] * p[3];
b += (unsigned long) p[2] * p[3];
a += p[3];
}
o[0] = a ? (unsigned char) (r / a) : 0;
o[1] = a ? (unsigned char) (g / a) : 0;
o[2] = a ? (unsigned char) (b / a) : 0;
o[3] = (unsigned char) (a / n);
}
}
/* an XPM of SIZE x SIZE from RGBA: colours to 4 bits a channel (4096 at
* most, two characters a pixel), alpha below one half is transparent */
static int
WriteXpm(const char *path, const char *name, const unsigned char *px,
int size)
{
static const char chars[] =
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789#+";
static short slot[4096];
static unsigned short used[4096];
int nused = 0, i, x, y;
FILE *f;
char tmp[2048];
for (i = 0; i < 4096; i++)
slot[i] = -1;
for (i = 0; i < size * size; i++) {
const unsigned char *p = px + (size_t) i * 4;
int c = ((p[0] >> 4) << 8) | ((p[1] >> 4) << 4) | (p[2] >> 4);
if (p[3] >= 128 && slot[c] < 0) {
slot[c] = (short) (nused + 1);
used[nused++] = (unsigned short) c;
}
}
snprintf(tmp, sizeof tmp, "%s.new", path);
if ((f = fopen(tmp, "w")) == NULL)
return -1;
fprintf(f, "/* XPM */\n/* MCDE: made by dtappscan */\n"
"static char *%s[] = {\n\"%d %d %d 2\",\n\" c None\",\n", name,
size, size, nused + 1);
for (i = 0; i < nused; i++) {
int c = used[i];
fprintf(f, "\"%c%c c #%02x%02x%02x\",\n", chars[(i + 1) / 64],
chars[(i + 1) % 64], ((c >> 8) & 15) * 17,
((c >> 4) & 15) * 17, (c & 15) * 17);
}
for (y = 0; y < size; y++) {
fputc('"', f);
for (x = 0; x < size; x++) {
const unsigned char *p = px + ((size_t) y * size + x) * 4;
int c = ((p[0] >> 4) << 8) | ((p[1] >> 4) << 4) |
(p[2] >> 4);
if (p[3] < 128)
fputs(" ", f);
else
fprintf(f, "%c%c", chars[slot[c] / 64],
chars[slot[c] % 64]);
}
fprintf(f, "\"%s\n", y + 1 < size ? "," : "};");
}
if (fclose(f) != 0)
return -1;
return rename(tmp, path);
}
/* the file of icon NAME: a path, or a name looked up in the hicolor theme
* (biggest first) and in pixmaps */
static int
FindIcon(const char *name, char *buf, size_t len)
{
static const char *const sizes[] = { "48x48", "64x64", "128x128",
"256x256", "96x96", "72x72", "32x32", "24x24", "22x22", "16x16",
NULL };
int r, s;
if (name[0] == '/') {
snprintf(buf, len, "%s", name);
return access(buf, R_OK) == 0;
}
for (r = 0; r < nroots; r++)
for (s = 0; sizes[s] != NULL; s++) {
snprintf(buf, len, "%s/icons/hicolor/%s/apps/%s.png",
roots[r], sizes[s], name);
if (access(buf, R_OK) == 0)
return 1;
}
for (r = 0; r < nroots; r++) {
snprintf(buf, len, "%s/pixmaps/%s.png", roots[r], name);
if (access(buf, R_OK) == 0)
return 1;
snprintf(buf, len, "%s/pixmaps/%s.xpm", roots[r], name);
if (access(buf, R_OK) == 0)
return 1;
}
return 0;
}
/* ~/.dt/icons/McdeApp_<id>.[lmt].pm for A, if its icon is found; made
* again only when the picture is newer. 0 when there is none. */
static int
MakeIcon(const App *a)
{
static const struct { int size; char suffix; } sizes[] = {
{ 48, 'l' }, { 32, 'm' }, { 16, 't' } };
char src[2048], dst[2048], name[200];
struct stat ss, ds;
Image img;
unsigned char px[48 * 48 * 4];
size_t i;
if (a->icon[0] == '\0' || !FindIcon(a->icon, src, sizeof src))
return 0;
snprintf(dst, sizeof dst, "%s/.dt/icons/%s.l.pm", home, a->action);
if (stat(src, &ss) == 0 && stat(dst, &ds) == 0 &&
ds.st_mtime >= ss.st_mtime)
return 1;
if (strlen(src) > 4 && strcmp(src + strlen(src) - 4, ".xpm") == 0) {
/* an XPM is used as it is, for every size */
for (i = 0; i < 3; i++) {
FILE *in, *out;
char b[8192];
size_t n;
snprintf(dst, sizeof dst, "%s/.dt/icons/%s.%c.pm", home,
a->action, sizes[i].suffix);
if ((in = fopen(src, "r")) == NULL)
return 0;
if ((out = fopen(dst, "w")) == NULL) {
fclose(in);
return 0;
}
while ((n = fread(b, 1, sizeof b, in)) > 0)
fwrite(b, 1, n, out);
fclose(in);
fclose(out);
}
return 1;
}
if (LoadPng(src, &img) != 0)
return 0;
for (i = 0; i < 3; i++) {
Scale(&img, sizes[i].size, px);
snprintf(dst, sizeof dst, "%s/.dt/icons/%s.%c.pm", home,
a->action, sizes[i].suffix);
snprintf(name, sizeof name, "%s_%c", a->action, sizes[i].suffix);
if (WriteXpm(dst, name, px, sizes[i].size) != 0) {
free(img.rgba);
return 0;
}
}
free(img.rgba);
return 1;
}
/* ---- output ------------------------------------------------------ */
/* a value for the action database: one line, no '$' (put in by the
* action service), no '%' (its argument syntax) */
static void
DbValue(FILE *f, const char *s)
{
for (; *s != '\0'; s++)
if (*s != '\n' && *s != '$')
fputc(*s, f);
}
static const char actionFile[] =
"# This file represents a Common Desktop Environment (CDE) action. The\n"
"# contents of the file do not matter; if this file is executable and\n"
"# its name matches an action, the File Manager treats it as that\n"
"# action. Made by dtappscan (MCDE); it is made again at every login.\n";
static int
ActionFile(const char *folder, const char *action)
{
char dir[2048], path[2200];
FILE *f;
snprintf(dir, sizeof dir, "%s/.dt/Applications/%s", home, folder);
if (MakeDirs(dir) != 0)
return -1;
snprintf(path, sizeof path, "%s/%s", dir, action);
if ((f = fopen(path, "w")) == NULL)
return -1;
fputs(actionFile, f);
fclose(f);
return chmod(path, 0755);
}
int
main(int argc, char **argv)
{
const char *h = getenv("HOME"), *test = getenv("MCDE_APPSCAN_DIRS");
char path[2048], tmp[2100], dirs[8192], *d, *last = NULL;
FILE *f;
int i, nbuiltin = 0, nicons = 0;
size_t b;
if (argc > 1 && strcmp(argv[1], "-v") == 0)
verbose = 1;
else if (argc > 1) {
fprintf(stderr, "usage: dtappscan [-v]\n");
return 2;
}
if (h == NULL || h[0] == '\0') {
fprintf(stderr, "dtappscan: HOME is not set\n");
return 1;
}
snprintf(home, sizeof home, "%s", h);
/* where the desktop entries are (Desktop Entry Specification and
* XDG Base Directory Specification: $XDG_DATA_HOME first) */
if (test != NULL)
snprintf(dirs, sizeof dirs, "%s", test);
else {
const char *dh = getenv("XDG_DATA_HOME");
const char *dd = getenv("XDG_DATA_DIRS");
char tmpdirs[4096], *e, *l2 = NULL;
dirs[0] = '\0';
if (dh != NULL && dh[0] != '\0')
snprintf(dirs, sizeof dirs, "%s/applications:", dh);
else
snprintf(dirs, sizeof dirs, "%s/.local/share/applications:",
home);
snprintf(tmpdirs, sizeof tmpdirs, "%s:/usr/pkg/share:"
"/usr/local/share:/usr/share", dd != NULL ? dd : "");
for (e = strtok_r(tmpdirs, ":", &l2); e != NULL;
e = strtok_r(NULL, ":", &l2))
if (e[0] != '\0') {
strncat(dirs, e, sizeof dirs - strlen(dirs) - 1);
strncat(dirs, "/applications:",
sizeof dirs - strlen(dirs) - 1);
}
}
for (d = strtok_r(dirs, ":", &last); d != NULL;
d = strtok_r(NULL, ":", &last)) {
size_t n = strlen(d);
ScanDir(d);
if (nroots < 16 && n > 13 &&
strcmp(d + n - 13, "/applications") == 0)
snprintf(roots[nroots++], sizeof roots[0], "%.*s",
(int) (n - 13), d);
}
snprintf(path, sizeof path, "%s/.dt/types", home);
(void) MakeDirs(path);
snprintf(path, sizeof path, "%s/.dt/icons", home);
(void) MakeDirs(path);
/* the actions, written beside and renamed over the old file */
snprintf(path, sizeof path, "%s/.dt/types/mcde-apps.dt", home);
snprintf(tmp, sizeof tmp, "%s.new", path);
if ((f = fopen(tmp, "w")) == NULL) {
perror(tmp);
return 1;
}
fprintf(f, "# The applications of this computer, made by dtappscan "
"(MCDE) at login.\n# Changes here are lost; a desktop entry in "
"~/.local/share/applications\n# is the way to add one.\n\n"
"set DtDbVersion=1.0\n\n");
for (i = 0; i < napps; i++) {
App *a = &apps[i];
int icon = MakeIcon(a);
nicons += icon;
fprintf(f, "ACTION %s\n{\n\tLABEL\t\t", a->action);
DbValue(f, a->name);
fprintf(f, "\n\tICON\t\t%s\n\tTYPE\t\tCOMMAND\n",
icon ? a->action : "Dtapps");
fprintf(f, "\tWINDOW_TYPE\t%s\n",
a->terminal ? "TERMINAL" : "NO_STDIO");
fprintf(f, "\tARG_COUNT\t%s\n", a->takesFiles ? "*" : "0");
fprintf(f, "\tEXEC_STRING\t");
DbValue(f, a->exec);
fprintf(f, "%s\n", a->takesFiles ? " %Args%" : "");
if (a->comment[0] != '\0') {
fprintf(f, "\tDESCRIPTION\t");
DbValue(f, a->comment);
fputc('\n', f);
}
fprintf(f, "}\n\n");
}
if (fclose(f) != 0 || rename(tmp, path) != 0) {
perror(path);
return 1;
}
/* the folders, made new */
snprintf(path, sizeof path, "%s/.dt/Applications", home);
RemoveTree(path);
(void) MakeDirs(path);
for (b = 0; b < NBUILTIN; b++)
if (OnPath(builtin[b].program) &&
ActionFile(builtin[b].folder, builtin[b].action) == 0)
nbuiltin++;
for (i = 0; i < napps; i++)
(void) ActionFile(apps[i].folder, apps[i].action);
if (verbose) {
for (b = 0; b < NBUILTIN; b++)
if (OnPath(builtin[b].program))
fprintf(stderr, "dtappscan: %-24s %s (built in)\n",
builtin[b].folder, builtin[b].action);
for (i = 0; i < napps; i++)
fprintf(stderr, "dtappscan: %-14s %s (%s)\n",
apps[i].folder, apps[i].name, apps[i].id);
fprintf(stderr, "dtappscan: %d applications, %d with an icon, "
"%d of the desktop's own\n", napps, nicons, nbuiltin);
}
return 0;
}