-
Notifications
You must be signed in to change notification settings - Fork 0
/
xvdir.c
2088 lines (1594 loc) · 54.9 KB
/
xvdir.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* xvdir.c - Directory changin', file i/o dialog box
*
* callable functions:
*
* CreateDirW(geom,bwidth)- creates the dirW window. Doesn't map it.
* DirBox(vis) - random processing based on value of 'vis'
* maps/unmaps window, etc.
* ClickDirW() - handles mouse clicks in DirW
* LoadCurrentDirectory() - loads up current dir information for dirW
* GetDirPath() - returns path that 'dirW' is looking at
* DoSave() - calls appropriate save routines
* SetDirFName() - sets the 'load/save-as' filename and default
* GetDirFName() - gets the 'load/save-as' filename (no path)
* SetDirSaveMode() - sets default format/color settings
*
* InitPoll() - called whenever a file is first loaded
* CheckPoll(int) - checks to see whether we should reload
*/
#include "copyright.h"
#define NEEDSTIME /* for CheckPoll */
#define NEEDSDIR
#include "xv.h"
#include "bits/d_load"
#include "bits/d_save"
#ifndef VMS
#include <pwd.h> /* for getpwnam() prototype and passwd struct */
#endif
#define DIRWIDE 350 /* (fixed) size of directory window */
#define DIRHIGH 400
#define NLINES 15 /* # of lines in list control (keep odd) */
#define LISTWIDE 237 /* width of list window */
#define BUTTW 60 /* width of buttons */
#define BUTTH 24 /* height of buttons */
#define DDWIDE (LISTWIDE-80+15) /* max width of dirMB */
#define DNAMWIDE 252 /* width of 'file name' entry window */
#define MAXDEEP 30 /* max num of directories in cwd path */
#define MAXFNLEN 256 /* max len of filename being entered */
#define FMTLABEL "Format:" /* label shown next to fmtMB */
#define COLLABEL "Colors:" /* label shown next to colMB */
#define FMTWIDE 150 /* width of fmtMB */
#define COLWIDE 150 /* width of colMB */
/* NOTE: make sure these match up with F_* definitions in xv.h */
static char *saveColors[] = { "Full Color",
"Greyscale",
"B/W Dithered",
"Reduced Color" };
static char *saveFormats[] = { "GIF",
#ifdef HAVE_JPEG
"JPEG",
#endif
#ifdef HAVE_TIFF
"TIFF",
#endif
#ifdef HAVE_PNG
"PNG",
#endif
"PostScript",
"PBM/PGM/PPM (raw)",
"PBM/PGM/PPM (ascii)",
"X11 Bitmap",
"XPM",
"BMP",
"Sun Rasterfile",
"IRIS RGB",
"Targa (24-bit)",
"FITS",
"PM",
MBSEP,
"Filename List"};
static void arrangeButts PARM((int));
static void RedrawDList PARM((int, SCRL *));
static void changedDirMB PARM((int));
static int dnamcmp PARM((const void *, const void *));
static int FNameCdable PARM((void));
static void loadCWD PARM((void));
#ifdef FOO
static int cd_able PARM((char *));
#endif
static void scrollToFileName PARM((void));
static void setFName PARM((char *));
static void showFName PARM((void));
static void changeSuffix PARM((void));
static int autoComplete PARM((void));
static byte *handleBWandReduced PARM((byte *, int,int,int, int, int *,
byte **, byte **, byte **));
static byte *handleNormSel PARM((int *, int *, int *, int *));
static char *fnames[MAXNAMES];
static int numfnames = 0, ndirs = 0;
static char path[MAXPATHLEN+1]; /* '/' terminated */
static char loadpath[MAXPATHLEN+1]; /* '/' terminated */
static char savepath[MAXPATHLEN+1]; /* '/' terminated */
static char *dirs[MAXDEEP]; /* list of directory names */
static char *dirMBlist[MAXDEEP]; /* list of dir names in right order */
static char *lastdir; /* name of the directory we're in */
static char filename[MAXFNLEN+100]; /* filename being entered */
static char deffname[MAXFNLEN+100]; /* default filename */
static int savemode; /* if 0 'load box', if 1 'save box' */
static int curPos, stPos, enPos; /* filename textedit stuff */
static MBUTT dirMB; /* popup path menu */
static MBUTT fmtMB; /* 'format' menu button (Save only) */
static MBUTT colMB; /* 'colors' menu button (Save only) */
static Pixmap d_loadPix, d_savePix;
static int haveoldinfo = 0;
static int oldformat, oldcolors;
static char oldfname[MAXFNLEN+100];
/* the name of the file actually opened. (the temp file if we are piping) */
static char outFName[256];
static int dopipe;
/***************************************************/
void CreateDirW(geom)
char *geom;
{
path[0] = '\0';
xv_getwd(loadpath, sizeof(loadpath));
xv_getwd(savepath, sizeof(savepath));
dirW = CreateWindow("","XVdir", geom, DIRWIDE, DIRHIGH, infofg, infobg, 0);
if (!dirW) FatalError("couldn't create 'directory' window!");
LSCreate(&dList, dirW, 10, 5 + 3*(6+LINEHIGH) + 6, LISTWIDE,
LINEHIGH*NLINES, NLINES, fnames, numfnames, infofg, infobg,
hicol, locol, RedrawDList, 1, 0);
dnamW = XCreateSimpleWindow(theDisp, dirW, 80, dList.y + (int) dList.h + 30,
(u_int) DNAMWIDE+6, (u_int) LINEHIGH+5,
1, infofg, infobg);
if (!dnamW) FatalError("can't create name window");
XSelectInput(theDisp, dnamW, ExposureMask);
CBCreate(&browseCB, dirW, DIRWIDE/2, dList.y + (int) dList.h + 6,
"Browse", infofg, infobg, hicol,locol);
CBCreate(&savenormCB, dirW, 220, dList.y + (int) dList.h + 6,
"Normal Size", infofg, infobg,hicol,locol);
CBCreate(&saveselCB, dirW, 80, dList.y + (int) dList.h + 6,
"Selected Area", infofg, infobg,hicol,locol);
/* y-coordinates get filled in when window is opened */
BTCreate(&dbut[S_BOK], dirW, 259, 0, 80, BUTTH,
"Ok", infofg, infobg,hicol,locol);
BTCreate(&dbut[S_BCANC], dirW, 259, 0, 80, BUTTH,
"Cancel", infofg,infobg,hicol,locol);
BTCreate(&dbut[S_BRESCAN], dirW, 259, 0, 80, BUTTH,
"Rescan", infofg,infobg,hicol,locol);
BTCreate(&dbut[S_BOLDSET], dirW, 259, 0, 80, BUTTH,
"Prev Set", infofg,infobg,hicol,locol);
BTCreate(&dbut[S_BOLDNAM], dirW, 259, 0, 80, BUTTH,
"Prev Name", infofg,infobg,hicol,locol);
SetDirFName("");
XMapSubwindows(theDisp, dirW);
numfnames = 0;
/*
* create MBUTTs *after* calling XMapSubWindows() to keep popup unmapped
*/
MBCreate(&dirMB, dirW, 50, dList.y -(LINEHIGH+6),
(u_int) DDWIDE, (u_int) LINEHIGH, NULL, NULL, 0,
infofg,infobg,hicol,locol);
MBCreate(&fmtMB, dirW, DIRWIDE-FMTWIDE-10, 5,
(u_int) FMTWIDE, (u_int) LINEHIGH, NULL, saveFormats, F_MAXFMTS,
infofg,infobg,hicol,locol);
fmtMB.hascheck = 1;
MBSelect(&fmtMB, 0);
MBCreate(&colMB, dirW, DIRWIDE-COLWIDE-10, 5+LINEHIGH+6,
(u_int) COLWIDE, (u_int) LINEHIGH, NULL, saveColors, F_MAXCOLORS,
infofg,infobg,hicol,locol);
colMB.hascheck = 1;
MBSelect(&colMB, 0);
d_loadPix = XCreatePixmapFromBitmapData(theDisp, dirW,
(char *) d_load_bits, d_load_width, d_load_height,
infofg, infobg, dispDEEP);
d_savePix = XCreatePixmapFromBitmapData(theDisp, dirW,
(char *) d_save_bits, d_save_width, d_save_height,
infofg, infobg, dispDEEP);
}
/***************************************************/
void DirBox(mode)
int mode;
{
static int firstclose = 1;
if (!mode) {
if (savemode) strcpy(savepath, path);
else strcpy(loadpath, path);
if (firstclose) {
strcpy(loadpath, path);
strcpy(savepath, path);
firstclose = 0;
}
XUnmapWindow(theDisp, dirW); /* close */
}
else if (mode == BLOAD) {
strcpy(path, loadpath);
WaitCursor(); LoadCurrentDirectory(); SetCursors(-1);
XStoreName(theDisp, dirW, "xv load");
XSetIconName(theDisp, dirW, "xv load");
dbut[S_BLOADALL].str = "Load All";
BTSetActive(&dbut[S_BLOADALL], 1);
arrangeButts(mode);
MBSetActive(&fmtMB, 0);
MBSetActive(&colMB, 0);
CenterMapWindow(dirW, dbut[S_BOK].x+30, dbut[S_BOK].y + BUTTH/2,
DIRWIDE, DIRHIGH);
savemode = 0;
}
else if (mode == BSAVE) {
strcpy(path, savepath);
WaitCursor(); LoadCurrentDirectory(); SetCursors(-1);
XStoreName(theDisp, dirW, "xv save");
XSetIconName(theDisp, dirW, "xv save");
dbut[S_BOLDSET].str = "Prev Set";
arrangeButts(mode);
BTSetActive(&dbut[S_BOLDSET], haveoldinfo);
BTSetActive(&dbut[S_BOLDNAM], haveoldinfo);
CBSetActive(&saveselCB, HaveSelection());
MBSetActive(&fmtMB, 1);
if (MBWhich(&fmtMB) == F_FILELIST) {
MBSetActive(&colMB, 0);
CBSetActive(&savenormCB, 0);
}
else {
MBSetActive(&colMB, 1);
CBSetActive(&savenormCB, 1);
}
CenterMapWindow(dirW, dbut[S_BOK].x+30, dbut[S_BOK].y + BUTTH/2,
DIRWIDE, DIRHIGH);
savemode = 1;
}
scrollToFileName();
dirUp = mode;
BTSetActive(&but[BLOAD], !dirUp);
BTSetActive(&but[BSAVE], !dirUp);
}
/***************************************************/
static void arrangeButts(mode)
int mode;
{
int i, nbts, ngaps, szdiff, top, gap;
nbts = (mode==BLOAD) ? S_LOAD_NBUTTS : S_NBUTTS;
ngaps = nbts-1;
szdiff = dList.h - (nbts * BUTTH);
gap = szdiff / ngaps;
if (gap>16) {
gap = 16;
top = dList.y + (dList.h - (nbts*BUTTH) - (ngaps*gap))/2;
for (i=0; i<nbts; i++) dbut[i].y = top + i*(BUTTH+gap);
}
else {
for (i=0; i<nbts; i++)
dbut[i].y = dList.y + ((dList.h-BUTTH)*i) / ngaps;
}
}
/***************************************************/
void RedrawDirW(x,y,w,h)
int x,y,w,h;
{
int i, ypos, txtw;
char foo[30], *str;
if (dList.nstr==1) strcpy(foo,"1 file");
else sprintf(foo,"%d files",dList.nstr);
ypos = dList.y + dList.h + 8 + ASCENT;
XSetForeground(theDisp, theGC, infobg);
XFillRectangle(theDisp, dirW, theGC, 10, ypos-ASCENT,
(u_int) DIRWIDE, (u_int) CHIGH);
XSetForeground(theDisp, theGC, infofg);
DrawString(dirW, 10, ypos, foo);
if (dirUp == BLOAD) str = "Load file:";
else str = "Save file:";
DrawString(dirW, 10, dList.y + (int) dList.h + 30 + 4 + ASCENT, str);
/* draw dividing line */
XSetForeground(theDisp, theGC, infofg);
XDrawLine(theDisp, dirW, theGC, 0, dirMB.y-6, DIRWIDE, dirMB.y-6);
if (ctrlColor) {
XSetForeground(theDisp, theGC, locol);
XDrawLine(theDisp, dirW, theGC, 0, dirMB.y-5, DIRWIDE, dirMB.y-5);
XSetForeground(theDisp, theGC, hicol);
}
XDrawLine(theDisp, dirW, theGC, 0, dirMB.y-4, DIRWIDE, dirMB.y-4);
for (i=0; i<(savemode ? S_NBUTTS : S_LOAD_NBUTTS); i++) BTRedraw(&dbut[i]);
MBRedraw(&dirMB);
MBRedraw(&fmtMB);
MBRedraw(&colMB);
XSetForeground(theDisp, theGC, infofg);
XSetBackground(theDisp, theGC, infobg);
txtw = StringWidth(FMTLABEL);
if (StringWidth(COLLABEL) > txtw) txtw = StringWidth(COLLABEL);
if (!savemode) {
XCopyArea(theDisp, d_loadPix, dirW, theGC, 0,0,d_load_width,d_load_height,
10, (dirMB.y-6)/2 - d_load_height/2);
XSetFillStyle(theDisp, theGC, FillStippled);
XSetStipple(theDisp, theGC, dimStip);
DrawString(dirW, fmtMB.x-6-txtw, 5+3+ASCENT, FMTLABEL);
DrawString(dirW, fmtMB.x-6-txtw, 5+3+ASCENT + (LINEHIGH+6), COLLABEL);
XSetFillStyle(theDisp,theGC,FillSolid);
CBRedraw(&browseCB);
}
else {
XCopyArea(theDisp, d_savePix, dirW, theGC, 0,0,d_save_width,d_save_height,
10, (dirMB.y-6)/2 - d_save_height/2);
XSetForeground(theDisp, theGC, infofg);
DrawString(dirW, fmtMB.x-6-txtw, 5+3+ASCENT, FMTLABEL);
DrawString(dirW, fmtMB.x-6-txtw, 5+3+ASCENT + (LINEHIGH+6), COLLABEL);
CBRedraw(&savenormCB);
CBRedraw(&saveselCB);
}
}
/***************************************************/
int ClickDirW(x,y)
int x,y;
{
BUTT *bp;
int bnum,i,maxbut,v;
char buf[1024];
if (savemode) { /* check format/colors MBUTTS */
i = v = 0;
if (MBClick(&fmtMB, x,y) && (v=MBTrack(&fmtMB))>=0) i=1;
else if (MBClick(&colMB, x,y) && (v=MBTrack(&colMB))>=0) i=2;
if (i) { /* changed one of them */
if (i==1) SetDirSaveMode(F_FORMAT, v);
else SetDirSaveMode(F_COLORS, v);
changeSuffix();
}
}
if (!savemode) { /* LOAD */
if (CBClick(&browseCB,x,y)) CBTrack(&browseCB);
}
else { /* SAVE */
if (CBClick(&savenormCB,x,y)) CBTrack(&savenormCB);
else if (CBClick(&saveselCB,x,y)) CBTrack(&saveselCB);
}
maxbut = (savemode) ? S_NBUTTS : S_LOAD_NBUTTS;
for (bnum=0; bnum<maxbut; bnum++) {
bp = &dbut[bnum];
if (PTINRECT(x, y, bp->x, bp->y, bp->w, bp->h)) break;
}
if (bnum<maxbut && BTTrack(bp)) { /* found one */
if (bnum<S_BOLDSET) return bnum; /* do Ok,Cancel,Rescan in xvevent.c */
if (bnum == S_BOLDSET && savemode && haveoldinfo) {
MBSelect(&fmtMB, oldformat);
MBSelect(&colMB, oldcolors);
changeSuffix();
}
else if (bnum == S_BOLDNAM && savemode && haveoldinfo) {
setFName(oldfname);
}
else if (bnum == S_BLOADALL && !savemode) {
int j, oldnumnames;
char *dname;
oldnumnames = numnames;
for (i=0; i<numfnames && numnames<MAXNAMES; i++) {
if (fnames[i][0] == C_REG || fnames[i][0] == C_EXE) {
sprintf(buf,"%s%s", path, fnames[i]+1);
/* check for dups. Don't add it if it is. */
for (j=0; j<numnames && strcmp(buf,namelist[j]); j++);
if (j==numnames) { /* add to list */
namelist[numnames] = (char *) malloc(strlen(buf)+1);
if (!namelist[numnames]) FatalError("out of memory!\n");
strcpy(namelist[numnames],buf);
dname = namelist[numnames];
/* figure out how much of name can be shown */
if (StringWidth(dname) > (nList.w-10-16)) { /* truncate */
char *tmp;
int prelen = 0;
tmp = dname;
while (1) {
tmp = (char *) index(tmp,'/'); /* find next '/' in buf */
if (!tmp) break;
tmp++; /* move to char following the '/' */
prelen = tmp - dname;
if (StringWidth(tmp) <= (nList.w-10-16)) break; /* cool now */
}
dispnames[numnames] = dname + prelen;
}
else dispnames[numnames] = dname;
numnames++;
}
}
}
if (oldnumnames != numnames) { /* added some */
if (numnames>0) BTSetActive(&but[BDELETE],1);
windowMB.dim[WMB_TEXTVIEW] = (numnames==0);
LSNewData(&nList, dispnames, numnames);
nList.selected = oldnumnames;
curname = oldnumnames - 1;
ActivePrevNext();
ScrollToCurrent(&nList);
DrawCtrlNumFiles();
if (!browseCB.val) DirBox(0);
}
}
}
if (MBClick(&dirMB, x, y)) {
i = MBTrack(&dirMB);
if (i >= 0) changedDirMB(i);
}
return -1;
}
/***************************************************/
void SelectDir(n)
int n;
{
/* called when entry #n in the dir list was selected/double-clicked */
/* if n<0, nothing was double-clicked, but perhaps the selection
has changed. Copy the selection to the filename if a) we're in
the 'load' box, and b) it's not a directory name */
if (n<0) {
if (dList.selected>=0)
setFName(dList.str[dList.selected]+1);
return;
}
/* can just pretend 'enter' was hit on a double click, as the original
click would've copied the string to filename */
if (!DirCheckCD()) FakeButtonPress(&dbut[S_BOK]);
}
/***************************************************/
static void changedDirMB(sel)
int sel;
{
if (sel != 0) { /* changed directories */
char tmppath[MAXPATHLEN+1], *trunc_point;
/* end 'path' by changing trailing '/' (of dir name) to a '\0' */
trunc_point = (dirs[(ndirs-1)-sel + 1] - 1);
*trunc_point = '\0';
if (path[0] == '\0') {
/* special case: if cd to '/', fix path (it's currently "") */
#ifdef apollo /*** Apollo DomainOS uses // as the network root ***/
strcpy(tmppath,"//");
#else
strcpy(tmppath,"/");
#endif
}
else strcpy(tmppath, path);
#ifdef VMS
/*
* The VMS chdir always needs 2 components (device and directory),
* so convert "/device" to "/device/000000" and convert
* "/" to "/XV_Root_Device/000000" (XV_Root_Device will need to be
* a special concealed device setup to provide a list of available
* disks).
*/
if ( ((ndirs-sel) == 2) && (strlen(tmppath) > 1) )
strcat ( tmppath, "/000000" ); /* add root dir for device */
else if ((ndirs-sel) == 1 ) {
strcpy ( tmppath, "/XV_Root_Device/000000" ); /* fake top level */
}
#endif
if (chdir(tmppath)) {
char str[512];
sprintf(str,"Unable to cd to '%s'\n", tmppath);
*trunc_point = '/'; /* restore the path */
MBRedraw(&dirMB);
ErrPopUp(str, "\nWhatever");
}
else {
loadCWD();
}
}
}
/***************************************************/
static void RedrawDList(delta, sptr)
int delta;
SCRL *sptr;
{
LSRedraw(&dList,delta);
}
/***************************************************/
static void loadCWD()
{
/* loads up current-working-directory into load/save list */
xv_getwd(path, sizeof(path));
LoadCurrentDirectory();
}
/***************************************************/
void LoadCurrentDirectory()
{
/* rescans current load/save directory */
DIR *dirp;
int i, j, ftype, mode, changedDir;
struct stat st;
char *dbeg, *dend;
static char oldpath[MAXPATHLEN + 2] = { '\0' };
#ifdef NODIRENT
struct direct *dp;
#else
struct dirent *dp;
#endif
/* get rid of previous file names */
for (i=0; i<numfnames; i++) free(fnames[i]);
numfnames = 0;
/* get rid of old dirMBlist */
for (i=0; i<ndirs; i++) free(dirMBlist[i]);
#ifndef VMS
if (strlen(path) == 0) xv_getwd(path, sizeof(path)); /* no dir, use cwd */
#else
xv_getwd(path, sizeof(path));
#endif
if (chdir(path)) {
ErrPopUp("Current load/save directory seems to have gone away!",
"\nYikes!");
#ifdef apollo
strcpy(path,"//");
#else
strcpy(path,"/");
#endif
chdir(path);
}
changedDir = strcmp(path, oldpath);
strcpy(oldpath, path);
if ((strlen(path) > (size_t) 1) && path[strlen(path)-1] != '/')
strcat(path,"/"); /* tack on a trailing '/' to make path consistent */
/* path will be something like: "/u3/bradley/src/weiner/whatever/" */
/* parse path into individual directory names */
dbeg = dend = path;
for (i=0; i<MAXDEEP && dend; i++) {
dend = (char *) index(dbeg,'/'); /* find next '/' char */
#ifdef apollo
/** On apollos the path will be something like //machine/users/foo/ **/
/** handle the initial // **/
if ((dend == dbeg ) && (dbeg[0] == '/') && (dbeg[1] == '/')) dend += 1;
#endif
dirs[i] = dbeg;
dbeg = dend+1;
}
ndirs = i-1;
/* build dirMBlist */
for (i=ndirs-1,j=0; i>=0; i--,j++) {
size_t stlen = (i<(ndirs-1)) ? dirs[i+1] - dirs[i] : strlen(dirs[i]);
dirMBlist[j] = (char *) malloc(stlen+1);
if (!dirMBlist[j]) FatalError("unable to malloc dirMBlist[]");
strncpy(dirMBlist[j], dirs[i], stlen);
dirMBlist[j][stlen] = '\0';
}
lastdir = dirs[ndirs-1];
dirMB.list = dirMBlist;
dirMB.nlist = ndirs;
XClearArea(theDisp, dirMB.win, dirMB.x, dirMB.y,
(u_int) dirMB.w+3, (u_int) dirMB.h+3, False);
i = StringWidth(dirMBlist[0]) + 10;
dirMB.x = dirMB.x + dirMB.w/2 - i/2;
dirMB.w = i;
MBRedraw(&dirMB);
dirp = opendir(".");
if (!dirp) {
LSNewData(&dList, fnames, 0);
RedrawDirW(0,0,DIRWIDE,DIRHIGH);
return;
}
WaitCursor();
i=0;
while ( (dp = readdir(dirp)) != NULL) {
if (strcmp(dp->d_name, ".")==0 ||
(strcmp(dp->d_name, "..")==0 &&
(strcmp(path,"/")==0 || strcmp(path,"//")==0)) ||
strcmp(dp->d_name, THUMBDIR)==0) {
/* skip over '.' and '..' and THUMBDIR */
}
else {
if (i == MAXNAMES) {
fprintf(stderr,
"%s: too many directory entries. Only using first %d.\n",
cmd, MAXNAMES);
break;
}
if ((i&31)==0) WaitCursor();
fnames[i] = (char *) malloc(strlen(dp->d_name)+2); /* +2=ftype + '\0' */
if (!fnames[i]) FatalError("malloc error while reading directory");
strcpy(fnames[i]+1, dp->d_name);
/* figure out what type of file the beastie is */
fnames[i][0] = C_REG; /* default to normal file, if stat fails */
#ifdef VMS
/* For VMS we will default all files EXCEPT directories to avoid
the high cost of the VAX C implementation of the stat function.
Suggested by Kevin Oberman ([email protected]) */
if (xv_strstr (fnames[i]+1, ".DIR") != NULL) fnames[i][0] = C_DIR;
if (xv_strstr (fnames[i]+1, ".EXE") != NULL) fnames[i][0] = C_EXE;
if (xv_strstr (fnames[i]+1, ".OBJ") != NULL) fnames[i][0] = C_BLK;
#else
if (!nostat && (stat(fnames[i]+1, &st)==0)) {
mode = st.st_mode & 0777; /* rwx modes */
ftype = st.st_mode;
if (S_ISDIR(ftype)) fnames[i][0] = C_DIR;
else if (S_ISCHR(ftype)) fnames[i][0] = C_CHR;
else if (S_ISBLK(ftype)) fnames[i][0] = C_BLK;
else if (S_ISLINK(ftype)) fnames[i][0] = C_LNK;
else if (S_ISFIFO(ftype)) fnames[i][0] = C_FIFO;
else if (S_ISSOCK(ftype)) fnames[i][0] = C_SOCK;
else if (fnames[i][0] == C_REG && (mode&0111)) fnames[i][0] = C_EXE;
}
else {
/* fprintf(stderr,"problems 'stat-ing' files\n");*/
fnames[i][0] = C_REG;
}
#endif /* VMS */
i++;
}
}
closedir(dirp);
numfnames = i;
qsort((char *) fnames, (size_t) numfnames, sizeof(char *), dnamcmp);
if (changedDir) LSNewData(&dList, fnames, numfnames);
else LSChangeData(&dList, fnames, numfnames);
RedrawDirW(0,0,DIRWIDE,DIRHIGH);
SetCursors(-1);
}
/***************************************************/
void GetDirPath(buf)
char *buf;
{
/* returns current 'dirW' path. buf should be MAXPATHLEN long */
strcpy(buf, path);
}
/***************************************************/
#ifdef FOO
static int cd_able(str)
char *str;
{
return ((str[0] == C_DIR || str[0] == C_LNK));
}
#endif
/***************************************************/
static int dnamcmp(p1,p2)
const void *p1, *p2;
{
char **s1, **s2;
s1 = (char **) p1;
s2 = (char **) p2;
#ifdef FOO
/* sort so that directories are at beginning of list */
/* if both dir/lnk or both NOT dir/lnk, sort on name */
if ( ( cd_able(*s1) && cd_able(*s2)) ||
(!cd_able(*s1) && !cd_able(*s2)))
return (strcmp((*s1)+1, (*s2)+1));
else if (cd_able(*s1)) return -1; /* s1 is first */
else return 1; /* s2 is first */
#else
/* sort in pure alpha order */
return(strcmp((*s1)+1, (*s2)+1));
#endif
}
/***************************************************/
int DirKey(c)
int c;
{
/* got keypress in dirW. stick on end of filename */
int len;
len = strlen(filename);
if (c>=' ' && c<'\177') { /* printable characters */
/* note: only allow 'piped commands' in savemode... */
/* only allow spaces in 'piped commands', not filenames */
if (c==' ' && (!ISPIPE(filename[0]) || curPos==0)) return (-1);
/* only allow vertbars in 'piped commands', not filenames */
if (c=='|' && curPos!=0 && !ISPIPE(filename[0])) return(-1);
if (len >= MAXFNLEN-1) return(-1); /* max length of string */
xvbcopy(&filename[curPos], &filename[curPos+1], (size_t) (len-curPos+1));
filename[curPos]=c; curPos++;
scrollToFileName();
}
else if (c=='\010' || c=='\177') { /* BS or DEL */
if (curPos==0) return(-1); /* at beginning of str */
xvbcopy(&filename[curPos], &filename[curPos-1], (size_t) (len-curPos+1));
curPos--;
if (strlen(filename) > (size_t) 0) scrollToFileName();
}
else if (c=='\025') { /* ^U: clear entire line */
filename[0] = '\0';
curPos = 0;
}
else if (c=='\013') { /* ^K: clear to end of line */
filename[curPos] = '\0';
}
else if (c=='\001') { /* ^A: move to beginning */
curPos = 0;
}
else if (c=='\005') { /* ^E: move to end */
curPos = len;
}
else if (c=='\004') { /* ^D: delete character at curPos */
if (curPos==len) return(-1);
xvbcopy(&filename[curPos+1], &filename[curPos], (size_t) (len-curPos));
}
else if (c=='\002') { /* ^B: move backwards char */
if (curPos==0) return(-1);
curPos--;
}
else if (c=='\006') { /* ^F: move forwards char */
if (curPos==len) return(-1);
curPos++;
}
else if (c=='\012' || c=='\015') { /* CR or LF */
if (!DirCheckCD()) FakeButtonPress(&dbut[S_BOK]);
}
else if (c=='\033') { /* ESC = Cancel */
FakeButtonPress(&dbut[S_BCANC]);
}
else if (c=='\011') { /* tab = filename expansion */
if (!autoComplete()) XBell(theDisp, 0);
else {
curPos = strlen(filename);
scrollToFileName();
}
}
else return(-1); /* unhandled character */
showFName();
/* if we cleared out filename, clear out deffname as well */
if (!filename[0]) deffname[0] = '\0';
return(0);
}
/***************************************************/
static int autoComplete()
{
/* called to 'auto complete' a filename being entered. If the name that
has been entered so far is anything but a simple filename (ie, has
spaces, pipe char, '/', etc) fails. If it is a simple filename,
looks through the name list to find something that matches what's already
been typed. If nothing matches, it fails. If more than one thing
matches, it sets the name to the longest string that the multiple
matches have in common, and succeeds (and beeps).
If only one matches, sets the string to the match and succeeds.
returns zero on failure, non-zero on success */
int i, firstmatch, slen, nummatch, cnt;
/* is filename a simple filename? */
if (strlen(filename)==0 ||
ISPIPE(filename[0]) ||
index(filename, '/') ||
filename[0]=='~' ) return 0;
slen = strlen(filename);
for (i=0; i<dList.nstr; i++) {
if (strncmp(filename, dList.str[i]+1, (size_t) slen) <= 0) break;
}
if (i==dList.nstr) return 0;
if (strncmp(filename, dList.str[i]+1, (size_t) slen) < 0) return 0;
/* there's a match of some sort... */
firstmatch = i;
/* count # of matches */
for (i=firstmatch, nummatch=0;
i<dList.nstr && strncmp(filename, dList.str[i]+1, (size_t) slen)==0;
i++, nummatch++);
if (nummatch == 1) { /* only one match */
strcpy(filename, dList.str[firstmatch]+1);
return 1;
}
/* compute longest common prefix among the matches */
while (dList.str[firstmatch][slen+1]!='\0') {
filename[slen] = dList.str[firstmatch][slen+1];
slen++; filename[slen] = '\0';
for (i=firstmatch, cnt=0;
i<dList.nstr && strncmp(filename, dList.str[i]+1, (size_t) slen)==0;
i++, cnt++);
if (cnt != nummatch) { slen--; filename[slen] = '\0'; break; }
}
XBell(theDisp, 0);
return 1;
}
/***************************************************/
static void scrollToFileName()
{
int i, hi, lo, pos, cmp;
/* called when 'fname' changes. Tries to scroll the directory list
so that fname would be centered in it */
/* nothing to do if scrlbar not enabled ( <= NLINES names in list) */
if (dList.scrl.max <= 0) return;
/* find the position in the namelist that the current name should be at
(binary search) */
pos = 0; lo = 0; hi = dList.nstr-1;
i = strlen(filename);
if (!i) { SCSetVal(&dList.scrl, 0); return; }
while ((hi-lo)>=0) {
pos = lo + (hi-lo)/2;
cmp = strcmp(filename, dList.str[pos]+1);