Files
mcde/programs/dtfile/NetView.c
T
lotherkandClaude Sonnet 5.5 edb0e6949c MCDE: Mac OS X keyboard shortcuts through the desktop (Cmd+space dtlaunch, f.mcde_app, bar commands, dtterm/dtfile keys); fuse-afpfs-ng dropped from the build
The makronix set list is regenerated (46569 entries,
build/logs/x-userland-2026-11-13d.log); mcde-host exit=0
(build/logs/mcde-host-2026-11-13c.log).

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-13 12:05:06 +00:00

582 lines
17 KiB
C

/*
* NetView.c -- MCDE: the network in the file manager window
*
* Copyright (c) 2026 The Makronix project. GNU LGPL 2.0 or later, as CDE.
*
* "Network" in the sidebar (Navigation.c) shows, in place of the work area
* (as the column view does, Columns.c):
*
* - at the top "Connect to Server:" with an address -- sftp://host/path,
* nfs://host/path, afp://host/volume;
* - on the left the computers on the network that announce themselves
* (McdeSys, mcde_sys_net_browse: DNS-SD), with Refresh;
* - on the right what the chosen one offers: its files by SFTP, a
* terminal by SSH, its NFS exports, its SMB shares (listed; this
* computer cannot open them yet).
*
* Connecting mounts the share on ~/Network/HOST[/NAME] (mcde_sys_net_mount)
* and opens that folder in the window: the view goes back to the icons and
* dtfile goes there (_McdeNavGoTo). When the server wants a password, a
* dialog asks for it (the field shows stars) and the mount is tried again.
* Finding the computers takes a few seconds: it runs after the view is
* drawn.
*
* C99, POSIX and Motif (ext/src/mcde/AGENTS.md).
*/
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <Xm/Xm.h>
#include <Xm/Form.h>
#include <Xm/Frame.h>
#include <Xm/Label.h>
#include <Xm/List.h>
#include <Xm/MessageB.h>
#include <Xm/PushB.h>
#include <Xm/RowColumn.h>
#include <Xm/SelectioB.h>
#include <Xm/TextF.h>
#include <Mcde/McdeSys.h>
#include "Encaps.h"
#include "SharedProcs.h"
#include "FileMgr.h"
#include "Navigation.h"
#define MAX_HOSTS 64
#define MAX_ITEMS 64
/* what the right side lists: an action on the chosen computer */
typedef enum { ITEM_SFTP, ITEM_SSH, ITEM_NFS, ITEM_SMB, ITEM_AFP } ItemKind;
static struct {
FileMgrRec *rec;
Widget form, address, hostList, itemList, note;
McdeSysNetHost hosts[MAX_HOSTS];
int nhosts, chosen;
ItemKind kind[MAX_ITEMS];
char item[MAX_ITEMS][256];
int nitems;
char pendingUrl[600], pendingMount[1100];
} nv;
/*
* A name as the computers announce it is UTF-8 ("Konrad\u2019s computer");
* CDE's interface fonts are ISO 8859-1 and show the typographic quote as
* three wrong characters (measured with a real announcement). The usual
* punctuation becomes its ASCII form; the rest is left as it is.
*/
static void
Plain(const char *in, char *out, size_t len)
{
static const struct { const char *utf8, *ascii; } map[] = {
{ "\xe2\x80\x98", "'" }, { "\xe2\x80\x99", "'" },
{ "\xe2\x80\x9c", "\"" }, { "\xe2\x80\x9d", "\"" },
{ "\xe2\x80\x93", "-" }, { "\xe2\x80\x94", "-" },
{ "\xe2\x80\xa6", "..." }, { "\xc2\xa0", " " },
};
size_t o = 0, i, n;
while (*in != '\0' && o + 4 < len) {
for (i = 0; i < sizeof map / sizeof map[0]; i++) {
n = strlen(map[i].utf8);
if (strncmp(in, map[i].utf8, n) == 0)
break;
}
if (i < sizeof map / sizeof map[0]) {
n = strlen(map[i].utf8);
o += (size_t) snprintf(out + o, len - o, "%s",
map[i].ascii);
in += n;
} else
out[o++] = *in++;
}
out[o] = '\0';
}
static void
Note(const char *text)
{
XmString s = XmStringCreateLocalized((char *) text);
XtVaSetValues(nv.note, XmNlabelString, s, NULL);
XmStringFree(s);
XmUpdateDisplay(nv.note);
}
static void
AddItem(ItemKind k, const char *text, const char *label)
{
XmString s;
if (nv.nitems >= MAX_ITEMS)
return;
nv.kind[nv.nitems] = k;
snprintf(nv.item[nv.nitems], sizeof nv.item[0], "%s", text);
nv.nitems++;
{
char plain[512];
Plain(label, plain, sizeof plain);
s = XmStringCreateLocalized(plain);
}
XmListAddItemUnselected(nv.itemList, s, 0);
XmStringFree(s);
}
/* ---- connecting --------------------------------------------------------- */
/* ~/Network/HOST[/NAME], made if need be */
static void
MountPoint(const char *host, const char *name, char *buf, size_t len)
{
const char *home = getenv("HOME");
char sub[300];
size_t i;
snprintf(buf, len, "%s/Network", home != NULL ? home : "/tmp");
(void) mkdir(buf, 0700);
snprintf(buf + strlen(buf), len - strlen(buf), "/%s", host);
(void) mkdir(buf, 0700);
if (name != NULL && name[0] != '\0') {
/* "/export/home" -> "export-home" */
snprintf(sub, sizeof sub, "%s", name[0] == '/' ? name + 1 : name);
for (i = 0; sub[i] != '\0'; i++)
if (sub[i] == '/')
sub[i] = '-';
snprintf(buf + strlen(buf), len - strlen(buf), "/%s",
sub[0] != '\0' ? sub : "root");
(void) mkdir(buf, 0700);
}
}
/* something is mounted on PATH already: its device differs from its
* parent's */
static Boolean
Mounted(const char *path)
{
char parent[1100];
struct stat a, b;
char *slash;
snprintf(parent, sizeof parent, "%s", path);
if ((slash = strrchr(parent, '/')) == NULL || slash == parent)
return False;
*slash = '\0';
return stat(path, &a) == 0 && stat(parent, &b) == 0 &&
a.st_dev != b.st_dev;
}
static void Open(const char *mountpoint);
static void AskPassword(void);
static void
Connect(const char *url, const char *mountpoint, const char *password)
{
char text[700];
if (Mounted(mountpoint)) {
Open(mountpoint);
return;
}
snprintf(text, sizeof text, "Connecting to %s ...", url);
Note(text);
if (mcde_sys_net_mount(url, mountpoint, password) == MCDE_SYS_OK &&
Mounted(mountpoint)) {
Note("");
Open(mountpoint);
return;
}
/* SFTP without a key, AFP: the server wants a password */
if (password == NULL && (strncmp(url, "sftp://", 7) == 0 ||
strncmp(url, "afp://", 6) == 0)) {
snprintf(nv.pendingUrl, sizeof nv.pendingUrl, "%s", url);
snprintf(nv.pendingMount, sizeof nv.pendingMount, "%s",
mountpoint);
AskPassword();
return;
}
snprintf(text, sizeof text, "Could not connect: %s", mcde_sys_error());
Note(text);
}
/* the mounted folder in this window, in the icon view */
static void
Open(const char *mountpoint)
{
FileMgrRec *rec = nv.rec;
_McdeNetShow(rec, False);
_McdeNavGoTo(rec, mountpoint);
}
/* ---- the password ------------------------------------------------------- */
static char secret[128];
static size_t secretLen;
/* the field shows a star for every character; the text is kept here */
static void
SecretVerify(Widget w, XtPointer data, XtPointer call)
{
XmTextVerifyCallbackStruct *cb = (XmTextVerifyCallbackStruct *) call;
size_t from = (size_t) cb->startPos, to = (size_t) cb->endPos;
size_t add = cb->text != NULL ? (size_t) cb->text->length : 0, i;
(void) w;
(void) data;
if (to > secretLen)
to = secretLen;
if (from > to || secretLen - (to - from) + add >= sizeof secret) {
cb->doit = False;
return;
}
memmove(secret + from + add, secret + to, secretLen - to);
if (add > 0)
memcpy(secret + from, cb->text->ptr, add);
secretLen = secretLen - (to - from) + add;
secret[secretLen] = '\0';
for (i = 0; i < add; i++)
cb->text->ptr[i] = '*';
}
static void
PasswordDone(Widget w, XtPointer data, XtPointer call)
{
XmAnyCallbackStruct *cb = (XmAnyCallbackStruct *) call;
(void) data;
if (cb->reason == XmCR_OK)
Connect(nv.pendingUrl, nv.pendingMount, secret);
else
Note("");
memset(secret, 0, sizeof secret);
secretLen = 0;
XtDestroyWidget(XtParent(w));
}
static void
AskPassword(void)
{
Widget d, field;
XmString msg;
char text[700];
memset(secret, 0, sizeof secret);
secretLen = 0;
snprintf(text, sizeof text, "The password for %s:", nv.pendingUrl);
msg = XmStringCreateLocalized(text);
d = XmCreatePromptDialog(nv.form, "mcdeNetPassword", NULL, 0);
{
XmString title = XmStringCreateLocalized("Connect to Server");
XtVaSetValues(d, XmNselectionLabelString, msg,
XmNdialogTitle, title, NULL);
XmStringFree(title);
}
XmStringFree(msg);
field = XmSelectionBoxGetChild(d, XmDIALOG_TEXT);
XtAddCallback(field, XmNmodifyVerifyCallback, SecretVerify, NULL);
XtUnmanageChild(XmSelectionBoxGetChild(d, XmDIALOG_HELP_BUTTON));
XtAddCallback(d, XmNokCallback, PasswordDone, NULL);
XtAddCallback(d, XmNcancelCallback, PasswordDone, NULL);
XtManageChild(d);
}
/* ---- the computers ------------------------------------------------------ */
static void
HostChosen(Widget w, XtPointer client, XtPointer call)
{
XmListCallbackStruct *cb = (XmListCallbackStruct *) call;
static char names[MAX_ITEMS][256];
McdeSysNetHost *h;
char label[400];
int i, n;
(void) w;
(void) client;
nv.chosen = cb->item_position - 1;
XmListDeleteAllItems(nv.itemList);
nv.nitems = 0;
if (nv.chosen < 0 || nv.chosen >= nv.nhosts)
return;
h = &nv.hosts[nv.chosen];
if (h->kinds & MCDE_NET_HAS_SSH) {
AddItem(ITEM_SFTP, "", "Files (SFTP)");
AddItem(ITEM_SSH, "", "Terminal (SSH)");
}
if (h->kinds & MCDE_NET_HAS_AFP)
AddItem(ITEM_AFP, "", "Files (AFP) -- the volume by address");
if (h->kinds & MCDE_NET_HAS_NFS) {
Note("Asking for its NFS folders ...");
n = mcde_sys_net_shares(h->host, MCDE_NET_HAS_NFS, names,
MAX_ITEMS);
for (i = 0; i < n; i++) {
snprintf(label, sizeof label, "NFS: %s", names[i]);
AddItem(ITEM_NFS, names[i], label);
}
}
if (h->kinds & MCDE_NET_HAS_SMB) {
Note("Asking for its SMB shares ...");
n = mcde_sys_net_shares(h->host, MCDE_NET_HAS_SMB, names,
MAX_ITEMS);
for (i = 0; i < n; i++) {
snprintf(label, sizeof label, "SMB: %s (cannot be opened "
"here yet)", names[i]);
AddItem(ITEM_SMB, names[i], label);
}
if (n < 0) {
snprintf(label, sizeof label, "SMB: shares not listed -- "
"%s", mcde_sys_error());
AddItem(ITEM_SMB, "", label);
}
}
Note(nv.nitems == 0 ? "It offers nothing this computer can open." : "");
}
static void
ItemOpen(Widget w, XtPointer client, XtPointer call)
{
XmListCallbackStruct *cb = (XmListCallbackStruct *) call;
int i = cb->item_position - 1;
McdeSysNetHost *h;
char url[600], mp[1100], cmd[700];
(void) w;
(void) client;
if (i < 0 || i >= nv.nitems || nv.chosen < 0 || nv.chosen >= nv.nhosts)
return;
h = &nv.hosts[nv.chosen];
switch (nv.kind[i]) {
case ITEM_SFTP:
snprintf(url, sizeof url, "sftp://%s", h->host);
MountPoint(h->host, NULL, mp, sizeof mp);
Connect(url, mp, NULL);
break;
case ITEM_SSH:
/* a terminal of CDE's with ssh in it (the host name was
* checked by McdeSys when it was found: letters, digits,
* dots, dashes) */
snprintf(cmd, sizeof cmd, "dtterm -title '%s' -e ssh '%s' &",
h->name, h->host);
if (strchr(h->host, '\'') == NULL &&
strchr(h->name, '\'') == NULL)
(void) system(cmd);
break;
case ITEM_NFS:
snprintf(url, sizeof url, "nfs://%s%s", h->host, nv.item[i]);
MountPoint(h->host, nv.item[i], mp, sizeof mp);
Connect(url, mp, NULL);
break;
case ITEM_AFP:
snprintf(url, sizeof url, "afp://%s/", h->host);
XmTextFieldSetString(nv.address, url);
Note("Add the volume to the address and press Connect.");
break;
case ITEM_SMB:
Note("SMB shares can be listed, not opened, on this computer "
"yet.");
break;
}
}
static void
Browse(XtPointer client, XtIntervalId *id)
{
XmString s;
char label[500];
int i;
(void) client;
(void) id;
if (nv.form == NULL)
return;
Note("Looking for computers on the network ...");
XmListDeleteAllItems(nv.hostList);
XmListDeleteAllItems(nv.itemList);
nv.nitems = 0;
nv.chosen = -1;
nv.nhosts = mcde_sys_net_browse(nv.hosts, MAX_HOSTS, 3000);
if (nv.nhosts < 0) {
nv.nhosts = 0;
Note(mcde_sys_error());
return;
}
for (i = 0; i < nv.nhosts; i++) {
Plain(nv.hosts[i].name, label, sizeof label);
s = XmStringCreateLocalized(label);
XmListAddItemUnselected(nv.hostList, s, 0);
XmStringFree(s);
}
snprintf(label, sizeof label, nv.nhosts == 1 ? "%d computer found." :
"%d computers found.", nv.nhosts);
Note(label);
}
static void
RefreshCB(Widget w, XtPointer client, XtPointer call)
{
(void) w;
(void) client;
(void) call;
Browse(NULL, NULL);
}
/* "Connect to Server": the address as written */
static void
ConnectCB(Widget w, XtPointer client, XtPointer call)
{
char *url = XmTextFieldGetString(nv.address), host[256], mp[1100];
const char *p, *end;
const char *path;
(void) w;
(void) client;
(void) call;
if (url == NULL || (p = strstr(url, "://")) == NULL) {
Note("An address looks like sftp://host/folder, nfs://host/path "
"or afp://host/volume.");
XtFree(url);
return;
}
p += 3;
if (strchr(p, '@') != NULL && (strchr(p, '/') == NULL ||
strchr(p, '@') < strchr(p, '/')))
p = strchr(p, '@') + 1;
end = strchr(p, '/');
snprintf(host, sizeof host, "%.*s",
(int) (end != NULL ? (size_t) (end - p) : strlen(p)), p);
path = end != NULL ? end : "";
MountPoint(host, path, mp, sizeof mp);
Connect(url, mp, NULL);
XtFree(url);
}
/* ---- the view ----------------------------------------------------------- */
void
_McdeNetCreate(FileMgrRec *rec, Widget parent, Widget work)
{
Widget top, label, b;
XmString s;
Arg a[10];
int n;
if (nv.rec != NULL) /* one network view: in the first window */
return;
nv.rec = rec;
n = 0;
XtSetArg(a[n], XmNtopAttachment, XmATTACH_OPPOSITE_WIDGET); n++;
XtSetArg(a[n], XmNtopWidget, work); n++;
XtSetArg(a[n], XmNleftAttachment, XmATTACH_OPPOSITE_WIDGET); n++;
XtSetArg(a[n], XmNleftWidget, work); n++;
XtSetArg(a[n], XmNrightAttachment, XmATTACH_FORM); n++;
XtSetArg(a[n], XmNbottomAttachment, XmATTACH_FORM); n++;
nv.form = XmCreateForm(parent, "mcdeNetwork", a, n);
rec->mcde_network = nv.form;
top = XtVaCreateManagedWidget("connect", xmRowColumnWidgetClass,
nv.form, XmNorientation, XmHORIZONTAL,
XmNtopAttachment, XmATTACH_FORM, XmNleftAttachment, XmATTACH_FORM,
XmNrightAttachment, XmATTACH_FORM, NULL);
s = XmStringCreateLocalized("Connect to Server:");
label = XtVaCreateManagedWidget("label", xmLabelWidgetClass, top,
XmNlabelString, s, NULL);
XmStringFree(s);
(void) label;
nv.address = XtVaCreateManagedWidget("address", xmTextFieldWidgetClass,
top, XmNcolumns, 34, NULL);
XtAddCallback(nv.address, XmNactivateCallback, ConnectCB, NULL);
s = XmStringCreateLocalized("Connect");
b = XtVaCreateManagedWidget("connectButton", xmPushButtonWidgetClass,
top, XmNlabelString, s, NULL);
XmStringFree(s);
XtAddCallback(b, XmNactivateCallback, ConnectCB, NULL);
s = XmStringCreateLocalized("Refresh");
b = XtVaCreateManagedWidget("refresh", xmPushButtonWidgetClass, top,
XmNlabelString, s, NULL);
XmStringFree(s);
XtAddCallback(b, XmNactivateCallback, RefreshCB, NULL);
nv.note = XtVaCreateManagedWidget("note", xmLabelWidgetClass, nv.form,
XmNbottomAttachment, XmATTACH_FORM, XmNleftAttachment, XmATTACH_FORM,
XmNalignment, XmALIGNMENT_BEGINNING, NULL);
Note("");
n = 0;
XtSetArg(a[n], XmNselectionPolicy, XmBROWSE_SELECT); n++;
XtSetArg(a[n], XmNtopAttachment, XmATTACH_WIDGET); n++;
XtSetArg(a[n], XmNtopWidget, top); n++;
XtSetArg(a[n], XmNleftAttachment, XmATTACH_FORM); n++;
XtSetArg(a[n], XmNbottomAttachment, XmATTACH_WIDGET); n++;
XtSetArg(a[n], XmNbottomWidget, nv.note); n++;
nv.hostList = XmCreateScrolledList(nv.form, "computers", a, n);
XtVaSetValues(XtParent(nv.hostList), XmNwidth, 230, NULL);
XtManageChild(nv.hostList);
XtAddCallback(nv.hostList, XmNbrowseSelectionCallback, HostChosen,
NULL);
n = 0;
XtSetArg(a[n], XmNselectionPolicy, XmBROWSE_SELECT); n++;
XtSetArg(a[n], XmNtopAttachment, XmATTACH_WIDGET); n++;
XtSetArg(a[n], XmNtopWidget, top); n++;
XtSetArg(a[n], XmNleftAttachment, XmATTACH_WIDGET); n++;
XtSetArg(a[n], XmNleftWidget, XtParent(nv.hostList)); n++;
XtSetArg(a[n], XmNleftOffset, 8); n++;
XtSetArg(a[n], XmNrightAttachment, XmATTACH_FORM); n++;
XtSetArg(a[n], XmNbottomAttachment, XmATTACH_WIDGET); n++;
XtSetArg(a[n], XmNbottomWidget, nv.note); n++;
nv.itemList = XmCreateScrolledList(nv.form, "offers", a, n);
XtManageChild(nv.itemList);
XtAddCallback(nv.itemList, XmNdefaultActionCallback, ItemOpen, NULL);
}
/* the network on (in place of the work area and the columns) or off */
void
_McdeNetShow(FileMgrRec *rec, Boolean on)
{
Widget other;
if (rec->mcde_network == NULL || rec->mcde_work == NULL)
return;
other = rec->mcde_columns_on && rec->mcde_columns != NULL ?
rec->mcde_columns : rec->mcde_work;
/* the form around keeps its size while one part replaces the other
* (Columns.c) */
XtVaSetValues(XtParent(rec->mcde_work), XmNresizePolicy, XmRESIZE_NONE,
NULL);
if (on) {
Dimension w = 0, h = 0;
if (XtIsManaged(rec->mcde_network))
return;
XtVaGetValues(other, XmNwidth, &w, XmNheight, &h, NULL);
XtVaSetValues(rec->mcde_network, XmNwidth, w, XmNheight, h,
XmNresizePolicy, XmRESIZE_NONE, NULL);
XtManageChild(rec->mcde_network);
XtUnmanageChild(other);
XtAppAddTimeOut(XtWidgetToApplicationContext(rec->mcde_network),
100, Browse, NULL);
} else if (XtIsManaged(rec->mcde_network)) {
XtManageChild(other);
XtUnmanageChild(rec->mcde_network);
}
}
/* Command+K: the network view with the cursor in "Connect to Server:", as
* the field asks for it. The network view is the first window's (Create). */
void
_McdeNetConnect(FileMgrRec *rec)
{
if (rec == NULL || rec->mcde_network == NULL)
return;
_McdeNetShow(rec, True);
XmProcessTraversal(nv.address, XmTRAVERSE_CURRENT);
}