-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgclib.c
1858 lines (1694 loc) · 36.9 KB
/
gclib.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.
* gram@aztec.co.za
*
* 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 "gclib.h"
#include "gckey.h"
#ifdef __MSDOS__
#define PTR_TO_LONG(p) ((((unsigned long)(p)) >> 16l)*16l +\
(unsigned long)((unsigned short)(p)))
#ifndef PATH_SEP
# define PATH_SEP '\\'
# define PATH_SEP_STR "\\"
#endif
#ifndef FIELD_SEP
# define FIELD_SEP ';'
#endif
#ifndef MAXPATHNAME
# define MAXPATHNAME 64
#endif
typedef unsigned char uchar;
#else /* UNIX */
#define PTR_TO_LONG(p) ((unsigned long)p)
#ifndef PATH_SEP
# define PATH_SEP '/'
# define PATH_SEP_STR "/"
#endif
#ifndef FIELD_SEP
# define FIELD_SEP ':'
#endif
#ifndef MAXPATHNAME
# define MAXPATHNAME 1024
#endif
#endif
int allowColor=1;
/*
* STRCHR - strchr() replacement. Useful as not all systems have
* strchr(), and it doesn't seem to be prototyped
* in <string.h> on many systems.
*/
#if __STDC__
char *STRCHR(const char *s, int c)
#else
char *STRCHR(s, c)
char *s;
int c;
#endif
{
register char *sp = (char *)s;
do { /* As NUL is considered part of it... */
if (*sp==c) return sp;
} while (*sp++);
return NULL;
}
#if __STDC__
char *STRRCHR(const char *s, int c)
#else
char *STRRCHR(s, c)
char *s;
int c;
#endif
{
register char *sp = (char *)s;
char *rtn = NULL;
do
{
if (*sp==c) rtn = sp;
}
while (*sp++);
return rtn;
}
/*
* STRUPR - Convert string to upper-case in place.
*/
#if __STDC__
void STRUPR(char *s)
#else
void STRUPR(s)
char *s;
#endif
{
if (s)
while (*s)
{
*s = toupper(*s);
s++;
}
}
/***********************************************************
GET WORKING DIRECTORY
************************************************************/
#if __STDC__
void getCWD(char *name)
#else
void getCWD(name)
char *name;
#endif /* __STDC__ */
{
#ifdef NO_GETCWD
FILE *fp;
char *n = tmpnam(NULL), buf[64];
sprintf(buf,"pwd > %s",n);
system(buf);
if ((fp = fopen(n,"r")) != NULL)
{
fscanf(fp,"%s",name);
fclose(fp);
unlink(n);
}
#else
(void)getcwd(name,MAXPATHNAME-1);
#endif
}
/* fixPath takes a path which can be abs or rel, with or
without a drive specifier, and containing . and ..
components. It returns a minimised absolute path
(under DOS the return path includes the drive ID and
is all upper case */
#if __STDC__
char *fixPath(char *path, char *rtn)
#else
char *fixPath(path, rtn)
char *path, *rtn;
#endif
{
char *p, *rslt = rtn;
#ifdef __MSDOS__
char wb[MAXPATHNAME];
int drv;
assert(path && rtn);
STRUPR(path);
/* Put the drive ID in to get it over with... */
if (path[1]==':')
{
rtn[0] = path[0];
path+=2;
} else
rtn[0] = getdisk() + 'A';
rtn[1] = ':';
drv = rtn[0] - 'A';
rtn+=2;
#else
assert(path && rtn);
#endif
if (path[0]==PATH_SEP) /* abs path */
p = path;
else
{
#ifdef __MSDOS__
p = makePath(_getdcwd(drv+1,rslt,MAXPATHNAME), path); /* relative path */
rslt[2] = '\0'; /* return to state before calling _getdcwd */
#else
getCWD(rtn);
p = makePath(rtn, path);
#endif
}
/* Now strip .. amd . components */
{
char *bp = STRCHR(p, PATH_SEP), *sp;
int addSep = 0;
*bp++ = '\0'; /* Split at first sep */
rtn[0] = PATH_SEP;
rtn[1] = '\0';
do
{
sp = STRCHR(bp, PATH_SEP);
if (sp) *sp = '\0';
if (strcmp(bp,"..")==0)
{
char *last = STRRCHR(rtn,PATH_SEP);
if (last > rtn) *last = '\0';
else
{
rtn[1] = '\0';
addSep = 0;
}
} else if (strcmp(bp,"."))
{
if (addSep) strcat(rtn,PATH_SEP_STR);
strcat(rtn,bp);
addSep = 1;
}
bp = sp+1;
}
while (sp);
}
return rslt;
}
/*
* makePath is passed two string arguments `path' and `file' (the
* former may be NULL). It concatenates the two, inserting a path
* separator character if necessary. If the path is relative, it is
* made absolute. Any `.' and `..' entries in the path are stripped,
* and the path adjusted accordingly. makePath keeps the result in
* a static string buffer and returns a pointer to this buffer.
*/
#if __STDC__
char *makePath(char *path, char *file)
#else
char *makePath(path, file)
char *path, *file;
#endif
{
int plen, flen = strlen(file);
static char fbuf[MAXPATHNAME];
if (path && path[0])
{
plen = strlen(path);
assert(plen<MAXPATHNAME);
strcpy(fbuf,path);
if (path[plen-1]!=PATH_SEP && file[0])
{
plen++;
strcat(fbuf,PATH_SEP_STR);
}
assert((plen+flen)<MAXPATHNAME);
strcat(fbuf,file);
} else
{
assert(flen<MAXPATHNAME);
strcpy(fbuf,file);
}
#ifdef __MSDOS__
STRUPR(fbuf);
#endif
return fbuf;
}
/*
* searchDirectoryList searches a set of directories whose
* names are given in the string `list', separated by
* the character `sep'. If the file specified by `name'
* is found in one of these directories, a pointer to
* a string holding the full path name is returned. This
* string is given by the parameter `result', and must
* have space for 256 bytes. If this parameter is NULL,
* the result is returned in a static local buffer.
*
* If the file is not found in any of the directories,
* NULL is returned.
*
* An array of file extensions can be provided in the `exts'
* argument; these will be tried in turn as well if they exist.
*
* Note that if `name' is NULL or empty, then `list' can
* be a list of files rather than directories.
* If the list is NULL, then `name' is returned in the
* string if it exists, else NULL is returned.
*
*/
#if __STDC__
static int extAccess(char *name, char **ext)
#else
static int extAccess(name, ext)
char *name, **ext;
#endif
{
if (ext && ext[0])
{
int i = 0;
char path[MAXPATHNAME];
while (ext[i])
{
strcpy(path,name);
strcat(path,ext[i]);
if (access(path,0)==0)
return i;
i++;
}
return -1;
}
else
return access(name,0);
}
#if __STDC__
char *searchDirectoryList(char *list, char sep, char *name,
char **exts, char *result)
#else
char *searchDirectoryList(list, sep, name, exts, result)
char *list, sep, *name, **exts, *result;
#endif
{
char *path;
static char pathbuf[256]; /* holds the path of the file, if found */
int startPos=0, endPos, length, av;
path = ( result ? result : pathbuf );
path[0] = '\0';
if (list==NULL)
{
av = extAccess(name,exts);
if (av >= 0)
{
strcpy(path,name);
if (exts) strcat(path,exts[av]);
return path;
}
else return NULL;
}
while (list[startPos])
{
if (list[startPos]==sep)
{
startPos++;
continue;
}
endPos = startPos+1;
while (list[endPos] && list[endPos]!=sep)
endPos++;
length = endPos-startPos;
assert(length<256);
strncpy(path,list+startPos,length);
path[length] = '\0';
if (name && name[0])
{
if (path[length-1]!=PATH_SEP)
strcat(path,PATH_SEP_STR);
strcat(path,name);
}
av = extAccess(path,exts);
if (av>=0)
{
if (exts) strcat(path,exts[av]);
return path;
}
startPos = endPos;
}
return NULL;
}
/*
* findReadPath searches for a file in the order given by
* `order'. The latter is a string which can contain
* environment variables, a `.' (for the current directory),
* or a / or \ (the top level directory). Under DOS, \ is
* treated as C:\, rather than the root directory on the
* working drive.
*/
#if __STDC__
char *findReadPath(char *name, char *order, char **exts, char *result)
#else
char *findReadPath(name, order, exts, result)
char *name, *order, **exts, *result;
#endif
{
char *rtn=NULL, *evptr, evar[40];
int len;
if (order==NULL || name==NULL || *name==0)
return NULL;
while (*order && !rtn)
{
switch(*order++)
{
case '.':
rtn = searchDirectoryList(".", 0, name, exts, result);
break;
case PATH_SEP:
#if __MSDOS__
rtn = searchDirectoryList("C:\\", 0, name, exts, result);
#else
rtn = searchDirectoryList("/", 0, name, exts, result);
#endif
break;
case '$': /* environment var */
evptr = evar;
len = 40;
while (*order && *order!='.' && *order != PATH_SEP
&& *order!='$')
{
*evptr++ = *order++;
len--;
assert(len);
}
*evptr = 0;
rtn = searchDirectoryList(getenv(evar),FIELD_SEP, name, exts, result);
break;
}
}
return rtn;
}
/*
* findWritePath is similar to findReadPath, except it just makes
* the first path it can.
*/
#if __STDC__
char *findWritePath(char *name, char *order, char *result)
#else
char *findWritePath(name, order, result)
char *name, *order, *result;
#endif
{
char *evptr, evar[40], *rtn = NULL;
int len;
if (order==NULL || name==NULL || *name==0)
return NULL;
while (*order && rtn==NULL)
{
switch(*order++)
{
case '.':
rtn = makePath(".",name);
break;
case PATH_SEP:
rtn = makePath(PATH_SEP_STR,name);
break;
case '$': /* environment var */
evptr = evar;
len = 40;
while (*order && *order!='.' && *order != PATH_SEP
&& *order!='$')
{
*evptr++ = *order++;
len--;
assert(len);
}
*evptr = 0;
if ((evptr = getenv(evar)) != NULL)
rtn = makePath(evptr,name);
break;
}
}
if (rtn && result) strcpy(result,rtn);
return rtn;
}
/*
* searchPath uses findReadPath to search the list of directories
* specified by the $PATH environment variable.
*/
#if __STDC__
char *searchPath(char *name)
#else
char *searchPath(name)
char *name;
#endif
{
return findReadPath(name, "$PATH", NULL, NULL);
}
#ifdef __MSDOS__
void doIrq(int irq, short ax, short bx, short cx, short dx)
{
union REGS regs;
regs.x.ax = ax;
regs.x.bx = bx;
regs.x.cx = cx;
regs.x.dx = dx;
int86(irq,®s,®s);
}
int getReturnCode(void)
{
union REGS regs;
regs.x.ax = 0x4d00; /* get return code function */
intdos(®s,®s);
/* low byte has 0 (ok) 1 (^C) 2 (Critical error) or 3 (TSR)
high byte has child return */
return (regs.h.al<<8) + regs.h.ah;
}
int isDOSinternal(char *cmd)
{
char c[16]; int i;
static char *intCmd[] = {
/* this is not complete */
"BREAK", "CHCP", "CD", "CHDIR", "CLS", "COPY", "CTTY", "DATE",
"DEL", "DIR", "ECHO", "EXIT", "ERASE", "FOR", "GOTO", "IF",
"MKDIR", "MOVE", "PATH", "PAUSE", "PROMPT", "REM", "REN",
"RENAME", "RMDIR", "SET", "SHIFT", "TIME", "TYPE", "VER",
"VERIFY", "VOL", NULL
};
strncpy(c,cmd,14);
c[14] = '\0';
STRUPR(c);
i = 0;
while (intCmd[i]) /* should do a binary search, but I'm lazy... */
if (strcmp(intCmd[i++],c)==0) return 1;
return 0;
}
/* findCommandType, given a command, returns one of the following:
0 - unknown type
1 - internal DOS command
2 - DOS batch file
3 - DOS .EXE
4 - DOS .COM
If the command has an extension, this is checked, and a value
returned. Otherwise, if the file has a backslash in its name,
we check for a .BAT, .EXE or .COM file of that name and path.
If no backslash, we check the current directory and then the
path.
*/
static char *DOSexts[] = { ".BAT", ".EXE", ".COM", NULL };
int findCommandType(char *cmd)
{
char *sepPtr, *dotPtr, *pathPtr;
char Cmd[MAXPATHNAME], ext[5];
int rtn, i;
if (isDOSinternal(cmd)) return 1;
strcpy(Cmd,cmd);
ext[4] = '\0'; /* for safety */
dotPtr = STRRCHR(Cmd,'.');
sepPtr = STRRCHR(Cmd,'\\');
if (dotPtr && (sepPtr == NULL || dotPtr>sepPtr))
pathPtr = dotPtr; /* we have an extension */
else if (sepPtr) /* we have a path - check that directory */
{
*sepPtr = '\0';
pathPtr = findReadPath(sepPtr+1, Cmd, DOSexts, NULL);
}
else
/* no path & no extension; check . then $PATH */
pathPtr = findReadPath(Cmd, ".$PATH", DOSexts, NULL);
if (pathPtr == NULL) return 0;
strncpy(ext,pathPtr+strlen(pathPtr)-4,4);
STRUPR(ext);
for (i=0;i<3;i++)
{
if (strcmp(DOSexts[i],ext)==0)
{
/*fprintf(stderr,"Command %s is of type %s\n",cmd,DOSexts[i]);*/
return i+2;
}
}
/*fprintf(stderr,"Command %s is of unknown type\n",cmd);*/
return 0;
}
#else
/* UNIX version of DOS's spawnvp() */
#include <sys/wait.h>
#if __STDC__
int spawnvp(int flag, char *cmd, char *argv[])
#else
int spawnvp(flag, cmd, argv)
int flag;
char *cmd, *argv[];
#endif
{
int status, pid, wpid;
if ((pid = fork()) == 0)
{
execvp(cmd,argv);
if (errno == ENOEXEC)
{
/* we assume no more than 100 args! */
char *newArgs[102];
int i;
newArgs[0] = "sh";
newArgs[1] = "-c";
for (i=0;i<100;i++)
{
newArgs[i+2] = argv[i];
if (argv[i]==NULL) break;
}
newArgs[101] = NULL; /* safety */
execvp(cmd,newArgs);
}
}
while ((wpid = wait(&status)) != pid)
if (wpid == -1 && errno != EINTR)
return -1;
return (status >> 8) & 0377;
}
#endif
/****************************************************
GC3 EXPRESSION EVALUATOR
This code evaluates numeric expressions contained
in a string. It should be portable across any
decent C compiler.
Entry to this code is through the routine:
char *compute(char *str, int *rtn)
which takes as arguments a pointer to the string
containing the expression, and a pointer to the
variable in which the result should be placed.
If an error is found in the expression, a message
is returned, but if the expression is acceptable
then NULL is returned.
*****************************************************/
#ifdef __MSDOS__
# ifndef __STDC__
# define __STDC__ 1
# endif
#endif
/*
* Lexical tokens
*/
#define NUM_TOK 1
#define OR_TOK 2
#define AND_TOK 3
#define EQU_TOK 4
#define NEQ_TOK 5
#define ADD_TOK 6
#define SUB_TOK 7
#define MUL_TOK 8
#define DIV_TOK 9
#define MOD_TOK 10
#define LFT_TOK 11
#define RGT_TOK 12
#define GEQ_TOK 13
#define GTR_TOK 14
#define LEQ_TOK 15
#define LES_TOK 16
/*
* Local Variables
*/
static char *ebuf, /* Address of expression string */
*msg, /* Return message */
*fail = "Syntax error in expression"; /* Failure message */
static int epos, /* Current position in ebuf buffer */
tok, /* Current lexical token */
tokval; /* Current token value, if a number */
/*
* Forward declarations
*/
#if __STDC__
static int _cond_expr(void);
#endif
/*
* Lexical analyser
*/
#if __STDC__
static void nexttok(void)
#else
static void nexttok()
#endif
{
while (isspace(ebuf[epos])) epos++;
if (ebuf[epos])
{
if (isdigit(ebuf[epos])) /* Integer? */
{
tok = NUM_TOK;
tokval = 0;
while (isdigit(ebuf[epos]))
{
tokval = tokval * 10 + ebuf[epos++] - '0';
}
}
else
{ /* Operator */
switch(ebuf[epos])
{
case '|': tok = OR_TOK; break;
case '&': tok = AND_TOK; break;
case '=': tok = EQU_TOK; break;
case '#': tok = NEQ_TOK; break;
case '+': tok = ADD_TOK; break;
case '-': tok = SUB_TOK; break;
case '*': tok = MUL_TOK; break;
case '/': tok = DIV_TOK; break;
case '%': tok = MOD_TOK; break;
case '(': tok = LFT_TOK; break;
case ')': tok = RGT_TOK; break;
case '>':
if (ebuf[epos+1]=='=')
{
epos++;
tok = GEQ_TOK;
}
else tok = GTR_TOK;
break;
case '<':
if (ebuf[epos+1]=='=')
{
epos++;
tok = LEQ_TOK;
}
else tok = LES_TOK;
break;
}
epos++;
}
}
else tok=-1;
}
/*
* PARSER
*/
#if __STDC__
static int _cond_expr(void);
#else
static int _cond_expr();
#endif
/*
* A primitive expression can be either a
* conditional expression in parentheses,
* or an integer.
*/
#if __STDC__
static int _prim_expr(void)
#else
static int _prim_expr()
#endif
{
int sgn = 1, v;
if (tok==LFT_TOK)
{
nexttok();
v = _cond_expr();
if (tok!=RGT_TOK)
{
msg = fail;
return 0;
}
}
else
{
if (tok==SUB_TOK)
{
sgn = -1;
nexttok();
}
if (tok!=NUM_TOK)
{
msg = fail;
return 0;
}
v = sgn * tokval;
}
nexttok();
return v;
}
/*
* Multiplicative expression
*/
#if __STDC__
static int _mul_expr(void)
#else
static int _mul_expr()
#endif
{
int v = _prim_expr();
for (;;)
{
if (tok==MUL_TOK)
{
nexttok();
v *= _prim_expr();
}
else if (tok==DIV_TOK)
{
nexttok();
v /= _prim_expr();
}
else if (tok==MOD_TOK)
{
nexttok();
v %= _prim_expr();
}
else return v;
}
}
/*
* Additive expression
*/
#if __STDC__
static int _add_expr(void)
#else
static int _add_expr()
#endif
{
int v = _mul_expr();
for (;;)
{
if (tok==ADD_TOK)
{
nexttok();
v += _mul_expr();
}
else if (tok==SUB_TOK)
{
nexttok();
v -= _mul_expr();
}
else return v;
}
}
/*
* Relational expression
*/
#if __STDC__
static int _rel_expr(void)
#else
static int _rel_expr()
#endif
{
int v = _add_expr();
if (tok==GTR_TOK)
{
nexttok();
return (v > _rel_expr());
}
else if (tok==GEQ_TOK)
{
nexttok();
return (v >= _rel_expr());
}
else if (tok==LEQ_TOK)
{
nexttok();
return (v <= _rel_expr());
}
else if (tok==LES_TOK)
{
nexttok();
return (v < _rel_expr());
}
else return v;
}
/*
* Equality expression
*/
#if __STDC__
static int _equ_expr(void)
#else
static int _equ_expr()
#endif
{
int v = _rel_expr();
if (tok==EQU_TOK)
{
nexttok();
return (v == _equ_expr());
}
else if (tok==NEQ_TOK)
{
nexttok();
return (v != _equ_expr());
}
else return v;
}
/*
* AND conditional expressions
*/
#if __STDC__
static int _and_expr(void)
#else
static int _and_expr()
#endif
{
int v = _equ_expr();
if (tok==AND_TOK)
{
nexttok();
return (v && _and_expr());
}
else return v;
}
/*
* OR conditional expressions
*/
#if __STDC__
static int _cond_expr(void)
#else
static int _cond_expr()
#endif
{
int v = _and_expr();
if (tok==OR_TOK)
{
nexttok();
return (v || _cond_expr());
}
else return v;
}
/*
* Main entry point. The computed value is stored in the
* area pointed to by *rtn. `compute' returns NULL upon