-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgcterp.c
2736 lines (2552 loc) · 51.8 KB
/
gcterp.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.
*/
#include "gcport.h"
#include "gc3.h"
#include "gckey.h"
#include "gcops.h"
#define GC_CURSES
#include "gclib.h"
#include <fcntl.h>
#define MAX_REGS 16
static int regBuf[MAX_REGS];
static char *regPtr[MAX_REGS];
#define PARAM(n) codespace[old_ip+n]
#undef HAS_SPAWN0
#ifndef min
#define min(a,b) (((a)<(b))?(a):(b))
#endif
#define MAX_NEST 16 /* Maximum nesting of function calls */
#define MAX_LOOP 8 /* Maximum nesting of loops in function */
typedef struct {
short v;
short type;
short idx;
short s, e;
short pos;
} loopInfo;
typedef struct {
short ip; /* instruction pointer for return */
short loopP; /* Loop nest level */
short name; /* function name index */
short np; /* number of params */
short nv; /* number of vars */
short param[MAX_PARAMS]; /* Parameter indices */
short pnames[MAX_PARAMS]; /* Parameter names */
short vnames[MAX_LOCVARS]; /* Local variable names */
char *locvar[MAX_LOCVARS]; /* Local var value pointers */
loopInfo loopStack[MAX_LOOP];
} fcallInfo_t;
static int returnVal; /* Last return value */
fcallInfo_t callStack[MAX_NEST];
static short callStkLvl = 0,
old_ip,
lookupLevel;
char *valspace[MAX_VARS] = {NULL};
short ip = 0;
FILE *catchFP=NULL;
int fgCol=White,
bgCol=Blue;
int fnameLen = 22;
int winInvalid[2] = { 2, 2 };
int winState[2] = { W_LIST, W_LIST };
int showLines[2] = { 99, 99 };
int hborder = 0;
int vborder = 0;
int filtering[2] = { 0, 0 };
int changedWinSize = 0;
short helpFile = 0, helpEntry = 0;
char tempName[MAXPATHNAME];
char *BUF[MAX_BUFFERS];
static char *bufFile[MAX_BUFFERS] = { NULL };
static int bufLine[MAX_BUFFERS] = { 0 };
#ifdef DEBUG
static int maxBufsUsed = 0;
#endif
char file2view[MAXPATHNAME];
int viewingDir = 0;
#ifdef __MSDOS__
REcompileF_t compileRegexp = DOScompileRE;
REmatchF_t matchRegexp = DOSmatchRE;
#else
REcompileF_t compileRegexp = compileRE;
REmatchF_t matchRegexp = matchRE;
#endif
#ifdef __MSDOS__
char filters[2][14] = { "*.*", "*.*" };
#else
char filters[2][64] = { ".", "." };
#endif
#ifndef __MSDOS__
#ifdef NO_SRAND48
#define randomize() srand((int)time(NULL))
#define random(n) (rand() % (n))
#else
#define randomize() srand48((long)time(NULL))
#define random(n) (lrand48() % (n))
#endif
#endif
#if __STDC__
static short lookupName(char *name);
#else
static short lookupName();
#endif
#if __STDC__
int testOption(int opt)
#else
int testOption(opt)
int opt;
#endif
{
char c = 0;
if (valspace[opt]) c = valspace[opt][0];
return (c=='y' || c=='Y' || (isdigit(c) && c!='0'));
}
/************************************************************/
char *stringspace = NULL;
#if __STDC__
void AllocateStringSpace(void)
#else
void AllocateStringSpace()
#endif
{
assert(stringspace == NULL);
stringspace = (char *)calloc(STRINGSPACE,sizeof(char));
assert(stringspace);
}
#if __STDC__
void FreeStringSpace(void)
#else
void FreeStringSpace()
#endif
{
assert(stringspace);
free(stringspace);
stringspace = NULL;
}
#if __STDC__
int GetBuffer(char *fname, int lnum)
#else
int GetBuffer(fname, lnum)
char *fname;
int lnum;
#endif
{
char msg[80];
int i = 1;
while (i<MAX_BUFFERS)
{
if (bufFile[i] == NULL)
{
bufFile[i] = fname;
bufLine[i] = lnum;
#ifdef DEBUG
if (i>=maxBufsUsed)
maxBufsUsed = i+1;
#endif
BUF[i][0] = '\0';
return i;
}
else i++;
}
sprintf(msg,"Out of buffers at %s (%d)! (press ENTER for more)",
fname, lnum);
showMsg(msg); (void)my_getch();
for (i=1;i<MAX_BUFFERS;i++)
{
sprintf(msg,"Buf %d allocated at %s (%d) (press ENTER for more)",
i,bufFile[i], bufLine[i]);
showMsg(msg); (void)my_getch();
}
return 0; /* Reserved for this case to hopefully recover */
}
#if __STDC__
void ReleaseBuffer(int buf, char *fname, int lnum)
#else
void ReleaseBuffer(buf, fname, lnum)
int buf, lnum;
char *fname;
#endif
{
if (bufFile[buf]) bufFile[buf] = NULL;
else
{
char msg[60];
sprintf(msg,"ReleaseBuffer called in error from %s (%d)!\n",
fname, lnum);
showMsg(msg);
sleep(1);
}
}
#if __STDC__
static int promptForKey(void)
#else
static int promptForKey()
#endif
{
if (inCurses)
showMsg("Press a key...");
else
{
fprintf(stderr,"\nPress a key...");
fflush(stderr);
}
return my_getch();
}
#if __STDC__
static int prepareRE(char *re)
#else
static int prepareRE(re)
char *re;
#endif
{
char *dbg = compileRegexp(re);
if (dbg)
{
showMsg(dbg);
return -1;
} else return 0;
}
#if __STDC__
static char *randname(void)
#else
static char *randname()
#endif
{
static char _randname[10];
int i=0;
(void)randomize();
while (i<8)
_randname[i++] = 'A' + (char)random(26);
_randname[8] = '\0';
return _randname;
}
/********************************************************
FILE SELECTION
*********************************************************/
#if __STDC__
void nselect(int L, int n, int v)
#else
void nselect(L, n, v)
int L, n, v;
#endif /* __STDC__ */
{
fInfo_t *p = &fInfo[L][n];
if (v)
{
#ifdef __MSDOS__
if (!testOption(VAR_DIRSELECT) && p->typID==PATH_SEP) return;
#else
if (!testOption(VAR_DIRSELECT) && S_ISDIR(p->mode)) return;
#endif
if ((p->flag & F_SELECTED)==0)
{
p->flag |= F_SELECTED;
selCnt[L]++;
selSize[L]+=p->size;
}
}
else
{
if (p->flag & F_SELECTED)
{
p->flag &= ~F_SELECTED;
selCnt[L]--;
selSize[L]-=p->size;
}
}
}
/***************************************************************/
#if __STDC__
short dereference(short var, char ***space)
#else
short dereference(var, space)
short var;
char ***space;
#endif
{
int lev = lookupLevel-1;
#ifdef DEBUG
short i, startIdx=var;
int dbg = testOption(VAR_DEBUGGING);
if (dbg)
{
fprintf(debug,"\tIn deref(%d), lookupLevel=%d\n\tStack:\n",
var,lookupLevel);
for (i=0;i<lookupLevel;i++)
{
short j;
fprintf(debug,"\t\tLevel: %d %-16s ( ",i,
LITERAL(callStack[i].name));
for (j=0;j<callStack[i].np;j++)
fprintf(debug,"%d ",callStack[i].param[j]);
fprintf(debug," ) [ ");
for (j=0;j<callStack[i].nv;j++)
fprintf(debug,"%s ",callStack[i].locvar[j]);
fprintf(debug,"]\n");
}
}
#endif
*space = valspace;
while (var>=MAX_IDS && lev>=0)
{
if (IS_PARAM(var))
{
var = callStack[lev].param[FROM_PARAM(var)];
#ifdef DEBUG
if (dbg) fprintf(debug,"\tparameter - derefed to lev %d, idx %d\n",lev,var);
#endif
}
else
{
if (IS_LOCPAR(var))
{
var = DEREF_LOCPAR(var);
#ifdef DEBUG
if (dbg) fprintf(debug,"\tlocvar parameter - derefed to lev %d, idx %d\n",lev,var);
#endif
}
if (IS_LOCAL(var))
{
*space = callStack[lev].locvar;
#ifdef DEBUG
if (dbg) fprintf(debug,"\tlocvar - derefed to lev %d, idx %d - %s\n",lev,var,(*space)[FROM_LOCAL(var)]);
#endif
return var;
}
}
lev--;
}
if (var>=MAX_IDS)
{
showMsg("Variable dereference error! (ignoring)");
sleep(1);
#ifdef DEBUG
fprintf(debug,"Dereference error! Index %d, at end %d (levs %d)\n",
startIdx, var, lookupLevel);
#endif
return 0; /* semi-recovery */
}
#ifdef DEBUG
if (dbg)
{
fprintf(debug,"\tdereference returns %d from level %d, callStkLvl=%d (",
var, lev+1, callStkLvl);
printArg(debug,var);
fprintf(debug,")\n");
}
#endif
return var;
}
#if __STDC__
static int _expand(char *str, int buf, int esc)
#else
static int _expand(str, buf,esc)
char *str;
int buf, esc;
#endif
{
int i, j, L, more=0;
for (i=j=0, L = (int)strlen(str); i<L && j<BUFFER_SIZE;i++)
{
if (str[i]=='#')
{
/* if esc is zero we don't do it yet */
if (esc==0)
{
BUF[buf][j++] = '#';
BUF[buf][j++] = str[++i];
}
/* check for escape #nnn */
else if (i<(L-3) && isdigit(str[i+1]) &&
isdigit(str[i+2]) && isdigit(str[i+3]))
{
char num[4];
strncpy(num,str+i+1,3);
num[3]=0;
BUF[buf][j++] = atoi(num);
i+=3;
}
else if (i<(L-1))
{
i++;
if (str[i]=='n') BUF[buf][j++] = '\n';
else if (str[i]=='t') BUF[buf][j++] = '\t';
else if (str[i]=='r') BUF[buf][j++] = '\r';
else if (str[i]=='w')
{
BUF[buf][j++] = ' ';
BUF[buf][j++] = '\t';
}
else BUF[buf][j++] = str[i];
}
}
else if (!esc && str[i]=='$')
{
char varname[40]; int p=0; short idx;
i++;
while (isalnum((int)str[i]) || str[i]=='_')
varname[p++] = str[i++];
varname[p] = '\0';
i--;
STRUPR(varname);
if ((idx = lookupName(varname))>=0)
{
int buf2 = GRABBUF();
char *s = lookupVar(idx,buf2);
if (STRCHR(s,'$')) more=1;
strcpy(BUF[buf]+j,s);
FREEBUF(buf2);
}
else /* No such variable */
{
BUF[buf][j++] = '$';
strcpy(BUF[buf]+j,varname);
}
j = (int)strlen(BUF[buf]);
}
else BUF[buf][j++] = str[i];
}
BUF[buf][min(j,(BUFFER_SIZE-1))]='\0';
if (j>=BUFFER_SIZE) showMsg("Buffer overflow!");
return more;
}
#if __STDC__
static char *expand(char *str, int buf)
#else
static char *expand(str, buf)
char *str;
int buf;
#endif
{
int t, tmpBuffer = GRABBUF(), b=buf, more;
int B = buf, O = tmpBuffer;
lookupLevel = callStkLvl;
strcpy(BUF[buf],str);
for (;;)
{
if (lookupLevel<0) break;
B = tmpBuffer; O = buf;
more = _expand(BUF[O],B,0);
if (more==0) break;
else if (more==2) lookupLevel--; /* lower scope */
t = tmpBuffer; tmpBuffer=buf;buf=t; /* faster than strcpy! */
}
lookupLevel = callStkLvl;
if (b==B)
{
_expand(BUF[b],O,1);
strcpy(BUF[b],BUF[O]);
FREEBUF(O);
}
else
{
_expand(BUF[B],b,1);
FREEBUF(B);
}
#ifdef DEBUG
if (testOption(VAR_DEBUGGING))
fprintf(debug,"After expand, str %s=%s\n",str,BUF[b]);
#endif
return BUF[b];
}
#if __STDC__
int findFile(int L, int s, char *fname, int *idx)
#else
int findFile(L, s, fname, idx)
int L, s, *idx;
char *fname;
#endif
{
int i = s;
do
{
if (strcmp(INFO(L,i).name,fname)==0)
{
if (idx) *idx=fIndex[L][i];
return i;
}
i = (i+1) % numfiles[L];
}
while (i!=s);
return -1;
}
#if __STDC__
static int findMatch(int s, char *re, int *idx)
#else
static int findMatch(s, re, idx)
int s, *idx;
char *re;
#endif
{
returnVal=1;
#ifdef DEBUG
if (testOption(VAR_DEBUGGING))
{
fprintf(debug,"In findMatch(%d,%s,)\n",s,re); fflush(debug);
}
#endif
if (re==NULL || prepareRE(re)==0)
{
int n, m;
m = n = numfiles[l];
do
{
s = (s+1) % m;
if ((*matchRegexp)(INFO(l,s).name)==1)
{
if (idx) *idx=fIndex[l][s];
return s;
}
}
while (n--);
}
returnVal = 0;
return -1;
}
#if __STDC__
static short lookupName(char *name)
#else
static short lookupName(name)
char *name;
#endif
{
int i, n;
short L = callStkLvl-1;
if (name==NULL || name[0]=='\0') return -1;
/* first check locvars and params */
n = callStack[L].nv;
for (i=0;i<n;i++)
if (strcmp(name,LITERAL(callStack[L].vnames[i]))==0)
return TO_LOCAL(i);
n = callStack[L].np;
for (i=0;i<n;i++)
if (strcmp(name,LITERAL(callStack[L].pnames[i]))==0)
return TO_PARAM(i); /* param 0 predefed */
/* then check the symbol table for global vars */
for (i=0;i<idents;i++)
if ((objects[i].type & VAR_TYPE) &&
strcmp(name,ID_NAME(i))==0)
return i;
return -1;
}
#if __STDC__
static void buildList(char *buf, int L, int doAll)
#else
static void buildList(buf, L, doAll)
char *buf;
int L, doAll;
#endif
{
int i, len = 1;
for (i=0;i<numfiles[L];i++)
{
if (doAll || (INFO(L,i).flag & F_SELECTED))
{
char *name = INFO(L,i).name;
len += strlen(name)+1;
if (len>=BUFFER_SIZE)
{
showMsg("Buffer overflow!");
break;
}
if (buf[0]) strcat(buf," ");
strcat(buf,name);
}
}
}
#if __STDC__
char *lookupVar(short idx, int buf)
#else
char *lookupVar(idx, buf)
short idx;
int buf;
#endif
{
struct tm *tp; time_t tv;
char *cmd, *rtn = "";
char **space;
#ifdef DEBUG
int dbg = testOption(VAR_DEBUGGING);
if (dbg)
{
fprintf(debug,"in lookupVar(%d, ",idx);
printArg(debug,idx);
fprintf(debug,")\n");
}
#endif
BUF[buf][0] = '\0';
idx = dereference(idx,&space);
#ifdef DEBUG
if (dbg)
{
fprintf(debug,"in lookupVar, after deref (%d, ",idx);
printArg(debug,idx);
fprintf(debug,")\n");
}
#endif
if (IS_LITERAL(idx))
{
char *tmp = STRING(idx);
#ifdef DEBUG
if (dbg) fprintf(debug,"\tlookupVar: returning %s\n",tmp);
#endif
return tmp;
}
#ifdef DEBUG
else if (dbg)
{
if (IS_GLOBAL(idx))
fprintf(debug,"\tlookupVar: space[%d] is %s\n",idx,
space[VARNUM(idx)]);
else if (IS_LOCAL(idx))
fprintf(debug,"\tlookupVar: space[%d] is %s\n",idx,
space[FROM_LOCAL(idx)]);
}
#endif
rtn = BUF[buf];
switch(idx)
{
case VAR_PFILE:
rtn = INFO_NOW(l).name;
break;
case VAR_SFILE:
rtn = INFO_NOW(1-l).name;
break;
case VAR_PFILES:
buildList(rtn,l,1);
break;
case VAR_PSEL:
buildList(rtn,l,0);
break;
case VAR_SFILES:
buildList(rtn,1-l,1);
break;
case VAR_SSEL:
buildList(rtn,1-l,0);
break;
case VAR_PPATH:
rtn = paths[l];
break;
case VAR_SPATH:
rtn = paths[1-l];
break;
case VAR_PSELSIZE:
sprintf(rtn,"%ld",selSize[l]);
break;
case VAR_SSELSIZE:
sprintf(rtn,"%ld",selSize[1-l]);
break;
case VAR_PSELCNT:
sprintf(rtn,"%d",selCnt[l]);
break;
case VAR_SSELCNT:
sprintf(rtn,"%d",selCnt[1-l]);
break;
case VAR_PSIZE:
sprintf(rtn,"%ld",INFO_NOW(l).size);
break;
case VAR_SSIZE:
sprintf(rtn,"%ld",INFO_NOW(1-l).size);
break;
case VAR_PMTIME:
rtn = asctime(localtime(&INFO_NOW(l).modtime));
break;
case VAR_SMTIME:
rtn = asctime(localtime(&INFO_NOW(1-l).modtime));
break;
#ifndef __MSDOS__
case VAR_PATIME:
rtn = asctime(localtime(&INFO_NOW(l).acctime));
break;
case VAR_SATIME:
rtn = asctime(localtime(&INFO_NOW(1-l).acctime));
break;
case VAR_POWNER:
rtn = GetUserID(INFO_NOW(l).uid);
break;
case VAR_SOWNER:
rtn = GetUserID(INFO_NOW(1-l).uid);
break;
case VAR_PGROUP:
rtn = GetGroupID(INFO_NOW(l).gid);
break;
case VAR_SGROUP:
rtn = GetGroupID(INFO_NOW(1-l).gid);
break;
#endif
case VAR_PPERMS:
#ifdef __MSDOS__
rtn[0] = INFO_NOW(l).typID;
rtn[1] = '\0';
#else
rtn = getPerms(INFO_NOW(l).mode);
#endif
break;
case VAR_SPERMS:
#ifdef __MSDOS__
rtn[0] = INFO_NOW(1-l).typID;
rtn[1] = '\0';
#else
rtn = getPerms(INFO_NOW(1-l).mode);
#endif
break;
#ifndef __MSDOS__
case VAR_USERNAME:
rtn = (char *)getlogin();
break;
#endif
case VAR_TEMPNAME:
rtn = CreateTempName();
break;
case VAR_TEMPFILE:
rtn = tempName;
break;
case VAR_DATE:
tp = localtime((tv=time(NULL),&tv));
sprintf(rtn,"%2d/%02d/%02d",
tp->tm_year,tp->tm_mon+1,tp->tm_mday);
break;
case VAR_TIME:
tp = localtime((tv=time(NULL),&tv));
sprintf(rtn,"%2d:%02d:%02d",
tp->tm_hour,tp->tm_min,tp->tm_sec);
break;
case VAR_RANDOM:
rtn = randname();
break;
case VAR_PREVCMD:
cmd = getPrevCmd("");
strncpy(rtn,cmd?cmd:"",BUFFER_SIZE-1);
break;
case VAR_NEXTCMD:
cmd = getNextCmd("");
strncpy(rtn,cmd?cmd:"",BUFFER_SIZE-1);
break;
case VAR_DISKFREE: /* Disk space free */
getDiskSpace(l, paths[l]);
sprintf(rtn,"%ld",freeSpace[l]);
break;
case VAR_DISKUSED: /* Disk space used */
getDiskSpace(l, paths[l]);
sprintf(rtn,"%ld",usedSpace[l]);
break;
case VAR_VERSION: rtn = VERSION; break;
case VAR_PATCHLEVEL: rtn = PATCHLEVEL; break;
case VAR_CODEPATH: rtn = codepath; break;
case VAR_VIEWLINE: rtn = viewLine; break;
case VAR_VIEWFILE:
rtn = fileViewed; break;
case VAR_WINDOW:
switch(winState[l])
{
case W_VIEW: rtn[0] = 'v'; break;
case W_TREE: rtn[0] = 't'; break;
default: rtn[0] = 'd'; break;
}
rtn[1] = (l==0)? 'l' : 'r';
rtn[2] = '\0';
break;
case VAR_WIDTH:
sprintf(rtn,"%d",winWidth[l]); break;
case VAR_HEIGHT:
sprintf(rtn,"%d",showLines[l]); break;
case VAR_LEFTMARGIN:
sprintf(rtn,"%d",leftMargin); break;
case VAR_TOPMARGIN:
sprintf(rtn,"%d",topMargin); break;
case VAR_SHOWVIEW:
sprintf(rtn,"%d",listVis[l]); break;
case VAR_SHOWSTATUS:
sprintf(rtn,"%d",statusVis[l]); break;
default:
if (IS_LOCAL(idx)) idx = FROM_LOCAL(idx);
else if (IS_GLOBAL(idx)) idx = VARNUM(idx);
rtn = space[idx] ? space[idx] : "";
break;
}
if (rtn != BUF[buf]) strcpy(BUF[buf],rtn);
#ifdef DEBUG
if (testOption(VAR_DEBUGGING))
fprintf(debug,"lookupVar returning %s\n",BUF[buf]);
#endif
return BUF[buf];
}
#if __STDC__
char *lookupArg(short idx, int buf)
#else
char *lookupArg(idx, buf)
short idx;
int buf;
#endif
{
char *tmp;
if (idx>=0) tmp = lookupVar(idx,buf);
else tmp = STRING(idx);
return testOption(VAR_EXPANDING) ? expand(tmp,buf) : strcpy(BUF[buf],tmp);
}
#if __STDC__
void assign2var(short varnum, char *val)
#else
void assign2var(varnum, val)
short varnum;
char *val;
#endif /* __STDC__ */
{
unsigned nb = strlen(val)+1;
int isGlob = 0, changed;
char **space;
#ifdef DEBUG
int dbg = testOption(VAR_DEBUGGING);
if (dbg) fprintf(debug,"In assign2var(%d,%s)\n",varnum,val);
#endif
varnum = dereference(varnum,&space); /* look up positional parameters */
if (!IS_LVAL(varnum))
{
/* shouldn't happen if script is right! */
char msg[80];
sprintf(msg,"Script error: bad reference parameter %d at %d\n",varnum,ip);
showMsg(msg);
sleep(1);
returnVal = 0;
return;
}
if (IS_LOCAL(varnum)) varnum = FROM_LOCAL(varnum);
else if (IS_GLOBAL(varnum))
{
isGlob = 1;
varnum = VARNUM(varnum);
}
#ifdef DEBUG
if (dbg) fprintf(debug,"\tassign2var - after deref varnum=%d, space[v]=%s\n",
varnum,space[varnum]);
#endif
#ifdef __MSDOS__
if (isGlob && varnum==VAR_USEDOSPATTERNS)
{
val = "y";
nb = 2;
}
#endif
#if defined(NO_COLOR) || defined(NO_COLOUR)
if (isGlob && varnum==VAR_ISCOLOR)
{
val = "n";
nb = 2;
}
#endif
if (space[varnum])
{
changed = strcmp(space[varnum],val);
if (changed) space[varnum] = realloc(space[varnum],nb);
}
else
{
changed = 1;
space[varnum] = calloc(nb,sizeof(char));
}
if (!changed) goto end;
if (space[varnum]) strcpy(space[varnum],val);
else showMsg("Failed to allocate space for variable!");
/* The rest of the code in this routine makes changes that are
required when certain global variables are modified
*/
if (isGlob)
{
int isNZ = ( val[0]>='1' && val[0]<='9'),
isY = (val[0]=='y' || val[0]=='Y'), enbl, change = 0, ws = 0;
enbl = isNZ || isY;
if (varnum == VAR_USEDOSPATTERNS)
{
change = (testOption(VAR_USEDOSPATTERNS)) ? (!enbl) : enbl ;
if (change)
{
#ifdef __MSDOS__
compileRegexp = DOScompileRE;
matchRegexp = DOSmatchRE;
#else
if (enbl)
{
compileRegexp = DOScompileRE;
matchRegexp = DOSmatchRE;
}
else
{
compileRegexp = compileRE;
matchRegexp = matchRE;
}
#endif
}
}
else if (varnum==VAR_TREE)
{
enbl = isNZ;
ws=W_TREE;
}
else if (varnum==VAR_VIEWER)
ws=W_VIEW;
else if (varnum == VAR_ISCOLOR)
allowColor = enbl;
change = (winState[l]==ws) ? (!enbl) : enbl ;
if (change && ws)
{
if (enbl)
{
winState[l] = ws;
/* Enabling a tree or viewer affects the other window */
if (ws == W_TREE || (ws==W_VIEW && winState[1-l] != W_LIST))