Menu

[7c54b3]: / joe / umath.c  Maximize  Restore  History

Download this file

899 lines (840 with data), 17.3 kB

  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
/*
* Math
* Copyright
* (C) 1992 Joseph H. Allen
*
* This file is part of JOE (Joe's Own Editor)
*/
#include "types.h"
const char *merr;
int mode_hex;
int mode_eng;
int mode_ins;
double vzero = 0.0;
static RETSIGTYPE fperr(int unused)
{
if (!merr) {
merr = joe_gettext(_("Float point exception"));
}
REINSTALL_SIGHANDLER(SIGFPE, fperr);
}
struct var {
char *name;
double (*func)(double n);
int set;
double val;
struct var *next;
} *vars = NULL;
static struct var *get(const char *str)
{
struct var *v;
for (v = vars; v; v = v->next) {
if (!zcmp(v->name, str)) {
return v;
}
}
v = (struct var *) joe_malloc(SIZEOF(struct var));
v->set = 0;
v->func = 0;
v->val = 0;
v->next = vars;
vars = v;
v->name = zdup(str);
return v;
}
const char *ptr;
struct var *dumb;
static double eval(const char *s, int secure);
int recur=0;
/* en means enable evaluation */
static double expr(int prec, int en,struct var **rtv, int secure)
{
double x = 0.0, y, z;
struct var *v = NULL;
char *ident, *macr;
ident = vsmk(0);
macr = vsmk(0);
parse_ws(&ptr, '#');
if (!parse_ident(&ptr, &ident)) {
if (!secure && !zcmp(ident ,"joe")) {
v = 0;
x = 0.0;
parse_ws(&ptr, '#');
if (*ptr=='(') {
ptrdiff_t idx;
MACRO *m;
ptrdiff_t sta;
idx = 0;
++ptr;
while (*ptr && *ptr != ')') {
if (idx != SIZEOF(macr) - 1)
macr[idx++] = *ptr;
++ptr;
}
macr[idx] = 0;
if (*ptr != ')') {
if (!merr)
merr = joe_gettext(_("Missing )"));
} else {
ptr++;
}
if (en) {
m = mparse(NULL,macr,&sta,0);
if (m) {
x = !exmacro(m, 1, NO_MORE_DATA);
rmmacro(m);
} else {
if (!merr)
merr = joe_gettext(_("Syntax error in macro"));
}
}
} else {
if (!merr)
merr = joe_gettext(_("Missing ("));
}
} else if (!en) {
v = 0;
x = 0.0;
} else if (!zcmp(ident, "hex")) {
mode_hex = 1;
mode_eng = 0;
v = get("ans");
x = v->val;
} else if (!zcmp(ident, "dec")) {
mode_hex = 0;
mode_eng = 0;
v = get("ans");
x = v->val;
} else if (!zcmp(ident, "eng")) {
mode_hex = 0;
mode_eng = 1;
v = get("ans");
x = v->val;
} else if (!zcmp(ident, "ins")) {
mode_ins = 1;
v = get("ans");
x = v->val;
} else if (!zcmp(ident, "sum")) {
double xsq;
int cnt = blksum(&x, &xsq);
if (!merr && cnt<=0)
merr = joe_gettext(_("No numbers in block"));
v = 0;
} else if (!zcmp(ident, "cnt")) {
double xsq;
int cnt = blksum(&x, &xsq);
if (!merr && cnt<=0)
merr = joe_gettext(_("No numbers in block"));
v = 0;
x = cnt;
} else if (!zcmp(ident, "avg")) {
double xsq;
int cnt = blksum(&x, &xsq);
if (!merr && cnt<=0)
merr = joe_gettext(_("No numbers in block"));
v = 0;
if (cnt)
x /= (double)cnt;
} else if (!zcmp(ident, "dev")) {
double xsq;
double avg;
int cnt = blksum(&x, &xsq);
if (!merr && cnt<=0)
merr = joe_gettext(_("No numbers in block"));
v = 0;
if (cnt) {
avg = x / (double)cnt;
x = sqrt(xsq + (double)cnt*avg*avg - 2.0*avg*x);
}
} else if (!zcmp(ident, "eval")) {
const char *save = ptr;
char *e = blkget();
if (e) {
v = 0;
x = eval(e,secure);
joe_free(e);
ptr = save;
} else if (!merr) {
merr = joe_gettext(_("No block"));
}
} else {
v = get(ident);
x = v->val;
}
} else if ((*ptr >= '0' && *ptr <= '9') || *ptr == '.') {
if (ptr[0] == '0' && ptr[1] == 'x') {
x = 0.0;
ptr += 2;
while ((*ptr >= '0' && *ptr <= '9') || (*ptr >= 'a' && *ptr <='f') ||
(*ptr >= 'A' && *ptr <= 'F'))
if (*ptr >= '0' && *ptr <= '9')
x = x * 16.0 + *ptr++ - '0';
else if (*ptr >= 'a' && *ptr <= 'f')
x = x * 16.0 + *ptr++ - 'a' + 10;
else
x = x * 16.0 + *ptr++ - 'A' + 10;
} else {
char *eptr;
x = strtod(ptr,&eptr);
ptr = eptr;
}
} else if (*ptr == '(') {
++ptr;
x = expr(0, en, &v, secure);
if (*ptr == ')')
++ptr;
else {
if (!merr)
merr = joe_gettext(_("Missing )"));
}
} else if (*ptr == '-') {
++ptr;
x = -expr(10, en, &dumb, secure);
} else if (*ptr == '!') {
++ptr;
x = !expr(10, en, &dumb, secure);
}
loop:
while (*ptr == ' ' || *ptr == '\t')
++ptr;
if (*ptr == '(' && 11 > prec) {
++ptr;
y = expr(0, en, &dumb, secure);
if (*ptr == ')')
++ptr;
else {
if (!merr)
merr = joe_gettext(_("Missing )"));
}
if (v && v->func)
x = v->func(y);
else {
if (!merr)
merr = joe_gettext(_("Called object is not a function"));
}
goto loop;
} else if (*ptr == '!' && ptr[1]!='=' && 10 >= prec) {
++ptr;
if (x == (int)x && x>=1.0 && x<70.0) {
y = 1.0;
while (x>1.0) {
y *= x;
x -= 1.0;
}
x = y;
} else {
if (!merr)
merr = joe_gettext(_("Factorial can only take positive integers"));
}
v = 0;
goto loop;
} else if (*ptr == '*' && ptr[1] == '*' && 8 > prec) {
ptr+=2;
x = pow(x, expr(8, en, &dumb, secure));
v = 0;
goto loop;
} else if (*ptr == '^' && 8 > prec) {
++ptr;
x = pow(x, expr(8, en, &dumb, secure));
v = 0;
goto loop;
} else if (*ptr == '*' && 7 > prec) {
++ptr;
x *= expr(7, en, &dumb, secure);
v = 0;
goto loop;
} else if (*ptr == '/' && 7 > prec) {
++ptr;
x /= expr(7, en, &dumb, secure);
v = 0;
goto loop;
} else if(*ptr=='%' && 7>prec) {
++ptr;
y = expr(7, en, &dumb, secure);
if ((int)y == 0) x = 1.0/vzero;
else x = ((int) x) % (int)y;
v = 0;
goto loop;
} else if (*ptr == '+' && 6 > prec) {
++ptr;
x += expr(6, en, &dumb, secure);
v = 0;
goto loop;
} else if (*ptr == '-' && 6 > prec) {
++ptr;
x -= expr(6, en, &dumb, secure);
v = 0;
goto loop;
} else if (*ptr == '<' && 5 > prec) {
++ptr;
if (*ptr == '=') ++ptr, x = (x <= expr(5, en, &dumb, secure));
else x = (x < expr(5,en,&dumb, secure));
v = 0;
goto loop;
} else if (*ptr == '>' && 5 > prec) {
++ptr;
if (*ptr == '=') ++ptr, x=(x >= expr(5,en,&dumb, secure));
else x = (x > expr(5,en,&dumb, secure));
v = 0;
goto loop;
} else if (*ptr == '=' && ptr[1] == '=' && 5 > prec) {
++ptr, ++ptr;
x = (x == expr(5,en,&dumb, secure));
v = 0;
goto loop;
} else if (*ptr == '!' && ptr[1] == '=' && 5 > prec) {
++ptr, ++ptr;
x = (x != expr(5,en,&dumb,secure));
v = 0;
goto loop;
} else if (*ptr == '&' && ptr[1] == '&' && 3 > prec) {
++ptr, ++ptr;
y = expr(3,x!=0.0 && en,&dumb,secure);
x = (int)x && (int)y;
v = 0;
goto loop;
} else if (*ptr=='|' && ptr[1]=='|' && 3 > prec) {
++ptr, ++ptr;
y = expr(3,x==0.0 && en,&dumb,secure);
x = (int)x || (int)y;
v= 0;
goto loop;
} else if (*ptr=='?' && 2 >= prec) {
++ptr;
y = expr(2,x!=0.0 && en,&dumb,secure);
if (*ptr==':') {
++ptr;
z = expr(2,x==0.0 && en,&dumb,secure);
if (x != 0.0)
x = y;
else
x = z;
v = 0;
} else if (!merr) {
merr = ": missing after ?";
}
goto loop;
} else if (*ptr == '=' && 1 >= prec) {
++ptr;
x = expr(1, en,&dumb,secure);
if (v) {
v->val = x;
v->set = 1;
} else {
if (!merr)
merr = joe_gettext(_("Left side of = is not an l-value"));
}
v = 0;
goto loop;
}
*rtv = v;
return x;
}
static double eval(const char *s, int secure)
{
double result = 0.0;
struct var *v;
if(++recur==1000) {
merr = joe_gettext(_("Recursion depth exceeded"));
--recur;
return 0.0;
}
ptr = s;
while (!merr && *ptr && *ptr != '#') {
result = expr(0, 1, &dumb,secure);
v = get("ans");
v->val = result;
v->set = 1;
if (!merr) {
parse_ws(&ptr, '#');
if (*ptr == ':') {
++ptr;
parse_ws(&ptr, '#');
} else if (*ptr && *ptr != '#') {
merr = joe_gettext(_("Extra junk after end of expr"));
}
}
}
--recur;
return result;
}
/* These don't all exist on some systems... */
#ifdef HAVE_SIN
static double m_sin(double n) { return sin(n); }
#else
#ifdef sin
static double m_sin(double n) { return sin(n); }
#endif
#endif
#ifdef HAVE_COS
static double m_cos(double n) { return cos(n); }
#else
#ifdef cos
static double m_cos(double n) { return cos(n); }
#endif
#endif
#ifdef HAVE_TAN
static double m_tan(double n) { return tan(n); }
#else
#ifdef tan
static double m_tan(double n) { return tan(n); }
#endif
#endif
#ifdef HAVE_EXP
static double m_exp(double n) { return exp(n); }
#else
#ifdef exp
static double m_exp(double n) { return exp(n); }
#endif
#endif
#ifdef HAVE_SQRT
static double m_sqrt(double n) { return sqrt(n); }
#else
#ifdef sqrt
static double m_sqrt(double n) { return sqrt(n); }
#endif
#endif
#ifdef HAVE_CBRT
static double m_cbrt(double n) { return cbrt(n); }
#else
#ifdef cbrt
static double m_cbrt(double n) { return cbrt(n); }
#endif
#endif
#ifdef HAVE_LOG
static double m_log(double n) { return log(n); }
#else
#ifdef log
static double m_log(double n) { return log(n); }
#endif
#endif
#ifdef HAVE_LOG10
static double m_log10(double n) { return log10(n); }
#else
#ifdef log10
static double m_log10(double n) { return log10(n); }
#endif
#endif
#ifdef HAVE_ASIN
static double m_asin(double n) { return asin(n); }
#else
#ifdef asin
static double m_asin(double n) { return asin(n); }
#endif
#endif
#ifdef HAVE_ACOS
static double m_acos(double n) { return acos(n); }
#else
#ifdef acos
static double m_acos(double n) { return acos(n); }
#endif
#endif
#ifdef HAVE_ATAN
static double m_atan(double n) { return atan(n); }
#else
#ifdef atan
static double m_atan(double n) { return atan(n); }
#endif
#endif
#ifdef HAVE_SINH
static double m_sinh(double n) { return sinh(n); }
#else
#ifdef sinh
static double m_sinh(double n) { return sinh(n); }
#endif
#endif
#ifdef HAVE_COSH
static double m_cosh(double n) { return cosh(n); }
#else
#ifdef cosh
static double m_cosh(double n) { return cosh(n); }
#endif
#endif
#ifdef HAVE_TANH
static double m_tanh(double n) { return tanh(n); }
#else
#ifdef tanh
static double m_tanh(double n) { return tanh(n); }
#endif
#endif
#ifdef HAVE_ASINH
static double m_asinh(double n) { return asinh(n); }
#else
#ifdef asinh
static double m_asinh(double n) { return asinh(n); }
#endif
#endif
#ifdef HAVE_ACOSH
static double m_acosh(double n) { return acosh(n); }
#else
#ifdef acosh
static double m_acosh(double n) { return acosh(n); }
#endif
#endif
#ifdef HAVE_ATANH
static double m_atanh(double n) { return atanh(n); }
#else
#ifdef atanh
static double m_atanh(double n) { return atanh(n); }
#endif
#endif
#ifdef HAVE_FLOOR
static double m_floor(double n) { return floor(n); }
#else
#ifdef floor
static double m_floor(double n) { return floor(n); }
#endif
#endif
#ifdef HAVE_CEIL
static double m_ceil(double n) { return ceil(n); }
#else
#ifdef ceil
static double m_ceil(double n) { return ceil(n); }
#endif
#endif
#ifdef HAVE_FABS
static double m_fabs(double n) { return fabs(n); }
#else
#ifdef fabs
static double m_fabs(double n) { return fabs(n); }
#endif
#endif
#ifdef HAVE_ERF
static double m_erf(double n) { return erf(n); }
#else
#ifdef erf
static double m_erf(double n) { return erf(n); }
#endif
#endif
#ifdef HAVE_ERFC
static double m_erfc(double n) { return erfc(n); }
#else
#ifdef erfc
static double m_erfc(double n) { return erfc(n); }
#endif
#endif
#ifdef HAVE_J0
static double m_j0(double n) { return j0(n); }
#else
#ifdef j0
static double m_j0(double n) { return j0(n); }
#endif
#endif
#ifdef HAVE_J1
static double m_j1(double n) { return j1(n); }
#else
#ifdef j1
static double m_j1(double n) { return j1(n); }
#endif
#endif
#ifdef HAVE_Y0
static double m_y0(double n) { return y0(n); }
#else
#ifdef y0
static double m_y0(double n) { return y0(n); }
#endif
#endif
#ifdef HAVE_Y1
static double m_y1(double n) { return y1(n); }
#else
#ifdef y1
static double m_y1(double n) { return y1(n); }
#endif
#endif
static double m_int(double n) { return (int)(n); }
double calc(BW *bw, char *s, int secure)
{
/* BW *tbw = bw->parent->main->object; */
BW *tbw = bw;
struct var *v;
int c = brch(bw->cursor);
if (!vars) {
#ifdef HAVE_SIN
v = get("sin"); v->func = m_sin;
#else
#ifdef sin
v = get("sin"); v->func = m_sin;
#endif
#endif
#ifdef HAVE_COS
v = get("cos"); v->func = m_cos;
#else
#ifdef cos
v = get("cos"); v->func = m_cos;
#endif
#endif
#ifdef HAVE_TAN
v = get("tan"); v->func = m_tan;
#else
#ifdef tan
v = get("tan"); v->func = m_tan;
#endif
#endif
#ifdef HAVE_EXP
v = get("exp"); v->func = m_exp;
#else
#ifdef exp
v = get("exp"); v->func = m_exp;
#endif
#endif
#ifdef HAVE_SQRT
v = get("sqrt"); v->func = m_sqrt;
#else
#ifdef sqrt
v = get("sqrt"); v->func = m_sqrt;
#endif
#endif
#ifdef HAVE_CBRT
v = get("cbrt"); v->func = m_cbrt;
#else
#ifdef cbrt
v = get("cbrt"); v->func = m_cbrt;
#endif
#endif
#ifdef HAVE_LOG
v = get("ln"); v->func = m_log;
#else
#ifdef log
v = get("ln"); v->func = m_log;
#endif
#endif
#ifdef HAVE_LOG10
v = get("log"); v->func = m_log10;
#else
#ifdef log10
v = get("log"); v->func = m_log10;
#endif
#endif
#ifdef HAVE_ASIN
v = get("asin"); v->func = m_asin;
#else
#ifdef asin
v = get("asin"); v->func = m_asin;
#endif
#endif
#ifdef HAVE_ACOS
v = get("acos"); v->func = m_acos;
#else
#ifdef acos
v = get("acos"); v->func = m_acos;
#endif
#endif
#ifdef HAVE_ATAN
v = get("atan"); v->func = m_atan;
#else
#ifdef atan
v = get("atan"); v->func = m_atan;
#endif
#endif
#ifdef M_PI
v = get("pi"); v->val = M_PI; v->set = 1;
#endif
#ifdef M_E
v = get("e"); v->val = M_E; v->set = 1;
#endif
#ifdef HAVE_SINH
v = get("sinh"); v->func = m_sinh;
#else
#ifdef sinh
v = get("sinh"); v->func = m_sinh;
#endif
#endif
#ifdef HAVE_COSH
v = get("cosh"); v->func = m_cosh;
#else
#ifdef cosh
v = get("cosh"); v->func = m_cosh;
#endif
#endif
#ifdef HAVE_TANH
v = get("tanh"); v->func = m_tanh;
#else
#ifdef tanh
v = get("tanh"); v->func = m_tanh;
#endif
#endif
#ifdef HAVE_ASINH
v = get("asinh"); v->func = m_asinh;
#else
#ifdef asinh
v = get("asinh"); v->func = m_asinh;
#endif
#endif
#ifdef HAVE_ACOSH
v = get("acosh"); v->func = m_acosh;
#else
#ifdef acosh
v = get("acosh"); v->func = m_acosh;
#endif
#endif
#ifdef HAVE_ATANH
v = get("atanh"); v->func = m_atanh;
#else
#ifdef atanh
v = get("atanh"); v->func = m_atanh;
#endif
#endif
#ifdef HAVE_FLOOR
v = get("floor"); v->func = m_floor;
#else
#ifdef floor
v = get("floor"); v->func = m_floor;
#endif
#endif
#ifdef HAVE_CEIL
v = get("ceil"); v->func = m_ceil;
#else
#ifdef ceil
v = get("ceil"); v->func = m_ceil;
#endif
#endif
#ifdef HAVE_FABS
v = get("abs"); v->func = m_fabs;
#else
#ifdef fabs
v = get("abs"); v->func = m_fabs;
#endif
#endif
#ifdef HAVE_ERF
v = get("erf"); v->func = m_erf;
#else
#ifdef erf
v = get("erf"); v->func = m_erf;
#endif
#endif
#ifdef HAVE_ERFC
v = get("erfc"); v->func = m_erfc;
#else
#ifdef erfc
v = get("erfc"); v->func = m_erfc;
#endif
#endif
#ifdef HAVE_J0
v = get("j0"); v->func = m_j0;
#else
#ifdef j0
v = get("j0"); v->func = m_j0;
#endif
#endif
#ifdef HAVE_J1
v = get("j1"); v->func = m_j1;
#else
#ifdef j1
v = get("j1"); v->func = m_j1;
#endif
#endif
#ifdef HAVE_Y0
v = get("y0"); v->func = m_y0;
#else
#ifdef y0
v = get("y0"); v->func = m_y0;
#endif
#endif
#ifdef HAVE_Y1
v = get("y1"); v->func = m_y1;
#else
#ifdef y1
v = get("y1"); v->func = m_y1;
#endif
#endif
v = get("int"); v->func = m_int;
}
v = get("top");
v->val = (double)(tbw->top->line + 1);
v->set = 1;
v = get("lines");
v->val = (double)(tbw->b->eof->line + 1);
v->set = 1;
v = get("line");
v->val = (double)(tbw->cursor->line + 1);
v->set = 1;
v = get("col");
v->val = (double)(tbw->cursor->col + 1);
v->set = 1;
v = get("byte");
v->val = (double)(tbw->cursor->byte + 1);
v->set = 1;
v = get("size");
v->val = (double)tbw->b->eof->byte;
v->set = 1;
v = get("height");
v->val = (double)tbw->h;
v->set = 1;
v = get("width");
v->val = (double)tbw->w;
v->set = 1;
v = get("char");
v->val = (c == NO_MORE_DATA ? -1.0 : c);
v->set = 1;
v = get("markv");
v->val = markv(1) ? 1.0 : 0.0;
v->set = 1;
v = get("rdonly");
v->val = tbw->b->rdonly;
v->set = 1;
v = get("arg");
v->val = current_arg;
v->set = 1;
v = get("argset");
v->val = current_arg_set;
v->set = 1;
v = get("no_windows");
v->val = countmain(bw->parent->t);
v->set = 1;
merr = 0;
v = get("is_shell");
v->val = tbw->b->pid;
v->set = 1;
merr = 0;
return eval(s, secure);
}
/* Main user interface */
B *mathhist = NULL;
int domath(W *w, int k, int secure)
{
BW *bw;
char *s;
WIND_BW(bw, w);
joe_set_signal(SIGFPE, fperr);
again:
s = ask(w, "=", &mathhist, "Math", utypebw, utf8_map, 0, 0, NULL);
if (s) {
char buf[128];
double result = calc(bw, s, secure);
if (merr) {
msgnw(bw->parent, merr);
return -1;
}
if (mode_hex)
#ifdef HAVE_LONG_LONG
joe_snprintf_1(buf, sizeof(buf), "0x%llX", (long long)result);
#else
joe_snprintf_1(buf, sizeof(buf), "0x%lX", (long)result);
#endif
else if (mode_eng)
joe_snprintf_1(buf, sizeof(buf), "%.16G", result);
else
joe_snprintf_1(buf, sizeof(buf), "%.16G", result);
if (bw->parent->watom->what != TYPETW || mode_ins) {
binsm(bw->cursor, sz(buf));
pfwrd(bw->cursor, zlen(buf));
bw->cursor->xcol = piscol(bw->cursor);
} else {
msgnw(bw->parent, buf);
}
mode_ins = 0;
goto again;
return 0;
} else {
return -1;
}
}
int umath(W *w, int k)
{
return domath(w, k, 0);
}
/* Secure version: no macros allowed */
int usmath(W *w, int k)
{
return domath(w, k, 1);
}
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.