2 * Copyright 1989 Software Research Associates, Inc., Tokyo, Japan
4 * Permission to use, copy, modify, and distribute this software and its
5 * documentation for any purpose and without fee is hereby granted, provided
6 * that the above copyright notice appear in all copies and that both that
7 * copyright notice and this permission notice appear in supporting
8 * documentation, and that the name of Software Research Associates not be used
9 * in advertising or publicity pertaining to distribution of the software
10 * without specific, written prior permission. Software Research Associates
11 * makes no representations about the suitability of this software for any
12 * purpose. It is provided "as is" without express or implied warranty.
14 * SOFTWARE RESEARCH ASSOCIATES DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS
15 * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS,
16 * IN NO EVENT SHALL SOFTWARE RESEARCH ASSOCIATES BE LIABLE FOR ANY SPECIAL,
17 * INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
18 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
19 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
20 * PERFORMANCE OF THIS SOFTWARE.
22 * Author: Erik M. van der Poel
23 * Software Research Associates, Inc., Tokyo, Japan
28 #include <stdlib.h> /* for qsort */
31 #ifdef SEL_FILE_IGNORE_CASE
33 #endif /* def SEL_FILE_IGNORE_CASE */
39 #include <X11/Xaw/Scrollbar.h>
42 #define MAXPATHLEN 1024
43 #endif /* ndef MAXPATHLEN */
46 extern uid_t getuid();
47 #endif /* def HAS_DIRENT_H */
60 int SFbuttonPressed = 0;
62 static int SFdoNotTouchDirPtr = 0;
64 static int SFdoNotTouchVorigin = 0;
66 static SFDir SFrootDir, SFhomeDir;
68 static SFLogin *SFlogins;
70 static int SFtwiddle = 0;
72 void SFsetText(char *path);
82 if (strcmp(path, SFcurrentDir)) {
85 (void) strncpy(SFcurrentDir, path, MAXPATHLEN);
101 for (j = dir->nEntries - 1; j >= 0; j--) {
102 if (dir->entries[j].shown != dir->entries[j].real) {
103 XtFree(dir->entries[j].shown);
105 XtFree(dir->entries[j].real);
108 XtFree((char *) dir->entries);
121 *s1 = strcpy(XtMalloc((unsigned) (strlen(s2) + 1)), s2);
129 char *cannotOpen = "<cannot open> ";
131 dir->entries = (SFEntry *) XtMalloc(sizeof(SFEntry));
132 dir->entries[0].statDone = 1;
133 SFstrdup(&dir->entries[0].real, cannotOpen);
134 dir->entries[0].shown = dir->entries[0].real;
136 dir->nChars = strlen(cannotOpen);
140 #ifdef SEL_FILE_IGNORE_CASE
143 register char *p, *q;
146 register char c1, c2;
163 while ((--n >= 0) && (c1 == c2)) {
165 return strncmp(psave, qsave, nsave);
178 return strncmp(psave, qsave, nsave);
183 #endif /* def SEL_FILE_IGNORE_CASE */
186 SFreplaceText(dir, str)
194 if (str[len - 1] == '/') {
195 (void) strcat(SFcurrentPath, str);
197 (void) strncat(SFcurrentPath, str, len - 1);
199 if (strncmp(SFcurrentPath, SFstartDir, strlen(SFstartDir))) {
200 SFsetText(SFcurrentPath);
202 SFsetText(&(SFcurrentPath[strlen(SFstartDir)]));
215 char *name, *growing;
217 SFEntry *entry, *max;
221 dir = &(SFdirs[SFdirEnd - 1]);
223 if (dir->beginSelection == -1) {
225 SFreplaceText(dir, str);
228 } else if (dir->beginSelection == dir->endSelection) {
229 SFreplaceText(dir, dir->entries[dir->beginSelection].shown);
233 max = &(dir->entries[dir->endSelection + 1]);
235 name = dir->entries[dir->beginSelection].shown;
236 SFstrdup(&growing, name);
240 entry = &(dir->entries[dir->beginSelection]);
241 while (entry < max) {
242 if (cmp = strncmp(growing, entry->shown, len)) {
251 * SFreplaceText() expects filename
253 growing[len - 2] = ' ';
255 growing[len - 1] = 0;
256 SFreplaceText(dir, growing);
265 register int i, last, max;
266 register char *name, save;
274 if (str[len - 1] == ' ') {
277 } else if (str[len - 1] == '/') {
283 entries = dir->entries;
287 name = entries[i].shown;
288 last = strlen(name) - 1;
292 #ifdef SEL_FILE_IGNORE_CASE
293 result = SFstrncmp(str, name, len);
294 #else /* def SEL_FILE_IGNORE_CASE */
295 result = strncmp(str, name, len);
296 #endif /* def SEL_FILE_IGNORE_CASE */
306 name = entries[i].shown;
307 last = strlen(name) - 1;
311 #ifdef SEL_FILE_IGNORE_CASE
312 result = SFstrncmp(str, name, len);
313 #else /* def SEL_FILE_IGNORE_CASE */
314 result = strncmp(str, name, len);
315 #endif /* def SEL_FILE_IGNORE_CASE */
327 (dir->beginSelection != begin) ||
328 (dir->endSelection != end - 1)
331 dir->beginSelection = begin;
332 if (str[strlen(str) - 1] == '/') {
333 dir->endSelection = begin;
335 dir->endSelection = end - 1;
339 if (dir->beginSelection != -1) {
341 dir->beginSelection = -1;
342 dir->endSelection = -1;
347 SFdoNotTouchVorigin ||
348 ((begin > dir->vOrigin) && (end < dir->vOrigin + SFlistSize))
350 SFdoNotTouchVorigin = 0;
355 if (i > max - SFlistSize) {
356 i = max - SFlistSize;
362 if (dir->vOrigin != i) {
375 dir = &(SFdirs[SFdirEnd - 1]);
376 if (dir->beginSelection != -1) {
379 dir->beginSelection = -1;
380 dir->endSelection = -1;
385 SFcompareLogins(p, q)
388 return strcmp(p->name, q->name);
397 SFEntry *entries = NULL;
404 entries = (SFEntry *) XtMalloc(sizeof(SFEntry));
405 SFlogins = (SFLogin *) XtMalloc(sizeof(SFLogin));
406 entries[0].real = XtMalloc(3);
407 (void) strcpy(entries[0].real, "~");
408 entries[0].shown = entries[0].real;
409 entries[0].statDone = 1;
410 SFlogins[0].name = "";
411 pw = getpwuid((int) getuid());
412 SFstrdup(&SFlogins[0].dir, pw ? pw->pw_dir : "/");
418 while ((pw = getpwent()) && (*(pw->pw_name))) {
421 entries = (SFEntry *) XtRealloc(
423 (unsigned) (alloc * sizeof(SFEntry))
425 SFlogins = (SFLogin *) XtRealloc(
427 (unsigned) (alloc * sizeof(SFLogin))
430 len = strlen(pw->pw_name);
431 entries[i].real = XtMalloc((unsigned) (len + 3));
432 (void) strcat(strcpy(entries[i].real, "~"),
434 entries[i].shown = entries[i].real;
435 entries[i].statDone = 1;
436 if (len > maxChars) {
439 SFstrdup(&SFlogins[i].name, pw->pw_name);
440 SFstrdup(&SFlogins[i].dir, pw->pw_dir);
444 SFhomeDir.dir = XtMalloc(1) ;
445 SFhomeDir.dir[0] = 0 ;
446 SFhomeDir.path = SFcurrentPath ;
447 SFhomeDir.entries = entries ;
448 SFhomeDir.nEntries = i ;
449 SFhomeDir.vOrigin = 0 ; /* :-) */
450 SFhomeDir.nChars = maxChars + 2 ;
451 SFhomeDir.hOrigin = 0 ;
452 SFhomeDir.changed = 1 ;
453 SFhomeDir.beginSelection = -1 ;
454 SFhomeDir.endSelection = -1 ;
456 qsort((char *) entries, (size_t)i, sizeof(SFEntry), SFcompareEntries);
457 qsort((char *) SFlogins, (size_t)i, sizeof(SFLogin), SFcompareLogins);
459 for (i--; i >= 0; i--) {
460 (void) strcat(entries[i].real, "/");
466 SFfindHomeDir(begin, end)
476 for (i = SFhomeDir.nEntries - 1; i >= 0; i--) {
477 if (!strcmp(SFhomeDir.entries[i].real, begin)) {
479 SFstrdup(&theRest, end);
480 (void) strcat(strcat(strncpy(SFcurrentPath,SFlogins[i].dir,
484 SFsetText(SFcurrentPath);
499 static int wasTwiddle = 0;
503 int SFdirPtrSave, SFdirEndSave;
507 SFdirs = (SFDir *) XtMalloc((alloc = 10) * sizeof(SFDir));
509 SFstrdup(&dir->dir, "/");
511 (void) SFgetDir(dir);
512 for (j = 1; j < alloc; j++) {
513 SFdirs[j].dir = NULL;
515 dir->path = SFcurrentPath + 1;
519 dir->beginSelection = -1;
520 dir->endSelection = -1;
521 SFhomeDir.dir = NULL;
524 SFdirEndSave = SFdirEnd;
527 SFdirPtrSave = SFdirPtr;
532 if (SFcurrentPath[0] == '~') {
537 if (!SFhomeDir.dir) {
544 SFdoNotTouchDirPtr = 1;
553 end = SFcurrentPath + 1;
561 while (*end++ == '/') {
566 while ((*end) && (*end++ != '/')) {
569 if ((end - SFcurrentPath <= SFtextPos) && (*(end - 1) == '/')) {
576 if (*(end - 1) == '/') {
580 if (SFfindHomeDir(begin, end)) {
588 SFdirs = (SFDir *) XtRealloc(
590 (unsigned) ((alloc *= 2) *
593 for (j = alloc / 2; j < alloc; j++) {
594 SFdirs[j].dir = NULL;
601 strcmp(dir->dir, begin)
607 SFstrdup(&dir->dir, begin);
612 dir->beginSelection = -1;
613 dir->endSelection = -1;
614 (void) SFfindFile(dir - 1, begin);
616 SFchdir(SFcurrentPath) ||
619 SFunreadableDir(dir);
628 if (SFfindFile(&(SFdirs[SFdirEnd-1]), begin)) {
637 if ((end == SFcurrentPath + 1) && (!SFtwiddle)) {
641 for (i = SFdirEnd; i < alloc; i++) {
647 if (SFdoNotTouchDirPtr) {
650 SFdirPtr = SFdirEnd - 1;
655 SFdirPtr = SFdirPtrSave;
657 SFdoNotTouchDirPtr = 0;
660 if ((SFdirPtr != SFdirPtrSave) || (SFdirEnd != SFdirEndSave)) {
661 XawScrollbarSetThumb(
663 (float) (((double) SFdirPtr) / SFdirEnd),
664 (float) (((double) ((SFdirEnd < NR) ? SFdirEnd : NR)) /
669 if (SFdirPtr != SFdirPtrSave) {
670 SFdrawLists(SF_DO_SCROLL);
672 for (i = 0; i < NR; i++) {
673 if (SFdirPtr + i < SFdirEnd) {
674 if (SFdirs[SFdirPtr + i].changed) {
675 SFdirs[SFdirPtr + i].changed = 0;
676 SFdrawList(i, SF_DO_SCROLL);
679 SFclearList(i, SF_DO_SCROLL);
688 SFbuttonPressList(w, n, event)
691 XButtonPressedEvent *event;
694 if(event->button == Button4) dir = -2; // kludge to indicate relative motion
695 if(event->button == Button5) dir = -1;
696 if(dir) SFvSliderMovedCallback(w, n, dir); else
702 SFbuttonReleaseList(w, n, event)
705 XButtonReleasedEvent *event;
708 static int lastClick;
710 if(event->button == Button4 || event->button == Button5) return; // [HGM] mouse wheel does not select
713 if (SFcurrentInvert[n] != -1) {
715 SFdoNotTouchDirPtr = 1;
717 SFdoNotTouchVorigin = 1;
718 dir = &(SFdirs[SFdirPtr + n]);
721 dir->entries[dir->vOrigin + SFcurrentInvert[n]].shown
723 SFmotionList(w, n, (XMotionEvent *) event);
724 if(lastClick == 256*n + SFcurrentInvert[n]) SFstatus = SEL_FILE_OK; // [HGM] double click implies OK
726 lastClick = 256*n + SFcurrentInvert[n];
738 (!stat(".", &statBuf)) &&
739 (statBuf.st_mtime != dir->mtime)
743 * If the pointer is currently in the window that we are about
744 * to update, we must warp it to prevent the user from
745 * accidentally selecting the wrong file.
747 if (SFcurrentInvert[n] != -1) {
751 XtWindow(selFileLists[n]),
761 for (i = dir->nEntries - 1; i >= 0; i--) {
762 if (dir->entries[i].shown != dir->entries[i].real) {
763 XtFree(dir->entries[i].shown);
765 XtFree(dir->entries[i].real);
767 XtFree((char *) dir->entries);
769 SFunreadableDir(dir);
771 if (dir->vOrigin > dir->nEntries - SFlistSize) {
772 dir->vOrigin = dir->nEntries - SFlistSize;
774 if (dir->vOrigin < 0) {
777 if (dir->hOrigin > dir->nChars - SFcharsPerEntry) {
778 dir->hOrigin = dir->nChars - SFcharsPerEntry;
780 if (dir->hOrigin < 0) {
783 dir->beginSelection = -1;
784 dir->endSelection = -1;
785 SFdoNotTouchVorigin = 1;
786 if ((dir + 1)->dir) {
787 (void) SFfindFile(dir, (dir + 1)->dir);
789 (void) SFfindFile(dir, dir->path);
792 if (!SFworkProcAdded) {
793 (void) XtAppAddWorkProc(SFapp, SFworkProc, NULL);
803 /* Return a single character describing what kind of file STATBUF is. */
807 struct stat *statBuf;
809 if (S_ISDIR (statBuf->st_mode)) {
811 } else if (S_ISREG (statBuf->st_mode)) {
812 return S_ISXXX (statBuf->st_mode) ? '*' : ' ';
814 } else if (S_ISSOCK (statBuf->st_mode)) {
816 #endif /* S_ISSOCK */
837 to = dir->vOrigin + SFlistSize;
838 if (to > dir->nEntries) {
842 for (i = from; i < to; i++) {
843 str = dir->entries[i].real;
844 last = strlen(str) - 1;
847 if (stat(str, &statBuf)) {
850 new = SFstatChar(&statBuf);
862 SFdirModTimer(cl, id)
871 if ((!SFtwiddle) && (SFdirPtr < SFdirEnd)) {
873 if ((n > NR-1) || (SFdirPtr + n >= SFdirEnd)) {
876 if ((f > NR-1) || (SFdirPtr + f >= SFdirEnd)) {
880 dir = &(SFdirs[SFdirPtr + n]);
883 if (SFchdir(SFcurrentPath)) {
895 SFcheckDir(n, dir) ||
896 ((f == n) && SFcheckFiles(dir))
898 SFdrawList(n, SF_DO_SCROLL);
903 SFdirModTimerId = XtAppAddTimeOut(SFapp, (unsigned long) 1000,
904 SFdirModTimer, (XtPointer) NULL);