-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgcscreen.c
2747 lines (2625 loc) · 56.7 KB
/
gcscreen.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
/*
* gc3 - Gram's Commander v3.3: A configureable file manager
*
* YAGH! (tm) (Yet Another Gramish Hack)
*
* (c) 1994 by Graham Wheeler. All Rights Reserved.
*
* DISCLAIMER: The author is not responsible for any loss, damage,
* riots, earthquakes, catastrophes, marriages, annulments,
* etc that arise from the use of this program. Sorry...
*
* This software may be freely copied and distributed, provided it is
* distributed with all files intact and unmodified. You may use this
* software for a 30 day trial period, after which you should register
* your copy with me. I have spent a large number of weekends and nights
* trying to make a file manager for UNIX (and DOS) which combines ease
* of use and power, and complements the power of UNIX to process the
* contents of files with the power to *select* the files to be processed.
* By registering, you will encourage me to keep improving gc3, instead
* of seeking paying work which benefits only a few.
*
* Full details of how to register, and what you will receive if
* you do, are contained in the file LEGAL.DOC. Of course, if you
* don't register, I can't do much about it, but in that case
* may an obscure bug in gc3 erase all of your files!
*
* Enjoy using GC!
*
* See INSTALL.DOC for instructions on how to install GC3
*
* Version History
* ---------------
* v1.0 Nov 1992 Initial release
* v2.0 Nov 1992 Largely rewritten to be table-driven
* v2.1 Dec 1992 Support for non-ANSI C compilers contributed
* by George Sipe
* Support for old ioctls TCGETA and TCSETAW for
* those with no POSIX-style tcgetattr and tcsetattr.
* v2.2 Dec 1992 Termio ioctls removed.Hopefully much more
* portable. Command mode removed. A few bugs
* fixed. Placeholder replacement now done in
* a preprocessing phase before parsing commands.
* v2.3 Jan 1993 Split into a number of source files
* Basic port to MS-DOS
* v3.0 Feb/March '93 New script language; large parts rewritten
* v3.1 March '93 Ported back to UNIX and cleaned up
* v3.2b July '93 Many bugs fixed and features added
* v3.2 Oct '93 Viewer added, .gc3rul file handling
* added, bugs fixed, local variables and
* parameters, tree view and container handling.
* v3.3 April '94 Fixed lots of bugs. Large parts of compiler
* and interpreter are now table-driven. Changed
* screen repainting, and added support for colour
* and mono terminals. Added script-driven menus
* and forms, and hypertext help browser.
* Made windows moveable and resizeable.
* Made command handling in the script much more
* sophisticated with multiple prefix/suffixes.
* Improved command line history facility. Added
* repeat counts to most commands.
*/
/***********************************************************
GC3 SCREEN HANDLING
************************************************************/
#include "gcport.h"
#include "gc3.h"
#include "gckey.h"
#define GC_CURSES
#include "gclib.h"
int Lines;
static WINDOW *panels[2];
static WINDOW *panelTop[2];
static WINDOW *statusWin[2];
static WINDOW *statusTop[2];
static WINDOW *cmdWin;
int statusVis[2] =
{1, 1};
int listVis[2] =
{1, 1};
int topMargin = 0, leftMargin = 0;
int winWidth[2] =
{40, 40};
static int cmdWinCursorPos = 0;
int inCurses = 0;
char viewLine[256]; /* top line in view window; accessible in script
as $viewline */
static char ipTrace[MAX_COLS] =
{0, 0, 0, 0, 0};
char fileViewed[MAXPATHNAME] =
{0};
static FILE *viewFP = NULL;
static long *viewPos = NULL;
static long topLine = 0l, viewLines = 0l, viewSize = 0l;
static int viewHex = 0;
static int viewColumn = 0;
static char *space = " ";
#define PADBUF(w,b) ( strcat(BUF[b],space), BUF[b][winWidth[w]-2] = '\0' )
/*********************************************************************
O/S INDEPENDENT STUFF
**********************************************************************/
static int showFileCursor = 1;
#define CursorLine(n) (highlight[n]-startLn[n])
static char *Cursors[] =
{"->", "=>", " "};
#if __STDC__
void InitialiseScreenAttributes(void)
#else
void InitialiseScreenAttributes()
#endif
{
/* Set up very basic default screen attributes */
#ifdef __MSDOS__
initscr(); /* Do this now as it just gets the screen type (color/mono) */
#endif
setAttrEntry(0, -1, 0);
setAttrEntry(1, -1, 2);
setAttrEntry(2, -1, 2);
setAttrEntry(3, -1, 0);
}
#if __STDC__
int isListWindow(int n)
#else
int isListWindow(n)
int n;
#endif /* __STDC__ */
{
return (testOption(VAR_DISPLAYON) && listVis[n] &&
(winState[n] != W_VIEW || viewingDir));
}
#if __STDC__
void showCursor(int n, int sel)
#else
void showCursor(n, sel)
int n;
int sel;
#endif /* __STDC__ */
{
if (isListWindow(n))
{
mvwprintw(panels[n], CursorLine(n), 1, Cursors[sel]);
UPDATE(panels[n]);
}
}
#if __STDC__
void hideCursor(int n)
#else
void hideCursor(n)
int n;
#endif /* __STDC__ */
{
if (isListWindow(n))
mvwprintw(panels[n], CursorLine(n), 1, Cursors[2]);
}
#if __STDC__
int Truncate(char *str, int maxlen)
#else
int Truncate(str, maxlen)
char *str;
int maxlen;
#endif /* __STDC__ */
{
int len = strlen(str);
if (len > maxlen)
{
len = maxlen;
str[len] = 0;
}
return len;
}
#if __STDC__
void CvtCtrl(char *msg)
#else
void CvtCtrl(msg)
char *msg;
#endif /* __STDC__ */
{
while (*msg)
{
if (*msg < 32)
*msg = ' ';
msg++;
}
}
#if __STDC__
void fillString(char *str, int cnt, int ch)
#else
void fillString(str, cnt, ch)
char *str;
int cnt, ch;
#endif /* __STDC__ */
{
str[cnt] = 0;
while (--cnt >= 0)
str[cnt] = ch;
}
#if __STDC__
void writeMsg(char *msg)
#else
void writeMsg(msg)
char *msg;
#endif /* __STDC__ */
{
int len = Truncate(msg, COLS - 1);
CvtCtrl(msg);
if (inCurses)
{
clearCmdWin();
mvwprintw(cmdWin, 0, 0, msg);
cmdWinCursorPos = len + 1;
}
else
fprintf(stderr, "%s\n", msg);
}
#if __STDC__
void showMsg(char *msg)
#else
void showMsg(msg)
char *msg;
#endif /* __STDC__ */
{
writeMsg(msg);
if (inCurses)
{
UPDATE(cmdWin);
REFRESH();
}
}
#if __STDC__
void displayIP(int ipv)
#else
void displayIP(ipv)
int ipv;
#endif /* __STDC__ */
{
if (inCurses)
{
/* shunt over by 5 places */
strncpy(ipTrace, ipTrace + 5, 70);
sprintf(ipTrace + 70, "%5d", ipv);
ipTrace[75] = '\0';
showMsg(ipTrace);
}
else
fprintf(stderr, "%d\n", ip);
}
#if __STDC__
void clearCmdWin(void)
#else
void clearCmdWin()
#endif
{
if (inCurses)
{
#ifndef __MSDOS__
wattrset(cmdWin, 0);
#endif
werase(cmdWin);
cmdWinCursorPos = 0;
if (showIP)
displayIP(ip);
}
}
#if __STDC__
void fillAttribute(WINDOW * w, int width, int height)
#else
void fillAttribute(w, width, height)
WINDOW *w;
int width, height;
#endif
{
char buff[82];
int r;
fillString(buff, 80, ' ');
buff[width] = '\0';
for (r = 0; r < height; r++)
mvwprintw(w, r, 0, buff);
}
#if __STDC__
int gcEditWrap(int c, char *start, char **end, char **now,
int *_insmode, int maxlen)
#else
int gcEditWrap(c, start, end, now, _insmode, maxlen)
int c, *_insmode, maxlen;
char *start, **end, **now;
#endif
{
int rtn;
/* Do cursor updates in file window while in command line editor */
if (maxlen && showFileCursor)
hideCursor(l);
rtn = EditString(c, start, end, now, _insmode, maxlen);
if (maxlen && showFileCursor)
showCursor(l, 1);
return rtn;
}
#if __STDC__
int getInput(char *buf, int len, int mode)
#else
int getInput(buf, len, mode)
char *buf;
int len, mode;
#endif /* __STDC__ */
{
int saveLine = 0;
if (len < -1)
{
saveLine = 1;
len = -len;
}
if (len)
showCursor(l, 1);
myRead(cmdWin, 80, 0, cmdWinCursorPos, buf, len, &mode, gcEditWrap, my_getch);
if (len)
hideCursor(l);
if (buf[0] == '\0')
clearCmdWin();
else if (saveLine)
saveHist(buf);
return (int) strlen(buf);
}
#if __STDC__
static void showTitle(void)
#else
static void showTitle()
#endif /* __STDC__ */
{
WINDOW *w = newwin(13, 27, (LINES - 14) / 2, (COLS - 24) / 2);
selectAttrib(w, 0);
werase(w);
fillAttribute(w, 27, 13);
Box(w);
mvwprintw(w, 2, 6, "Gram's Commander");
mvwprintw(w, 4, 12, "by");
mvwprintw(w, 6, 6, "Graham Wheeler");
mvwprintw(w, 8, 2, "[email protected]");
mvwprintw(w, 10, 4, "Version %s (PL %s)", VERSION, PATCHLEVEL);
UPDATE(w);
REFRESH();
sleep(1);
delwin(w);
}
#if __STDC__
void showPath(int p)
#else
void showPath(p)
int p;
#endif /* __STDC__ */
{
int useAttr = (p == l);
int b;
char *buf;
if (!inCurses || !testOption(VAR_DISPLAYON) || !listVis[p])
return;
Box(panelTop[p]);
buf = BUF[b = GRABBUF()];
if (winState[p] == W_VIEW)
{
char *vf = file2view[0] ? file2view : INFO_NOW(1 - p).name;
if (!viewingDir && !viewHex)
sprintf(buf, "[Viewing %s (%ld/%d/%ld)]", vf, topLine,
viewColumn, viewSize);
else
sprintf(buf, "[Viewing %s]", vf);
}
else if (winState[p] == W_TREE)
getTreePath(p, buf);
else
{
int L1 = (int) strlen(paths[p]), L2 = (int) strlen(filters[p]);
char c = paths[p][L1 - 1];
if (L1 > (winWidth[p] - 6 - L2))
L1 -= (winWidth[p] - 6 - L2);
else
L1 = 0;
if (c != PATH_SEP)
sprintf(buf, "<%s%c%s>", paths[p] + L1, PATH_SEP, filters[p]);
else
sprintf(buf, "<%s%s>", paths[p] + L1, filters[p]);
}
BUF[b][winWidth[p] - 6] = '\0';
if (useAttr)
selectAttrib(panelTop[p], 1);
mvwprintw(panelTop[p], 0, 2, BUF[b]);
if (useAttr)
selectAttrib(panelTop[p], 0);
FREEBUF(b);
}
#ifndef __MSDOS__
#if __STDC__
char *GetUserID(int uid)
#else
char *GetUserID(uid)
int uid;
#endif
{
static char uname[40];
struct passwd *usr = getpwuid(uid);
if (usr && usr->pw_name)
strcpy(uname, usr->pw_name);
else if (uid == UNKNOWN_ID)
strcpy(uname, "?");
else
sprintf(uname, "%d", uid);
return uname;
}
#if __STDC__
char *GetGroupID(int gid)
#else
char *GetGroupID(gid)
int gid;
#endif
{
static char gname[40];
struct group *grp = getgrgid(gid);
if (grp && grp->gr_name)
strcpy(gname, grp->gr_name);
else if (gid == UNKNOWN_ID)
strcpy(gname, "?");
else
sprintf(gname, "%d", gid);
return gname;
}
#endif
#if __STDC__
static void showStatus(int n)
#else
static void showStatus(n)
int n;
#endif /* __STDC__ */
{
#ifdef __MSDOS__
long ts;
#endif
int buf, w = winWidth[n] - 2;
if (!inCurses || !testOption(VAR_DISPLAYON) || !statusVis[n])
return;
buf = GRABBUF();
selectAttrib(statusTop[n], 0);
Box(statusTop[n]);
selectAttrib(statusWin[n], 0);
if (winState[n] != W_VIEW) /* Viewer doesn't have status pane */
{
if (w >= 38)
sprintf(BUF[buf], " %4d files selected (%-9ld bytes)", selCnt[n], selSize[n]);
else if (w >= 12)
sprintf(BUF[buf], " %4d selected", selCnt[n]);
else
BUF[buf][0] = '\0';
#ifdef __MSDOS__
PADBUF(n, buf);
mvwprintw(statusWin[n], 1, 0, BUF[buf]);
sprintf(BUF[buf], " %s", asctime(localtime(&INFO_NOW(n).modtime)));
BUF[buf][strlen(BUF[buf]) - 1] = '\0'; /* clear out newline */
PADBUF(n, buf);
mvwprintw(statusWin[n], 0, 0, BUF[buf]);
ts = freeSpace[n] + usedSpace[n];
if (w >= 36)
sprintf(BUF[buf], " %7ldkb of %7ld kb free (%3d%%)",
freeSpace[n] / 1024l,
ts / 1024l, (int) (freeSpace[n] / (ts / 100l)));
else if (w > 15)
sprintf(BUF[buf], " %7ldkb free", freeSpace[n] / 1024l);
else
BUF[buf][0] = '\0';
PADBUF(n, buf);
mvwprintw(statusWin[n], 2, 0, BUF[buf]);
#else
PADBUF(n, buf);
mvwprintw(statusWin[n], 0, 0, BUF[buf]);
sprintf(BUF[buf], " %9s %10s/%-10s",
getPerms(INFO_NOW(n).mode),
GetUserID(INFO_NOW(n).uid),
GetGroupID(INFO_NOW(n).gid));
PADBUF(n, buf);
mvwprintw(statusWin[n], 1, 0, BUF[buf]);
sprintf(BUF[buf], " Modified %26s",
asctime(localtime(&INFO_NOW(n).modtime)));
BUF[buf][36] = 0; /* clear out the newline */
PADBUF(n, buf);
mvwprintw(statusWin[n], 2, 0, BUF[buf]);
sprintf(BUF[buf], " Accessed %26s",
asctime(localtime(&INFO_NOW(n).acctime)));
BUF[buf][36] = 0; /* clear out the newline */
PADBUF(n, buf);
mvwprintw(statusWin[n], 3, 0, BUF[buf]);
#endif
}
FREEBUF(buf);
}
/*
* showInfo - display the i'th entry for the n panel on the
* r'th row of the panel window
*/
#if __STDC__
static void showInfo(int n, int r, int i)
#else
static void showInfo(n, r, i)
int n;
int r;
int i;
#endif /* __STDC__ */
{
extern short info2show;
int j, buf, sel;/*, F = fIndex[n][i];*/
fInfo_t *fip = &INFO(n,i);
struct tm *tp;
char buff[42];
buf = GRABBUF();
BUF[buf][0] = '\0';
PADBUF(n, buf);
mvwprintw(panels[n], r, 0, BUF[buf]);
if (i >= numfiles[n])
goto end;
strncpy(buff, fip->name, (unsigned) fnameLen);
buff[fnameLen] = 0;
sel = fip->flag & F_SELECTED;
sprintf(BUF[buf], " %c ", fip->typID);
for (j = 0; j < fip->depth; j++)
strcat(BUF[buf], " ");
strcat(BUF[buf], buff);
if (sel)
selectAttrib(panels[n], 1);
PADBUF(n, buf);
mvwprintw(panels[n], r, 0, BUF[buf]);
if (sel)
selectAttrib(panels[n], 0);
if (winState[n] != W_TREE)
{
switch (info2show)
{
case 1: /* size */
sprintf(BUF[buf], "%8ld", fip->size);
break;
#ifndef __MSDOS__
case 2: /* owner */
sprintf(BUF[buf], "%8s", GetUserID(fip->uid));
break;
case 3: /* group */
sprintf(BUF[buf], "%8s", GetGroupID(fip->gid));
break;
case 4: /* perms */
strcpy(BUF[buf], getPerms(fip->mode));
break;
#endif
case 5: /* mtime */
tp = localtime(&fip->modtime);
sprintf(BUF[buf], "%2d:%02d:%02d %2d/%02d/%02d",
tp->tm_hour, tp->tm_min, tp->tm_sec,
tp->tm_mday, tp->tm_mon+1, tp->tm_year);
break;
#ifndef __MSDOS__
case 6: /* atime */
tp = localtime(&fip->acctime);
sprintf(BUF[buf], "%2d:%02d:%02d %2d/%02d/%02d",
tp->tm_hour, tp->tm_min, tp->tm_sec,
tp->tm_mday, tp->tm_mon+1, tp->tm_year);
break;
#endif
default:
goto end;
}
if ((winWidth[n] - fnameLen - 10) >= 0)
{
BUF[buf][winWidth[n] - fnameLen - 10] = '\0';
if (sel)
selectAttrib(panels[n], 1);
mvwprintw(panels[n], r, fnameLen + 8, BUF[buf]);
if (sel)
selectAttrib(panels[n], 0);
}
}
end:
FREEBUF(buf);
}
#define VB(p) viewBufs[(p)/(16*1024)]
#define VC(p) VB(p)[(p)%(16*1024)]
#if __STDC__
static void showFile(int n, char *vf)
#else
static void showFile(n, vf)
int n;
char *vf;
#endif
{
int r, c, buf = GRABBUF(), cnt, exBuf = -1;
static int isExp = 0;
int number = testOption(VAR_NUMBERLNS), w = (winWidth[n] - 2);
static time_t view_mtime = (time_t) 0;
long p, o;
char *s;
char *Line, *S = BUF[buf]; /* gross out! */
s = (char *) calloc(4096, sizeof(char));
if (s == NULL)
{
showMsg("Cannot allocate read buffer!");
goto end2;
}
/* Compare with the file being viewed... */
if (viewFP)
{
if (strcmp(vf, fileViewed) ||
(exBuf < 0 && view_mtime != getFileModTime(fileViewed)))
{
CloseFile(viewFP);
viewFP = NULL;
if (isExp > 0)
unlink(fileViewed);
}
}
if (viewPos == NULL)
viewPos = (long *) calloc(MAXVIEWLINES, sizeof(long));
/* if viewFP is NULL, open the file and read it in */
if (viewFP == NULL)
{
#ifndef __MSDOS__
/* If it is a device file or FIFO, just indicate that... */
if (checkType(getFileMode(vf), 4 | 8 | 2))
{
sprintf(BUF[buf], "Cannot view %s : device or pipe", vf);
BEEP;
sleep(1);
showMsg(BUF[buf]);
goto end;
}
#endif
viewSize = getFileSize(vf);
view_mtime = getFileModTime(vf);
#ifdef __MSDOS__
viewFP = OpenFile(vf, "rb");
#else
viewFP = OpenFile(vf, "r");
#endif
if (viewFP == NULL)
{
showMsg("Cannot open view file!");
BEEP;
sleep(1);
goto end;
}
strcpy(fileViewed, vf);
isExp = (exBuf >= 0);
viewHex = viewColumn = 0;
topLine = viewLines = 0l;
/* check if it is binary */
cnt = fread(S, sizeof(char), BUFFER_SIZE, viewFP);
for (p = 0; p < cnt; p++)
{
if (((unsigned char) S[p]) > 127)
viewHex++;
}
if (viewHex > (cnt / 10))
viewLines = (viewSize + 7) / 8;
else
viewHex = 0;
if (!viewHex)
{
/* Paginate as a text file */
fseek(viewFP, 0l, 0);
while (!feof(viewFP) && viewLines < MAXVIEWLINES)
{
viewPos[viewLines] = ftell(viewFP);
if (fgets(S, BUFFER_SIZE, viewFP) == NULL)
break;
else
viewLines++;
}
if (!feof(viewFP))
{
showMsg("Pagination table overflow!");
viewSize = ftell(viewFP);
}
}
else if (w > 56)
viewHex = 16;
else
viewHex = 8;
}
/* now we view the data in the file, starting from topLine */
werase(panels[n]);
Line = S;
if (viewHex)
fseek(viewFP, o = topLine * viewHex, 0);
else
fseek(viewFP, o = viewPos[topLine], 0);
cnt = fread(s, sizeof(char), 4096, viewFP);
for (p = r = 0;
r < showLines[n] && (o + p) < viewSize /*&& (topLine+r)<viewLines*/ ;
r++)
{
/* pad up to the end of the line with spaces */
strcpy(S = Line, space);
if (viewHex)
{
sprintf(S, "%06lX: ", o + p);
for (c = 0; c < viewHex && (o + p + c) < viewSize; c++)
{
sprintf(S + 8 + (5 * c) / 2, "%02X ", (s[p + c]) & 0xFF);
#ifndef __MSDOS__
if (s[p + c] < 32 || s[p + c] >= 127)
sprintf(S + 10 + (5 * viewHex) / 2 + c, ".");
else
#endif
sprintf(S + 10 + (5 * viewHex) / 2 + c, "%c", s[p + c]);
}
if (viewColumn >= (10 + (7 * viewHex) / 2))
viewColumn = (9 + (7 * viewHex) / 2);
/* fix up NUL put in by sprintf... */
S[8 + (5 * (viewHex - 1)) / 2 + 3] = ' ';
Line = S + viewColumn;
p += viewHex;
}
else
{
/* Save the first line in a script variable */
if (r == 0)
{
int i;
strncpy(viewLine, s, 255);
for (i = 0; i < 256; i++)
{
if (viewLine[i] != 9 && viewLine[i] < 32)
{
viewLine[i] = 0;
break;
}
}
}
/* Do line numbering if option set */
if (number)
{
sprintf(S, "%-4d:", (int) (topLine + r + 1));
S += 5;
}
/* Put in the text starting from the view column */
p = viewPos[topLine + r] - o;
for (c = 0; (o + p) < viewSize && p < 4096;)
{
int si = c - viewColumn;
/* Convert tabs to spaces */
if (s[p] == '\t')
{
int k = 8 - (c % 8);
while (k--)
{
if (si >= 0)
S[si] = ' ';
c++;
si++;
}
}
else if (s[p] == '\n' || s[p] == '\r')
break;
#ifndef __MSDOS__
/* Print non-printable chars as \XX hex numbers */
else if (s[p] < 32 || s[p] >= 127)
{
int msb = s[p] / 16;
int lsb = s[p] - 16 * msb;
msb %= 16;
if (si >= 0)
S[si] = '\\';
c++;
si++;
if (si >= 0)
S[si] = (msb > 9) ? ('A' + msb - 10) : ('0' + msb);
c++;
si++;
if (si >= 0)
S[si] = (lsb > 9) ? ('A' + lsb - 10) : ('0' + lsb);
c++;
si++;
}
#endif
else
/* plain old printable ASCII character */
{
if (si >= 0)
S[si] = s[p];
c++;
si++;
}
p++;
}
}
Line[w] = 0;
if (viewHex && r == 0)
strcpy(viewLine, Line);
mvwprintw(panels[n], r, 0, "%s", Line);
}
/* Blank out any remaining lines */
strcpy(Line, space);
Line[w] = '\0';
while (r < showLines[n])
mvwprintw(panels[n], r++, 0, Line);
end:
free(s);
if (exBuf >= 0)
FREEBUF(exBuf);
end2:
FREEBUF(buf);
}
#if __STDC__
void doViewNav(int w, int i)
#else
void doViewNav(w, i)
int w, i;
#endif
{
/* Fairly hackish approximations of the basic cursor key
movements */
switch (i)
{
case 0:
if (topLine > 0)
topLine--;
break;
case 1:
if (topLine < (viewLines + 1))
topLine++;
break;
case 2:
if (viewColumn > 0)
viewColumn--;
break;
case 3:
viewColumn++;
break;
case 4:
if (topLine >= showLines[w])
topLine -= showLines[w];
else
topLine = 0;
break;
case 5:
if (topLine < (viewLines - showLines[w]))
topLine += showLines[w];
else
topLine = viewLines - 1;
break;
case 6:
topLine = 0;
break;
case 7:
topLine = viewLines - 1;
break;
}
winInvalid[w] = 1;
showList(w);
}
/* showList is now a bit of a misnomer; it should be showWindowContents
or something */
#if __STDC__
void showList(int n)
#else
void showList(n)
int n;
#endif /* __STDC__ */
{
int i;
if (!testOption(VAR_DISPLAYON) || !listVis[n])
return;
selectAttrib(panels[n], 0);
switch (winState[n])
{
case W_VIEW:
{
if (winInvalid[n] == 2)
{
int ex = GRABBUF(), ne = GRABBUF(), typ;
char *vf;
if (file2view[0] == '\0') /* view file at cursor in window 1-n */
vf = makePath(paths[1 - n], INFO_NOW(1 - n).name);
else /* view the file given by file2view */
vf = file2view;
typ = splitPath(vf, BUF[ex], BUF[ne]);
if (typ >= 0 && BUF[ne][0])
{
/* element of a container. Extract and view */
int exBuf = GRABBUF();
showFile(n, strcpy(fileViewed, getExtraction(BUF[ex], BUF[ne], BUF[exBuf], typ)));
FREEBUF(exBuf);
viewingDir = 0;
}
else if (typ >= 0 || typ == -3) /* dir or container? - do a CD */
{
int top = startLn[n];
strcpy(paths[n], BUF[ex]);
readList(n, 1);
if (strcmp(fileViewed, BUF[ex]))
{
top = 0;
strcpy(fileViewed, BUF[ex]);
if (viewFP)
{
CloseFile(viewFP);
viewFP = NULL;
}
}
else if (top >= numfiles[n])
top = numfiles[n] - 1;
startLn[n] = top;
for (i = 0; i < showLines[n]; i++)
showInfo(n, i, startLn[n] + i);
viewingDir = 1;
}
else
{
showFile(n, vf);
viewingDir = 0;
}
FREEBUF(ex);
FREEBUF(ne);
}
else if (winInvalid[n] == 1)
{
if (viewingDir)
for (i = 0; i < showLines[n]; i++)
showInfo(n, i, startLn[n] + i);
else
showFile(n, fileViewed);
}
break;
}
default:
if (winInvalid[n] == 2)
{
readList(n, 1);
startLn[n] = 0;
}
else if (winInvalid[n])
readList(n, 0);
for (i = 0; i < showLines[n]; i++)
showInfo(n, i, startLn[n] + i);
break;
}
winInvalid[n] = 0;
}
/*
* normalise() is called after the cursor is moved, to ensure
* the cursor is still displayed in the window.
*/
#if __STDC__
void normalise(int n)
#else
void normalise(n)
int n;
#endif /* __STDC__ */
{
if ((highlight[n] - startLn[n]) >= showLines[n])
{
startLn[n] = highlight[n] - showLines[n] + 1;
}