/
calc.c
1479 lines (1362 loc) · 32.5 KB
/
calc.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
/* calc.c */
/* Copyright 1995 by Steve Kirkendall */
char id_calc[] = "$Id: calc.c,v 2.61 1998/12/04 23:12:25 steve Exp $";
#include "elvis.h"
#include <setjmp.h>
#ifdef TRY
# include <getopt.h>
# undef o_true
# define o_true "True"
# undef o_false
# define o_false "False"
# undef isdigit
# undef isupper
# undef islower
# undef isalnum
# undef toupper
# undef tolower
# if USE_PROTOTYPES
extern int isdigit(int c);
extern int isupper(int c);
extern int islower(int c);
extern int isalnum(int c);
extern int toupper(int c);
extern int tolower(int c);
# endif
#endif
#if USE_PROTOTYPES
static int copyname(CHAR *dest, CHAR *src, BOOLEAN num);
static BOOLEAN func(CHAR *name, CHAR *arg);
static BOOLEAN apply(void);
static CHAR *applyall(int prec);
#endif
#ifndef TRY
static CHAR *feature[] =
{
# ifdef PROTOCOL_HTTP
toCHAR("http"),
# endif
# ifdef PROTOCOL_FTP
toCHAR("ftp"),
# endif
# ifdef FEATURE_SHOWTAG
toCHAR("showtag"),
# endif
# ifdef FEATURE_LPR
toCHAR("lpr"),
# endif
# ifdef FEATURE_ALIAS
toCHAR("alias"),
# endif
# ifdef FEATURE_MKEXRC
toCHAR("mkexrc"),
# endif
# ifdef FEATURE_COMPLETE
toCHAR("complete"),
#endif
# ifdef FEATURE_RAM
toCHAR("ram"),
# endif
NULL
};
#endif
/* This array describes the operators */
static struct
{
char *name; /* name of the operator */
short prec; /* natural precedence of the operator */
char code; /* function code for applying operator */
char subcode;/* details, depend on function */
} opinfo[] =
{
{"Func",1, 'f', '('},
{";", 2, 'c', ';'},
{",", 2, 'c', ','},
{"||", 4, 'b', '|'},
{"&&", 5, 'b', '&'},
{"!=", 9, 's', '!'},
{"==", 9, 's', '='},
{"<=", 10, 's', 'l'},
{">=", 10, 's', 'g'},
{"<<", 11, 'i', '<'},
{">>", 11, 'i', '>'},
{":", 3, 't', ':'},
{"?", 2, 't', '?'},
{"|", 6, 'i', '|'},
{"^", 7, 'i', '^'},
{"&", 8, 'i', '&'},
{"=", 9, 's', '='},
{"<", 10, 's', '<'},
{">", 10, 's', '>'},
{"+", 12, 'i', '+'},
{"-", 12, 'i', '-'},
{"%", 13, 'i', '%'},
{"*", 13, 'i', '*'},
{"/", 13, 'i', '/'},
{"!", 14, 'u', '!'},
{"~", 14, 'u', '~'},
};
/* The following variables are used during the evaluation of each expression */
static struct
{
CHAR *first; /* first argument to operator */
int idx; /* index into opinfo of the operator */
int prec; /* precedence of operator */
} opstack[10];
static int ops; /* stack pointer for opstack[] */
/* The following variables are used for storing the parenthesis level */
static CHAR *parstack[20];
/* ultimately, the result returned by calculate() */
static CHAR result[1024];
#define RESULT_END (&result[QTY(result) - 3])
#define RESULT_AVAIL(from) ((int)(RESULT_END - (CHAR *)(from)))
#define RESULT_OVERFLOW(from, need) (RESULT_AVAIL(from) < (int)(need))
/* This function returns True iff a string looks like an integer */
BOOLEAN calcnumber(str)
CHAR *str; /* a nul-terminated string to check */
{
BOOLEAN dp = False; /* has decimal point been seen yet? */
/* allow a leading "-" */
if (*str == '-')
str++;
/* The only decimal number that can start with '0' is zero */
if (str[0] == '0' && str[1])
return False;
/* Require at least one digit, and don't allow any non-digits */
do
{
if (*str == '.' && dp == False && str[1])
dp = True;
else if (!isdigit(*str))
return False;
} while (*++str);
return True;
}
/* This function returns True if passed a string which looks like a number,
* and False if it doesn't. This function differs from calcnumber() in that
* this one also converts octal numbers, hex numbers, and literal characters
* to base ten. Note that the length of the string may change.
*/
BOOLEAN calcbase10(str)
CHAR *str;
{
long num;
int i;
/* do the easy tests first */
if (calcnumber(str)) return True;
if (*str != '0' && *str != '\'') return False;
if (str[0] == '\'')
{
if (str[1] == '\\')
{
if (!str[2] || str[3] != '\'')
return False;
switch (str[2])
{
case '0': num = 0; break;
case 'a': num = '\007'; break;
case 'b': num = '\b'; break;
case 'E': num = '\033'; break;
case 'f': num = '\f'; break;
case 'n': num = '\n'; break;
case 'r': num = '\r'; break;
case 't': num = '\t'; break;
default: num = str[2]; break;
}
i = 4;
}
else
{
if (str[1] == '\'' || !str[1] || str[2] != '\'')
return False;
num = str[1];
i = 3;
}
}
else if (str[1] == 'x')
{
/* is it hex? */
for (i = 2, num = 0; ; i++)
{
if (str[i] >= '0' && str[i] <= '9')
num = num * 16 + str[i] - '0';
else if (str[i] >= 'a' && str[i] <= 'f')
num = num * 16 + str[i] - 'a' + 10;
else
break;
}
}
else
{
/* is it octal? */
for (i = 1, num = 0; ; i++)
{
if (str[i] >= '0' && str[i] <= '7')
num = num * 8 + str[i] - '0';
else
break;
}
}
/* If we hit a problem before the end of the string, it isn't a number */
if (str[i]) return False;
/* We have a number! Convert to decimal */
long2CHAR(str, num);
return True;
}
/* This function returns False if the string looks like any kind of "false"
* value, and True otherwise. The false values are "", "0", "false", "no",
* and the value of the `false' option.
*/
BOOLEAN calctrue(str)
CHAR *str; /* the sting to be checked */
{
if (!str || !*str || !CHARcmp(str, toCHAR("0"))
|| !CHARcmp(str, toCHAR("false")) || !CHARcmp(str, toCHAR("no"))
|| (o_false && !CHARcmp(str, o_false)))
{
return False;
}
return True;
}
/* This function copies characters up to the next non-alphanumeric character,
* nul-terminates the copy, and returns the number of characters copied.
*/
static int copyname(dest, src, num)
CHAR *dest; /* where to copy into */
CHAR *src; /* start of alphanumeric string to copy from */
BOOLEAN num; /* treat numbers specially? */
{
int i;
/* copy until non-alphanumeric character is encountered */
if (num && isdigit(*src))
{
for (i = 0; isdigit(*src); i++)
{
if (RESULT_OVERFLOW(dest, 2)) return 0;
*dest++ = *src++;
}
}
else
{
for (i = 0; isalnum(*src) || *src == '_'; i++)
{
if (RESULT_OVERFLOW(dest, 2)) return 0;
*dest++ = *src++;
}
}
*dest = '\0';
return i;
}
/* This function implements the built-in "functions". The name indicates
* which function should be performed, and arg is its only argument. The
* result should be a string, and it should be copied over the name; func
* should then return True to indicate success. If the function fails for
* any reason, func() should emit an error message and return False.
*
* The functions supported are:
* strlen(string) return the number of characters in the string.
* tolower(string) returns a lowercase version of string.
* toupper(string) returns an uppercase version of string.
* isnumber(string) return "true" iff string is a decimal number
* htmlsafe(string) return an HTML-encoded version of string
* quote(chars,str) return a copy of str with backslashes before chars
* unquote(chars,str) return a copy of str with backslashes before chars
* hex(number) return a string representing the hex value of number
* octal(number) return a string representing the octal value of number
* char(number) return a single-character string
* exists(file) return "true" iff file exists
* dirperm(file) return a string indicating file attributes.
* dirfile(file) return the filename part of a pathname (including ext)
* dirname(file) return the directory part of a pathname.
* dirdir(file) return the directory, like dirname(file).
* dirext(file) return the filename extension.
* basename(file) return the filename without extension.
* elvispath(file) return the pathname of a file in elvis' path, or ""
* buffer(buf) return "true" iff buffer exist
* feature(name) return "true" iff the named feature is supported
* knownsyntax(file) return language if the file's extension is in elvis.syn
* current(what) return line, column, word, mode, next, prev, or tag
*/
static BOOLEAN func(name, arg)
CHAR *name; /* name of function to apply */
CHAR *arg; /* the argument to the function */
{
CHAR *tmp;
#ifndef TRY
char *c;
int i;
MARK begin;
MARKBUF end;
#endif
if (!CHARcmp(name, toCHAR("strlen")))
{
long2CHAR(name, (long)CHARlen(arg));
return True;
}
else if (!CHARcmp(name, toCHAR("tolower")))
{
for (; *arg; arg++)
*name++ = (isupper(*arg) ? tolower(*arg) : *arg);
*name = '\0';
return True;
}
else if (!CHARcmp(name, toCHAR("toupper")))
{
for (; *arg; arg++)
*name++ = (islower(*arg) ? toupper(*arg) : *arg);
*name = '\0';
return True;
}
else if (!CHARcmp(name, toCHAR("isnumber")))
{
if (RESULT_OVERFLOW(name, 6)) goto Overflow;
CHARcpy(name, calcnumber(arg) ? o_true : o_false);
return True;
}
#ifndef TRY
else if (!CHARcmp(name, toCHAR("htmlsafe")))
{
for (i = 0, tmp = NULL; arg[i]; i++)
{
switch (arg[i])
{
case '&': buildstr(&tmp, "&"); break;
case '<': buildstr(&tmp, "<"); break;
case '>': buildstr(&tmp, ">"); break;
case '"': buildstr(&tmp, """); break;
case '\t': buildCHAR(&tmp, ' '); break;
default: buildCHAR(&tmp, arg[i]);
}
}
if (tmp)
{
if (RESULT_OVERFLOW(name, CHARlen(tmp))) goto Overflow;
CHARcpy(name, tmp);
safefree(tmp);
}
else
{
*name = '\0';
}
return True;
}
#endif
else if (!CHARcmp(name, toCHAR("hex")))
{
if (!calcnumber(arg)) goto NeedNumber;
if (RESULT_OVERFLOW(name, 10)) goto Overflow;
sprintf((char *)name, "0x%lx", CHAR2long(arg));
return True;
}
else if (!CHARcmp(name, toCHAR("octal")))
{
if (!calcnumber(arg)) goto NeedNumber;
if (RESULT_OVERFLOW(name, 12)) goto Overflow;
sprintf((char *)name, "0%lo", CHAR2long(arg));
return True;
}
else if (!CHARcmp(name, toCHAR("char")))
{
if (!calcnumber(arg)) goto NeedNumber;
*name++ = (CHAR)CHAR2long(arg);
*name = '\0';
return True;
}
else if (!CHARcmp(name, toCHAR("dirext")))
{
/* find the last '.' in the name */
for (tmp = arg + CHARlen(arg); --tmp >= arg && isalnum(*tmp); )
{
}
if (*tmp != '.')
{
tmp = toCHAR("");
}
CHARcpy(name, tmp);
return True;
}
#ifndef TRY
else if (!CHARcmp(name, toCHAR("quote")))
{
/* divide the arg into "chars" and "str" fields */
tmp = CHARchr(arg, (CHAR)',');
if (!tmp)
{
goto Need2Args;
}
*tmp++ = '\0';
/* build a copy with backslashes */
tmp = addquotes(arg, tmp);
/* if the resulting string fits in buffer, then store it */
if (RESULT_OVERFLOW(name, CHARlen(tmp)))
goto Overflow;
CHARcpy(name, tmp);
safefree(tmp);
return True;
}
else if (!CHARcmp(name, toCHAR("unquote")))
{
/* divide the arg into "chars" and "str" fields */
tmp = CHARchr(arg, (CHAR)',');
if (!tmp)
{
goto Need2Args;
}
*tmp++ = '\0';
/* build a copy with backslashes */
tmp = removequotes(arg, tmp);
/* store the resulting string (it *will* fit) */
CHARcpy(name, tmp);
safefree(tmp);
return True;
}
else if (!CHARcmp(name, toCHAR("exists")))
{
switch (urlperm(tochar8(arg)))
{
case DIR_INVALID:
case DIR_BADPATH:
case DIR_NOTFILE:
case DIR_NEW:
CHARcpy(name, o_false);
break;
default:
CHARcpy(name, o_true);
break;
}
return True;
}
else if (!CHARcmp(name, toCHAR("dirperm")))
{
switch (urlperm(tochar8(arg)))
{
case DIR_INVALID:
CHARcpy(name, toCHAR("invalid"));
break;
case DIR_BADPATH:
CHARcpy(name, toCHAR("badpath"));
break;
case DIR_NOTFILE:
CHARcpy(name, toCHAR("notfile"));
break;
case DIR_NEW:
CHARcpy(name, toCHAR("new"));
break;
case DIR_UNREADABLE:
CHARcpy(name, toCHAR("unreadable"));
break;
case DIR_READONLY:
CHARcpy(name, toCHAR("readonly"));
break;
case DIR_READWRITE:
CHARcpy(name, toCHAR("readwrite"));
break;
}
return True;
}
else if (!CHARcmp(name, toCHAR("dirfile")))
{
CHARcpy(name, toCHAR(dirfile(tochar8(arg))));
return True;
}
else if (!CHARcmp(name, toCHAR("dirname")) || !CHARcmp(name, toCHAR("dirdir")))
{
CHARcpy(name, toCHAR(dirdir(tochar8(arg))));
return True;
}
else if (!CHARcmp(name, toCHAR("basename")))
{
CHARcpy(name, toCHAR(dirfile(tochar8(arg))));
/* find the last '.' in the name */
for (tmp = name + CHARlen(name); --tmp >= name && isalnum(*tmp); )
{
}
if (*tmp == '.')
{
*tmp = '\0';
}
return True;
}
else if (!CHARcmp(name, toCHAR("fileeol")))
{
CHARcpy(name, toCHAR(ioeol(tochar8(arg))));
return True;
}
else if (!CHARcmp(name, toCHAR("elvispath")))
{
tmp = toCHAR(iopath(tochar8(o_elvispath), tochar8(arg), False));
if (!tmp)
*name = '\0';
else
{
if (RESULT_OVERFLOW(name, CHARlen(tmp))) goto Overflow;
CHARcpy(name, tmp);
}
return True;
}
else if (!CHARcmp(name, toCHAR("getcwd")))
{
c = dircwd();
if (RESULT_OVERFLOW(name, strlen(c))) goto Overflow;
CHARcpy(name, toCHAR(c));
return True;
}
else if (!CHARcmp(name, toCHAR("absolute")))
{
c = urllocal(tochar8(arg));
if (c)
c = dirpath(dircwd(), c);
else
c = tochar8(arg);
if (RESULT_OVERFLOW(name, strlen(c))) goto Overflow;
CHARcpy(name, toCHAR(c));
return True;
}
else if (!CHARcmp(name, toCHAR("buffer")))
{
CHARcpy(name, buffind(arg) ? o_true : o_false);
return True;
}
else if (!CHARcmp(name, toCHAR("alias")))
{
# ifdef FEATURE_ALIAS
c = exisalias(tochar8(arg), True);
tmp = (c ? o_true : o_false);
# else
tmp = o_false;
# endif
if (RESULT_OVERFLOW(name, CHARlen(tmp))) goto Overflow;
CHARcpy(name, tmp);
return True;
}
else if (!CHARcmp(name, toCHAR("feature")))
{
/* for now */
CHARcpy(name, o_false);
/* is it the name of a supported display mode? */
i = 0;
do
{
if (!CHARcmp(toCHAR(allmodes[i]->name), arg))
{
CHARcpy(name, o_true);
return True;
}
} while (allmodes[i++] != &dmnormal);
/* is it the name of a supported protocol */
for (i = 0; feature[i] && CHARcmp(feature[i], arg); i++)
{
}
if (feature[i])
CHARcpy(name, o_true);
return True;
}
else if (!CHARcmp(name, toCHAR("knownsyntax")))
{
#ifdef DISPLAY_SYNTAX
tmp = dmsknown(tochar8(arg));
if (!tmp)
*name = '\0';
else if (RESULT_OVERFLOW(name, CHARlen(tmp)))
goto Overflow;
else
CHARcpy(name, tmp);
#else
*name = '\0'; /* no syntax modes are supported */
#endif
return True;
}
else if (!CHARcmp(name, toCHAR("current")))
{
/* The default return value is an empty string */
*name = '\0';
/* Other possible values depend on the arg */
switch (*arg)
{
case 'l': /* line number */
if (windefault)
sprintf(tochar8(name), "%ld", markline(windefault->cursor));
break;
case 'c': /* column number */
if (windefault)
sprintf(tochar8(name), "%ld", (*windefault->md->mark2col)(windefault, windefault->cursor, viiscmd(windefault)) + 1);
break;
case 'w': /* word at cursor */
if (windefault)
{
end = *windefault->cursor;
begin = wordatcursor(&end);
if (begin && RESULT_OVERFLOW(name, markoffset(&end) - markoffset(begin)))
goto Overflow;
if (begin)
{
scanalloc(&tmp, begin);
for (i = (int)(markoffset(&end) - markoffset(begin)); i > 0; i--)
{
*name++ = *tmp;
scannext(&tmp);
}
scanfree(&tmp);
*name = '\0';
}
}
break;
case 'm': /* mode */
if (windefault && !windefault->state->acton)
{
for (c = windefault->state->modename; *c; c++)
{
if (*c != ' ')
*name++ = tolower(*c);
}
}
break;
case 's': /* visible selection */
if (windefault && !windefault->state->acton && windefault->seltop)
{
switch (windefault->seltype)
{
case 'c':
CHARcpy(name, toCHAR("character"));
break;
case 'r':
CHARcpy(name, toCHAR("rectangle"));
break;
default:
CHARcpy(name, toCHAR("line"));
}
}
break;
case 'n': /* next arg */
if (arglist[argnext])
{
if (RESULT_OVERFLOW(name, strlen(arglist[argnext])))
goto Overflow;
for (c = arglist[argnext]; *c; )
{
*name++ = *c++;
}
*name = '\0';
}
break;
case 'p': /* previous arg */
if (argnext >= 2)
{
if (RESULT_OVERFLOW(name, strlen(arglist[argnext - 2])))
goto Overflow;
for (c = arglist[argnext - 2]; *c; )
{
*name++ = *c++;
}
*name = '\0';
}
break;
case 't': /* tag or tagstack */
tmp = NULL;
#ifdef FEATURE_SHOWTAG
if (!CHARcmp(arg, "tag"))
tmp = telabel(windefault->cursor);
else
#endif
if (windefault && windefault->tagstack->origin)
tmp = o_bufname(markbuffer(windefault->tagstack->origin));
if (tmp)
{
if (RESULT_OVERFLOW(name, CHARlen(tmp)))
goto Overflow;
CHARcpy(name, tmp);
}
break;
}
return True;
}
#endif
#ifdef TRY
msg(MSG_ERROR, "unknown function %s", name);
#else
msg(MSG_ERROR, "[S]unknown function $1", name);
#endif
return False;
NeedNumber:
#ifdef TRY
msg(MSG_ERROR, "%s requires a numeric argument", name);
#else
msg(MSG_ERROR, "[S]$1 requires a numeric argument", name);
#endif
return False;
#ifndef TRY
Need2Args:
msg(MSG_ERROR, "[S]$1 requires two arguments", name);
return False;
#endif
Overflow:
msg(MSG_ERROR, "result too long");
return False;
}
/* This function applies a single operator. Returns False on error. Its
* side effects are that it decrements the "ops" counter, and alters the
* contents of the result buffer.
*/
static BOOLEAN apply()
{
long i, j;
CHAR *first, *second, *third;
char subcode;
assert(ops >= 1);
second = opstack[ops--].first;
first = opstack[ops].first;
subcode = opinfo[opstack[ops].idx].subcode;
switch (opinfo[opstack[ops].idx].code)
{
case 'u': /* unary operators */
/* Unary operators depend only on their second argument.
* The result is concatenated to the first argument, which
* is normally an empty string.
*/
if (subcode == '!')
{
(void)CHARcat(first, calctrue(second) ? o_false : o_true);
}
else /* '~' */
{
if (calcnumber(second))
{
/* bitwise negation */
long2CHAR(first + CHARlen(first), ~CHAR2long(second));
}
else
{
/* stuff a ~ between the strings */
second[-1] = '~';
}
}
break;
case 'i': /* integer operators */
/* If either argument is a non-number, then concatenate them
* with the operator between them. This is tricky because
* the << and >> operators are too large to simply replace
* the '\0' between the strings.
*/
if (!calcnumber(first) || !calcnumber(second))
{
if (subcode == '<' || subcode == '>')
{
/* As a special case, "string << number" and
* "string >> number" truncate the string to
* the length given by the number, keeping
* characters from the left or right.
*/
if (calcnumber(second))
{
/* convert arguments */
i = CHARlen(first);
j = CHAR2long(second);
/* make sure this width wouldn't
* overflow the result buffer.
*/
if (RESULT_OVERFLOW(first, j))
{
msg(MSG_ERROR, "result too long");
return False;
}
/* Pad or truncate */
if (subcode == '<')
{
/* Pad or truncate, keeping
* chars on left. The first
* arg's characters are already
* in the right place, so we
* don't need to copy them.
* Just pad if first is short.
*/
while (i < j)
first[i++] = ' ';
first[j] = '\0';
}
else
{
if (i < j)
{
/* String needs to be
* padded. Shift it
* to right, and then
* pad on the left.
*/
first[j] = '\0';
while (i > 0)
first[--j] = first[--i];
while (j > 0)
first[--j] = ' ';
}
else if (i > j)
{
/* String needs to be
* truncated. Shift it
* to the left.
*/
CHARcpy(first, first + i - j);
}
}
break;
}
msg(MSG_WARNING, "<< and >> only partially implemented");
}
#ifndef TRY
else if (subcode == '/')
{
/* When the / operator is passed strings as
* arguments, it contatenates them as a
* directory name and a file name.
*/
CHARcpy(first, toCHAR(dirpath(tochar8(first), tochar8(second))));
break;
}
#endif
second[-1] = subcode;
}
else
{
i = CHAR2long(first);
j = CHAR2long(second);
switch (subcode)
{
case '*': i *= j; break;
case '+': i += j; break;
case '-': i -= j; break;
case '<': i <<= j; break;
case '>': i >>= j; break;
case '&': i &= j; break;
case '^': i ^= j; break;
case '|': i |= j; break;
case '/': if (j == 0) goto DivZero;
i /= j; break;
case '%': if (j == 0) goto DivZero;
i %= j; break;
}
long2CHAR(first, i);
}
break;
case 's': /* string or integer comparison operators */
/* if both arguments look like numbers, then compare
* numerically; else compare as strings.
*/
if (calcnumber(first) && calcnumber(second))
{
i = CHAR2long(first) - CHAR2long(second);
}
else
{
i = CHARcmp(first, second);
}
switch (subcode)
{
case '<': i = (i < 0); break;
case 'l': i = (i <= 0); break;
case '>': i = (i > 0); break;
case 'g': i = (i >= 0); break;
case '=': i = (i == 0); break;
case '!': i = (i != 0); break;
}
(void)CHARcpy(first, toCHAR(i ? o_true : o_false));
break;
case 'c': /* concatenation operators */
if (subcode == ';')
memmove(second - 1, second, sizeof(CHAR) * (CHARlen(second) + 1));
else
second[-1] = subcode;
break;
case 'b': /* boolean operators */
if (subcode == '&')
{
i = (calctrue(first) && calctrue(second));
}
else /* subcode == '|' */
{
i = (calctrue(first) || calctrue(second));
}
(void)CHARcpy(first, toCHAR(i ? o_true : o_false));
break;
case 't': /* ternary operator */
/* This should be either (bool ? string : string) if we're
* evaluating a ':', or just (bool ? string) if we're
* evaluating a '?'. The '?' and ':' operators are parsed as
* if they were separate binary operators. The '?' has a
* slightly lower precedence than ':', so if we're evaluating
* a ':' we can expect the '?' to be the preceding operator on
* the stack. That's very important!
*/
if (subcode == ':')
{
/* complain if not after a '?' */
if (ops < 1 || opstack[ops - 1].prec != opstack[ops].prec - 1)
{
msg(MSG_ERROR, "bad operator");
return False;
}
/* shift the arguments */
third = second;
second = first;
first = opstack[--ops].first;
}
else
{
/* (bool ? string) is legal -- assume third arg is "" */
third = toCHAR("");
}
/* replace the first boolean with either second or third arg */
CHARcpy(first, calctrue(first) ? second : third);
break;
case 'f': /* functions */
/* use func() to apply the function. */
return func(first, second);
}
return True;
DivZero:
msg(MSG_ERROR, "division by 0");
return False;
}
/* This function iteratively applies all preceding operators with a precedence
* no lower than some given level. Leaves "result" and "ops" altered. Returns
* a pointer to the end of the result, or NULL if error.
*/
static CHAR *applyall(prec)
int prec; /* lowest precedence to apply */
{
while (ops > 0 && opstack[ops - 1].prec >= prec)
{
if (!apply())
{
return (CHAR *)0;