Files
mcde/programs/dtfile/Search.c
T
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

507 lines
14 KiB
C

/*
* Search.c -- MCDE: dtfile's search over folders (Search.h)
*
* License: CNCL 1.0 (Could Not Care Less), see LICENSE at the top of the tree.
*
* The reference's search as a window of its own: a scope (the home folder,
* or the whole computer), a name to look for, when the file was changed --
* any time, today, yesterday, the past week -- and the list of what was
* found. "Save..." keeps the search in ~/.dt/searches/NAME.search, and the
* sidebar's SEARCH FOR group shows them with the six defaults.
*
* The walk is opendir/readdir in C, depth first, with a limit on depth,
* entries and results: a search that runs away would freeze the window, and
* the 1995-class machine has to stay usable. Nothing is indexed and nothing
* runs in the background.
*
* C99, POSIX, Xt and Motif only (ext/src/mcde/AGENTS.md).
*/
#include <dirent.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <time.h>
#include <unistd.h>
#include <Xm/Form.h>
#include <Xm/Label.h>
#include <Xm/List.h>
#include <Xm/PushB.h>
#include <Xm/PushBG.h>
#include <Xm/RowColumn.h>
#include <Xm/ScrolledW.h>
#include <Xm/Separator.h>
#include <Xm/TextF.h>
#include <Xm/ToggleB.h>
#include <Xm/Xm.h>
#include "Encaps.h"
#include "SharedProcs.h"
#include "FileMgr.h"
#include "Navigation.h"
#include "Search.h"
#define MAX_FOUND 500
#define MAX_ENTRIES 200000
#define MAX_DEPTH 24
typedef struct {
FileMgrRec *rec;
Widget shell;
Widget form;
Widget scope;
Widget whole;
Widget name;
Widget when;
Widget results;
Widget note;
Widget whenItems[4];
char **found;
int nfound;
char *savedName; /* the search's file, when one was opened */
char base[2400]; /* where the last walk started */
} Search;
static Search srch;
static void
SetNote(const char *text)
{
XmString s = XmStringCreateLocalized((char *) text);
XtVaSetValues(srch.note, XmNlabelString, s, NULL);
XmStringFree(s);
}
static Widget
Label(Widget parent, const char *name, const char *text)
{
XmString s = XmStringCreateLocalized((char *) text);
Widget w = XtVaCreateManagedWidget(name, xmLabelWidgetClass, parent,
XmNlabelString, s, XmNalignment, XmALIGNMENT_BEGINNING, NULL);
XmStringFree(s);
return w;
}
/* ---- the criteria ------------------------------------------------------- */
/* the option menu's chosen item: 0 any, 1 today, 2 yesterday, 3 week */
static int
WhenChosen(void)
{
Widget item = NULL;
int i;
XtVaGetValues(srch.when, XmNmenuHistory, &item, NULL);
for (i = 0; i < 4; i++)
if (item == srch.whenItems[i])
return i;
return 0;
}
static void
SetWhen(int when)
{
if (when < 0 || when > 3)
when = 0;
XtVaSetValues(srch.when, XmNmenuHistory, srch.whenItems[when], NULL);
}
/* the start of the day WHEN days ago, local time */
static time_t
DayStart(int daysAgo)
{
time_t now = time(NULL);
struct tm tm;
localtime_r(&now, &tm);
tm.tm_hour = 0;
tm.tm_min = 0;
tm.tm_sec = 0;
tm.tm_isdst = -1;
tm.tm_mday -= daysAgo;
return mktime(&tm);
}
/* ---- the walk ----------------------------------------------------------- */
static void
Report(const char *path, const char *base, const struct stat *st,
const struct stat *root, int when, int *entries, int *found)
{
time_t since = 0;
char shown[2400];
size_t rl = strlen(base);
if (when == MCDE_SEARCH_TODAY)
since = DayStart(0);
else if (when == MCDE_SEARCH_YESTERDAY)
since = DayStart(1);
else if (when == MCDE_SEARCH_WEEK)
since = DayStart(7);
if (since != 0 && st->st_mtime < since)
return;
/* the scope itself is not a finding */
if (st->st_ino == root->st_ino && st->st_dev == root->st_dev)
return;
(void)snprintf(shown, sizeof shown, "%s",
path + (strncmp(path, base, rl) == 0 ? rl : 0));
if (shown[0] == '/')
memmove(shown, shown + 1, strlen(shown));
srch.found[(*found)++] = XtNewString(shown);
{
XmString s = XmStringCreateLocalized(shown);
XmListAddItemUnselected(srch.results, s, 0);
XmStringFree(s);
}
}
static void
Walk(const char *path, const char *base, const struct stat *root,
const char *name, int when, int depth, int *entries, int *found)
{
DIR *d;
struct dirent *de;
if (depth > MAX_DEPTH || *found >= MAX_FOUND || *entries >= MAX_ENTRIES)
return;
if ((d = opendir(path)) == NULL)
return;
while ((de = readdir(d)) != NULL) {
char full[2400];
struct stat st;
if (strcmp(de->d_name, ".") == 0 || strcmp(de->d_name, "..") == 0)
continue;
if (de->d_name[0] == '.')
continue; /* hidden: .dt, .git, caches */
if ((size_t) snprintf(full, sizeof full, "%s/%s", path,
de->d_name) >= sizeof full)
continue;
if (lstat(full, &st) != 0)
continue;
if (++(*entries) > MAX_ENTRIES || *found >= MAX_FOUND)
break;
if (S_ISDIR(st.st_mode)) {
if (name == NULL || name[0] == '\0' ||
strstr(de->d_name, name) != NULL)
Report(full, base, &st, root, when, entries,
found);
Walk(full, base, root, name, when, depth + 1, entries,
found);
} else if (name == NULL || name[0] == '\0' ||
strstr(de->d_name, name) != NULL)
Report(full, base, &st, root, when, entries, found);
}
closedir(d);
}
static void
Clear(void)
{
int i;
XmListDeleteAllItems(srch.results);
for (i = 0; i < srch.nfound; i++)
XtFree(srch.found[i]);
srch.nfound = 0;
}
static void
Run(void)
{
char *text = XmTextFieldGetString(srch.name);
char *scope = XmTextFieldGetString(srch.scope);
char base[2400], note[128];
struct stat root;
int entries = 0;
time_t t0, t1;
Clear();
if (scope[0] == '\0')
(void)snprintf(scope, 2, "%s", "~");
/* the home folder and the whole computer, and a path as typed */
if (XmToggleButtonGetState(srch.whole))
(void)snprintf(scope, 2, "%s", "/");
if (strcmp(scope, "~") == 0 || scope[0] == '\0') {
const char *home = getenv("HOME");
(void)snprintf(base, sizeof base, "%s", home != NULL ? home :
"/");
} else if (strcmp(scope, "/") == 0)
(void)snprintf(base, sizeof base, "/");
else
(void)snprintf(base, sizeof base, "%s", scope);
(void)snprintf(srch.base, sizeof srch.base, "%s", base);
if (stat(base, &root) != 0) {
SetNote( "There is no such folder.");
XtFree(text);
XtFree(scope);
return;
}
SetNote( "Searching...");
XmUpdateDisplay(srch.shell);
t0 = time(NULL);
Walk(base, base, &root, text, WhenChosen(), 0, &entries, &srch.nfound);
t1 = time(NULL);
(void)snprintf(note, sizeof note, "%d found in %d folders' worth of "
"entries, %ld second%s.", srch.nfound, entries,
(long) (t1 - t0), t1 - t0 == 1 ? "" : "s");
SetNote( note);
XtFree(text);
XtFree(scope);
}
/* ---- the buttons -------------------------------------------------------- */
static void
SearchCB(Widget w, XtPointer client, XtPointer call)
{
(void)w;
(void)client;
(void)call;
Run();
}
/* a finding: its folder in the window, a folder itself opened */
static void
OpenCB(Widget w, XtPointer client, XtPointer call)
{
char *text = NULL;
Widget *items = NULL;
int n = 0;
char full[2400];
(void)w;
(void)client;
(void)call;
XtVaGetValues(srch.results, XmNselectedItems, &items,
XmNselectedItemCount, &n, NULL);
if (n < 1 || items == NULL || items[0] == NULL)
return;
if (!XmStringGetLtoR(items[0], XmFONTLIST_DEFAULT_TAG, &text))
return;
(void)snprintf(full, sizeof full, "%s/%s",
srch.base[0] != '\0' ? srch.base : "", text);
if (srch.rec != NULL) {
struct stat st;
if (stat(full, &st) == 0 && S_ISDIR(st.st_mode))
_McdeNavGoTo(srch.rec, full);
else {
char *slash = strrchr(full, '/');
if (slash != NULL) {
*slash = '\0';
_McdeNavGoTo(srch.rec, full);
}
}
}
XtFree(text);
}
/* ~/.dt/searches/NAME.search: name=, when=, scope= */
static void
SaveCB(Widget w, XtPointer client, XtPointer call)
{
char *text = XmTextFieldGetString(srch.name);
char path[1024], base[256], line[1200];
const char *home = getenv("HOME");
FILE *fp;
size_t i, n = 0;
(void)w;
(void)client;
(void)call;
if (home == NULL) {
XtFree(text);
return;
}
/* the file's name from the search: letters, digits, '-' and '_' */
for (i = 0; text != NULL && text[i] != '\0' && n + 1 < sizeof base; i++)
if ((text[i] >= 'a' && text[i] <= 'z') ||
(text[i] >= 'A' && text[i] <= 'Z') ||
(text[i] >= '0' && text[i] <= '9') || text[i] == '-' ||
text[i] == '_')
base[n++] = text[i];
if (n == 0)
(void)snprintf(base, sizeof base, "search");
else
base[n] = '\0';
{
char dir[1024];
(void)snprintf(dir, sizeof dir, "%s/.dt/searches", home);
(void)mkdir(dir, 0700);
}
(void)snprintf(path, sizeof path, "%s/.dt/searches/%s.search", home,
base);
if ((fp = fopen(path, "w")) != NULL) {
static const char *const whens[] = { "any", "today",
"yesterday", "week" };
char *scope = XmTextFieldGetString(srch.scope);
fprintf(fp, "name=%s\nwhen=%s\nscope=%s\n",
text != NULL ? text : "", whens[WhenChosen()], scope);
fclose(fp);
XtFree(scope);
(void)snprintf(line, sizeof line, "Saved as %s.", path);
SetNote( line);
}
XtFree(text);
}
/* ---- the window --------------------------------------------------------- */
static void
Build(void)
{
Widget form, row, lab, buttons;
XmString s;
static const char *const whens[] = { "Any time", "Today",
"Yesterday", "Past week" };
int i;
srch.shell = XtVaCreatePopupShell("mcdeSearch",
topLevelShellWidgetClass, srch.rec->shell,
XmNtitle, "Search", XmNallowShellResize, True, NULL);
form = XtVaCreateManagedWidget("searchForm", xmFormWidgetClass,
srch.shell, XmNmarginWidth, 10, XmNmarginHeight, 10,
XmNwidth, 520, XmNheight, 380, NULL);
srch.form = form;
/* the scope */
row = XtVaCreateManagedWidget("scopeRow", xmRowColumnWidgetClass, form,
XmNorientation, XmHORIZONTAL, XmNspacing, 6,
XmNtopAttachment, XmATTACH_FORM,
XmNleftAttachment, XmATTACH_FORM,
XmNrightAttachment, XmATTACH_FORM, NULL);
lab = Label(row, "scopeLabel", "Search in:");
(void)lab;
srch.scope = XtVaCreateManagedWidget("scope", xmTextFieldWidgetClass,
row, XmNcolumns, 26, NULL);
XmTextFieldSetString(srch.scope, "~");
srch.whole = XtVaCreateManagedWidget("whole", xmToggleButtonWidgetClass,
row, XmNlabelString, XmStringCreateLocalized((char *)
"This computer"), NULL);
{
XmString w = XmStringCreateLocalized((char *) "This computer");
XtVaSetValues(srch.whole, XmNlabelString, w, NULL);
XmStringFree(w);
}
/* the name and the time */
row = XtVaCreateManagedWidget("criteria", xmRowColumnWidgetClass, form,
XmNorientation, XmHORIZONTAL, XmNspacing, 6,
XmNtopAttachment, XmATTACH_WIDGET, XmNtopWidget, row,
XmNtopOffset, 6,
XmNleftAttachment, XmATTACH_FORM,
XmNrightAttachment, XmATTACH_FORM, NULL);
(void) Label(row, "nameLabel", "Name contains:");
srch.name = XtVaCreateManagedWidget("searchName",
xmTextFieldWidgetClass, row, XmNcolumns, 22, NULL);
(void) Label(row, "whenLabel", "Changed:");
srch.when = XmCreateOptionMenu(row, "when", NULL, 0);
XtManageChild(srch.when);
{
Widget pd = XmCreatePulldownMenu(srch.when, "whenMenu", NULL, 0);
for (i = 0; i < 4; i++) {
XmString w = XmStringCreateLocalized((char *) whens[i]);
srch.whenItems[i] = XtVaCreateManagedWidget("whenItem",
xmPushButtonGadgetClass, pd, XmNlabelString, w,
NULL);
XmStringFree(w);
}
XtVaSetValues(srch.when, XmNsubMenuId, pd, NULL);
SetWhen(0);
}
srch.note = XtVaCreateManagedWidget("note", xmLabelWidgetClass, form,
XmNalignment, XmALIGNMENT_BEGINNING,
XmNtopAttachment, XmATTACH_WIDGET, XmNtopWidget, row,
XmNtopOffset, 6,
XmNleftAttachment, XmATTACH_FORM,
XmNrightAttachment, XmATTACH_FORM, NULL);
{
Arg a[3];
XtSetArg(a[0], XmNvisibleItemCount, 14);
XtSetArg(a[1], XmNselectionPolicy, XmBROWSE_SELECT);
srch.results = XmCreateScrolledList(form, "found", a, 2);
XtManageChild(srch.results);
}
XtVaSetValues(XtParent(srch.results),
XmNtopAttachment, XmATTACH_WIDGET, XmNtopWidget, srch.note,
XmNtopOffset, 6,
XmNleftAttachment, XmATTACH_FORM,
XmNrightAttachment, XmATTACH_FORM, NULL);
buttons = XtVaCreateManagedWidget("buttons", xmFormWidgetClass, form,
XmNbottomAttachment, XmATTACH_FORM,
XmNleftAttachment, XmATTACH_FORM,
XmNrightAttachment, XmATTACH_FORM, NULL);
XtVaSetValues(XtParent(srch.results),
XmNbottomAttachment, XmATTACH_WIDGET, XmNbottomWidget, buttons,
XmNbottomOffset, 8, NULL);
{
Widget a = XtVaCreateManagedWidget("open", xmPushButtonWidgetClass,
buttons, XmNlabelString, XmStringCreateLocalized((char *)
"Open"), XmNtopAttachment, XmATTACH_FORM,
XmNbottomAttachment, XmATTACH_FORM,
XmNleftAttachment, XmATTACH_FORM, NULL);
Widget sv = XtVaCreateManagedWidget("save", xmPushButtonWidgetClass,
buttons, XmNlabelString, XmStringCreateLocalized((char *)
"Save..."), XmNtopAttachment, XmATTACH_FORM,
XmNbottomAttachment, XmATTACH_FORM,
XmNleftAttachment, XmATTACH_WIDGET, XmNleftWidget, a,
XmNleftOffset, 8, NULL);
Widget go = XtVaCreateManagedWidget("search", xmPushButtonWidgetClass,
buttons, XmNlabelString, XmStringCreateLocalized((char *)
"Search"), XmNtopAttachment, XmATTACH_FORM,
XmNbottomAttachment, XmATTACH_FORM,
XmNrightAttachment, XmATTACH_FORM, NULL);
XtAddCallback(a, XmNactivateCallback, OpenCB, NULL);
XtAddCallback(sv, XmNactivateCallback, SaveCB, NULL);
XtAddCallback(go, XmNactivateCallback, SearchCB, NULL);
XtAddCallback(srch.results, XmNdefaultActionCallback, OpenCB,
NULL);
}
XtAddCallback(srch.name, XmNactivateCallback, SearchCB, NULL);
XtAddCallback(srch.scope, XmNactivateCallback, SearchCB, NULL);
XtVaSetValues(srch.shell, XmNtransientFor, srch.rec->shell, NULL);
}
void
_McdeSearchShow(FileMgrRec *rec, const char *name, int when,
const char *scope)
{
if (rec == NULL)
return;
srch.rec = rec;
if (srch.shell == NULL) {
Build();
srch.found = XtCalloc(MAX_FOUND, sizeof *srch.found);
}
XmTextFieldSetString(srch.name, name != NULL ? name : "");
SetWhen(when);
if (scope != NULL && scope[0] != '\0')
XmTextFieldSetString(srch.scope, scope);
SetNote( "");
Clear();
XtPopup(srch.shell, XtGrabNone);
XmProcessTraversal(srch.name, XmTRAVERSE_CURRENT);
/* a search from the sidebar's SEARCH FOR runs at once */
if (when != 0 || (name != NULL && name[0] != '\0'))
Run();
}