Debian Codegraph @

common/core_helpers.cc

raw
1 /*****************************************************************************
2 * Free42 -- an HP-42S calculator simulator
3 * Copyright (C) 2004-2026 Thomas Okken
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License, version 2,
7 * as published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, see http://www.gnu.org/licenses/.
16 *****************************************************************************/
17
18 #include <stdlib.h>
19 #include <stdio.h>
20 #include <string.h>
21
22 #include "core_helpers.h"
23 #include "core_commands2.h"
24 #include "core_display.h"
25 #include "core_phloat.h"
26 #include "core_main.h"
27 #include "core_variables.h"
28 #include "shell.h"
29
30
31 int resolve_ind_arg(arg_struct *arg, char *buf, int *buflen) {
32 vartype *v;
33 switch (arg->type) {
34 case ARGTYPE_IND_NUM: {
35 vartype *regs = recall_var("REGS", 4);
36 if (regs == NULL)
37 return ERR_SIZE_ERROR;
38 if (regs->type != TYPE_REALMATRIX)
39 return ERR_INVALID_TYPE;
40 else {
41 vartype_realmatrix *rm = (vartype_realmatrix *) regs;
42 int4 size = rm->rows * rm->columns;
43 int4 num = arg->val.num;
44 if (num >= size)
45 return ERR_SIZE_ERROR;
46 if (rm->array->is_string[num] == 0) {
47 phloat x = rm->array->data[num];
48 if (x < 0)
49 x = -x;
50 if (x >= 2147483648.0)
51 arg->val.num = 2147483647;
52 else
53 arg->val.num = to_int4(x);
54 arg->type = ARGTYPE_NUM;
55 } else {
56 char *text;
57 int4 len;
58 get_matrix_string(rm, num, &text, &len);
59 if (len == 0)
60 return ERR_RESTRICTED_OPERATION;
61 if (buf == NULL) {
62 if (len > 7)
63 return ERR_NAME_TOO_LONG;
64 arg->length = len;
65 memcpy(arg->val.text, text, len);
66 } else {
67 if (len < *buflen)
68 *buflen = len;
69 memcpy(buf, text, *buflen);
70 }
71 arg->type = ARGTYPE_STR;
72 }
73 return ERR_NONE;
74 }
75 }
76 case ARGTYPE_IND_STK: {
77 int idx;
78 switch (arg->val.stk) {
79 case 'X': idx = 0; break;
80 case 'Y': idx = 1; break;
81 case 'Z': idx = 2; break;
82 case 'T': idx = 3; break;
83 case 'L': idx = -1; break;
84 }
85 if (idx == -1) {
86 v = lastx;
87 } else {
88 if (idx > sp)
89 return ERR_STACK_DEPTH_ERROR;
90 v = stack[sp - idx];
91 }
92 goto finish_resolve;
93 }
94 case ARGTYPE_IND_STR: {
95 v = recall_var(arg->val.text, arg->length);
96 if (v == NULL)
97 return ERR_NONEXISTENT;
98 finish_resolve:
99 if (v->type == TYPE_REAL) {
100 phloat x = ((vartype_real *) v)->x;
101 if (x < 0)
102 x = -x;
103 if (x >= 2147483648.0)
104 arg->val.num = 2147483647;
105 else
106 arg->val.num = to_int4(x);
107 arg->type = ARGTYPE_NUM;
108 return ERR_NONE;
109 } else if (v->type == TYPE_STRING) {
110 vartype_string *s = (vartype_string *) v;
111 if (s->length == 0)
112 return ERR_RESTRICTED_OPERATION;
113 if (buf == NULL) {
114 if (s->length > 7)
115 return ERR_NAME_TOO_LONG;
116 arg->length = s->length;
117 memcpy(arg->val.text, s->txt(), s->length);
118 } else {
119 if (s->length < *buflen)
120 *buflen = s->length;
121 memcpy(buf, s->txt(), *buflen);
122 }
123 arg->type = ARGTYPE_STR;
124 return ERR_NONE;
125 } else
126 return ERR_INVALID_TYPE;
127 }
128 default:
129 /* Should not happen; this function should only
130 * be called to resolve indirect arguments.
131 */
132 return ERR_INTERNAL_ERROR;
133 }
134 }
135
136 int arg_to_num(arg_struct *arg, int4 *num) {
137 if (arg->type == ARGTYPE_IND_NUM
138 || arg->type == ARGTYPE_IND_STK
139 || arg->type == ARGTYPE_IND_STR) {
140 int err = resolve_ind_arg(arg);
141 if (err != ERR_NONE)
142 return err;
143 }
144 if (arg->type == ARGTYPE_NUM) {
145 *num = arg->val.num;
146 return ERR_NONE;
147 } else if (arg->type == ARGTYPE_STR)
148 return ERR_ALPHA_DATA_IS_INVALID;
149 else
150 return ERR_INVALID_TYPE;
151 }
152
153 bool dim_to_int4(vartype *dim, int4 *res) {
154 phloat d = ((vartype_real *) dim)->x;
155 if (d <= -2147483648.0 || d >= 2147483648.0)
156 return false;
157 int4 dd = to_int4(d);
158 if (dd == 0)
159 return false;
160 if (dd < 0)
161 dd = -dd;
162 *res = dd - 1;
163 return true;
164 }
165
166 int recall_result_silently(vartype *v) {
167 if (flags.f.stack_lift_disable) {
168 if (sp == -1)
169 sp = 0;
170 else
171 free_vartype(stack[sp]);
172 } else if (flags.f.big_stack) {
173 if (!ensure_stack_capacity(1)) {
174 free_vartype(v);
175 return ERR_INSUFFICIENT_MEMORY;
176 }
177 sp++;
178 } else {
179 free_vartype(stack[REG_T]);
180 stack[REG_T] = stack[REG_Z];
181 stack[REG_Z] = stack[REG_Y];
182 stack[REG_Y] = stack[REG_X];
183 }
184 stack[sp] = v;
185 return ERR_NONE;
186 }
187
188 int recall_result(vartype *v) {
189 int res = recall_result_silently(v);
190 if (res == ERR_NONE)
191 print_trace();
192 return res;
193 }
194
195 int recall_two_results(vartype *x, vartype *y) {
196 if (flags.f.big_stack) {
197 bool sld = flags.f.stack_lift_disable && sp != -1;
198 int off = sld ? 1 : 2;
199 if (!ensure_stack_capacity(off)) {
200 free_vartype(x);
201 free_vartype(y);
202 return ERR_INSUFFICIENT_MEMORY;
203 }
204 if (sld)
205 free_vartype(stack[sp]);
206 sp += off;
207 } else {
208 if (flags.f.stack_lift_disable) {
209 free_vartype(stack[REG_T]);
210 free_vartype(stack[REG_X]);
211 stack[REG_T] = stack[REG_Z];
212 stack[REG_Z] = stack[REG_Y];
213 } else {
214 free_vartype(stack[REG_T]);
215 free_vartype(stack[REG_Z]);
216 stack[REG_T] = stack[REG_Y];
217 stack[REG_Z] = stack[REG_X];
218 }
219 }
220 stack[sp - 1] = y;
221 stack[sp] = x;
222 print_trace();
223 return ERR_NONE;
224 }
225
226 void unary_result(vartype *x) {
227 free_vartype(lastx);
228 lastx = stack[sp];
229 stack[sp] = x;
230 print_trace();
231 }
232
233 int unary_two_results(vartype *x, vartype *y) {
234 if (flags.f.big_stack) {
235 if (!ensure_stack_capacity(1)) {
236 free_vartype(x);
237 free_vartype(y);
238 return ERR_INSUFFICIENT_MEMORY;
239 }
240 free_vartype(lastx);
241 lastx = stack[sp];
242 sp++;
243 } else {
244 free_vartype(stack[REG_T]);
245 stack[REG_T] = stack[REG_Z];
246 stack[REG_Z] = stack[REG_Y];
247 free_vartype(lastx);
248 lastx = stack[REG_X];
249 }
250 stack[sp - 1] = y;
251 stack[sp] = x;
252 print_trace();
253 return ERR_NONE;
254 }
255
256 int unary_no_result() {
257 if (!flags.f.big_stack) {
258 vartype *t = dup_vartype(stack[REG_T]);
259 if (t == NULL)
260 return ERR_INSUFFICIENT_MEMORY;
261 free_vartype(lastx);
262 lastx = stack[REG_X];
263 stack[REG_X] = stack[REG_Y];
264 stack[REG_Y] = stack[REG_Z];
265 stack[REG_Z] = t;
266 } else {
267 free_vartype(lastx);
268 lastx = stack[sp];
269 sp--;
270 }
271 return ERR_NONE;
272 }
273
274 int binary_result(vartype *x) {
275 vartype *t;
276 if (!flags.f.big_stack) {
277 t = dup_vartype(stack[REG_T]);
278 if (t == NULL) {
279 free_vartype(x);
280 return ERR_INSUFFICIENT_MEMORY;
281 }
282 }
283 free_vartype(lastx);
284 lastx = stack[sp];
285 free_vartype(stack[sp - 1]);
286 if (flags.f.big_stack) {
287 sp--;
288 } else {
289 stack[REG_Y] = stack[REG_Z];
290 stack[REG_Z] = t;
291 }
292 stack[sp] = x;
293 print_trace();
294 return ERR_NONE;
295 }
296
297 void binary_two_results(vartype *x, vartype *y) {
298 if (flags.f.big_stack) {
299 while (sp < 1)
300 stack[++sp] = NULL;
301 }
302 if (stack[sp] != NULL) {
303 free_vartype(lastx);
304 lastx = stack[sp];
305 }
306 free_vartype(stack[sp - 1]);
307 stack[sp - 1] = y;
308 stack[sp] = x;
309 print_trace();
310 }
311
312 int ternary_result(vartype *x) {
313 if (flags.f.big_stack) {
314 free_vartype(lastx);
315 lastx = stack[sp];
316 free_vartype(stack[sp - 1]);
317 free_vartype(stack[sp - 2]);
318 sp -= 2;
319 } else {
320 vartype *tt = dup_vartype(stack[REG_T]);
321 if (tt == NULL) {
322 free_vartype(x);
323 return ERR_INSUFFICIENT_MEMORY;
324 }
325 vartype *ttt = dup_vartype(stack[REG_T]);
326 if (ttt == NULL) {
327 free_vartype(x);
328 free_vartype(tt);
329 return ERR_INSUFFICIENT_MEMORY;
330 }
331 free_vartype(lastx);
332 lastx = stack[REG_X];
333 free_vartype(stack[REG_Y]);
334 free_vartype(stack[REG_Z]);
335 stack[REG_Y] = tt;
336 stack[REG_Z] = ttt;
337 }
338 stack[sp] = x;
339 print_trace();
340 return ERR_NONE;
341 }
342
343 bool ensure_stack_capacity(int n) {
344 if (!flags.f.big_stack)
345 return true;
346 if (stack_capacity > sp + n)
347 return true;
348 int new_capacity = stack_capacity + n + 16;
349 vartype **new_stack = (vartype **) realloc(stack, new_capacity * sizeof(vartype *));
350 if (new_stack == NULL)
351 return false;
352 stack = new_stack;
353 stack_capacity = new_capacity;
354 return true;
355 }
356
357 void shrink_stack() {
358 int new_capacity = sp + 1;
359 if (new_capacity < 4)
360 new_capacity = 4;
361 vartype **new_stack = (vartype **) realloc(stack, new_capacity * sizeof(vartype *));
362 if (new_stack != NULL) {
363 stack = new_stack;
364 stack_capacity = new_capacity;
365 }
366 }
367
368 phloat rad_to_angle(phloat x) {
369 if (flags.f.rad)
370 return x;
371 else if (flags.f.grad)
372 return x * (200 / PI);
373 else
374 return x * (180 / PI);
375 }
376
377 phloat rad_to_deg(phloat x) {
378 return x * (180 / PI);
379 }
380
381 phloat deg_to_rad(phloat x) {
382 return x / (180 / PI);
383 }
384
385 void append_alpha_char(char c) {
386 if (reg_alpha_length == 44) {
387 int i;
388 for (i = 0; i < 43; i++)
389 reg_alpha[i] = reg_alpha[i + 1];
390 reg_alpha[43] = c;
391 } else
392 reg_alpha[reg_alpha_length++] = c;
393 }
394
395 void append_alpha_string(const char *buf, int buflen, int reverse) {
396 int needed, i;
397 if (buflen > 44) {
398 if (!reverse)
399 buf += buflen - 44;
400 buflen = 44;
401 }
402 needed = reg_alpha_length + buflen - 44;
403 if (needed > 0) {
404 for (i = 0; i < reg_alpha_length - needed; i++)
405 reg_alpha[i] = reg_alpha[i + needed];
406 reg_alpha_length -= needed;
407 }
408 if (reverse)
409 for (i = buflen - 1; i >= 0; i--)
410 reg_alpha[reg_alpha_length++] = buf[i];
411 else
412 for (i = 0; i < buflen; i++)
413 reg_alpha[reg_alpha_length++] = buf[i];
414 }
415
416 void string_copy(char *dst, int *dstlen, const char *src, int srclen) {
417 int i;
418 *dstlen = srclen;
419 for (i = 0; i < srclen; i++)
420 dst[i] = src[i];
421 }
422
423 bool string_equals(const char *s1, int s1len, const char *s2, int s2len) {
424 int i;
425 if (s1len != s2len)
426 return false;
427 for (i = 0; i < s1len; i++)
428 if (s1[i] != s2[i])
429 return false;
430 return true;
431 }
432
433 int string_pos(const char *ntext, int nlen, const vartype *hs, int startpos) {
434 int pos = -1;
435 if (hs->type == TYPE_REAL) {
436 phloat x = ((const vartype_real *) hs)->x;
437 char c;
438 int i;
439 if (x < 0)
440 x = -x;
441 if (x >= 256)
442 return -2;
443 c = to_char(x);
444 for (i = startpos; i < nlen; i++)
445 if (ntext[i] == c) {
446 pos = i;
447 break;
448 }
449 } else {
450 const vartype_string *s = (const vartype_string *) hs;
451 if (s->length != 0) {
452 int i, j;
453 const char *text = s->txt();
454 for (i = startpos; i < nlen - s->length + 1; i++) {
455 for (j = 0; j < s->length; j++)
456 if (ntext[i + j] != text[j])
457 goto notfound;
458 pos = i;
459 break;
460 notfound:;
461 }
462 }
463 }
464 return pos;
465 }
466
467 bool vartype_equals(const vartype *v1, const vartype *v2) {
468 if (v1->type != v2->type)
469 return false;
470 switch (v1->type) {
471 case TYPE_REAL: {
472 const vartype_real *x = (const vartype_real *) v1;
473 const vartype_real *y = (const vartype_real *) v2;
474 return x->x == y->x;
475 }
476 case TYPE_COMPLEX: {
477 const vartype_complex *x = (const vartype_complex *) v1;
478 const vartype_complex *y = (const vartype_complex *) v2;
479 return x->re == y->re && x->im == y->im;
480 }
481 case TYPE_REALMATRIX: {
482 const vartype_realmatrix *x = (const vartype_realmatrix *) v1;
483 const vartype_realmatrix *y = (const vartype_realmatrix *) v2;
484 if (x->array == y->array)
485 return true;
486 int4 sz, i;
487 if (x->rows != y->rows || x->columns != y->columns)
488 return false;
489 sz = x->rows * x->columns;
490 for (i = 0; i < sz; i++) {
491 int xstr = x->array->is_string[i];
492 int ystr = y->array->is_string[i];
493 if (xstr != ystr)
494 return false;
495 if (xstr == 0) {
496 if (x->array->data[i] != y->array->data[i])
497 return false;
498 } else {
499 int len1, len2;
500 const char *text1, *text2;
501 get_matrix_string(x, i, &text1, &len1);
502 get_matrix_string(y, i, &text2, &len2);
503 if (!string_equals(text1, len1, text2, len2))
504 return false;
505 }
506 }
507 return true;
508 }
509 case TYPE_COMPLEXMATRIX: {
510 const vartype_complexmatrix *x = (const vartype_complexmatrix *) v1;
511 const vartype_complexmatrix *y = (const vartype_complexmatrix *) v2;
512 if (x->array == y->array)
513 return true;
514 int4 sz, i;
515 if (x->rows != y->rows || x->columns != y->columns)
516 return false;
517 sz = 2 * x->rows * x->columns;
518 for (i = 0; i < sz; i++)
519 if (x->array->data[i] != y->array->data[i])
520 return false;
521 return true;
522 }
523 case TYPE_STRING: {
524 const vartype_string *x = (const vartype_string *) v1;
525 const vartype_string *y = (const vartype_string *) v2;
526 return string_equals(x->txt(), x->length, y->txt(), y->length);
527 }
528 case TYPE_LIST: {
529 const vartype_list *x = (const vartype_list *) v1;
530 const vartype_list *y = (const vartype_list *) v2;
531 if (x->array == y->array)
532 return true;
533 if (x->size != y->size)
534 return false;
535 int4 sz = x->size;
536 const vartype **data1 = (const vartype **) x->array->data;
537 const vartype **data2 = (const vartype **) y->array->data;
538 for (int4 i = 0; i < sz; i++)
539 if (!vartype_equals(data1[i], data2[i]))
540 return false;
541 return true;
542 }
543 default:
544 /* Looks like someone added a type that we're not handling yet! */
545 return false;
546 }
547 }
548
549 int anum(const char *text, int len, phloat *res) {
550 char buf[50];
551 int src_pos = 0;
552 retry:
553 bool have_mant = false;
554 bool have_mant_digits = false;
555 bool neg_mant = false;
556 bool have_radix = false;
557 bool have_exp = false;
558 bool neg_exp = false;
559 int exp_pos = 0;
560 int buf_pos = 0;
561 buf[buf_pos++] = '+';
562 for (; src_pos < len; src_pos++) {
563 char c = text[src_pos];
564 if (!flags.f.decimal_point)
565 if (c == '.')
566 c = ',';
567 else if (c == ',')
568 c = '.';
569 if (c == '+' || flags.f.thousands_separators && c == ',')
570 continue;
571 if (!have_mant) {
572 if (c == '-') {
573 neg_mant = !neg_mant;
574 } else if (c >= '0' && c <= '9') {
575 buf[buf_pos++] = c;
576 have_mant = true;
577 have_mant_digits = true;
578 } else if (c == '.') {
579 buf[buf_pos++] = '0';
580 buf[buf_pos++] = '.';
581 have_mant = true;
582 have_radix = true;
583 } else {
584 neg_mant = false;
585 }
586 } else if (!have_exp) {
587 if (c == '-') {
588 neg_mant = !neg_mant;
589 } else if (c >= '0' && c <= '9') {
590 buf[buf_pos++] = c;
591 have_mant_digits = true;
592 } else if (c == '.') {
593 if (!have_radix) {
594 buf[buf_pos++] = c;
595 have_radix = true;
596 }
597 } else if (c == 'E' || c == 'e' || c == 24) {
598 if (!have_mant_digits)
599 goto retry;
600 buf[buf_pos++] = 'e';
601 exp_pos = buf_pos;
602 buf[buf_pos++] = '+';
603 have_exp = true;
604 } else if (c == '.') {
605 /* ignore */
606 } else {
607 if (!have_mant_digits)
608 goto retry;
609 break;
610 }
611 } else {
612 if (c == '-') {
613 neg_exp = !neg_exp;
614 } else if (c >= '0' && c <= '9') {
615 buf[buf_pos++] = c;
616 } else if (c == '.' || c == 'E' || c == 'e' || c == 24) {
617 /* ignore */
618 } else {
619 break;
620 }
621 }
622 }
623 if (!have_mant_digits)
624 return false;
625 if (neg_mant)
626 buf[0] = '-';
627 if (have_exp && buf_pos == exp_pos + 1) {
628 buf_pos -= 2;
629 have_exp = false;
630 }
631 if (have_exp && neg_exp)
632 buf[exp_pos] = '-';
633 buf[buf_pos++] = 0;
634 phloat p;
635 #ifdef BCD_MATH
636 BID_UINT128 b;
637 bid128_from_string(&b, buf);
638 p = b;
639 #else
640 sscanf(buf, "%le", &p);
641 #endif
642 if (p_isnan(p))
643 return false;
644 if (p_isinf(p))
645 p = p > 0 ? POS_HUGE_PHLOAT : NEG_HUGE_PHLOAT;
646 *res = p;
647 return true;
648 }
649
650 void fix_thousands_separators(char *buf, int *bufptr) {
651 /* First, remove the old separators... */
652 int i, j = 0;
653 char dot = flags.f.decimal_point ? '.' : ',';
654 char sep = flags.f.decimal_point ? ',' : '.';
655 int intdigits = 0;
656 int counting_intdigits = 1;
657 int nsep;
658 for (i = 0; i < *bufptr; i++) {
659 char c = buf[i];
660 if (c != sep)
661 buf[j++] = c;
662 if (c == dot || c == 24)
663 counting_intdigits = 0;
664 else if (counting_intdigits && c >= '0' && c <= '9')
665 intdigits++;
666 }
667 /* Now, put 'em back... */
668 if (!flags.f.thousands_separators) {
669 *bufptr = j;
670 return;
671 }
672 nsep = (intdigits - 1) / 3;
673 if (nsep == 0) {
674 *bufptr = j;
675 return;
676 }
677 for (i = j - 1; i >= 0; i--)
678 buf[i + nsep] = buf[i];
679 j += nsep;
680 for (i = 0; i < j; i++) {
681 char c = buf[i + nsep];
682 buf[i] = c;
683 if (nsep > 0 && c >= '0' && c <= '9') {
684 if (intdigits % 3 == 1) {
685 buf[++i] = sep;
686 nsep--;
687 }
688 intdigits--;
689 }
690 }
691 *bufptr = j;
692 }
693
694 void fix_base_separators(char *buf, int *bufptr) {
695 /* First, remove the old separators... */
696 int digits = 0;
697 for (int i = 0; i < *bufptr; i++) {
698 char c = buf[i];
699 if (c != ' ')
700 buf[digits++] = c;
701 }
702 /* Now, put 'em back... */
703 *bufptr = digits;
704 switch (get_base()) {
705 case 2: if (!mode_bin_sep) return; else break;
706 case 8: if (!mode_oct_sep) return; else break;
707 case 10: if (!mode_dec_sep) return; else break;
708 case 16: if (!mode_hex_sep) return; else break;
709 }
710 int nsep = (digits - 1) / 4;
711 if (nsep == 0)
712 return;
713 *bufptr += nsep;
714 for (int i = digits - 1; i >= 0; i--) {
715 buf[i + nsep] = buf[i];
716 if (((digits - i) & 3) == 0 && i != 0)
717 buf[i + (--nsep)] = ' ';
718 }
719 }
720
721 #if (!defined(ANDROID) && !defined(IPHONE))
722 static bool always_on = false;
723 bool shell_always_on(int ao) {
724 bool ret = always_on;
725 if (ao != -1)
726 always_on = ao == 1;
727 return ret;
728 }
729 #endif
730
731 int virtual_flag_handler(int flagop, int flagnum) {
732 /* NOTE: the determination which flag numbers are handled by this
733 * function is made by docmd_sf() etc.; they do this based on a constant
734 * string 'virtual_flags' defined in core_globals.cc.
735 */
736 switch (flagnum) {
737 case 27: /* custom_menu */ {
738 int its_on = mode_plainmenu == MENU_CUSTOM1
739 || mode_plainmenu == MENU_CUSTOM2
740 || mode_plainmenu == MENU_CUSTOM3;
741 switch (flagop) {
742 case FLAGOP_SF:
743 if (!its_on)
744 set_menu(MENULEVEL_PLAIN, MENU_CUSTOM1);
745 return ERR_NONE;
746 case FLAGOP_CF:
747 if (its_on)
748 set_menu(MENULEVEL_PLAIN, MENU_NONE);
749 return ERR_NONE;
750 case FLAGOP_FS_T:
751 return its_on ? ERR_YES : ERR_NO;
752 case FLAGOP_FC_T:
753 return its_on ? ERR_NO : ERR_YES;
754 case FLAGOP_FSC_T:
755 if (its_on)
756 set_menu(MENULEVEL_PLAIN, MENU_NONE);
757 return its_on ? ERR_YES : ERR_NO;
758 case FLAGOP_FCC_T:
759 if (its_on)
760 set_menu(MENULEVEL_PLAIN, MENU_NONE);
761 return its_on ? ERR_NO : ERR_YES;
762 default:
763 return ERR_INTERNAL_ERROR;
764 }
765 }
766 case 44: /* continuous on */ {
767 switch (flagop) {
768 case FLAGOP_FS_T:
769 return shell_always_on(-1) ? ERR_YES : ERR_NO;
770 case FLAGOP_FC_T:
771 return shell_always_on(-1) ? ERR_NO : ERR_YES;
772 default:
773 return ERR_INTERNAL_ERROR;
774 }
775 }
776 case 45: /* solving */ {
777 switch (flagop) {
778 case FLAGOP_FS_T:
779 return solve_active() ? ERR_YES : ERR_NO;
780 case FLAGOP_FC_T:
781 return solve_active() ? ERR_NO : ERR_YES;
782 default:
783 return ERR_INTERNAL_ERROR;
784 }
785 }
786 case 46: /* integrating */ {
787 switch (flagop) {
788 case FLAGOP_FS_T:
789 return integ_active() ? ERR_YES : ERR_NO;
790 case FLAGOP_FC_T:
791 return integ_active() ? ERR_NO : ERR_YES;
792 default:
793 return ERR_INTERNAL_ERROR;
794 }
795 }
796 case 47: /* variable_menu */ {
797 int its_on = mode_appmenu == MENU_VARMENU
798 && (varmenu_role == 0 || varmenu_role == 3);
799 switch (flagop) {
800 case FLAGOP_FS_T:
801 return its_on ? ERR_YES : ERR_NO;
802 case FLAGOP_FC_T:
803 return its_on ? ERR_NO : ERR_YES;
804 default:
805 return ERR_INTERNAL_ERROR;
806 }
807 }
808 case 48: /* alpha_mode */ {
809 bool alpha = alpha_active();
810 switch (flagop) {
811 case FLAGOP_FS_T:
812 return alpha ? ERR_YES : ERR_NO;
813 case FLAGOP_FC_T:
814 return alpha ? ERR_NO : ERR_YES;
815 default:
816 return ERR_INTERNAL_ERROR;
817 }
818 }
819 case 49: /* low_battery */ {
820 bool lowbat = shell_low_battery();
821 switch (flagop) {
822 case FLAGOP_FS_T:
823 return lowbat ? ERR_YES : ERR_NO;
824 case FLAGOP_FC_T:
825 return lowbat ? ERR_NO : ERR_YES;
826 default:
827 return ERR_INTERNAL_ERROR;
828 }
829 }
830 case 53: /* input */ {
831 switch (flagop) {
832 case FLAGOP_FS_T:
833 return input_length > 0 ? ERR_YES : ERR_NO;
834 case FLAGOP_FC_T:
835 return input_length > 0 ? ERR_NO : ERR_YES;
836 default:
837 return ERR_INTERNAL_ERROR;
838 }
839 }
840 case 65: /* matrix_editor */ {
841 int its_on = matedit_mode == 2 || matedit_mode == 3;
842 switch (flagop) {
843 case FLAGOP_FS_T:
844 return its_on ? ERR_YES : ERR_NO;
845 case FLAGOP_FC_T:
846 return its_on ? ERR_NO : ERR_YES;
847 default:
848 return ERR_INTERNAL_ERROR;
849 }
850 }
851 case 75: /* programmable_menu */ {
852 int its_on = mode_plainmenu == MENU_PROGRAMMABLE;
853 switch (flagop) {
854 case FLAGOP_FS_T:
855 return its_on ? ERR_YES : ERR_NO;
856 case FLAGOP_FC_T:
857 return its_on ? ERR_NO : ERR_YES;
858 default:
859 return ERR_INTERNAL_ERROR;
860 }
861 }
862 default:
863 return ERR_INTERNAL_ERROR;
864 }
865 }
866
867 int get_base() {
868 if (flags.f.prgm_mode)
869 return 10;
870 int base = 0;
871 if (flags.f.base_bit0) base += 1;
872 if (flags.f.base_bit1) base += 2;
873 if (flags.f.base_bit2) base += 4;
874 if (flags.f.base_bit3) base += 8;
875 if (base == 1 || base == 7 || base == 15)
876 return base + 1;
877 else
878 return 10;
879 }
880
881 void set_base(int base, bool a_thru_f) {
882 int oldbase = 0;
883 if (flags.f.base_bit0) oldbase += 1;
884 if (flags.f.base_bit1) oldbase += 2;
885 if (flags.f.base_bit2) oldbase += 4;
886 if (flags.f.base_bit3) oldbase += 8;
887
888 if (base == 2 || base == 8 || base == 16)
889 base--;
890 else
891 base = 0;
892 flags.f.base_bit0 = (base & 1) != 0;
893 flags.f.base_bit1 = (base & 2) != 0;
894 flags.f.base_bit2 = (base & 4) != 0;
895 flags.f.base_bit3 = (base & 8) != 0;
896 if (!a_thru_f && mode_appmenu == MENU_BASE_A_THRU_F)
897 set_menu(MENULEVEL_APP, MENU_BASE1);
898
899 if (base != oldbase)
900 print_trace();
901 }
902
903 int get_base_param(const vartype *v, int8 *n) {
904 phloat x = ((vartype_real *) v)->x;
905 return phloat2base(x, n) ? ERR_NONE : ERR_INVALID_DATA;
906 }
907
908 int base_range_check(int8 *n, bool force_wrap) {
909 int wsize = effective_wsize();
910 if (force_wrap || flags.f.base_wrap) {
911 if (flags.f.base_signed) {
912 if ((*n & (1LL << (wsize - 1))) != 0)
913 *n |= -1LL << (wsize - 1);
914 else
915 *n &= (1LL << (wsize - 1)) - 1;
916 } else {
917 if (wsize < 64)
918 *n &= (1ULL << wsize) - 1;
919 }
920 } else if (flags.f.base_signed) {
921 if (wsize == 64)
922 return ERR_NONE;
923 int8 high = 1LL << (wsize - 1);
924 int8 low = -high;
925 high--;
926 if (*n < low) {
927 if (flags.f.range_error_ignore)
928 *n = low;
929 else
930 return ERR_OUT_OF_RANGE;
931 } else if (*n > high) {
932 if (flags.f.range_error_ignore)
933 *n = high;
934 else
935 return ERR_OUT_OF_RANGE;
936 }
937 } else {
938 uint8 *un = (uint8 *) n;
939 uint8 high = wsize == 64 ? ~0ULL : (1ULL << wsize) - 1;
940 if (*un > high) {
941 if (flags.f.range_error_ignore)
942 *un = high;
943 else
944 return ERR_OUT_OF_RANGE;
945 }
946 }
947 return ERR_NONE;
948 }
949
950 int effective_wsize() {
951 #ifdef BCD_MATH
952 return mode_wsize;
953 #else
954 return mode_wsize > 53 ? 53 : mode_wsize;
955 #endif
956 }
957
958 phloat base2phloat(int8 n) {
959 if (flags.f.base_signed)
960 return phloat(n);
961 else
962 return phloat((uint8) n);
963 }
964
965 bool phloat2base(phloat p, int8 *res) {
966 int wsize = effective_wsize();
967 if (flags.f.base_wrap) {
968 phloat ip = p < 0 ? -floor(-p) : floor(p);
969 phloat d = pow(phloat(2), wsize);
970 phloat r = fmod(ip, d);
971 if (r < 0)
972 r += d;
973 int8 n = (int8) to_uint8(r);
974 if (flags.f.base_signed) {
975 int8 m = 1LL << (wsize - 1);
976 if ((n & m) != 0)
977 n |= -1LL << (wsize - 1);
978 else
979 n &= (1LL << (wsize - 1)) - 1;
980 } else {
981 if (wsize < 64)
982 n &= (1ULL << wsize) - 1;
983 }
984 *res = n;
985 } else if (flags.f.base_signed) {
986 phloat high = pow(phloat(2), wsize - 1);
987 phloat low = -high;
988 high--;
989 if (p > high || p < low)
990 return false;
991 int8 t = to_int8(p);
992 if ((t & (1LL << (wsize - 1))) != 0)
993 t |= -1LL << (wsize - 1);
994 else
995 t &= (1LL << (wsize - 1)) - 1;
996 *res = t;
997 } else {
998 if (p < 0)
999 return false;
1000 phloat high = pow(phloat(2), wsize) - 1;
1001 if (p > high)
1002 return false;
1003 *res = (int8) to_uint8(p);
1004 }
1005 return true;
1006 }
1007
1008 void print_text(const char *text, int length, bool left_justified) {
1009 /* TODO: check preferences so we don't waste any time generating
1010 * print-outs that aren't going to be accepted by the shell anyway
1011 */
1012 char buf[24];
1013 int width = flags.f.double_wide_print ? 12 : 24;
1014 int bufptr = 0, i;
1015 char bitmap[162];
1016
1017 /* Handle left/right justification and lowercase printing */
1018 if (length > width)
1019 length = width;
1020 if (!left_justified)
1021 for (i = length; i < width; i++)
1022 buf[bufptr++] = ' ';
1023 for (i = 0; i < length; i++) {
1024 char c = text[i];
1025 if (flags.f.lowercase_print && (c & 127) >= 'A' && (c & 127) <= 'Z')
1026 c += 32;
1027 else if (c == 10)
1028 c = (char) 138;
1029 buf[bufptr++] = c;
1030 }
1031
1032 /* Do bit-mapped printing */
1033 for (i = 0; i < 162; i++)
1034 bitmap[i] = 0;
1035 for (i = 0; i < bufptr; i++) {
1036 int j;
1037 const unsigned char *charbits = get_char(buf[i]);
1038 for (j = 0; j < 5; j++) {
1039 int x1 = i * 6 + j;
1040 int x2 = x1 + 1;
1041 int x, y;
1042 if (flags.f.double_wide_print) {
1043 x1 <<= 1;
1044 x2 <<= 1;
1045 }
1046 for (x = x1; x < x2; x++)
1047 for (y = 0; y < 8; y++)
1048 if ((charbits[j] & (1 << y)) != 0)
1049 bitmap[y * 18 + (x >> 3)] |= 1 << (x & 7);
1050 }
1051 }
1052
1053 /* Handle text-mode double-width printing by inserting spaces and
1054 * underscores; do text-mode printing */
1055 if (flags.f.double_wide_print) {
1056 for (i = bufptr - 1; i >= 0; i--) {
1057 char c = buf[i];
1058 buf[2 * i] = c;
1059 buf[2 * i + 1] = c == ' ' ? ' ' : '_';
1060 }
1061 bufptr *= 2;
1062 }
1063
1064 shell_print(buf, bufptr, bitmap, 18, 0, 0, 143, 9);
1065 }
1066
1067 void print_lines(const char *text, int length, bool left_justified) {
1068 int line_start = 0;
1069 int width = flags.f.double_wide_print ? 12 : 24;
1070 while (line_start + width < length) {
1071 print_text(text + line_start, width, left_justified);
1072 line_start += width;
1073 }
1074 print_text(text + line_start, length - line_start, length > width ? true : left_justified);
1075 }
1076
1077 void print_right(const char *left, int leftlen, const char *right, int rightlen) {
1078 char buf[24];
1079 int len;
1080 int width = flags.f.double_wide_print ? 12 : 24;
1081 int i, pad;
1082
1083 if (leftlen + rightlen + 1 <= width) {
1084 string_copy(buf, &len, left, leftlen);
1085 buf[len++] = ' ';
1086 pad = width - len - rightlen;
1087 if (pad > 6 - rightlen)
1088 pad = 6 - rightlen;
1089 if (pad < 0)
1090 pad = 0;
1091 for (i = 0; i < pad; i++)
1092 buf[len++] = ' ';
1093 for (i = 0; i < rightlen; i++)
1094 buf[len++] = right[i];
1095 print_text(buf, len, false);
1096 } else {
1097 int line_start = 0;
1098 while (leftlen - line_start >= width) {
1099 print_text(left + line_start, width, true);
1100 line_start += width;
1101 }
1102 if (leftlen - line_start + rightlen + 1 > width) {
1103 print_text(left + line_start, leftlen - line_start, true);
1104 print_text(right, rightlen, false);
1105 } else {
1106 string_copy(buf, &len, left + line_start, leftlen - line_start);
1107 pad = width - len - rightlen;
1108 for (i = 0; i < pad; i++)
1109 buf[len++] = ' ';
1110 for (i = 0; i < rightlen; i++)
1111 buf[len++] = right[i];
1112 print_text(buf, len, true);
1113 }
1114 }
1115 }
1116
1117 void print_wide(const char *left, int leftlen, const char *right, int rightlen) {
1118 int width = flags.f.double_wide_print ? 12 : 24;
1119 char buf[24];
1120 int bufptr = 0, i;
1121
1122 if (leftlen + rightlen <= width) {
1123 for (i = 0; i < leftlen; i++)
1124 buf[bufptr++] = left[i];
1125 for (i = width - leftlen - rightlen; i > 0; i--)
1126 buf[bufptr++] = ' ';
1127 for (i = 0; i < rightlen; i++)
1128 buf[bufptr++] = right[i];
1129 print_text(buf, bufptr, true);
1130 } else {
1131 for (i = 0; i < leftlen; i++) {
1132 buf[bufptr++] = left[i];
1133 if (bufptr == width) {
1134 print_text(buf, width, true);
1135 bufptr = 0;
1136 }
1137 }
1138 for (i = 0; i < rightlen; i++) {
1139 buf[bufptr++] = right[i];
1140 if (bufptr == width) {
1141 print_text(buf, width, true);
1142 bufptr = 0;
1143 }
1144 }
1145 if (bufptr > 0)
1146 print_text(buf, bufptr, true);
1147 }
1148 }
1149
1150 static void print_command_2(const char *text, int len) {
1151 set_annunciators(-1, -1, 1, -1, -1, -1);
1152
1153 if (deferred_print) {
1154 /* If the display mode is FIX n, and the user has not entered
1155 * an exponent, pad the fractional part to n, in order to get
1156 * decimal points to line up.
1157 */
1158 if (flags.f.fix_or_all && !flags.f.eng_or_all) {
1159 int i, n, dot_found = 0, ip_digits = 0, fp_digits = 0;
1160 char dot = flags.f.decimal_point ? '.' : ',';
1161 for (i = 0; i < cmdline_length; i++) {
1162 char c = cmdline[i];
1163 if (c == 24)
1164 goto never_mind;
1165 if (c == dot)
1166 dot_found = 1;
1167 if (c >= '0' && c <= '9') {
1168 if (dot_found)
1169 fp_digits++;
1170 else
1171 ip_digits++;
1172 }
1173 }
1174 n = 0;
1175 if (flags.f.digits_bit3) n += 8;
1176 if (flags.f.digits_bit2) n += 4;
1177 if (flags.f.digits_bit1) n += 2;
1178 if (flags.f.digits_bit0) n += 1;
1179 if (!dot_found)
1180 cmdline[cmdline_length++] = dot;
1181 while (fp_digits < n && ip_digits + fp_digits < 12) {
1182 cmdline[cmdline_length++] = '0';
1183 fp_digits++;
1184 }
1185 never_mind:;
1186 }
1187 print_right(cmdline, cmdline_length, text, len);
1188 } else {
1189 /* Normally we print commands right-justified, but if they don't fit
1190 * on one line, we print them left-justified, because having the excess
1191 * go near the right margin looks weird and confusing.
1192 */
1193 bool left = len > (flags.f.double_wide_print ? 12 : 24);
1194 print_lines(text, len, left);
1195 }
1196
1197 deferred_print = 0;
1198 set_annunciators(-1, -1, 0, -1, -1, -1);
1199 }
1200
1201 void print_command(int cmd, const arg_struct *arg) {
1202 char buf[100];
1203 int bufptr = 0;
1204
1205 if (cmd == CMD_NULL && !deferred_print)
1206 return;
1207
1208 if (cmd != CMD_NULL)
1209 bufptr += command2buf(buf, 100, cmd, arg);
1210
1211 print_command_2(buf, bufptr);
1212 }
1213
1214 void print_menu_trace(const char *name, int len) {
1215 if (!flags.f.prgm_mode
1216 && (flags.f.trace_print || flags.f.normal_print) && flags.f.printer_exists)
1217 print_command_2(name, len);
1218 }
1219
1220 void print_menu_trace_always(const char *name, int len) {
1221 if ((flags.f.trace_print || flags.f.normal_print) && flags.f.printer_exists)
1222 print_command_2(name, len);
1223 }
1224
1225 void print_trace() {
1226 if (flags.f.trace_print && flags.f.printer_exists)
1227 if (flags.f.normal_print || sp == -1)
1228 docmd_prstk(NULL);
1229 else
1230 docmd_prx(NULL);
1231 }
1232
1233 void print_stack_trace() {
1234 if (flags.f.trace_print && flags.f.normal_print && flags.f.printer_exists)
1235 docmd_prstk(NULL);
1236 }
1237
1238 void generic_r2p(phloat re, phloat im, phloat *r, phloat *phi) {
1239 if (im == 0) {
1240 if (re >= 0) {
1241 *r = re;
1242 *phi = 0;
1243 } else {
1244 *r = -re;
1245 *phi = flags.f.rad ? PI : flags.f.grad ? 200 : 180;
1246 }
1247 } else if (re == 0) {
1248 if (im > 0) {
1249 *r = im;
1250 *phi = flags.f.rad ? PI / 2 : flags.f.grad ? 100 : 90;
1251 } else {
1252 *r = -im;
1253 *phi = flags.f.rad ? -PI / 2 : flags.f.grad ? -100 : -90;
1254 }
1255 } else {
1256 *r = hypot(re, im);
1257 *phi = rad_to_angle(atan2(im, re));
1258 }
1259 }
1260
1261 void generic_p2r(phloat r, phloat phi, phloat *re, phloat *im) {
1262 phloat tre, tim;
1263 if (flags.f.rad) {
1264 p_sincos(phi, &tim, &tre);
1265 } else if (flags.f.grad) {
1266 phi = fmod(phi, 400);
1267 if (phi < 0)
1268 phi += 400;
1269 if (phi == 0) {
1270 tre = 1;
1271 tim = 0;
1272 } else if (phi == 100) {
1273 tre = 0;
1274 tim = 1;
1275 } else if (phi == 200) {
1276 tre = -1;
1277 tim = 0;
1278 } else if (phi == 300) {
1279 tre = 0;
1280 tim = -1;
1281 } else {
1282 tre = cos_grad(phi);
1283 tim = sin_grad(phi);
1284 }
1285 } else {
1286 phi = fmod(phi, 360);
1287 if (phi < 0)
1288 phi += 360;
1289 if (phi == 0) {
1290 tre = 1;
1291 tim = 0;
1292 } else if (phi == 90) {
1293 tre = 0;
1294 tim = 1;
1295 } else if (phi == 180) {
1296 tre = -1;
1297 tim = 0;
1298 } else if (phi == 270) {
1299 tre = 0;
1300 tim = -1;
1301 } else {
1302 tre = cos_deg(phi);
1303 tim = sin_deg(phi);
1304 }
1305 }
1306 *re = r * tre;
1307 *im = r * tim;
1308 }
1309
1310 static phloat sin_or_cos_deg(phloat x, bool do_sin) {
1311 bool neg = false;
1312 if (x < 0) {
1313 x = -x;
1314 if (do_sin)
1315 neg = true;
1316 }
1317 x = fmod(x, 360);
1318 if (x >= 180) {
1319 x -= 180;
1320 neg = !neg;
1321 }
1322 if (x >= 90) {
1323 x -= 90;
1324 do_sin = !do_sin;
1325 if (do_sin)
1326 neg = !neg;
1327 }
1328 phloat r;
1329 if (x == 45)
1330 r = sqrt(phloat(0.5));
1331 else {
1332 if (x > 45) {
1333 x = 90 - x;
1334 do_sin = !do_sin;
1335 }
1336 x /= 180 / PI;
1337 r = do_sin ? sin(x) : cos(x);
1338 }
1339 return neg ? -r : r;
1340 }
1341
1342 phloat sin_deg(phloat x) {
1343 return sin_or_cos_deg(x, true);
1344 }
1345
1346 phloat cos_deg(phloat x) {
1347 return sin_or_cos_deg(x, false);
1348 }
1349
1350 static phloat sin_or_cos_grad(phloat x, bool do_sin) {
1351 bool neg = false;
1352 if (x < 0) {
1353 x = -x;
1354 if (do_sin)
1355 neg = true;
1356 }
1357 x = fmod(x, 400);
1358 if (x >= 200) {
1359 x -= 200;
1360 neg = !neg;
1361 }
1362 if (x >= 100) {
1363 x -= 100;
1364 do_sin = !do_sin;
1365 if (do_sin)
1366 neg = !neg;
1367 }
1368 phloat r;
1369 if (x == 50)
1370 r = sqrt(phloat(0.5));
1371 else {
1372 if (x > 50) {
1373 x = 100 - x;
1374 do_sin = !do_sin;
1375 }
1376 x /= 200 / PI;
1377 r = do_sin ? sin(x) : cos(x);
1378 }
1379 return neg ? -r : r;
1380 }
1381
1382 phloat sin_grad(phloat x) {
1383 return sin_or_cos_grad(x, true);
1384 }
1385
1386 phloat cos_grad(phloat x) {
1387 return sin_or_cos_grad(x, false);
1388 }
1389
1390 int dimension_array(const char *name, int namelen, int4 rows, int4 columns, bool check_matedit) {
1391 int idx = lookup_var(name, namelen);
1392 if (check_matedit
1393 && (matedit_mode == 1 || matedit_mode == 3)
1394 && idx != -1 && vars[idx].level == matedit_level
1395 && string_equals(name, namelen, matedit_name, matedit_length)) {
1396 if (matedit_mode == 1)
1397 matedit_i = matedit_j = 0;
1398 else
1399 return ERR_RESTRICTED_OPERATION;
1400 }
1401
1402 vartype *matrix = idx == -1 ? NULL : vars[idx].value;
1403 /* NOTE: 'size' will only ever be 0 when we're called from
1404 * docmd_size(); docmd_dim() does not allow 0-size matrices.
1405 */
1406 int4 size = rows * columns;
1407 if (matrix == NULL || (matrix->type != TYPE_REALMATRIX
1408 && matrix->type != TYPE_COMPLEXMATRIX
1409 && matrix->type != TYPE_LIST)) {
1410 vartype *newmatrix;
1411 if (size == 0)
1412 return ERR_NONE;
1413 newmatrix = new_realmatrix(rows, columns);
1414 if (newmatrix == NULL)
1415 return ERR_INSUFFICIENT_MEMORY;
1416 int err = store_var(name, namelen, newmatrix);
1417 if (err != ERR_NONE)
1418 free_vartype(newmatrix);
1419 return err;
1420 } else if (size == 0) {
1421 purge_var(name, namelen);
1422 return ERR_NONE;
1423 } else
1424 return dimension_array_ref(matrix, rows, columns);
1425 }
1426
1427 int dimension_array_ref(vartype *matrix, int4 rows, int4 columns) {
1428 int4 size = rows * columns;
1429 if (matrix->type == TYPE_REALMATRIX) {
1430 vartype_realmatrix *oldmatrix = (vartype_realmatrix *) matrix;
1431 if (oldmatrix->rows == rows && oldmatrix->columns == columns)
1432 return ERR_NONE;
1433 if (oldmatrix->array->refcount == 1) {
1434 int4 oldsize = oldmatrix->rows * oldmatrix->columns;
1435 if (size == oldsize) {
1436 /* Easy case! */
1437 oldmatrix->rows = rows;
1438 oldmatrix->columns = columns;
1439 return ERR_NONE;
1440 } else if (size < oldsize) {
1441 /* Also pretty easy, shrinking means we don't have to worry
1442 * about allocation failures. We do deal with realloc()
1443 * failures, because technically, realloc() can fail even when
1444 * shrinking, but that is easy to handle by simply hanging onto
1445 * the existing block.
1446 */
1447 free_long_strings(oldmatrix->array->is_string + size, oldmatrix->array->data + size, oldsize - size);
1448 char *new_is_string = (char *) realloc(oldmatrix->array->is_string, size);
1449 if (new_is_string != NULL)
1450 oldmatrix->array->is_string = new_is_string;
1451 phloat *new_data = (phloat *) realloc((void *) oldmatrix->array->data, size * sizeof(phloat));
1452 if (new_data != NULL)
1453 oldmatrix->array->data = new_data;
1454 oldmatrix->rows = rows;
1455 oldmatrix->columns = columns;
1456 return ERR_NONE;
1457 }
1458 /* Since there are no shared references to this array,
1459 * I can modify it in place using a realloc(). However, I
1460 * only use realloc() on the 'data' array, not on the
1461 * 'is_string' array -- if I used it on both, and the second
1462 * call fails, I might be unable to roll back the first.
1463 * So, playing safe -- shouldn't be too big a handicap since
1464 * 'is_string' is a lot smaller than 'data', so the transient
1465 * memory overhead is only about 12.5%.
1466 */
1467 char *new_is_string = (char *) malloc(size);
1468 if (new_is_string == NULL)
1469 return ERR_INSUFFICIENT_MEMORY;
1470 phloat *new_data = (phloat *) realloc((void *) oldmatrix->array->data, size * sizeof(phloat));
1471 if (new_data == NULL) {
1472 free(new_is_string);
1473 return ERR_INSUFFICIENT_MEMORY;
1474 }
1475 memcpy(new_is_string, oldmatrix->array->is_string, oldsize);
1476 for (int4 i = oldsize; i < size; i++) {
1477 new_is_string[i] = 0;
1478 new_data[i] = 0;
1479 }
1480 free(oldmatrix->array->is_string);
1481 oldmatrix->array->is_string = new_is_string;
1482 oldmatrix->array->data = new_data;
1483 oldmatrix->rows = rows;
1484 oldmatrix->columns = columns;
1485 return ERR_NONE;
1486 } else {
1487 /* There are shared references to the matrix. This means I
1488 * can't realloc() it; I'm going to allocate a brand-new instance,
1489 * and copy the contents from the old instance. I don't use
1490 * disentangle(); that's only useful if you want to eliminate
1491 * shared references without resizing.
1492 */
1493 realmatrix_data *new_array;
1494 int4 i, s, oldsize;
1495 new_array = (realmatrix_data *) malloc(sizeof(realmatrix_data));
1496 if (new_array == NULL)
1497 return ERR_INSUFFICIENT_MEMORY;
1498 new_array->data = (phloat *) malloc(size * sizeof(phloat));
1499 if (new_array->data == NULL) {
1500 free(new_array);
1501 return ERR_INSUFFICIENT_MEMORY;
1502 }
1503 new_array->is_string = (char *) malloc(size);
1504 if (new_array->is_string == NULL) {
1505 nomem:
1506 free(new_array->data);
1507 free(new_array);
1508 return ERR_INSUFFICIENT_MEMORY;
1509 }
1510 oldsize = oldmatrix->rows * oldmatrix->columns;
1511 s = oldsize < size ? oldsize : size;
1512 for (i = 0; i < s; i++) {
1513 new_array->is_string[i] = oldmatrix->array->is_string[i];
1514 if (oldmatrix->array->is_string[i] == 2) {
1515 int4 *sp = *(int4 **) &oldmatrix->array->data[i];
1516 int4 *dp = (int4 *) malloc(*sp + 4);
1517 if (dp == NULL) {
1518 free_long_strings(new_array->is_string, new_array->data, i);
1519 free(new_array->is_string);
1520 goto nomem;
1521 }
1522 memcpy(dp, sp, *sp + 4);
1523 *(int4 **) &new_array->data[i] = dp;
1524 } else {
1525 new_array->data[i] = oldmatrix->array->data[i];
1526 }
1527 }
1528 for (i = s; i < size; i++) {
1529 new_array->is_string[i] = 0;
1530 new_array->data[i] = 0;
1531 }
1532 new_array->refcount = 1;
1533 oldmatrix->array->refcount--;
1534 oldmatrix->array = new_array;
1535 oldmatrix->rows = rows;
1536 oldmatrix->columns = columns;
1537 return ERR_NONE;
1538 }
1539 } else if (matrix->type == TYPE_COMPLEXMATRIX) {
1540 vartype_complexmatrix *oldmatrix = (vartype_complexmatrix *) matrix;
1541 if (oldmatrix->rows == rows && oldmatrix->columns == columns)
1542 return ERR_NONE;
1543 if (oldmatrix->array->refcount == 1) {
1544 /* Since there are no shared references to this array,
1545 * I can modify it in place using a realloc().
1546 */
1547 int4 i, oldsize;
1548 phloat *new_data = (phloat *)
1549 realloc((void *) oldmatrix->array->data, 2 * size * sizeof(phloat));
1550 if (new_data == NULL)
1551 return ERR_INSUFFICIENT_MEMORY;
1552 oldsize = oldmatrix->rows * oldmatrix->columns;
1553 for (i = 2 * oldsize; i < 2 * size; i++)
1554 new_data[i] = 0;
1555 oldmatrix->array->data = new_data;
1556 oldmatrix->rows = rows;
1557 oldmatrix->columns = columns;
1558 return ERR_NONE;
1559 } else {
1560 /* There are shared references to the matrix. This means I
1561 * can't realloc() it; I'm going to allocate a brand-new instance,
1562 * and copy the contents from the old instance. I don't use
1563 * disentangle(); that's only useful if you want to eliminate
1564 * shared references without resizing.
1565 */
1566 complexmatrix_data *new_array;
1567 int4 i, s, oldsize;
1568 new_array = (complexmatrix_data *)
1569 malloc(sizeof(complexmatrix_data));
1570 if (new_array == NULL)
1571 return ERR_INSUFFICIENT_MEMORY;
1572 new_array->data = (phloat *) malloc(2 * size * sizeof(phloat));
1573 if (new_array->data == NULL) {
1574 free(new_array);
1575 return ERR_INSUFFICIENT_MEMORY;
1576 }
1577 oldsize = oldmatrix->rows * oldmatrix->columns;
1578 s = oldsize < size ? oldsize : size;
1579 for (i = 0; i < 2 * s; i++)
1580 new_array->data[i] = oldmatrix->array->data[i];
1581 for (i = 2 * s; i < 2 * size; i++)
1582 new_array->data[i] = 0;
1583 new_array->refcount = 1;
1584 oldmatrix->array->refcount--;
1585 oldmatrix->array = new_array;
1586 oldmatrix->rows = rows;
1587 oldmatrix->columns = columns;
1588 return ERR_NONE;
1589 }
1590 } else /* matrix->type == TYPE_LIST */ {
1591 if (columns != 1)
1592 return ERR_DIMENSION_ERROR;
1593 vartype_list *oldlist = (vartype_list *) matrix;
1594 if (oldlist->size == size)
1595 return ERR_NONE;
1596 if (oldlist->array->refcount == 1) {
1597 /* Since there are no shared references to this array,
1598 * I can modify it in place using a realloc().
1599 */
1600 if (oldlist->size > size) {
1601 for (int4 i = size; i < oldlist->size; i++) {
1602 free_vartype(oldlist->array->data[i]);
1603 oldlist->array->data[i] = NULL;
1604 }
1605 vartype **new_data = (vartype **) realloc(oldlist->array->data, size * sizeof(vartype *));
1606 /* Note: If the realloc() fails to shrink the array, we just keep
1607 * using the existing one, basically pretending that it succeeded.
1608 */
1609 if (new_data != NULL)
1610 oldlist->array->data = new_data;
1611 oldlist->size = size;
1612 return ERR_NONE;
1613 } else {
1614 vartype **new_data = (vartype **) realloc(oldlist->array->data, size * sizeof(vartype *));
1615 if (new_data == NULL)
1616 return ERR_INSUFFICIENT_MEMORY;
1617 for (int4 i = oldlist->size; i < size; i++) {
1618 new_data[i] = new_real(0);
1619 if (new_data[i] == NULL) {
1620 /* Argh. Roll back everything and give up. */
1621 for (int4 j = oldlist->size; j < i; j++) {
1622 free_vartype(new_data[j]);
1623 new_data[j] = NULL;
1624 }
1625 vartype **reverted_data = (vartype **) realloc(new_data, oldlist->size * sizeof(vartype *));
1626 oldlist->array->data = reverted_data == NULL ? new_data : reverted_data;
1627 return ERR_INSUFFICIENT_MEMORY;
1628 }
1629 }
1630 oldlist->array->data = new_data;
1631 oldlist->size = size;
1632 return ERR_NONE;
1633 }
1634 } else {
1635 /* There are shared references to the list. This means I
1636 * can't realloc() it; I'm going to allocate a brand-new instance,
1637 * and copy the contents from the old instance. I don't use
1638 * disentangle(); that's only useful if you want to eliminate
1639 * shared references without resizing.
1640 */
1641 list_data *new_array = (list_data *) malloc(sizeof(list_data));
1642 if (new_array == NULL)
1643 return ERR_INSUFFICIENT_MEMORY;
1644 new_array->data = (vartype **) malloc(size * sizeof(vartype *));
1645 if (new_array->data == NULL) {
1646 free(new_array);
1647 return ERR_INSUFFICIENT_MEMORY;
1648 }
1649 for (int4 i = 0; i < size; i++) {
1650 new_array->data[i] = i < oldlist->size ? dup_vartype(oldlist->array->data[i]) : new_real(0);
1651 if (new_array->data[i] == NULL) {
1652 for (int4 j = 0; j < i; j++)
1653 free_vartype(new_array->data[j]);
1654 free(new_array->data);
1655 free(new_array);
1656 return ERR_INSUFFICIENT_MEMORY;
1657 }
1658 }
1659 new_array->refcount = 1;
1660 oldlist->array->refcount--;
1661 oldlist->array = new_array;
1662 oldlist->size = size;
1663 return ERR_NONE;
1664 }
1665 }
1666 }
1667
1668 phloat fix_hms(phloat x) {
1669 #ifdef BCD_MATH
1670 const phloat sec_corr(4, 1000);
1671 const phloat min_corr(4, 10);
1672 #endif
1673
1674 bool neg = x < 0;
1675 if (neg)
1676 x = -x;
1677 if (x == x + 1)
1678 return neg ? -x : x;
1679 if (x < 0.0059)
1680 return neg ? -x : x;
1681 #ifdef BCD_MATH
1682 if (floor(fmod(x * 10000, 100)) == 60)
1683 x += sec_corr;
1684 if (floor(fmod(x * 100, 100)) == 60) {
1685 x += min_corr;
1686 if (floor(fmod(x * 10000, 100)) == 60)
1687 x += sec_corr;
1688 }
1689 #else
1690 if (to_int8(x * 10000) % 100 == 60)
1691 x += 0.004;
1692 if (to_int8(x * 100) % 100 == 60) {
1693 x += 0.4;
1694 if (to_int8(x * 10000) % 100 == 60)
1695 x += 0.004;
1696 }
1697 #endif
1698 return neg ? -x : x;
1699 }
1700
1701 void char2buf(char *buf, int buflen, int *bufptr, char c) {
1702 if (*bufptr < buflen)
1703 buf[(*bufptr)++] = c;
1704 else
1705 buf[buflen - 1] = 26;
1706 }
1707
1708 void string2buf(char *buf, int buflen, int *bufptr, const char *s, int slen) {
1709 int i;
1710 for (i = 0; i < slen; i++)
1711 char2buf(buf, buflen, bufptr, s[i]);
1712 }
1713
1714 int uint2string(uint4 n, char *buf, int buflen) {
1715 uint4 pt = 1;
1716 int count = 0;
1717 while (n / pt >= 10)
1718 pt *= 10;
1719 while (pt != 0) {
1720 char2buf(buf, buflen, &count, (char) ('0' + (n / pt) % 10));
1721 pt /= 10;
1722 }
1723 return count;
1724 }
1725
1726 int int2string(int4 n, char *buf, int buflen) {
1727 uint4 u;
1728 int count = 0;
1729 if (n < 0) {
1730 char2buf(buf, buflen, &count, '-');
1731 u = -n;
1732 } else
1733 u = n;
1734 return count + uint2string(u, buf + count, buflen - count);
1735 }
1736
1737 int ulong2string(uint8 n, char *buf, int buflen) {
1738 uint8 pt = 1;
1739 int count = 0;
1740 while (n / pt >= 10)
1741 pt *= 10;
1742 while (pt != 0) {
1743 char2buf(buf, buflen, &count, (char) ('0' + (n / pt) % 10));
1744 pt /= 10;
1745 }
1746 return count;
1747 }
1748
1749 int vartype2string(const vartype *v, char *buf, int buflen, int max_mant_digits) {
1750 int dispmode;
1751 int digits = 0;
1752
1753 if (flags.f.fix_or_all)
1754 dispmode = flags.f.eng_or_all ? 3 : 0;
1755 else
1756 dispmode = flags.f.eng_or_all ? 2 : 1;
1757 if (flags.f.digits_bit3)
1758 digits += 8;
1759 if (flags.f.digits_bit2)
1760 digits += 4;
1761 if (flags.f.digits_bit1)
1762 digits += 2;
1763 if (flags.f.digits_bit0)
1764 digits += 1;
1765
1766 switch (v->type) {
1767
1768 case TYPE_REAL:
1769 return phloat2string(((vartype_real *) v)->x, buf, buflen,
1770 1, digits, dispmode,
1771 flags.f.thousands_separators,
1772 max_mant_digits);
1773
1774 case TYPE_COMPLEX: {
1775 phloat x, y;
1776 char x_buf[22];
1777 int x_len;
1778 char y_buf[22];
1779 int y_len;
1780 int i, ret_len;
1781
1782 if (flags.f.polar) {
1783 generic_r2p(((vartype_complex *) v)->re,
1784 ((vartype_complex *) v)->im, &x, &y);
1785 if (p_isinf(x))
1786 x = POS_HUGE_PHLOAT;
1787 } else {
1788 x = ((vartype_complex *) v)->re;
1789 y = ((vartype_complex *) v)->im;
1790 }
1791
1792 x_len = phloat2string(x, x_buf, 22,
1793 0, digits, dispmode,
1794 flags.f.thousands_separators,
1795 max_mant_digits);
1796 y_len = phloat2string(y, y_buf, 22,
1797 0, digits, dispmode,
1798 flags.f.thousands_separators,
1799 max_mant_digits);
1800
1801 if (x_len + y_len + 2 > buflen) {
1802 /* Too long? Fall back on ENG 2 */
1803 x_len = phloat2string(x, x_buf, 22,
1804 0, 2, 2,
1805 flags.f.thousands_separators,
1806 max_mant_digits);
1807 y_len = phloat2string(y, y_buf, 22,
1808 0, 2, 2,
1809 flags.f.thousands_separators,
1810 max_mant_digits);
1811 }
1812
1813 for (i = 0; i < buflen; i++) {
1814 if (i < x_len)
1815 buf[i] = x_buf[i];
1816 else if (i < x_len + 1)
1817 buf[i] = ' ';
1818 else if (i < x_len + 2) {
1819 if (y_buf[0] == '-' && !flags.f.polar)
1820 buf[i] = '-';
1821 else
1822 buf[i] = flags.f.polar ? 23 : 'i';
1823 } else if (i < x_len + 3) {
1824 if (y_buf[0] == '-' && !flags.f.polar)
1825 buf[i] = flags.f.polar ? 23 : 'i';
1826 else
1827 buf[i] = y_buf[0];
1828 } else
1829 buf[i] = y_buf[i - x_len - 2];
1830 }
1831
1832 ret_len = x_len + y_len + 2;
1833 if (ret_len > buflen) {
1834 buf[buflen - 1] = 26;
1835 ret_len = buflen;
1836 }
1837
1838 return ret_len;
1839 }
1840
1841 case TYPE_REALMATRIX: {
1842 vartype_realmatrix *m = (vartype_realmatrix *) v;
1843 int i;
1844 int chars_so_far = 0;
1845 string2buf(buf, buflen, &chars_so_far, "[ ", 2);
1846 i = int2string(m->rows, buf + chars_so_far, buflen - chars_so_far);
1847 chars_so_far += i;
1848 char2buf(buf, buflen, &chars_so_far, 'x');
1849 i = int2string(m->columns, buf + chars_so_far, buflen - chars_so_far);
1850 chars_so_far += i;
1851 string2buf(buf, buflen, &chars_so_far, " Matrix ]", 9);
1852 return chars_so_far;
1853 }
1854
1855 case TYPE_COMPLEXMATRIX: {
1856 vartype_complexmatrix *m = (vartype_complexmatrix *) v;
1857 int i;
1858 int chars_so_far = 0;
1859 string2buf(buf, buflen, &chars_so_far, "[ ", 2);
1860 i = int2string(m->rows, buf + chars_so_far, buflen - chars_so_far);
1861 chars_so_far += i;
1862 char2buf(buf, buflen, &chars_so_far, 'x');
1863 i = int2string(m->columns, buf + chars_so_far, buflen - chars_so_far);
1864 chars_so_far += i;
1865 string2buf(buf, buflen, &chars_so_far, " Cpx Matrix ]", 13);
1866 return chars_so_far;
1867 }
1868
1869 case TYPE_STRING: {
1870 vartype_string *s = (vartype_string *) v;
1871 int i;
1872 int chars_so_far = 0;
1873 char2buf(buf, buflen, &chars_so_far, '"');
1874 char *txt = s->txt();
1875 for (i = 0; i < s->length; i++)
1876 char2buf(buf, buflen, &chars_so_far, txt[i]);
1877 char2buf(buf, buflen, &chars_so_far, '"');
1878 return chars_so_far;
1879 }
1880
1881 case TYPE_LIST: {
1882 vartype_list *list = (vartype_list *) v;
1883 int i;
1884 int chars_so_far = 0;
1885 string2buf(buf, buflen, &chars_so_far, "{ ", 2);
1886 i = int2string(list->size, buf + chars_so_far, buflen - chars_so_far);
1887 chars_so_far += i;
1888 string2buf(buf, buflen, &chars_so_far, "-Elem List }", 12);
1889 return chars_so_far;
1890 }
1891
1892 default: {
1893 const char *msg = "UnsuppVarType";
1894 int msglen = 13;
1895 int i;
1896 for (i = 0; i < msglen; i++)
1897 buf[i] = msg[i];
1898 return msglen;
1899 }
1900 }
1901 }
1902
1903 const char *phloat2program(phloat d) {
1904 /* Converts a phloat to its most compact representation;
1905 * used for generating HP-42S style number literals in programs.
1906 */
1907 static char allbuf[50];
1908 static char scibuf[50];
1909 int alllen;
1910 int scilen;
1911 char dot = flags.f.decimal_point ? '.' : ',';
1912 int decimal, zeroes, last_nonzero, exponent;
1913 int i;
1914 alllen = phloat2string(d, allbuf, 49, 0, 0, 3, 0, MAX_MANT_DIGITS);
1915 scilen = phloat2string(d, scibuf, 49, 0, MAX_MANT_DIGITS - 1, 1, 0, MAX_MANT_DIGITS);
1916 /* Shorten SCI representation by removing trailing zeroes,
1917 * and decreasing the exponent until the decimal point
1918 * shifts out of the mantissa.
1919 */
1920 decimal = -1;
1921 exponent = -1;
1922 for (i = 0; i < scilen; i++) {
1923 char c = scibuf[i];
1924 if (c == dot) {
1925 decimal = i;
1926 last_nonzero = i;
1927 zeroes = 0;
1928 } else if (c == 24) {
1929 exponent = i;
1930 break;
1931 } else if (c != '0') {
1932 last_nonzero = i;
1933 zeroes = 0;
1934 } else
1935 zeroes++;
1936 }
1937 if (decimal != -1) {
1938 if (zeroes > 0) {
1939 for (i = last_nonzero + 1; i < scilen - zeroes; i++)
1940 scibuf[i] = scibuf[i + zeroes];
1941 scilen -= zeroes;
1942 }
1943 if ((exponent == -1 && decimal == scilen - 1)
1944 || (exponent != -1 && exponent - decimal == 1)){
1945 for (i = decimal; i < scilen - 1; i++)
1946 scibuf[i] = scibuf[i + 1];
1947 scilen--;
1948 } else if (exponent != -1) {
1949 int offset, ex, neg, newexplen, t;
1950 exponent -= zeroes;
1951 offset = exponent - decimal - 1;
1952 ex = 0;
1953 neg = 0;
1954 for (i = exponent + 1; i < scilen; i++) {
1955 char c = scibuf[i];
1956 if (c == '-')
1957 neg = 1;
1958 else
1959 ex = ex * 10 + c - '0';
1960 }
1961 if (neg)
1962 ex = -ex;
1963 ex -= offset;
1964 if (ex < 0) {
1965 ex = -ex;
1966 neg = 1;
1967 } else
1968 neg = 0;
1969 newexplen = neg ? 2 : 1;
1970 t = 10;
1971 while (ex >= t) {
1972 newexplen++;
1973 t *= 10;
1974 }
1975 if (newexplen <= scilen - exponent - 1) {
1976 for (i = decimal; i < exponent; i++)
1977 scibuf[i] = scibuf[i + 1];
1978 scilen = exponent;
1979 if (neg)
1980 ex = -ex;
1981 scilen += int2string(ex, scibuf + exponent,
1982 49 - exponent);
1983 }
1984 }
1985 }
1986 if (scilen < alllen) {
1987 scibuf[scilen] = 0;
1988 return scibuf;
1989 } else {
1990 allbuf[alllen] = 0;
1991 return allbuf;
1992 }
1993 }
1994
1995 int easy_phloat2string(phloat d, char *buf, int buflen, int base_mode) {
1996 int dispmode;
1997 int digits = 0;
1998
1999 if (flags.f.fix_or_all)
2000 dispmode = flags.f.eng_or_all ? 3 : 0;
2001 else
2002 dispmode = flags.f.eng_or_all ? 2 : 1;
2003 if (flags.f.digits_bit3)
2004 digits += 8;
2005 if (flags.f.digits_bit2)
2006 digits += 4;
2007 if (flags.f.digits_bit1)
2008 digits += 2;
2009 if (flags.f.digits_bit0)
2010 digits += 1;
2011
2012 return phloat2string(d, buf, buflen, base_mode,
2013 digits, dispmode, flags.f.thousands_separators);
2014 }
2015
2016 int ip2revstring(phloat d, char *buf, int buflen) {
2017 int s = 1;
2018 int bufpos = 0;
2019
2020 if (d < 0) {
2021 d = -d;
2022 s = -1;
2023 }
2024 d = floor(d);
2025 while (d != 0 && bufpos < buflen) {
2026 char c = '0' + to_digit(d);
2027 buf[bufpos++] = c;
2028 d = floor(d / 10);
2029 }
2030 if (bufpos == 0)
2031 buf[bufpos++] = '0';
2032 if (s == -1 && bufpos < buflen)
2033 buf[bufpos++] = '-';
2034 return bufpos;
2035 }
2036
2037 int matedit_get(vartype **res) {
2038 if (matedit_mode == 0)
2039 return ERR_NONEXISTENT;
2040
2041 vartype *m = NULL;
2042 if (matedit_mode == 2)
2043 m = matedit_x;
2044 else
2045 for (int i = vars_count - 1; i >= 0; i--) {
2046 var_struct *lv = vars + i;
2047 if ((lv->flags & VAR_PRIVATE) != 0)
2048 continue;
2049 if (matedit_level != -1 && lv->level < matedit_level)
2050 return ERR_NONEXISTENT;
2051 if (lv->level == matedit_level && string_equals(matedit_name, matedit_length, lv->name, lv->length)) {
2052 m = lv->value;
2053 break;
2054 }
2055 }
2056 int err = ERR_NONEXISTENT;
2057 if (m == NULL) {
2058 bad_matrix:
2059 if (matedit_mode == 2 || matedit_mode == 3)
2060 leave_matrix_editor();
2061 return err;
2062 }
2063
2064 for (int i = 0; i < matedit_stack_depth; i++) {
2065 if (m->type != TYPE_LIST) {
2066 err = i == 0 ? ERR_INVALID_TYPE : ERR_INVALID_DATA;
2067 goto bad_matrix;
2068 }
2069 vartype_list *list = (vartype_list *) m;
2070 if (matedit_stack[i] >= list->size) {
2071 err = ERR_INVALID_DATA;
2072 goto bad_matrix;
2073 }
2074 m = list->array->data[matedit_stack[i]];
2075 }
2076
2077 if (m->type != TYPE_REALMATRIX && m->type != TYPE_COMPLEXMATRIX && m->type != TYPE_LIST) {
2078 err = matedit_stack_depth == 0 ? ERR_INVALID_TYPE : ERR_INVALID_DATA;
2079 goto bad_matrix;
2080 }
2081
2082 // The following checks *should* be unnecessary, we're already preventing
2083 // those scenarios. Or that's what we're trying, anyway...
2084 if (m->type == TYPE_REALMATRIX) {
2085 vartype_realmatrix *rm = (vartype_realmatrix *) m;
2086 if (matedit_i >= rm->rows || matedit_j >= rm->columns)
2087 matedit_i = matedit_j = 0;
2088 } else if (m->type == TYPE_COMPLEXMATRIX) {
2089 vartype_complexmatrix *cm = (vartype_complexmatrix *) m;
2090 if (matedit_i >= cm->rows || matedit_j >= cm->columns)
2091 matedit_i = matedit_j = 0;
2092 } else { // m->type == TYPE_LIST
2093 vartype_list *list = (vartype_list *) m;
2094 if (matedit_i >= list->size)
2095 matedit_i = 0;
2096 matedit_j = 0;
2097 }
2098
2099 *res = m;
2100 return ERR_NONE;
2101 }
2102
2103 void leave_matrix_editor() {
2104 set_appmenu_exitcallback(0);
2105 set_menu(MENULEVEL_APP, MENU_NONE);
2106 matedit_mode = 0;
2107 free(matedit_stack);
2108 matedit_stack = NULL;
2109 matedit_stack_depth = 0;
2110 }