Files
mcde/programs/dtfile/Connect.c
T
lotherkandClaude Sonnet 5.5 cc770d000e dtfile: a mount no longer freezes the window; Connecting dialog, Cancel, Dtfile*connectTimeout (t0080 item 4)
mcde_sys_net_mount ran in the Xt callback and waited for sshfs/mount_afp
without a limit: a server that is gone froze the window for as long as the
tool lived (measured: the call returns after 20.0 s with a tool that sleeps
20 s; with one that never ends, never).

McdeSys: mcde_sys_net_mount_start/_job_fd/_job_finish/_job_cancel.  The tool
runs under a runner process (its own process group) that writes the answer
to a pipe: dtfile sets SIGCHLD to SIG_IGN (Main.c), where waitpid gives no
status.  mcde_sys_net_mount and _start share one plan (MountPlanFor).
test_job (make check, 5 of 5 pass) covers success, failure, a missing tool,
a password on stdin, and cancel with the tool AND what it started gone, with
SIGCHLD default and ignored; without the group kill it fails (mutant run).

dtfile: _McdeNetMount (NetView.c) for all three mounts (network view,
Connect to Server, column view): a modal "Connecting ..." dialog with Cancel
(also close button and Escape), the descriptor as Xt input (no timer
polling), one time-out, Dtfile*connectTimeout, 20 s, 3..600 (app-defaults,
man page).

Measured (real dtfile on Xvfb, fake sshfs that sleeps 40 s, connectTimeout
4): the dialog shows and repaints when moved; the time-out gives "Could not
connect: The server did not answer within 4 seconds.", Escape gives
"Could not connect: Cancelled."; sleep processes while connecting 1, after
the time-out 0, after Escape 0; no dialog left.  One first run showed a 40 s
stall of the whole session after Return (import blocked too); not
reproduced in three further runs, cause unknown.  Cross compile for NetBSD
(x86_64--netbsd-gcc, the build's flags): netmount.c and NetView.c without a
warning; common.c's old settimeofday declaration warning is not new.  Not
done: the NFS and AFP tools (only sshfs was faked), the afp://10.255.255.1/
case of the task with a real mount_afp.

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

536 lines
14 KiB
C

/*
* Connect.c -- MCDE: the Connect to Server dialog (Connect.h)
*
* License: CNCL 1.0 (Could Not Care Less), see LICENSE at the top of the tree.
*
* The reference's window: "Server Address:" with the address field, a "+"
* that keeps it, a menu of the servers used before, "Favorite Servers:" as
* a list, Remove, Browse (the network view) and Connect (Return). The
* favourites are one address a line in ~/.dt/servers, the history the last
* ten in ~/.dt/servers.recent, newest first.
*
* The mount is the network view's (NetView.c, McdeSys): this dialog only
* asks for the address and says what went wrong, in place under the field.
*
* C99, POSIX, Xt and Motif only (ext/src/mcde/AGENTS.md).
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <unistd.h>
#include <Xm/CascadeB.h>
#include <Xm/Form.h>
#include <Xm/LabelG.h>
#include <Xm/Label.h>
#include <Xm/List.h>
#include <Xm/MenuShell.h>
#include <Xm/PushB.h>
#include <Xm/PushBG.h>
#include <Xm/RowColumn.h>
#include <Xm/ScrolledW.h>
#include <Xm/TextF.h>
#include <Xm/Xm.h>
#include "Encaps.h"
#include "SharedProcs.h"
#include "FileMgr.h"
#include "Navigation.h"
#include "Connect.h"
#define RECENT_MAX 10
#define FAV_MAX 64
typedef struct {
FileMgrRec *rec;
Widget shell;
Widget form;
Widget address;
Widget history; /* the menu of servers used before */
Widget list; /* the favourites */
Widget error;
Widget remove;
} Connect;
static Connect conn;
static void RecentChosen(Widget w, XtPointer client, XtPointer call);
/* how many servers System Settings > Appearance asks for (~/.dt/recent.conf,
* "servers=N"); 10 when the file does not say */
static int
RecentServers(void)
{
char path[600], line[256];
const char *home = getenv("HOME");
FILE *fp;
int n = RECENT_MAX;
if (home == NULL || (size_t) snprintf(path, sizeof path,
"%s/.dt/recent.conf", home) >= sizeof path)
return RECENT_MAX;
if ((fp = fopen(path, "r")) == NULL)
return RECENT_MAX;
while (fgets(line, sizeof line, fp) != NULL) {
line[strcspn(line, "\r\n")] = '\0';
if (strncmp(line, "servers=", 8) == 0)
n = atoi(line + 8);
}
fclose(fp);
if (n < 0)
n = 0;
if (n > RECENT_MAX)
n = RECENT_MAX;
return n;
}
/* ---- the two files ------------------------------------------------------ */
static void
FilePath(const char *name, char *buf, size_t len)
{
const char *home = getenv("HOME");
(void)snprintf(buf, len, "%s/.dt/%s", home != NULL ? home : ".", name);
}
/* the lines of NAME, at most MAX, into LIST; how many */
static int
ReadLines(const char *name, char **list, int max)
{
char path[512], line[512];
FILE *fp;
int n = 0;
FilePath(name, path, sizeof path);
if ((fp = fopen(path, "r")) == NULL)
return 0;
while (n < max && fgets(line, sizeof line, fp) != NULL) {
line[strcspn(line, "\r\n")] = '\0';
if (line[0] == '\0')
continue;
list[n++] = XtNewString(line);
}
fclose(fp);
return n;
}
static void
WriteLines(const char *name, char **list, int n)
{
char path[512], tmp[600];
FILE *fp;
int i;
FilePath(name, path, sizeof path);
(void)snprintf(tmp, sizeof tmp, "%s.new", path);
if ((fp = fopen(tmp, "w")) == NULL)
return;
for (i = 0; i < n; i++)
fprintf(fp, "%s\n", list[i]);
fclose(fp);
(void)rename(tmp, path);
}
static void
FreeLines(char **list, int n)
{
int i;
for (i = 0; i < n; i++)
XtFree(list[i]);
}
/* the address into ~/.dt/servers, once */
static void
FavouriteAdd(const char *address)
{
char *list[FAV_MAX];
int n = ReadLines("servers", list, FAV_MAX), i;
Boolean have = False;
for (i = 0; i < n; i++)
if (strcmp(list[i], address) == 0)
have = True;
if (!have && n < FAV_MAX)
list[n++] = XtNewString(address);
WriteLines("servers", list, n);
FreeLines(list, n);
}
/* the address into ~/.dt/servers.recent, newest first, once */
static void
RecentAdd(const char *address)
{
char *list[RECENT_MAX + 1];
int max = RecentServers();
int n = ReadLines("servers.recent", list, RECENT_MAX), i, m = 0;
char *out[RECENT_MAX];
if (max <= 0) {
WriteLines("servers.recent", out, 0);
FreeLines(list, n);
return;
}
for (i = 0; i < n; i++)
if (strcmp(list[i], address) != 0 && m < max - 1)
out[m++] = list[i];
if (m < max)
out[m++] = XtNewString(address);
WriteLines("servers.recent", out, m);
for (i = 0; i < n; i++)
XtFree(list[i]);
XtFree(out[m - 1]);
}
/* ---- the dialog --------------------------------------------------------- */
static void
SetError(const char *text)
{
XmString s = XmStringCreateLocalized((char *) text);
XtVaSetValues(conn.error, XmNlabelString, s, NULL);
XmStringFree(s);
}
static void
FillFavourites(void)
{
char *list[FAV_MAX];
int n = ReadLines("servers", list, FAV_MAX), i;
XmListDeleteAllItems(conn.list);
for (i = 0; i < n; i++) {
XmString s = XmStringCreateLocalized(list[i]);
XmListAddItemUnselected(conn.list, s, 0);
XmStringFree(s);
}
FreeLines(list, n);
XtSetSensitive(conn.remove, False);
}
static void
FillHistory(Widget w, XtPointer client, XtPointer call)
{
char *list[RECENT_MAX];
Widget pd = NULL, *kids = NULL;
Cardinal nk = 0, k;
int n, i;
(void)w;
(void)client;
(void)call;
XtVaGetValues(conn.history, XmNsubMenuId, &pd, NULL);
if (pd == NULL)
return;
XtVaGetValues(pd, XmNchildren, &kids, XmNnumChildren, &nk, NULL);
for (k = 0; k < nk; k++)
XtDestroyWidget(kids[k]);
n = ReadLines("servers.recent", list, RecentServers());
for (i = 0; i < n; i++) {
XmString s = XmStringCreateLocalized(list[i]);
Widget item = XtVaCreateManagedWidget("recentItem",
xmPushButtonGadgetClass, pd, XmNlabelString, s, NULL);
XmStringFree(s);
XtAddCallback(item, XmNactivateCallback, RecentChosen,
XtNewString(list[i]));
XtFree(list[i]);
}
if (n == 0) {
XmString s = XmStringCreateLocalized((char *) "No servers yet");
(void)XtVaCreateManagedWidget("recentNone",
xmLabelGadgetClass, pd, XmNlabelString, s, NULL);
XmStringFree(s);
}
}
/* the address as the dialog gives it: an address without a scheme is an
* sftp one */
static void
Normalise(const char *in, char *out, size_t len)
{
if (strstr(in, "://") != NULL)
(void)snprintf(out, len, "%s", in);
else
(void)snprintf(out, len, "sftp://%s", in);
}
static void
ConnectNow(Widget w, XtPointer client, XtPointer call)
{
char *text = XmTextFieldGetString(conn.address);
char url[512], mp[1100], err[512];
(void)w;
(void)client;
(void)call;
if (text == NULL || text[0] == '\0') {
SetError("An address looks like sftp://host/folder, "
"nfs://host/path or afp://host/volume.");
return;
}
Normalise(text, url, sizeof url);
XtFree(text);
if (_McdeNetConnectAddress(conn.shell, url, mp, sizeof mp, err,
sizeof err)) {
RecentAdd(url);
XtPopdown(conn.shell);
if (conn.rec != NULL)
_McdeNavGoTo(conn.rec, mp);
} else {
SetError(err);
XmProcessTraversal(conn.address, XmTRAVERSE_CURRENT);
}
}
static void
AddFavourite(Widget w, XtPointer client, XtPointer call)
{
char *text = XmTextFieldGetString(conn.address);
char url[512];
(void)w;
(void)client;
(void)call;
if (text == NULL || text[0] == '\0')
return;
Normalise(text, url, sizeof url);
FavouriteAdd(url);
XtFree(text);
FillFavourites();
}
static void
RemoveFavourite(Widget w, XtPointer client, XtPointer call)
{
char path[512], *list[FAV_MAX], *sel = NULL;
int n, i, m = 0;
Widget *items = NULL;
(void)w;
(void)client;
(void)call;
XtVaGetValues(conn.list, XmNselectedItems, &items,
XmNselectedItemCount, &n, NULL);
if (n < 1 || items == NULL || items[0] == NULL)
return;
if (!XmStringGetLtoR(items[0], XmFONTLIST_DEFAULT_TAG, &sel))
return;
n = ReadLines("servers", list, FAV_MAX);
{
char *keep[FAV_MAX];
for (i = 0; i < n; i++)
if (strcmp(list[i], sel) != 0)
keep[m++] = list[i];
else
XtFree(list[i]);
WriteLines("servers", keep, m);
}
XtFree(sel);
FillFavourites();
FilePath("servers", path, sizeof path);
}
static void
BrowseServers(Widget w, XtPointer client, XtPointer call)
{
(void)w;
(void)client;
(void)call;
XtPopdown(conn.shell);
if (conn.rec != NULL)
_McdeNetShow(conn.rec, True);
}
static void
Chosen(Widget w, XtPointer client, XtPointer call)
{
XmListCallbackStruct *c = (XmListCallbackStruct *) call;
char *text = NULL;
(void)w;
(void)client;
if (XmStringGetLtoR(c->item, XmFONTLIST_DEFAULT_TAG, &text)) {
XmTextFieldSetString(conn.address, text);
XtFree(text);
}
XtSetSensitive(conn.remove, True);
}
static void
RecentChosen(Widget w, XtPointer client, XtPointer call)
{
char *text = (char *) client;
(void)w;
(void)call;
if (text == NULL)
return;
XmTextFieldSetString(conn.address, text);
ConnectNow(NULL, NULL, NULL);
}
static void
Build(void)
{
Widget form, row, lab;
XmString s;
conn.shell = XtVaCreatePopupShell("mcdeConnect",
topLevelShellWidgetClass, conn.rec->shell,
XmNtitle, "Connect to Server", XmNallowShellResize, True, NULL);
form = XtVaCreateManagedWidget("connectForm", xmFormWidgetClass,
conn.shell, XmNmarginWidth, 10, XmNmarginHeight, 10,
XmNwidth, 490, XmNheight, 250, NULL);
conn.form = form;
lab = XtVaCreateManagedWidget("addressLabel", xmLabelWidgetClass, form,
XmNtopAttachment, XmATTACH_FORM,
XmNleftAttachment, XmATTACH_FORM, NULL);
s = XmStringCreateLocalized((char *) "Server Address:");
XtVaSetValues(lab, XmNlabelString, s, NULL);
XmStringFree(s);
conn.address = XtVaCreateManagedWidget("address", xmTextFieldWidgetClass,
form, XmNcolumns, 30,
XmNtopAttachment, XmATTACH_WIDGET, XmNtopWidget, lab,
XmNtopOffset, 4,
XmNleftAttachment, XmATTACH_FORM, NULL);
{
Widget plus = XtVaCreateManagedWidget("plus",
xmPushButtonWidgetClass, form,
XmNlabelString, XmStringCreateLocalized((char *) "+"),
XmNtopAttachment, XmATTACH_OPPOSITE_WIDGET,
XmNtopWidget, conn.address,
XmNleftAttachment, XmATTACH_WIDGET,
XmNleftWidget, conn.address, XmNleftOffset, 6, NULL);
XtAddCallback(plus, XmNactivateCallback, AddFavourite, NULL);
}
/* the history: an option menu of the last servers, made on opening
* (a cascade button outside a menu bar did not post its menu,
* measured 2026-11-14) */
conn.history = XmCreateOptionMenu(form, "history", NULL, 0);
XtVaSetValues(conn.history,
XmNtopAttachment, XmATTACH_OPPOSITE_WIDGET,
XmNtopWidget, conn.address,
XmNleftAttachment, XmATTACH_WIDGET, XmNleftWidget, conn.address,
XmNleftOffset, 40, NULL);
XtManageChild(conn.history);
{
Widget pd = XmCreatePulldownMenu(conn.history, "historyMenu",
NULL, 0);
XtVaSetValues(conn.history, XmNsubMenuId, pd, NULL);
XtAddCallback(pd, XmNmapCallback, FillHistory, NULL);
{
XmString s = XmStringCreateLocalized((char *) "Recent");
XtVaSetValues(conn.history, XmNlabelString, s, NULL);
XmStringFree(s);
}
}
conn.error = XtVaCreateManagedWidget("error", xmLabelWidgetClass, form,
XmNalignment, XmALIGNMENT_BEGINNING,
XmNtopAttachment, XmATTACH_WIDGET, XmNtopWidget, conn.address,
XmNtopOffset, 8,
XmNleftAttachment, XmATTACH_FORM,
XmNrightAttachment, XmATTACH_FORM, NULL);
lab = XtVaCreateManagedWidget("favouritesLabel", xmLabelWidgetClass,
form,
XmNtopAttachment, XmATTACH_WIDGET, XmNtopWidget, conn.error,
XmNtopOffset, 8,
XmNleftAttachment, XmATTACH_FORM, NULL);
s = XmStringCreateLocalized((char *) "Favorite Servers:");
XtVaSetValues(lab, XmNlabelString, s, NULL);
XmStringFree(s);
conn.list = XmCreateScrolledList(form, "favourites", NULL, 0);
XtVaSetValues(conn.list, XmNvisibleItemCount, 5,
XmNselectionPolicy, XmBROWSE_SELECT, NULL);
XtVaSetValues(XtParent(conn.list),
XmNtopAttachment, XmATTACH_WIDGET, XmNtopWidget, lab,
XmNtopOffset, 4,
XmNleftAttachment, XmATTACH_FORM,
XmNrightAttachment, XmATTACH_FORM,
XmNbottomAttachment, XmATTACH_WIDGET,
XmNbottomWidget, NULL, NULL);
XtAddCallback(conn.list, XmNbrowseSelectionCallback, Chosen, NULL);
XtManageChild(conn.list);
row = XtVaCreateManagedWidget("buttons", xmFormWidgetClass, form,
XmNbottomAttachment, XmATTACH_FORM,
XmNleftAttachment, XmATTACH_FORM,
XmNrightAttachment, XmATTACH_FORM, NULL);
XtVaSetValues(XtParent(conn.list),
XmNbottomAttachment, XmATTACH_WIDGET, XmNbottomWidget, row,
XmNbottomOffset, 8, NULL);
conn.remove = XtVaCreateManagedWidget("remove", xmPushButtonWidgetClass,
row, XmNlabelString, XmStringCreateLocalized((char *) "Remove"),
XmNtopAttachment, XmATTACH_FORM,
XmNbottomAttachment, XmATTACH_FORM,
XmNleftAttachment, XmATTACH_FORM, NULL);
XtAddCallback(conn.remove, XmNactivateCallback, RemoveFavourite, NULL);
XtSetSensitive(conn.remove, False);
{
/* Browse left of Connect, as the reference has them */
Widget connectw = XtVaCreateManagedWidget("connect",
xmPushButtonWidgetClass, row,
XmNlabelString, XmStringCreateLocalized((char *)
"Connect"),
XmNtopAttachment, XmATTACH_FORM,
XmNbottomAttachment, XmATTACH_FORM,
XmNrightAttachment, XmATTACH_FORM, NULL);
Widget browse = XtVaCreateManagedWidget("browse",
xmPushButtonWidgetClass, row,
XmNlabelString, XmStringCreateLocalized((char *) "Browse"),
XmNtopAttachment, XmATTACH_FORM,
XmNbottomAttachment, XmATTACH_FORM,
XmNrightAttachment, XmATTACH_WIDGET, XmNrightWidget, connectw,
XmNrightOffset, 8, NULL);
XtAddCallback(browse, XmNactivateCallback, BrowseServers, NULL);
XtAddCallback(connectw, XmNactivateCallback, ConnectNow, NULL);
}
XtAddCallback(conn.address, XmNactivateCallback, ConnectNow, NULL);
}
void
_McdeConnectShow(FileMgrRec *rec)
{
if (rec == NULL)
return;
conn.rec = rec;
if (conn.shell == NULL) {
Build();
XtVaSetValues(conn.shell, XmNtransientFor, rec->shell, NULL);
}
SetError("");
FillFavourites();
{
char *recent[RECENT_MAX];
int n = ReadLines("servers.recent", recent, RecentServers());
XmTextFieldSetString(conn.address, n > 0 ? recent[0] : "sftp://");
FreeLines(recent, n);
}
XtPopup(conn.shell, XtGrabNone);
XmProcessTraversal(conn.address, XmTRAVERSE_CURRENT);
XmTextFieldSetSelection(conn.address, 0,
XmTextFieldGetLastPosition(conn.address), CurrentTime);
}