Debian Codegraph @

common/core_globals.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 <string.h>
20 #include <limits.h>
21
22 #include "core_globals.h"
23 #include "core_commands2.h"
24 #include "core_commands4.h"
25 #include "core_commands7.h"
26 #include "core_display.h"
27 #include "core_helpers.h"
28 #include "core_main.h"
29 #include "core_math1.h"
30 #include "core_tables.h"
31 #include "core_variables.h"
32 #include "shell.h"
33
34 #ifndef BCD_MATH
35 // We need these locally for BID128->double conversion
36 #include "bid_conf.h"
37 #include "bid_functions.h"
38 #endif
39
40 // File used for reading and writing the state file, and for importing and
41 // exporting programs. Since only one of these operations can be active at one
42 // time, having one FILE pointer for all of them is sufficient.
43 FILE *gfile = NULL;
44
45 const error_spec errors[] = {
46 { /* NONE */ NULL, 0 },
47 { /* ALPHA_DATA_IS_INVALID */ "Alpha Data Is Invalid", 21 },
48 { /* OUT_OF_RANGE */ "Out of Range", 12 },
49 { /* DIVIDE_BY_0 */ "Divide by 0", 11 },
50 { /* INVALID_TYPE */ "Invalid Type", 12 },
51 { /* INVALID_DATA */ "Invalid Data", 12 },
52 { /* NONEXISTENT */ "Nonexistent", 11 },
53 { /* DIMENSION_ERROR */ "Dimension Error", 15 },
54 { /* TOO_FEW_ARGUMENTS */ "Too Few Arguments", 17 },
55 { /* SIZE_ERROR */ "Size Error", 10 },
56 { /* STACK_DEPTH_ERROR */ "Stack Depth Error", 17 },
57 { /* RESTRICTED_OPERATION */ "Restricted Operation", 20 },
58 { /* YES */ "Yes", 3 },
59 { /* NO */ "No", 2 },
60 { /* STOP */ NULL, 0 },
61 { /* LABEL_NOT_FOUND */ "Label Not Found", 15 },
62 { /* NO_REAL_VARIABLES */ "No Real Variables", 17 },
63 { /* NO_COMPLEX_VARIABLES */ "No Complex Variables", 20 },
64 { /* NO_MATRIX_VARIABLES */ "No Matrix Variables", 19 },
65 { /* NO_MENU_VARIABLES */ "No Menu Variables", 17 },
66 { /* STAT_MATH_ERROR */ "Stat Math Error", 15 },
67 { /* INVALID_FORECAST_MODEL */ "Invalid Forecast Model", 22 },
68 { /* SINGULAR_MATRIX */ "Singular Matrix", 15 },
69 { /* SOLVE_SOLVE */ "Solve(Solve)", 12 },
70 { /* INTEG_INTEG */ "Integ(Integ)", 12 },
71 { /* RUN */ NULL, 0 },
72 { /* INTERRUPTED */ "Interrupted", 11 },
73 { /* PRINTING_IS_DISABLED */ "Printing Is Disabled", 20 },
74 { /* INTERRUPTIBLE */ NULL, 0 },
75 { /* NO_VARIABLES */ "No Variables", 12 },
76 { /* INSUFFICIENT_MEMORY */ "Insufficient Memory", 19 },
77 { /* NOT_YET_IMPLEMENTED */ "Not Yet Implemented", 19 },
78 { /* INTERNAL_ERROR */ "Internal Error", 14 },
79 { /* SUSPICIOUS_OFF */ "Suspicious OFF", 14 },
80 { /* RTN_STACK_FULL */ "RTN Stack Full", 14 },
81 { /* NUMBER_TOO_LARGE */ "Number Too Large", 16 },
82 { /* NUMBER_TOO_SMALL */ "Number Too Small", 16 },
83 { /* BIG_STACK_DISABLED */ "Big Stack Disabled", 18 },
84 { /* INVALID_CONTEXT */ "Invalid Context", 15 },
85 { /* NAME_TOO_LONG */ "Name Too Long", 13 },
86 { /* PROGRAM_LOCKED */ "Program Locked", 14 },
87 { /* NEXT_PROGRAM_LOCKED */ "Next Program Locked", 19 },
88 };
89
90
91 const menu_spec menus[] = {
92 { /* MENU_ALPHA1 */ MENU_NONE, MENU_ALPHA2, MENU_ALPHA2,
93 { { MENU_ALPHA_ABCDE1, 5, "ABCDE" },
94 { MENU_ALPHA_FGHI, 4, "FGHI" },
95 { MENU_ALPHA_JKLM, 4, "JKLM" },
96 { MENU_ALPHA_NOPQ1, 4, "NOPQ" },
97 { MENU_ALPHA_RSTUV1, 5, "RSTUV" },
98 { MENU_ALPHA_WXYZ, 4, "WXYZ" } } },
99 { /* MENU_ALPHA2 */ MENU_NONE, MENU_ALPHA1, MENU_ALPHA1,
100 { { MENU_ALPHA_PAREN, 5, "( [ {" },
101 { MENU_ALPHA_ARROW, 3, "\20^\16" },
102 { MENU_ALPHA_COMP, 5, "< = >" },
103 { MENU_ALPHA_MATH, 4, "MATH" },
104 { MENU_ALPHA_PUNC1, 4, "PUNC" },
105 { MENU_ALPHA_MISC1, 4, "MISC" } } },
106 { /* MENU_ALPHA_ABCDE1 */ MENU_ALPHA1, MENU_ALPHA_ABCDE2, MENU_ALPHA_ABCDE2,
107 { { MENU_NONE, 1, "A" },
108 { MENU_NONE, 1, "B" },
109 { MENU_NONE, 1, "C" },
110 { MENU_NONE, 1, "D" },
111 { MENU_NONE, 1, "E" },
112 { MENU_NONE, 1, " " } } },
113 { /* MENU_ALPHA_ABCDE2 */ MENU_ALPHA1, MENU_ALPHA_ABCDE1, MENU_ALPHA_ABCDE1,
114 { { MENU_NONE, 1, "\26" },
115 { MENU_NONE, 1, "\24" },
116 { MENU_NONE, 1, "\31" },
117 { MENU_NONE, 1, " " },
118 { MENU_NONE, 1, " " },
119 { MENU_NONE, 1, " " } } },
120 { /* MENU_ALPHA_FGHI */ MENU_ALPHA1, MENU_NONE, MENU_NONE,
121 { { MENU_NONE, 1, "F" },
122 { MENU_NONE, 1, "G" },
123 { MENU_NONE, 1, "H" },
124 { MENU_NONE, 1, "I" },
125 { MENU_NONE, 1, " " },
126 { MENU_NONE, 1, " " } } },
127 { /* MENU_ALPHA_JKLM */ MENU_ALPHA1, MENU_NONE, MENU_NONE,
128 { { MENU_NONE, 1, "J" },
129 { MENU_NONE, 1, "K" },
130 { MENU_NONE, 1, "L" },
131 { MENU_NONE, 1, "M" },
132 { MENU_NONE, 1, " " },
133 { MENU_NONE, 1, " " } } },
134 { /* MENU_ALPHA_NOPQ1 */ MENU_ALPHA1, MENU_ALPHA_NOPQ2, MENU_ALPHA_NOPQ2,
135 { { MENU_NONE, 1, "N" },
136 { MENU_NONE, 1, "O" },
137 { MENU_NONE, 1, "P" },
138 { MENU_NONE, 1, "Q" },
139 { MENU_NONE, 1, " " },
140 { MENU_NONE, 1, " " } } },
141 { /* MENU_ALPHA_NOPQ2 */ MENU_ALPHA1, MENU_ALPHA_NOPQ1, MENU_ALPHA_NOPQ1,
142 { { MENU_NONE, 1, "\25" },
143 { MENU_NONE, 1, "\34" },
144 { MENU_NONE, 1, " " },
145 { MENU_NONE, 1, " " },
146 { MENU_NONE, 1, " " },
147 { MENU_NONE, 1, " " } } },
148 { /* MENU_ALPHA_RSTUV1 */ MENU_ALPHA1, MENU_ALPHA_RSTUV2, MENU_ALPHA_RSTUV2,
149 { { MENU_NONE, 1, "R" },
150 { MENU_NONE, 1, "S" },
151 { MENU_NONE, 1, "T" },
152 { MENU_NONE, 1, "U" },
153 { MENU_NONE, 1, "V" },
154 { MENU_NONE, 1, " " } } },
155 { /* MENU_ALPHA_RSTUV2 */ MENU_ALPHA1, MENU_ALPHA_RSTUV1, MENU_ALPHA_RSTUV1,
156 { { MENU_NONE, 1, " " },
157 { MENU_NONE, 1, " " },
158 { MENU_NONE, 1, " " },
159 { MENU_NONE, 1, "\35" },
160 { MENU_NONE, 1, " " },
161 { MENU_NONE, 1, " " } } },
162 { /* MENU_ALPHA_WXYZ */ MENU_ALPHA1, MENU_NONE, MENU_NONE,
163 { { MENU_NONE, 1, "W" },
164 { MENU_NONE, 1, "X" },
165 { MENU_NONE, 1, "Y" },
166 { MENU_NONE, 1, "Z" },
167 { MENU_NONE, 1, " " },
168 { MENU_NONE, 1, " " } } },
169 { /* MENU_ALPHA_PAREN */ MENU_ALPHA2, MENU_NONE, MENU_NONE,
170 { { MENU_NONE, 1, "(" },
171 { MENU_NONE, 1, ")" },
172 { MENU_NONE, 1, "[" },
173 { MENU_NONE, 1, "]" },
174 { MENU_NONE, 1, "{" },
175 { MENU_NONE, 1, "}" } } },
176 { /* MENU_ALPHA_ARROW */ MENU_ALPHA2, MENU_NONE, MENU_NONE,
177 { { MENU_NONE, 1, "\20" },
178 { MENU_NONE, 1, "^" },
179 { MENU_NONE, 1, "\16" },
180 { MENU_NONE, 1, "\17" },
181 { MENU_NONE, 1, " " },
182 { MENU_NONE, 1, " " } } },
183 { /* MENU_ALPHA_COMP */ MENU_ALPHA2, MENU_NONE, MENU_NONE,
184 { { MENU_NONE, 1, "=" },
185 { MENU_NONE, 1, "\14" },
186 { MENU_NONE, 1, "<" },
187 { MENU_NONE, 1, ">" },
188 { MENU_NONE, 1, "\11" },
189 { MENU_NONE, 1, "\13" } } },
190 { /* MENU_ALPHA_MATH */ MENU_ALPHA2, MENU_NONE, MENU_NONE,
191 { { MENU_NONE, 1, "\5" },
192 { MENU_NONE, 1, "\3" },
193 { MENU_NONE, 1, "\2" },
194 { MENU_NONE, 1, "\27" },
195 { MENU_NONE, 1, "\23" },
196 { MENU_NONE, 1, "\21" } } },
197 { /* MENU_ALPHA_PUNC1 */ MENU_ALPHA2, MENU_ALPHA_PUNC2, MENU_ALPHA_PUNC2,
198 { { MENU_NONE, 1, "," },
199 { MENU_NONE, 1, ";" },
200 { MENU_NONE, 1, ":" },
201 { MENU_NONE, 1, "!" },
202 { MENU_NONE, 1, "?" },
203 { MENU_NONE, 1, "\"" } } },
204 { /* MENU_ALPHA_PUNC2 */ MENU_ALPHA2, MENU_ALPHA_PUNC1, MENU_ALPHA_PUNC1,
205 { { MENU_NONE, 1, "\32" },
206 { MENU_NONE, 1, "_" },
207 { MENU_NONE, 1, "`" },
208 { MENU_NONE, 1, "'" },
209 { MENU_NONE, 1, "\10" },
210 { MENU_NONE, 1, "\12" } } },
211 { /* MENU_ALPHA_MISC1 */ MENU_ALPHA2, MENU_ALPHA_MISC2, MENU_ALPHA_MISC2,
212 { { MENU_NONE, 1, "$" },
213 { MENU_NONE, 1, "*" },
214 { MENU_NONE, 1, "#" },
215 { MENU_NONE, 1, "/" },
216 { MENU_NONE, 1, "\37" },
217 { MENU_NONE, 1, " " } } },
218 { /* MENU_ALPHA_MISC2 */ MENU_ALPHA2, MENU_ALPHA_MISC1, MENU_ALPHA_MISC1,
219 { { MENU_NONE, 1, "\22" },
220 { MENU_NONE, 1, "&" },
221 { MENU_NONE, 1, "@" },
222 { MENU_NONE, 1, "\\" },
223 { MENU_NONE, 1, "~" },
224 { MENU_NONE, 1, "|" } } },
225 { /* MENU_ST */ MENU_NONE, MENU_NONE, MENU_NONE,
226 { { MENU_NONE, 4, "ST L" },
227 { MENU_NONE, 4, "ST X" },
228 { MENU_NONE, 4, "ST Y" },
229 { MENU_NONE, 4, "ST Z" },
230 { MENU_NONE, 4, "ST T" },
231 { MENU_NONE, 0, "" } } },
232 { /* MENU_IND_ST */ MENU_NONE, MENU_NONE, MENU_NONE,
233 { { MENU_NONE, 3, "IND" },
234 { MENU_NONE, 4, "ST L" },
235 { MENU_NONE, 4, "ST X" },
236 { MENU_NONE, 4, "ST Y" },
237 { MENU_NONE, 4, "ST Z" },
238 { MENU_NONE, 4, "ST T" } } },
239 { /* MENU_IND */ MENU_NONE, MENU_NONE, MENU_NONE,
240 { { MENU_NONE, 3, "IND" },
241 { MENU_NONE, 0, "" },
242 { MENU_NONE, 0, "" },
243 { MENU_NONE, 0, "" },
244 { MENU_NONE, 0, "" },
245 { MENU_NONE, 0, "" } } },
246 { /* MENU_MODES1 */ MENU_NONE, MENU_MODES2, MENU_MODES4,
247 { { 0x2000 + CMD_DEG, 0, "" },
248 { 0x2000 + CMD_RAD, 0, "" },
249 { 0x2000 + CMD_GRAD, 0, "" },
250 { 0x1000 + CMD_NULL, 0, "" },
251 { 0x2000 + CMD_RECT, 0, "" },
252 { 0x2000 + CMD_POLAR, 0, "" } } },
253 { /* MENU_MODES2 */ MENU_NONE, MENU_MODES3, MENU_MODES1,
254 { { 0x1000 + CMD_SIZE, 0, "" },
255 { 0x2000 + CMD_QUIET, 0, "" },
256 { 0x2000 + CMD_CPXRES, 0, "" },
257 { 0x2000 + CMD_REALRES, 0, "" },
258 { 0x2000 + CMD_KEYASN, 0, "" },
259 { 0x2000 + CMD_LCLBL, 0, "" } } },
260 { /* MENU_MODES3 */ MENU_NONE, MENU_MODES4, MENU_MODES2,
261 { { 0x2000 + CMD_MDY, 0, "" },
262 { 0x2000 + CMD_DMY, 0, "" },
263 { 0x2000 + CMD_YMD, 0, "" },
264 { 0x1000 + CMD_NULL, 0, "" },
265 { 0x2000 + CMD_CLK12, 0, "" },
266 { 0x2000 + CMD_CLK24, 0, "" } } },
267 { /* MENU_MODES4 */ MENU_NONE, MENU_MODES1, MENU_MODES3,
268 { { 0x2000 + CMD_4STK, 0, "" },
269 { 0x2000 + CMD_NSTK, 0, "" },
270 { 0x1000 + CMD_NULL, 0, "" },
271 { 0x1000 + CMD_NULL, 0, "" },
272 { 0x1000 + CMD_NULL, 0, "" },
273 { MENU_MODES_MENUS, 5, "MENUS" } } },
274 { /* MENU_MODES_MENUS */ MENU_MODES4, MENU_NONE, MENU_NONE,
275 { { 0x2000 + CMD_CAPS, 0, "" },
276 { 0x2000 + CMD_MIXED, 0, "" },
277 { 0x2000 + CMD_STATIC, 0, "" },
278 { 0x2000 + CMD_DYNAMIC, 0, "" },
279 { 0x1000 + CMD_NULL, 0, "" },
280 { 0x1000 + CMD_NULL, 0, "" } } },
281 { /* MENU_DISP */ MENU_NONE, MENU_NONE, MENU_NONE,
282 { { 0x2000 + CMD_FIX, 0, "" },
283 { 0x2000 + CMD_SCI, 0, "" },
284 { 0x2000 + CMD_ENG, 0, "" },
285 { 0x2000 + CMD_ALL, 0, "" },
286 { 0x2000 + CMD_RDXDOT, 0, "" },
287 { 0x2000 + CMD_RDXCOMMA, 0, "" } } },
288 { /* MENU_CLEAR1 */ MENU_NONE, MENU_CLEAR2, MENU_CLEAR2,
289 { { 0x1000 + CMD_CLSIGMA, 0, "" },
290 { 0x1000 + CMD_CLP, 0, "" },
291 { 0x1000 + CMD_CLV, 0, "" },
292 { 0x1000 + CMD_CLST, 0, "" },
293 { 0x1000 + CMD_CLA, 0, "" },
294 { 0x1000 + CMD_CLX, 0, "" } } },
295 { /* MENU_CLEAR2 */ MENU_NONE, MENU_CLEAR1, MENU_CLEAR1,
296 { { 0x1000 + CMD_CLRG, 0, "" },
297 { 0x1000 + CMD_DEL, 0, "" },
298 { 0x1000 + CMD_CLKEYS, 0, "" },
299 { 0x1000 + CMD_CLLCD, 0, "" },
300 { 0x1000 + CMD_CLMENU, 0, "" },
301 { 0x1000 + CMD_CLALLa, 0, "" } } },
302 { /* MENU_CONVERT1 */ MENU_NONE, MENU_CONVERT2, MENU_CONVERT2,
303 { { 0x1000 + CMD_TO_DEG, 0, "" },
304 { 0x1000 + CMD_TO_RAD, 0, "" },
305 { 0x1000 + CMD_TO_HR, 0, "" },
306 { 0x1000 + CMD_TO_HMS, 0, "" },
307 { 0x1000 + CMD_TO_REC, 0, "" },
308 { 0x1000 + CMD_TO_POL, 0, "" } } },
309 { /* MENU_CONVERT2 */ MENU_NONE, MENU_CONVERT1, MENU_CONVERT1,
310 { { 0x1000 + CMD_IP, 0, "" },
311 { 0x1000 + CMD_FP, 0, "" },
312 { 0x1000 + CMD_RND, 0, "" },
313 { 0x1000 + CMD_ABS, 0, "" },
314 { 0x1000 + CMD_SIGN, 0, "" },
315 { 0x1000 + CMD_MOD, 0, "" } } },
316 { /* MENU_FLAGS */ MENU_NONE, MENU_NONE, MENU_NONE,
317 { { 0x1000 + CMD_SF, 0, "" },
318 { 0x1000 + CMD_CF, 0, "" },
319 { 0x1000 + CMD_FS_T, 0, "" },
320 { 0x1000 + CMD_FC_T, 0, "" },
321 { 0x1000 + CMD_FSC_T, 0, "" },
322 { 0x1000 + CMD_FCC_T, 0, "" } } },
323 { /* MENU_PROB */ MENU_NONE, MENU_NONE, MENU_NONE,
324 { { 0x1000 + CMD_COMB, 0, "" },
325 { 0x1000 + CMD_PERM, 0, "" },
326 { 0x1000 + CMD_FACT, 0, "" },
327 { 0x1000 + CMD_GAMMA, 0, "" },
328 { 0x1000 + CMD_RAN, 0, "" },
329 { 0x1000 + CMD_SEED, 0, "" } } },
330 { /* MENU_CUSTOM1 */ MENU_NONE, MENU_CUSTOM2, MENU_CUSTOM3,
331 { { 0, 0, "" },
332 { 0, 0, "" },
333 { 0, 0, "" },
334 { 0, 0, "" },
335 { 0, 0, "" },
336 { 0, 0, "" } } },
337 { /* MENU_CUSTOM2 */ MENU_NONE, MENU_CUSTOM3, MENU_CUSTOM1,
338 { { 0, 0, "" },
339 { 0, 0, "" },
340 { 0, 0, "" },
341 { 0, 0, "" },
342 { 0, 0, "" },
343 { 0, 0, "" } } },
344 { /* MENU_CUSTOM3 */ MENU_NONE, MENU_CUSTOM1, MENU_CUSTOM2,
345 { { 0, 0, "" },
346 { 0, 0, "" },
347 { 0, 0, "" },
348 { 0, 0, "" },
349 { 0, 0, "" },
350 { 0, 0, "" } } },
351 { /* MENU_PGM_FCN1 */ MENU_NONE, MENU_PGM_FCN2, MENU_PGM_FCN4,
352 { { 0x1000 + CMD_LBL, 0, "" },
353 { 0x1000 + CMD_RTN, 0, "" },
354 { 0x1000 + CMD_INPUT, 0, "" },
355 { 0x1000 + CMD_VIEW, 0, "" },
356 { 0x1000 + CMD_AVIEW, 0, "" },
357 { 0x1000 + CMD_XEQ, 0, "" } } },
358 { /* MENU_PGM_FCN2 */ MENU_NONE, MENU_PGM_FCN3, MENU_PGM_FCN1,
359 { { MENU_PGM_XCOMP0, 3, "X?0" },
360 { MENU_PGM_XCOMPY, 3, "X?Y" },
361 { 0x1000 + CMD_PROMPT, 0, "" },
362 { 0x1000 + CMD_PSE, 0, "" },
363 { 0x1000 + CMD_ISG, 0, "" },
364 { 0x1000 + CMD_DSE, 0, "" } } },
365 { /* MENU_PGM_FCN3 */ MENU_NONE, MENU_PGM_FCN4, MENU_PGM_FCN2,
366 { { 0x1000 + CMD_AIP, 0, "" },
367 { 0x1000 + CMD_XTOA, 0, "" },
368 { 0x1000 + CMD_AGRAPH, 0, "" },
369 { 0x1000 + CMD_PIXEL, 0, "" },
370 { 0x1000 + CMD_BEEP, 0, "" },
371 { 0x1000 + CMD_TONE, 0, "" } } },
372 { /* MENU_PGM_FCN4 */ MENU_NONE, MENU_PGM_FCN1, MENU_PGM_FCN3,
373 { { 0x1000 + CMD_MVAR, 0, "" },
374 { 0x1000 + CMD_VARMENU, 0, "" },
375 { 0x1000 + CMD_GETKEY, 0, "" },
376 { 0x1000 + CMD_MENU, 0, "" },
377 { 0x1000 + CMD_KEYG, 0, "" },
378 { 0x1000 + CMD_KEYX, 0, "" } } },
379 { /* MENU_PGM_XCOMP0 */ MENU_PGM_FCN2, MENU_NONE, MENU_NONE,
380 { { 0x1000 + CMD_X_EQ_0, 0, "" },
381 { 0x1000 + CMD_X_NE_0, 0, "" },
382 { 0x1000 + CMD_X_LT_0, 0, "" },
383 { 0x1000 + CMD_X_GT_0, 0, "" },
384 { 0x1000 + CMD_X_LE_0, 0, "" },
385 { 0x1000 + CMD_X_GE_0, 0, "" } } },
386 { /* MENU_PGM_XCOMPY */ MENU_PGM_FCN2, MENU_NONE, MENU_NONE,
387 { { 0x1000 + CMD_X_EQ_Y, 0, "" },
388 { 0x1000 + CMD_X_NE_Y, 0, "" },
389 { 0x1000 + CMD_X_LT_Y, 0, "" },
390 { 0x1000 + CMD_X_GT_Y, 0, "" },
391 { 0x1000 + CMD_X_LE_Y, 0, "" },
392 { 0x1000 + CMD_X_GE_Y, 0, "" } } },
393 { /* MENU_PRINT1 */ MENU_NONE, MENU_PRINT2, MENU_PRINT3,
394 { { 0x1000 + CMD_PRSIGMA, 0, "" },
395 { 0x1000 + CMD_PRP, 0, "" },
396 { 0x1000 + CMD_PRV, 0, "" },
397 { 0x1000 + CMD_PRSTK, 0, "" },
398 { 0x1000 + CMD_PRA, 0, "" },
399 { 0x1000 + CMD_PRX, 0, "" } } },
400 { /* MENU_PRINT2 */ MENU_NONE, MENU_PRINT3, MENU_PRINT1,
401 { { 0x1000 + CMD_PRUSR, 0, "" },
402 { 0x1000 + CMD_LIST, 0, "" },
403 { 0x1000 + CMD_ADV, 0, "" },
404 { 0x1000 + CMD_PRLCD, 0, "" },
405 { 0x1000 + CMD_PRREG, 0, "" },
406 { 0x1000 + CMD_DELAY, 0, "" } } },
407 { /* MENU_PRINT3 */ MENU_NONE, MENU_PRINT1, MENU_PRINT2,
408 { { 0x2000 + CMD_PON, 0, "" },
409 { 0x2000 + CMD_POFF, 0, "" },
410 { 0x2000 + CMD_MAN, 0, "" },
411 { 0x2000 + CMD_NORM, 0, "" },
412 { 0x2000 + CMD_TRACE, 0, "" },
413 { 0x2000 + CMD_STRACE, 0, "" } } },
414 { /* MENU_TOP_FCN */ MENU_NONE, MENU_NONE, MENU_NONE,
415 { { 0x1000 + CMD_SIGMAADD, 0, "" },
416 { 0x1000 + CMD_INV, 0, "" },
417 { 0x1000 + CMD_SQRT, 0, "" },
418 { 0x1000 + CMD_LOG, 0, "" },
419 { 0x1000 + CMD_LN, 0, "" },
420 { 0x1000 + CMD_XEQ, 0, "" } } },
421 { /* MENU_CATALOG */ MENU_NONE, MENU_NONE, MENU_NONE,
422 { { 0, 0, "" },
423 { 0, 0, "" },
424 { 0, 0, "" },
425 { 0, 0, "" },
426 { 0, 0, "" },
427 { 0, 0, "" } } },
428 { /* MENU_BLANK */ MENU_NONE, MENU_NONE, MENU_NONE,
429 { { 0, 0, "" },
430 { 0, 0, "" },
431 { 0, 0, "" },
432 { 0, 0, "" },
433 { 0, 0, "" },
434 { 0, 0, "" } } },
435 { /* MENU_PROGRAMMABLE */ MENU_NONE, MENU_NONE, MENU_NONE,
436 { { 0, 0, "" },
437 { 0, 0, "" },
438 { 0, 0, "" },
439 { 0, 0, "" },
440 { 0, 0, "" },
441 { 0, 0, "" } } },
442 { /* MENU_VARMENU */ MENU_NONE, MENU_NONE, MENU_NONE,
443 { { 0, 0, "" },
444 { 0, 0, "" },
445 { 0, 0, "" },
446 { 0, 0, "" },
447 { 0, 0, "" },
448 { 0, 0, "" } } },
449 { /* MENU_STAT1 */ MENU_NONE, MENU_STAT2, MENU_STAT2,
450 { { 0x1000 + CMD_SIGMAADD, 0, "" },
451 { 0x1000 + CMD_SUM, 0, "" },
452 { 0x1000 + CMD_MEAN, 0, "" },
453 { 0x1000 + CMD_WMEAN, 0, "" },
454 { 0x1000 + CMD_SDEV, 0, "" },
455 { MENU_STAT_CFIT, 4, "CFIT" } } },
456 { /* MENU_STAT2 */ MENU_NONE, MENU_STAT1, MENU_STAT1,
457 { { 0x2000 + CMD_ALLSIGMA, 0, "" },
458 { 0x2000 + CMD_LINSIGMA, 0, "" },
459 { 0x1000 + CMD_NULL, 0, "" },
460 { MENU_STAT_SUMS1, 1, "\5" },
461 { 0x1000 + CMD_SIGMAREG, 0, "" },
462 { 0x1000 + CMD_SIGMAREG_T, 0, "" } } },
463 { /* MENU_STAT_CFIT */ MENU_STAT1, MENU_NONE, MENU_NONE,
464 { { 0x1000 + CMD_FCSTX, 0, "" },
465 { 0x1000 + CMD_FCSTY, 0, "" },
466 { 0x1000 + CMD_SLOPE, 0, "" },
467 { 0x1000 + CMD_YINT, 0, "" },
468 { 0x1000 + CMD_CORR, 0, "" },
469 { MENU_STAT_MODL, 4, "MODL" } } },
470 { /* MENU_STAT_MODL */ MENU_STAT_CFIT, MENU_NONE, MENU_NONE,
471 { { 0x2000 + CMD_LINF, 0, "" },
472 { 0x2000 + CMD_LOGF, 0, "" },
473 { 0x2000 + CMD_EXPF, 0, "" },
474 { 0x2000 + CMD_PWRF, 0, "" },
475 { 0x1000 + CMD_NULL, 0, "" },
476 { 0x1000 + CMD_BEST, 0, "" } } },
477 { /* MENU_STAT_SUMS1 */ MENU_STAT2, MENU_STAT_SUMS2, MENU_STAT_SUMS3,
478 { { 0x1000 + CMD_SX, 0, "" },
479 { 0x1000 + CMD_SX2, 0, "" },
480 { 0x1000 + CMD_SY, 0, "" },
481 { 0x1000 + CMD_SY2, 0, "" },
482 { 0x1000 + CMD_SXY, 0, "" },
483 { 0x1000 + CMD_SN, 0, "" } } },
484 { /* MENU_STAT_SUMS2 */ MENU_STAT2, MENU_STAT_SUMS3, MENU_STAT_SUMS1,
485 { { 0x1000 + CMD_SLNX, 0, "" },
486 { 0x1000 + CMD_SLNX2, 0, "" },
487 { 0x1000 + CMD_SLNY, 0, "" },
488 { 0x1000 + CMD_SLNY2, 0, "" },
489 { 0x1000 + CMD_SLNXLNY, 0, "" },
490 { 0x1000 + CMD_SXLNY, 0, "" } } },
491 { /* MENU_STAT_SUMS3 */ MENU_STAT2, MENU_STAT_SUMS1, MENU_STAT_SUMS2,
492 { { 0x1000 + CMD_SYLNX, 0, "" },
493 { 0x1000 + CMD_NULL, 0, "" },
494 { 0x1000 + CMD_NULL, 0, "" },
495 { 0x1000 + CMD_NULL, 0, "" },
496 { 0x1000 + CMD_NULL, 0, "" },
497 { 0x1000 + CMD_NULL, 0, "" } } },
498 { /* MENU_MATRIX1 */ MENU_NONE, MENU_MATRIX2, MENU_MATRIX3,
499 { { 0x1000 + CMD_NEWMAT, 0, "" },
500 { 0x1000 + CMD_INVRT, 0, "" },
501 { 0x1000 + CMD_DET, 0, "" },
502 { 0x1000 + CMD_TRANS, 0, "" },
503 { 0x1000 + CMD_SIMQ, 0, "" },
504 { 0x1000 + CMD_EDIT, 0, "" } } },
505 { /* MENU_MATRIX2 */ MENU_NONE, MENU_MATRIX3, MENU_MATRIX1,
506 { { 0x1000 + CMD_DOT, 0, "" },
507 { 0x1000 + CMD_CROSS, 0, "" },
508 { 0x1000 + CMD_UVEC, 0, "" },
509 { 0x1000 + CMD_DIM, 0, "" },
510 { 0x1000 + CMD_INDEX, 0, "" },
511 { 0x1000 + CMD_EDITN, 0, "" } } },
512 { /* MENU_MATRIX3 */ MENU_NONE, MENU_MATRIX1, MENU_MATRIX2,
513 { { 0x1000 + CMD_STOIJ, 0, "" },
514 { 0x1000 + CMD_RCLIJ, 0, "" },
515 { 0x1000 + CMD_STOEL, 0, "" },
516 { 0x1000 + CMD_RCLEL, 0, "" },
517 { 0x1000 + CMD_PUTM, 0, "" },
518 { 0x1000 + CMD_GETM, 0, "" } } },
519 { /* MENU_MATRIX_SIMQ */ MENU_MATRIX1, MENU_NONE, MENU_NONE,
520 { { 0x1000 + CMD_MATA, 0, "" },
521 { 0x1000 + CMD_MATB, 0, "" },
522 { 0x1000 + CMD_MATX, 0, "" },
523 { 0x1000 + CMD_NULL, 0, "" },
524 { 0x1000 + CMD_NULL, 0, "" },
525 { 0x1000 + CMD_NULL, 0, "" } } },
526 { /* MENU_MATRIX_EDIT1 */ MENU_NONE, MENU_MATRIX_EDIT2, MENU_MATRIX_EDIT2,
527 { { 0x1000 + CMD_LEFT, 0, "" },
528 { 0x1000 + CMD_OLD, 0, "" },
529 { 0x1000 + CMD_UP, 0, "" },
530 { 0x1000 + CMD_DOWN, 0, "" },
531 { 0x1000 + CMD_GOTOROW, 0, "" },
532 { 0x1000 + CMD_RIGHT, 0, "" } } },
533 { /* MENU_MATRIX_EDIT2 */ MENU_NONE, MENU_MATRIX_EDIT1, MENU_MATRIX_EDIT1,
534 { { 0x1000 + CMD_INSR, 0, "" },
535 { 0x1000 + CMD_NULL, 0, "" },
536 { 0x1000 + CMD_DELR, 0, "" },
537 { 0x1000 + CMD_NULL, 0, "" },
538 { 0x2000 + CMD_WRAP, 0, "" },
539 { 0x2000 + CMD_GROW, 0, "" } } },
540 { /* MENU_BASE1 */ MENU_NONE, MENU_BASE2, MENU_BASE5,
541 { { 0x1000 + CMD_A_THRU_F, 0, "" },
542 { 0x2000 + CMD_HEXM, 0, "" },
543 { 0x2000 + CMD_DECM, 0, "" },
544 { 0x2000 + CMD_OCTM, 0, "" },
545 { 0x2000 + CMD_BINM, 0, "" },
546 { MENU_BASE_LOGIC, 5, "LOGIC" } } },
547 { /* MENU_BASE2 */ MENU_NONE, MENU_BASE3, MENU_BASE1,
548 { { 0x1000 + CMD_SL, 0, "" },
549 { 0x1000 + CMD_SR, 0, "" },
550 { 0x1000 + CMD_RL, 0, "" },
551 { 0x1000 + CMD_RR, 0, "" },
552 { 0x1000 + CMD_RLC, 0, "" },
553 { 0x1000 + CMD_RRC, 0, "" } } },
554 { /* MENU_BASE3 */ MENU_NONE, MENU_BASE4, MENU_BASE2,
555 { { 0x1000 + CMD_LJ, 0, "" },
556 { 0x1000 + CMD_ASR, 0, "" },
557 { 0x1000 + CMD_RLN, 0, "" },
558 { 0x1000 + CMD_RRN, 0, "" },
559 { 0x1000 + CMD_RLCN, 0, "" },
560 { 0x1000 + CMD_RRCN, 0, "" } } },
561 { /* MENU_BASE4 */ MENU_NONE, MENU_BASE5, MENU_BASE3,
562 { { 0x1000 + CMD_SB, 0, "" },
563 { 0x1000 + CMD_CB, 0, "" },
564 { 0x1000 + CMD_B_T, 0, "" },
565 { 0x1000 + CMD_NUM_B, 0, "" },
566 { 0x1000 + CMD_MASKL, 0, "" },
567 { 0x1000 + CMD_MASKR, 0, "" } } },
568 { /* MENU_BASE5 */ MENU_NONE, MENU_BASE1, MENU_BASE4,
569 { { 0x1000 + CMD_SC, 0, "" },
570 { 0x1000 + CMD_CC, 0, "" },
571 { 0x1000 + CMD_C_T, 0, "" },
572 { MENU_BASE_FLOAT1, 5, "FLOAT" },
573 { MENU_BASE_MODES, 5, "MODES" },
574 { MENU_BASE_DISP, 4, "DISP" } } },
575 { /* MENU_BASE_A_THRU_F */ MENU_BASE1, MENU_NONE, MENU_NONE,
576 { { 0, 1, "A" },
577 { 0, 1, "B" },
578 { 0, 1, "C" },
579 { 0, 1, "D" },
580 { 0, 1, "E" },
581 { 0, 1, "F" } } },
582 { /* MENU_BASE_LOGIC */ MENU_BASE1, MENU_NONE, MENU_NONE,
583 { { 0x1000 + CMD_AND, 0, "" },
584 { 0x1000 + CMD_OR, 0, "" },
585 { 0x1000 + CMD_XOR, 0, "" },
586 { 0x1000 + CMD_NOT, 0, "" },
587 { 0x1000 + CMD_BIT_T, 0, "" },
588 { 0x1000 + CMD_ROTXY, 0, "" } } },
589 { /* MENU_BASE_FLOAT1 */ MENU_BASE5, MENU_BASE_FLOAT2, MENU_BASE_FLOAT2,
590 { { 0x1000 + CMD_N_TO_BS, 0, "" },
591 { 0x1000 + CMD_BS_TO_N, 0, "" },
592 { 0x1000 + CMD_N_TO_BD, 0, "" },
593 { 0x1000 + CMD_BD_TO_N, 0, "" },
594 { 0x1000 + CMD_N_TO_BQ, 0, "" },
595 { 0x1000 + CMD_BQ_TO_N, 0, "" } } },
596 { /* MENU_BASE_FLOAT2 */ MENU_BASE5, MENU_BASE_FLOAT1, MENU_BASE_FLOAT1,
597 { { 0x1000 + CMD_N_TO_DS, 0, "" },
598 { 0x1000 + CMD_DS_TO_N, 0, "" },
599 { 0x1000 + CMD_N_TO_DD, 0, "" },
600 { 0x1000 + CMD_DD_TO_N, 0, "" },
601 { 0x1000 + CMD_N_TO_DQ, 0, "" },
602 { 0x1000 + CMD_DQ_TO_N, 0, "" } } },
603 { /* MENU_BASE_MODES */ MENU_BASE5, MENU_NONE, MENU_NONE,
604 { { 0x1000 + CMD_WSIZE, 0, "" },
605 { 0x1000 + CMD_WSIZE_T, 0, "" },
606 { 0x2000 + CMD_BSIGNED, 0, "" },
607 { 0x2000 + CMD_BWRAP, 0, "" },
608 { 0x1000 + CMD_NULL, 0, "" },
609 { 0x1000 + CMD_BRESET, 0, "" } } },
610 { /* MENU_BASE_DISP */ MENU_BASE5, MENU_NONE, MENU_NONE,
611 { { 0x2000 + CMD_DECINT, 0, "" },
612 { 0x1000 + CMD_NULL, 0, "" },
613 { 0x2000 + CMD_HEXSEP, 0, "" },
614 { 0x2000 + CMD_DECSEP, 0, "" },
615 { 0x2000 + CMD_OCTSEP, 0, "" },
616 { 0x2000 + CMD_BINSEP, 0, "" } } },
617 { /* MENU_SOLVE */ MENU_NONE, MENU_NONE, MENU_NONE,
618 { { 0x1000 + CMD_MVAR, 0, "" },
619 { 0x1000 + CMD_NULL, 0, "" },
620 { 0x1000 + CMD_NULL, 0, "" },
621 { 0x1000 + CMD_NULL, 0, "" },
622 { 0x1000 + CMD_PGMSLV, 0, "" },
623 { 0x1000 + CMD_SOLVE, 0, "" } } },
624 { /* MENU_INTEG */ MENU_NONE, MENU_NONE, MENU_NONE,
625 { { 0x1000 + CMD_MVAR, 0, "" },
626 { 0x1000 + CMD_NULL, 0, "" },
627 { 0x1000 + CMD_NULL, 0, "" },
628 { 0x1000 + CMD_NULL, 0, "" },
629 { 0x1000 + CMD_PGMINT, 0, "" },
630 { 0x1000 + CMD_INTEG, 0, "" } } },
631 { /* MENU_INTEG_PARAMS */ MENU_NONE, MENU_NONE, MENU_NONE,
632 { { 0, 4, "LLIM" },
633 { 0, 4, "ULIM" },
634 { 0, 3, "ACC" },
635 { 0x1000 + CMD_NULL, 0, "" },
636 { 0x1000 + CMD_NULL, 0, "" },
637 { 0, 1, "\3" } } }
638 };
639
640
641 /* By how much do the variables, programs, and labels
642 * arrays grow when they are full
643 */
644 #define VARS_INCREMENT 25
645 #define PRGMS_INCREMENT 10
646 #define LABELS_INCREMENT 10
647
648 /* Registers */
649 vartype **stack = NULL;
650 int sp = -1;
651 int stack_capacity = 0;
652 vartype *lastx = NULL;
653 int reg_alpha_length = 0;
654 char reg_alpha[44];
655
656 /* Flags */
657 flags_struct flags;
658 const char *virtual_flags =
659 /* 00-49 */ "00000000000000000000000000010000000000000000111111"
660 /* 50-99 */ "00010000000000010000000001000000000000000000000000";
661
662 /* Variables */
663 int vars_capacity = 0;
664 int vars_count = 0;
665 var_struct *vars = NULL;
666
667 /* Programs */
668 int prgms_capacity = 0;
669 int prgms_count = 0;
670 prgm_struct *prgms = NULL;
671 int labels_capacity = 0;
672 int labels_count = 0;
673 label_struct *labels = NULL;
674
675 int current_prgm = -1;
676 int4 pc;
677 int prgm_highlight_row = 0;
678
679 int varmenu_length;
680 char varmenu[7];
681 int varmenu_rows;
682 int varmenu_row;
683 int varmenu_labellength[6];
684 char varmenu_labeltext[6][7];
685 int varmenu_role;
686
687 bool mode_clall;
688 int (*mode_interruptible)(bool) = NULL;
689 bool mode_stoppable;
690 bool mode_command_entry;
691 char mode_number_entry;
692 bool mode_alpha_entry;
693 bool mode_shift;
694 int mode_appmenu;
695 int mode_plainmenu;
696 bool mode_plainmenu_sticky;
697 int mode_transientmenu;
698 int mode_alphamenu;
699 int mode_commandmenu;
700 bool mode_running;
701 bool mode_getkey;
702 bool mode_getkey1;
703 bool mode_pause = false;
704 bool mode_disable_stack_lift; /* transient */
705 bool mode_caller_stack_lift_disabled;
706 bool mode_varmenu;
707 bool mode_updown;
708 int4 mode_sigma_reg;
709 int mode_goose;
710 bool mode_time_clktd;
711 bool mode_time_clk24;
712 int mode_wsize;
713 bool mode_carry;
714 bool mode_dec_int;
715 bool mode_bin_sep;
716 bool mode_oct_sep;
717 bool mode_dec_sep;
718 bool mode_hex_sep;
719 bool mode_menu_caps;
720 bool mode_menu_static;
721 #if defined(ANDROID) || defined(IPHONE)
722 bool mode_popup_unknown = true;
723 #endif
724
725 phloat entered_number;
726 int entered_string_length;
727 char entered_string[15];
728
729 int pending_command;
730 arg_struct pending_command_arg;
731 int xeq_invisible;
732
733 /* Multi-keystroke commands -- edit state */
734 /* Relevant when mode_command_entry != 0 */
735 int incomplete_command;
736 bool incomplete_ind;
737 bool incomplete_alpha;
738 int incomplete_length;
739 int incomplete_maxdigits;
740 int incomplete_argtype;
741 int incomplete_num;
742 char incomplete_str[22];
743 int4 incomplete_saved_pc;
744 int4 incomplete_saved_highlight_row;
745
746 /* Command line handling temporaries */
747 char cmdline[100];
748 int cmdline_length;
749 int cmdline_row;
750
751 /* Matrix editor / matrix indexing */
752 int matedit_mode; /* 0=off, 1=index, 2=edit, 3=editn */
753 int matedit_level;
754 char matedit_name[7];
755 int matedit_length;
756 vartype *matedit_x;
757 int4 matedit_i;
758 int4 matedit_j;
759 int matedit_prev_appmenu;
760 int4 *matedit_stack = NULL;
761 int matedit_stack_depth = 0;
762 bool matedit_is_list;
763
764 /* INPUT */
765 char input_name[11];
766 int input_length;
767 arg_struct input_arg;
768
769 /* ERRMSG/ERRNO */
770 int lasterr = 0;
771 int lasterr_length;
772 char lasterr_text[22];
773
774 /* BASE application */
775 int baseapp = 0;
776
777 /* Random number generator */
778 int8 random_number_low, random_number_high;
779
780 /* NORM & TRACE mode: number waiting to be printed */
781 int deferred_print = 0;
782
783 /* Keystroke buffer - holds keystrokes received while
784 * there is a program running.
785 */
786 int keybuf_head = 0;
787 int keybuf_tail = 0;
788 int keybuf[16];
789
790 int remove_program_catalog = 0;
791
792 int state_file_number_format;
793
794 /* No user interaction: we keep track of whether or not the user
795 * has pressed any keys since powering up, and we don't allow
796 * programmatic OFF until they have. The reason is that we want
797 * to prevent nastiness like
798 *
799 * LBL "YIKES" SF 11 OFF GTO "YIKES"
800 *
801 * from locking the user out.
802 */
803 bool no_keystrokes_yet;
804
805
806 /* Version number for the state file.
807 * State file versions correspond to application releases as follows:
808 * (Versions before 26 (2.5) are no longer supported.)
809 *
810 * Version 26: 2.5 Separate and portable core state file
811 * Version 27: 2.5.2 Recovery mode for corrupt 2.5 state files
812 * Version 28: 2.5.3 Recording platform name and app version in state file
813 * Version 29: 2.5.7 SOLVE: Tracking second best guess in order to be able to
814 * report it accurately in Y, and to provide additional data
815 * points for distinguishing between zeroes and poles.
816 * Version 30: 2.5.23 Private local variables
817 * Version 31: 2.5.24 Merge RTN and FRT functionality
818 * Version 32: 2.5.24 Replace FUNC[012] with FUNC [0-4][0-4]
819 * Version 33: 3.0 Big stack; parameterized RTNERR
820 * Version 34: 3.0 Long strings
821 * Version 35: 3.0 Changing 'int' to 'bool' where appropriate
822 * Version 36-38: Plus42 stuff
823 * Version 39: 3.0.3 ERRMSG/ERRNO
824 * Version 40: 3.0.3 Longer incomplete_str buffer
825 * Version 41: 3.0.3 Plus42 stuff
826 * Version 42: 3.0.6 CAPS/Mixed for menus
827 * Version 43: 3.0.7 Plus42 stuff
828 * Version 44: 3.0.8 cursor left, cursor right, del key handling
829 * Version 45: 3.0.12 SOLVE secant impatience
830 * Version 46: 3.1 CSLD?
831 * Version 47: 3.1 Back-port of Plus42 RTN stack; FUNC stack hiding
832 * Version 48: 3.1 Matrix editor nested lists
833 * Version 49: 3.1.13 Program locking
834 * Version 50: 3.3 Move BASE settings from MODES to BASE
835 * Version 51: 3.3 BASE enhancements (menu additions)
836 * Version 52: 3.3 BASE enhancements (carry; display modes)
837 * Version 53: 3.3.3 STATIC/DYNAMIC for menus
838 * Version 54: 3.3.11 Statistics sums menu
839 */
840 #define FREE42_VERSION 54
841
842
843 /*******************/
844 /* Private globals */
845 /*******************/
846
847 struct rtn_stack_entry {
848 int4 prgm;
849 int4 pc;
850 int4 get_prgm() {
851 int4 p = prgm & 0x1fffffff;
852 if ((p & 0x10000000) != 0)
853 p |= 0xe0000000;
854 return p;
855 }
856 void set_prgm(int4 prgm) {
857 this->prgm = prgm & 0x1fffffff;
858 }
859 bool has_matrix() {
860 return (prgm & 0x80000000) != 0;
861 }
862 void set_has_matrix(bool state) {
863 if (state)
864 prgm |= 0x80000000;
865 else
866 prgm &= 0x7fffffff;
867 }
868 bool has_func() {
869 return (prgm & 0x40000000) != 0;
870 }
871 void set_has_func(bool state) {
872 if (state)
873 prgm |= 0x40000000;
874 else
875 prgm &= 0xbfffffff;
876 }
877 bool is_csld() {
878 return (prgm & 0x20000000) != 0;
879 }
880 void set_csld() {
881 if (flags.f.stack_lift_disable)
882 prgm |= 0x20000000;
883 else
884 prgm &= 0xdfffffff;
885 }
886 };
887
888 #define MAX_RTN_LEVEL 1024
889 static int rtn_stack_capacity = 0;
890 static rtn_stack_entry *rtn_stack = NULL;
891 static int rtn_level = 0;
892 static bool rtn_level_0_has_matrix_entry;
893 static bool rtn_level_0_has_func_state;
894 static int rtn_stop_level = -1;
895 static bool rtn_solve_active = false;
896 static bool rtn_integ_active = false;
897
898 #ifdef IPHONE
899 /* For iPhone, we disable OFF by default, to satisfy App Store
900 * policy, but we allow users to enable it using a magic value
901 * in the X register. This flag determines OFF behavior.
902 */
903 bool off_enable_flag = false;
904 #endif
905
906 struct matrix_persister {
907 int type;
908 int4 rows;
909 int4 columns;
910 };
911
912 static int array_count;
913 static int array_list_capacity;
914 static void **array_list;
915
916
917 static bool array_list_grow();
918 static int array_list_search(void *array);
919 static bool persist_vartype(vartype *v);
920 static bool unpersist_vartype(vartype **v);
921 static void update_label_table(int prgm, int4 pc, int inserted);
922 static void invalidate_lclbls(int prgm_index, bool force);
923 static int pc_line_convert(int4 loc, int loc_is_pc);
924
925 #ifdef BCD_MATH
926 #define bin_dec_mode_switch() ( state_file_number_format == NUMBER_FORMAT_BINARY )
927 #else
928 #define bin_dec_mode_switch() ( state_file_number_format != NUMBER_FORMAT_BINARY )
929 #endif
930
931
932 void vartype_string::trim1() {
933 if (length > SSLENV + 1) {
934 memmove(t.ptr, t.ptr + 1, --length);
935 } else if (length == SSLENV + 1) {
936 char temp[SSLENV];
937 memcpy(temp, t.ptr + 1, --length);
938 free(t.ptr);
939 memcpy(t.buf, temp, length);
940 } else if (length > 0) {
941 memmove(t.buf, t.buf + 1, --length);
942 }
943 }
944
945 static bool array_list_grow() {
946 if (array_count < array_list_capacity)
947 return true;
948 array_list_capacity += 10;
949 void **p = (void **) realloc(array_list,
950 array_list_capacity * sizeof(void *));
951 if (p == NULL)
952 return false;
953 array_list = p;
954 return true;
955 }
956
957 static int array_list_search(void *array) {
958 for (int i = 0; i < array_count; i++)
959 if (array_list[i] == array)
960 return i;
961 return -1;
962 }
963
964 static bool persist_vartype(vartype *v) {
965 if (v == NULL)
966 return write_char(TYPE_NULL);
967 if (!write_char(v->type))
968 return false;
969 switch (v->type) {
970 case TYPE_REAL: {
971 vartype_real *r = (vartype_real *) v;
972 return write_phloat(r->x);
973 }
974 case TYPE_COMPLEX: {
975 vartype_complex *c = (vartype_complex *) v;
976 return write_phloat(c->re) && write_phloat(c->im);
977 }
978 case TYPE_STRING: {
979 vartype_string *s = (vartype_string *) v;
980 return write_int4(s->length)
981 && fwrite(s->txt(), 1, s->length, gfile) == s->length;
982 }
983 case TYPE_REALMATRIX: {
984 vartype_realmatrix *rm = (vartype_realmatrix *) v;
985 int4 rows = rm->rows;
986 int4 columns = rm->columns;
987 bool must_write = true;
988 if (rm->array->refcount > 1) {
989 int n = array_list_search(rm->array);
990 if (n == -1) {
991 // A negative row count signals a new shared matrix
992 rows = -rows;
993 if (!array_list_grow())
994 return false;
995 array_list[array_count++] = rm->array;
996 } else {
997 // A zero row count means this matrix shares its data
998 // with a previously written matrix
999 rows = 0;
1000 columns = n;
1001 must_write = false;
1002 }
1003 }
1004 write_int4(rows);
1005 write_int4(columns);
1006 if (must_write) {
1007 int size = rm->rows * rm->columns;
1008 if (fwrite(rm->array->is_string, 1, size, gfile) != size)
1009 return false;
1010 for (int i = 0; i < size; i++) {
1011 if (rm->array->is_string[i] == 0) {
1012 if (!write_phloat(rm->array->data[i]))
1013 return false;
1014 } else {
1015 char *text;
1016 int4 len;
1017 get_matrix_string(rm, i, &text, &len);
1018 if (!write_int4(len))
1019 return false;
1020 if (fwrite(text, 1, len, gfile) != len)
1021 return false;
1022 }
1023 }
1024 }
1025 return true;
1026 }
1027 case TYPE_COMPLEXMATRIX: {
1028 vartype_complexmatrix *cm = (vartype_complexmatrix *) v;
1029 int4 rows = cm->rows;
1030 int4 columns = cm->columns;
1031 bool must_write = true;
1032 if (cm->array->refcount > 1) {
1033 int n = array_list_search(cm->array);
1034 if (n == -1) {
1035 // A negative row count signals a new shared matrix
1036 rows = -rows;
1037 if (!array_list_grow())
1038 return false;
1039 array_list[array_count++] = cm->array;
1040 } else {
1041 // A zero row count means this matrix shares its data
1042 // with a previously written matrix
1043 rows = 0;
1044 columns = n;
1045 must_write = false;
1046 }
1047 }
1048 write_int4(rows);
1049 write_int4(columns);
1050 if (must_write) {
1051 int size = 2 * cm->rows * cm->columns;
1052 for (int i = 0; i < size; i++)
1053 if (!write_phloat(cm->array->data[i]))
1054 return false;
1055 }
1056 return true;
1057 }
1058 case TYPE_LIST: {
1059 vartype_list *list = (vartype_list *) v;
1060 int4 size = list->size;
1061 int data_index = -1;
1062 bool must_write = true;
1063 if (list->array->refcount > 1) {
1064 int n = array_list_search(list->array);
1065 if (n == -1) {
1066 // data_index == -2 indicates a new shared list
1067 data_index = -2;
1068 if (!array_list_grow())
1069 return false;
1070 array_list[array_count++] = list->array;
1071 } else {
1072 // data_index >= 0 refers to a previously shared list
1073 data_index = n;
1074 must_write = false;
1075 }
1076 }
1077 write_int4(size);
1078 write_int(data_index);
1079 if (must_write) {
1080 for (int4 i = 0; i < list->size; i++)
1081 if (!persist_vartype(list->array->data[i]))
1082 return false;
1083 }
1084 return true;
1085 }
1086 default:
1087 /* Should not happen */
1088 return false;
1089 }
1090 }
1091
1092
1093 // A few declarations to help unpersist_vartype get the offsets right,
1094 // in the case it needs to convert the state file.
1095
1096 struct fake_bcd {
1097 char data[16];
1098 };
1099
1100 struct old_vartype_string {
1101 int type;
1102 int length;
1103 char text[6];
1104 };
1105
1106 // For coping with bad 2.5 state files. 0 means don't do anything special;
1107 // 1 means check for bad string lengths in real matrices; 2 is bug-
1108 // compatibility mode; and 3 is a signal to switch from mode 1 to mode 2.
1109 // When a bad string length is found in mode 1, this function will switch
1110 // the mode to mode 3 and return false, indicating an error; the caller
1111 // should then clean up what has already been read, rewind the state file,
1112 // and try again in mode 2.
1113
1114 int bug_mode;
1115
1116 // Using a global for 'ver' so we don't have to pass it around all the time
1117
1118 int4 ver;
1119
1120 static bool unpersist_vartype(vartype **v) {
1121 char type;
1122 if (!read_char(&type))
1123 return false;
1124 switch (type) {
1125 case TYPE_NULL: {
1126 *v = NULL;
1127 return true;
1128 }
1129 case TYPE_REAL: {
1130 vartype_real *r = (vartype_real *) new_real(0);
1131 if (r == NULL)
1132 return false;
1133 if (!read_phloat(&r->x)) {
1134 free_vartype((vartype *) r);
1135 return false;
1136 }
1137 *v = (vartype *) r;
1138 return true;
1139 }
1140 case TYPE_COMPLEX: {
1141 vartype_complex *c = (vartype_complex *) new_complex(0, 0);
1142 if (c == NULL)
1143 return false;
1144 if (!read_phloat(&c->re) || !read_phloat(&c->im)) {
1145 free_vartype((vartype *) c);
1146 return false;
1147 }
1148 *v = (vartype *) c;
1149 return true;
1150 }
1151 case TYPE_STRING: {
1152 int4 len;
1153 if (ver < 34) {
1154 char c;
1155 if (!read_char(&c))
1156 return false;
1157 len = c;
1158 } else {
1159 if (!read_int4(&len))
1160 return false;
1161 }
1162 vartype_string *s = (vartype_string *) new_string(NULL, len);
1163 if (s == NULL)
1164 return false;
1165 if (fread(s->txt(), 1, len, gfile) != len) {
1166 free_vartype((vartype *) s);
1167 return false;
1168 }
1169 *v = (vartype *) s;
1170 return true;
1171 }
1172 case TYPE_REALMATRIX: {
1173 int4 rows, columns;
1174 if (!read_int4(&rows) || !read_int4(&columns))
1175 return false;
1176 if (rows == 0) {
1177 // Shared matrix
1178 vartype *m = dup_vartype((vartype *) array_list[columns]);
1179 if (m == NULL)
1180 return false;
1181 else {
1182 *v = m;
1183 return true;
1184 }
1185 }
1186 bool shared = rows < 0;
1187 if (shared)
1188 rows = -rows;
1189 vartype_realmatrix *rm = (vartype_realmatrix *) new_realmatrix(rows, columns);
1190 if (rm == NULL)
1191 return false;
1192 int4 size = rows * columns;
1193 if (fread(rm->array->is_string, 1, size, gfile) != size) {
1194 free_vartype((vartype *) rm);
1195 return false;
1196 }
1197 bool success = true;
1198 int4 i;
1199 for (i = 0; i < size; i++) {
1200 success = false;
1201 if (rm->array->is_string[i] == 0) {
1202 if (!read_phloat(&rm->array->data[i]))
1203 break;
1204 } else {
1205 rm->array->is_string[i] = 1;
1206 if (bug_mode == 0) {
1207 if (ver < 34) {
1208 // 6 bytes of text followed by length byte
1209 char *t = (char *) &rm->array->data[i];
1210 if (fread(t + 1, 1, 7, gfile) != 7)
1211 break;
1212 t[0] = t[7];
1213 } else {
1214 // 4-byte length followed by n bytes of text
1215 int4 len;
1216 if (!read_int4(&len))
1217 break;
1218 if (len > SSLENM) {
1219 int4 *p = (int4 *) malloc(len + 4);
1220 if (p == NULL)
1221 break;
1222 if (fread(p + 1, 1, len, gfile) != len) {
1223 free(p);
1224 break;
1225 }
1226 *p = len;
1227 *(int4 **) &rm->array->data[i] = p;
1228 rm->array->is_string[i] = 2;
1229 } else {
1230 char *t = (char *) &rm->array->data[i];
1231 *t = len;
1232 if (fread(t + 1, 1, len, gfile) != len)
1233 break;
1234 }
1235 }
1236 } else if (bug_mode == 1) {
1237 // Could be as above, or could be length-prefixed.
1238 // Read 7 bytes, and if byte 7 looks plausible,
1239 // carry on; otherwise, set bug_mode to 3, signalling
1240 // we should start over in bug-compatibility mode.
1241 char *t = (char *) &rm->array->data[i];
1242 if (fread(t + 1, 1, 7, gfile) != 7)
1243 break;
1244 if (t[7] < 0 || t[7] > 6) {
1245 bug_mode = 3;
1246 break;
1247 }
1248 t[0] = t[7];
1249 } else {
1250 // bug_mode == 2, means this has to be a file with
1251 // length-prefixed strings in matrices. Bear in
1252 // mind that the prefixes are bogus, so for reading,
1253 // clamp them to the 0..6 range, but for advancing
1254 // in the file, take them at face value.
1255 unsigned char len;
1256 if (fread(&len, 1, 1, gfile) != 1)
1257 break;
1258 unsigned char reallen = len > 6 ? 6 : len;
1259 char *t = (char *) &rm->array->data[i];
1260 if (fread(t + 1, 1, reallen, gfile) != reallen)
1261 break;
1262 t[0] = reallen;
1263 len -= reallen;
1264 if (len > 0 && fseek(gfile, len, SEEK_CUR) != 0)
1265 break;
1266 }
1267 }
1268 success = true;
1269 }
1270 if (!success) {
1271 memset(rm->array->is_string + i, 0, size - i);
1272 free_vartype((vartype *) rm);
1273 return false;
1274 }
1275 if (shared) {
1276 if (!array_list_grow()) {
1277 free_vartype((vartype *) rm);
1278 return false;
1279 }
1280 array_list[array_count++] = rm;
1281 }
1282 *v = (vartype *) rm;
1283 return true;
1284 }
1285 case TYPE_COMPLEXMATRIX: {
1286 int4 rows, columns;
1287 if (!read_int4(&rows) || !read_int4(&columns))
1288 return false;
1289 if (rows == 0) {
1290 // Shared matrix
1291 vartype *m = dup_vartype((vartype *) array_list[columns]);
1292 if (m == NULL)
1293 return false;
1294 else {
1295 *v = m;
1296 return true;
1297 }
1298 }
1299 bool shared = rows < 0;
1300 if (shared)
1301 rows = -rows;
1302 vartype_complexmatrix *cm = (vartype_complexmatrix *) new_complexmatrix(rows, columns);
1303 if (cm == NULL)
1304 return false;
1305 int4 size = 2 * rows * columns;
1306 for (int4 i = 0; i < size; i++) {
1307 if (!read_phloat(&cm->array->data[i])) {
1308 free_vartype((vartype *) cm);
1309 return false;
1310 }
1311 }
1312 if (shared) {
1313 if (!array_list_grow()) {
1314 free_vartype((vartype *) cm);
1315 return false;
1316 }
1317 array_list[array_count++] = cm;
1318 }
1319 *v = (vartype *) cm;
1320 return true;
1321 }
1322 case TYPE_LIST: {
1323 int4 size;
1324 int data_index;
1325 if (!read_int4(&size) || !read_int(&data_index))
1326 return false;
1327 if (data_index >= 0) {
1328 // Shared list
1329 vartype *m = dup_vartype((vartype *) array_list[data_index]);
1330 if (m == NULL)
1331 return false;
1332 else {
1333 *v = m;
1334 return true;
1335 }
1336 }
1337 bool shared = data_index == -2;
1338 vartype_list *list = (vartype_list *) new_list(size);
1339 if (list == NULL)
1340 return false;
1341 if (shared) {
1342 if (!array_list_grow()) {
1343 free_vartype((vartype *) list);
1344 return false;
1345 }
1346 array_list[array_count++] = list;
1347 }
1348 for (int4 i = 0; i < size; i++) {
1349 if (!unpersist_vartype(&list->array->data[i])) {
1350 free_vartype((vartype *) list);
1351 return false;
1352 }
1353 }
1354 *v = (vartype *) list;
1355 return true;
1356 }
1357 default:
1358 return false;
1359 }
1360 }
1361
1362 static bool persist_globals() {
1363 int i;
1364 array_count = 0;
1365 array_list_capacity = 0;
1366 array_list = NULL;
1367 bool ret = false;
1368
1369 if (!write_int(sp))
1370 goto done;
1371 for (int i = 0; i <= sp; i++)
1372 if (!persist_vartype(stack[i]))
1373 goto done;
1374 if (!persist_vartype(lastx))
1375 goto done;
1376 if (!write_int(reg_alpha_length))
1377 goto done;
1378 if (fwrite(reg_alpha, 1, 44, gfile) != 44)
1379 goto done;
1380 if (!write_int4(mode_sigma_reg))
1381 goto done;
1382 if (!write_int(mode_goose))
1383 goto done;
1384 if (!write_bool(mode_time_clktd))
1385 goto done;
1386 if (!write_bool(mode_time_clk24))
1387 goto done;
1388 if (!write_int(mode_wsize))
1389 goto done;
1390 if (!write_bool(mode_carry))
1391 goto done;
1392 if (!write_bool(mode_dec_int))
1393 goto done;
1394 if (!write_bool(mode_bin_sep))
1395 goto done;
1396 if (!write_bool(mode_oct_sep))
1397 goto done;
1398 if (!write_bool(mode_dec_sep))
1399 goto done;
1400 if (!write_bool(mode_hex_sep))
1401 goto done;
1402 if (!write_bool(mode_menu_caps))
1403 goto done;
1404 if (!write_bool(mode_menu_static))
1405 goto done;
1406 if (fwrite(&flags, 1, sizeof(flags_struct), gfile) != sizeof(flags_struct))
1407 goto done;
1408 if (!write_int(prgms_count))
1409 goto done;
1410 for (i = 0; i < prgms_count; i++)
1411 core_export_programs(1, &i, NULL);
1412 for (i = 0; i < prgms_count; i++)
1413 if (!write_bool(prgms[i].locked))
1414 goto done;
1415 if (!write_int(current_prgm))
1416 goto done;
1417 if (!write_int4(pc2line(pc)))
1418 goto done;
1419 if (!write_int(prgm_highlight_row))
1420 goto done;
1421 if (!write_int(vars_count))
1422 goto done;
1423 for (i = 0; i < vars_count; i++) {
1424 if (!write_char(vars[i].length)
1425 || fwrite(vars[i].name, 1, vars[i].length, gfile) != vars[i].length
1426 || !write_int2(vars[i].level)
1427 || !write_int2(vars[i].flags)
1428 || !persist_vartype(vars[i].value))
1429 goto done;
1430 }
1431 if (!write_int(varmenu_length))
1432 goto done;
1433 if (fwrite(varmenu, 1, 7, gfile) != 7)
1434 goto done;
1435 if (!write_int(varmenu_rows))
1436 goto done;
1437 if (!write_int(varmenu_row))
1438 goto done;
1439 for (i = 0; i < 6; i++)
1440 if (!write_char(varmenu_labellength[i])
1441 || fwrite(varmenu_labeltext[i], 1, varmenu_labellength[i], gfile) != varmenu_labellength[i])
1442 goto done;
1443 if (!write_int(varmenu_role))
1444 goto done;
1445 if (!write_int(rtn_level))
1446 goto done;
1447 if (!write_bool(rtn_level_0_has_matrix_entry))
1448 goto done;
1449 if (!write_bool(rtn_level_0_has_func_state))
1450 goto done;
1451 int saved_prgm;
1452 saved_prgm = current_prgm;
1453 for (i = rtn_level - 1; i >= 0; i--) {
1454 current_prgm = rtn_stack[i].get_prgm();
1455 int4 line = rtn_stack[i].pc;
1456 if (current_prgm >= 0)
1457 line = pc2line(line);
1458 if (!write_int4(rtn_stack[i].prgm) || !write_int4(line))
1459 goto done;
1460 }
1461 current_prgm = saved_prgm;
1462 if (!write_bool(rtn_solve_active))
1463 goto done;
1464 if (!write_bool(rtn_integ_active))
1465 goto done;
1466 ret = true;
1467
1468 done:
1469 free(array_list);
1470 return ret;
1471 }
1472
1473 bool loading_state = false;
1474
1475 static bool unpersist_globals() {
1476 int i;
1477 array_count = 0;
1478 array_list_capacity = 0;
1479 array_list = NULL;
1480 bool ret = false;
1481 char tmp_dmy = 2;
1482
1483 if (ver < 33) {
1484 sp = 3;
1485 } else {
1486 if (!read_int(&sp)) {
1487 sp = -1;
1488 goto done;
1489 }
1490 }
1491 stack_capacity = sp + 1;
1492 if (stack_capacity < 4)
1493 stack_capacity = 4;
1494 stack = (vartype **) malloc(stack_capacity * sizeof(vartype *));
1495 if (stack == NULL) {
1496 stack_capacity = 0;
1497 sp = -1;
1498 goto done;
1499 }
1500 for (int i = 0; i <= sp; i++) {
1501 if (!unpersist_vartype(&stack[i]) || stack[i] == NULL) {
1502 for (int j = 0; j < i; j++)
1503 free_vartype(stack[j]);
1504 free(stack);
1505 stack = NULL;
1506 sp = -1;
1507 stack_capacity = 0;
1508 goto done;
1509 }
1510 }
1511 if (ver < 33) {
1512 vartype *tmp = stack[REG_X];
1513 stack[REG_X] = stack[REG_T];
1514 stack[REG_T] = tmp;
1515 tmp = stack[REG_Y];
1516 stack[REG_Y] = stack[REG_Z];
1517 stack[REG_Z] = tmp;
1518 }
1519
1520 free_vartype(lastx);
1521 if (!unpersist_vartype(&lastx))
1522 goto done;
1523
1524 if (!read_int(&reg_alpha_length)) {
1525 reg_alpha_length = 0;
1526 goto done;
1527 }
1528 if (fread(reg_alpha, 1, 44, gfile) != 44) {
1529 reg_alpha_length = 0;
1530 goto done;
1531 }
1532 if (!read_int4(&mode_sigma_reg)) {
1533 mode_sigma_reg = 11;
1534 goto done;
1535 }
1536 if (!read_int(&mode_goose)) {
1537 mode_goose = -1;
1538 goto done;
1539 }
1540 if (!read_bool(&mode_time_clktd)) {
1541 mode_time_clktd = false;
1542 goto done;
1543 }
1544 if (!read_bool(&mode_time_clk24)) {
1545 mode_time_clk24 = false;
1546 goto done;
1547 }
1548 if (!read_int(&mode_wsize)) {
1549 mode_wsize = 36;
1550 goto done;
1551 }
1552 mode_carry = mode_dec_int = mode_bin_sep = mode_oct_sep = mode_dec_sep = mode_hex_sep = false;
1553 if (ver >= 52) {
1554 if (!read_bool(&mode_carry)) goto done;
1555 if (!read_bool(&mode_dec_int)) goto done;
1556 if (!read_bool(&mode_bin_sep)) goto done;
1557 if (!read_bool(&mode_oct_sep)) goto done;
1558 if (!read_bool(&mode_dec_sep)) goto done;
1559 if (!read_bool(&mode_hex_sep)) goto done;
1560 }
1561 if (ver >= 42) {
1562 if (!read_bool(&mode_menu_caps)) {
1563 mode_menu_caps = false;
1564 goto done;
1565 }
1566 } else
1567 mode_menu_caps = false;
1568 if (ver >= 53) {
1569 if (!read_bool(&mode_menu_static)) {
1570 mode_menu_static = false;
1571 goto done;
1572 }
1573 } else
1574 mode_menu_static = false;
1575 if (fread(&flags, 1, sizeof(flags_struct), gfile)
1576 != sizeof(flags_struct))
1577 goto done;
1578 if (tmp_dmy != 2)
1579 flags.f.dmy = tmp_dmy;
1580 if (ver < 33)
1581 flags.f.big_stack = 0;
1582
1583 prgms_count = 0;
1584 prgms_capacity = 0;
1585 if (prgms != NULL) {
1586 free(prgms);
1587 prgms = NULL;
1588 }
1589 int nprogs;
1590 if (!read_int(&nprogs)) {
1591 goto done;
1592 }
1593 loading_state = true;
1594 core_import_programs(nprogs, NULL);
1595 loading_state = false;
1596 if (ver >= 49)
1597 for (i = 0; i < nprogs; i++)
1598 if (!read_bool(&prgms[i].locked))
1599 goto done;
1600 if (!read_int(&current_prgm)) {
1601 current_prgm = 0;
1602 goto done;
1603 }
1604 if (!read_int4(&pc)) {
1605 pc = -1;
1606 goto done;
1607 }
1608 pc = line2pc(pc);
1609 incomplete_saved_pc = line2pc(incomplete_saved_pc);
1610 if (!read_int(&prgm_highlight_row)) {
1611 prgm_highlight_row = 0;
1612 goto done;
1613 }
1614
1615 vars_capacity = 0;
1616 if (vars != NULL) {
1617 free(vars);
1618 vars = NULL;
1619 }
1620 if (!read_int(&vars_count)) {
1621 vars_count = 0;
1622 goto done;
1623 }
1624 vars = (var_struct *) malloc(vars_count * sizeof(var_struct));
1625 if (vars == NULL) {
1626 vars_count = 0;
1627 goto done;
1628 }
1629 for (i = 0; i < vars_count; i++) {
1630 if (!read_char((char *) &vars[i].length))
1631 goto vars_fail;
1632 if (fread(vars[i].name, 1, vars[i].length, gfile) != vars[i].length)
1633 goto vars_fail;
1634 if (!read_int2(&vars[i].level))
1635 goto vars_fail;
1636 if (ver < 30) {
1637 bool hidden, hiding;
1638 if (!read_bool(&hidden) || !read_bool(&hiding))
1639 goto vars_fail;
1640 vars[i].flags = (hidden ? VAR_HIDDEN : 0)
1641 | (hiding ? VAR_HIDING : 0);
1642 } else {
1643 if (!read_int2(&vars[i].flags))
1644 goto vars_fail;
1645 }
1646 if (!unpersist_vartype(&vars[i].value)) {
1647 vars_fail:
1648 for (int j = 0; j < i; j++)
1649 free_vartype(vars[j].value);
1650 free(vars);
1651 vars = NULL;
1652 vars_count = 0;
1653 goto done;
1654 }
1655 }
1656 vars_capacity = vars_count;
1657
1658 if (!read_int(&varmenu_length)) {
1659 varmenu_length = 0;
1660 goto done;
1661 }
1662 if (fread(varmenu, 1, 7, gfile) != 7) {
1663 varmenu_length = 0;
1664 goto done;
1665 }
1666 if (!read_int(&varmenu_rows)) {
1667 varmenu_length = 0;
1668 goto done;
1669 }
1670 if (!read_int(&varmenu_row)) {
1671 varmenu_length = 0;
1672 goto done;
1673 }
1674 char c;
1675 for (i = 0; i < 6; i++) {
1676 if (!read_char(&c)
1677 || fread(varmenu_labeltext[i], 1, c, gfile) != c)
1678 goto done;
1679 varmenu_labellength[i] = c;
1680 }
1681 if (!read_int(&varmenu_role))
1682 goto done;
1683 if (ver < 47) {
1684 // rtn_sp; obsolete
1685 int dummy;
1686 if (!read_int(&dummy))
1687 goto done;
1688 }
1689 if (!read_int(&rtn_level))
1690 goto done;
1691 if (!read_bool(&rtn_level_0_has_matrix_entry))
1692 goto done;
1693 if (ver >= 31) {
1694 if (!read_bool(&rtn_level_0_has_func_state))
1695 goto done;
1696 } else {
1697 rtn_level_0_has_func_state = false;
1698 }
1699 rtn_stack_capacity = 16;
1700 while (rtn_level > rtn_stack_capacity)
1701 rtn_stack_capacity <<= 1;
1702 rtn_stack = (rtn_stack_entry *) realloc(rtn_stack, rtn_stack_capacity * sizeof(rtn_stack_entry));
1703 if (ver >= 47) {
1704 int saved_prgm = current_prgm;
1705 for (i = rtn_level - 1; i >= 0; i--) {
1706 int4 line;
1707 if (!read_int4(&rtn_stack[i].prgm)) goto done;
1708 if (!read_int4(&line)) goto done;
1709 current_prgm = rtn_stack[i].get_prgm();
1710 if (current_prgm >= 0)
1711 line = line2pc(line);
1712 rtn_stack[i].pc = line;
1713 }
1714 current_prgm = saved_prgm;
1715 } else {
1716 int saved_prgm = current_prgm;
1717 for (int lvl = rtn_level - 1; lvl >= -1; lvl--) {
1718 bool matrix_entry_follows;
1719 if (lvl == -1) {
1720 matrix_entry_follows = rtn_level_0_has_matrix_entry;
1721 } else {
1722 int4 prgm, line;
1723 if (!read_int4(&prgm) || !read_int4(&line))
1724 goto done;
1725 rtn_stack[lvl].prgm = prgm;
1726 matrix_entry_follows = rtn_stack[lvl].has_matrix();
1727 current_prgm = rtn_stack[lvl].get_prgm();
1728 if (current_prgm >= 0)
1729 line = line2pc(line);
1730 rtn_stack[lvl].pc = line;
1731 }
1732 if (matrix_entry_follows) {
1733 char m_len;
1734 char m_name[7];
1735 int4 m_i, m_j;
1736 if (!read_char(&m_len)
1737 || fread(m_name, 1, m_len, gfile) != m_len
1738 || !read_int4(&m_i)
1739 || !read_int4(&m_j))
1740 goto done;
1741 vartype_list *list = (vartype_list *) new_list(4);
1742 if (list == NULL)
1743 goto done;
1744 list->array->data[0] = new_string(m_name, m_len);
1745 list->array->data[1] = new_real(0);
1746 list->array->data[2] = new_real(m_i);
1747 list->array->data[3] = new_real(m_j);
1748 for (int i = 0; i < 4; i++)
1749 if (list->array->data[i] == NULL) {
1750 free_vartype((vartype *) list);
1751 goto done;
1752 }
1753 int pos = vars_count - 1;
1754 int m_lvl = -2;
1755 for (; pos >= 0; pos--) {
1756 var_struct *vs = vars + pos;
1757 if (vs->level < lvl + 1 && string_equals(vs->name, vs->length, m_name, m_len)) {
1758 m_lvl = vs->level;
1759 break;
1760 }
1761 }
1762 if (m_lvl == -2) {
1763 // shouldn't happen
1764 free_vartype((vartype *) list);
1765 goto done;
1766 }
1767 ((vartype_real *) list->array->data[1])->x = m_lvl;
1768 for (pos = 0; pos < vars_count; pos++)
1769 if (vars[pos].level >= lvl + 1)
1770 break;
1771 if (vars_count == vars_capacity) {
1772 int newcap = vars_capacity + 8;
1773 var_struct *newvars = (var_struct *) realloc(vars, newcap * sizeof(var_struct));
1774 if (newvars == NULL) {
1775 free_vartype((vartype *) list);
1776 goto done;
1777 }
1778 vars = newvars;
1779 vars_capacity = newcap;
1780 }
1781 memmove(vars + pos + 1, vars + pos, (vars_count - pos) * sizeof(var_struct));
1782 memcpy(vars[pos].name, "MAT", 3);
1783 vars[pos].length = 3;
1784 vars[pos].level = lvl + 1;
1785 vars[pos].flags = VAR_PRIVATE;
1786 vars[pos].value = (vartype *) list;
1787 vars_count++;
1788 }
1789 }
1790 current_prgm = saved_prgm;
1791 }
1792 if (!read_bool(&rtn_solve_active))
1793 goto done;
1794 if (!read_bool(&rtn_integ_active))
1795 goto done;
1796
1797 ret = true;
1798
1799 done:
1800 free(array_list);
1801 return ret;
1802 }
1803
1804 void clear_all_prgms() {
1805 if (prgms != NULL) {
1806 int i;
1807 for (i = 0; i < prgms_count; i++)
1808 if (prgms[i].text != NULL)
1809 free(prgms[i].text);
1810 free(prgms);
1811 }
1812 prgms = NULL;
1813 prgms_capacity = 0;
1814 prgms_count = 0;
1815 if (labels != NULL)
1816 free(labels);
1817 labels = NULL;
1818 labels_capacity = 0;
1819 labels_count = 0;
1820 }
1821
1822 int clear_prgm(const arg_struct *arg) {
1823 int prgm_index;
1824 if (arg->type == ARGTYPE_LBLINDEX)
1825 prgm_index = labels[arg->val.num].prgm;
1826 else { // arg->type == ARGTYPE_STR
1827 if (arg->length == 0) {
1828 if (current_prgm < 0 || current_prgm >= prgms_count)
1829 return ERR_INTERNAL_ERROR;
1830 prgm_index = current_prgm;
1831 } else {
1832 int i;
1833 for (i = labels_count - 1; i >= 0; i--)
1834 if (string_equals(arg->val.text, arg->length,
1835 labels[i].name, labels[i].length))
1836 goto found;
1837 return ERR_LABEL_NOT_FOUND;
1838 found:
1839 prgm_index = labels[i].prgm;
1840 }
1841 }
1842 return clear_prgm_by_index(prgm_index);
1843 }
1844
1845 int clear_prgm_by_index(int prgm_index) {
1846 int i, j;
1847 if (prgm_index < 0 || prgm_index >= prgms_count)
1848 return ERR_LABEL_NOT_FOUND;
1849 clear_all_rtns();
1850 if (prgm_index == current_prgm)
1851 pc = -1;
1852 else if (current_prgm > prgm_index)
1853 current_prgm--;
1854 free(prgms[prgm_index].text);
1855 for (i = prgm_index; i < prgms_count - 1; i++)
1856 prgms[i] = prgms[i + 1];
1857 prgms_count--;
1858 i = j = 0;
1859 while (j < labels_count) {
1860 if (j > i)
1861 labels[i] = labels[j];
1862 j++;
1863 if (labels[i].prgm > prgm_index) {
1864 labels[i].prgm--;
1865 i++;
1866 } else if (labels[i].prgm < prgm_index)
1867 i++;
1868 }
1869 labels_count = i;
1870 if (prgms_count == 0 || prgm_index == prgms_count) {
1871 int saved_prgm = current_prgm;
1872 int saved_pc = pc;
1873 goto_dot_dot(false);
1874 current_prgm = saved_prgm;
1875 pc = saved_pc;
1876 }
1877 update_catalog();
1878 return ERR_NONE;
1879 }
1880
1881 void clear_prgm_lines(int4 count) {
1882 int4 frompc, deleted, i, j;
1883 if (pc == -1)
1884 pc = 0;
1885 frompc = pc;
1886 while (count > 0) {
1887 int command;
1888 arg_struct arg;
1889 get_next_command(&pc, &command, &arg, 0, NULL);
1890 if (command == CMD_END) {
1891 pc -= 2;
1892 break;
1893 }
1894 count--;
1895 }
1896 deleted = pc - frompc;
1897
1898 for (i = pc; i < prgms[current_prgm].size; i++)
1899 prgms[current_prgm].text[i - deleted] = prgms[current_prgm].text[i];
1900 prgms[current_prgm].size -= deleted;
1901 pc = frompc;
1902
1903 i = j = 0;
1904 while (j < labels_count) {
1905 if (j > i)
1906 labels[i] = labels[j];
1907 j++;
1908 if (labels[i].prgm == current_prgm) {
1909 if (labels[i].pc < frompc)
1910 i++;
1911 else if (labels[i].pc >= frompc + deleted) {
1912 labels[i].pc -= deleted;
1913 i++;
1914 }
1915 } else
1916 i++;
1917 }
1918 labels_count = i;
1919
1920 invalidate_lclbls(current_prgm, false);
1921 clear_all_rtns();
1922 }
1923
1924 bool goto_dot_dot(bool force_new) {
1925 int command;
1926 arg_struct arg;
1927 if (prgms_count != 0 && !force_new) {
1928 /* Check if last program is empty */
1929 pc = 0;
1930 current_prgm = prgms_count - 1;
1931 get_next_command(&pc, &command, &arg, 0, NULL);
1932 if (command == CMD_END) {
1933 pc = -1;
1934 return true;
1935 }
1936 }
1937 int new_prgm_capacity = 2;
1938 unsigned char *new_prgm_text = (unsigned char *) malloc(new_prgm_capacity);
1939 if (new_prgm_text == NULL)
1940 return false;
1941 if (prgms_count == prgms_capacity) {
1942 prgm_struct *newprgms;
1943 int i;
1944 int new_prgms_capacity = prgms_capacity + 10;
1945 newprgms = (prgm_struct *) malloc(new_prgms_capacity * sizeof(prgm_struct));
1946 if (newprgms == NULL) {
1947 free(new_prgm_text);
1948 return false;
1949 }
1950 for (i = 0; i < prgms_count; i++)
1951 newprgms[i] = prgms[i];
1952 if (prgms != NULL)
1953 free(prgms);
1954 prgms = newprgms;
1955 prgms_capacity = new_prgms_capacity;
1956 }
1957 current_prgm = prgms_count++;
1958 prgms[current_prgm].capacity = new_prgm_capacity;
1959 prgms[current_prgm].size = 0;
1960 prgms[current_prgm].lclbl_invalid = true;
1961 prgms[current_prgm].locked = false;
1962 prgms[current_prgm].text = new_prgm_text;
1963 command = CMD_END;
1964 arg.type = ARGTYPE_NONE;
1965 store_command(0, command, &arg, NULL);
1966 pc = -1;
1967 return true;
1968 }
1969
1970 bool mvar_prgms_exist() {
1971 int i;
1972 for (i = 0; i < labels_count; i++)
1973 if (label_has_mvar(i))
1974 return true;
1975 return false;
1976 }
1977
1978 bool label_has_mvar(int lblindex) {
1979 int saved_prgm;
1980 int4 pc;
1981 int command;
1982 arg_struct arg;
1983 if (labels[lblindex].length == 0)
1984 return false;
1985 saved_prgm = current_prgm;
1986 current_prgm = labels[lblindex].prgm;
1987 pc = labels[lblindex].pc;
1988 pc += get_command_length(current_prgm, pc);
1989 get_next_command(&pc, &command, &arg, 0, NULL);
1990 current_prgm = saved_prgm;
1991 return command == CMD_MVAR;
1992 }
1993
1994 int get_command_length(int prgm_index, int4 pc) {
1995 prgm_struct *prgm = prgms + prgm_index;
1996 int4 pc2 = pc;
1997 int command = prgm->text[pc2++];
1998 int argtype = prgm->text[pc2++];
1999 command |= (argtype & 112) << 4;
2000 bool have_orig_num = command == CMD_NUMBER && (argtype & 128) != 0;
2001 argtype &= 15;
2002
2003 if ((command == CMD_GTO || command == CMD_XEQ)
2004 && (argtype == ARGTYPE_NUM || argtype == ARGTYPE_STK
2005 || argtype == ARGTYPE_LCLBL))
2006 pc2 += 4;
2007 switch (argtype) {
2008 case ARGTYPE_NUM:
2009 case ARGTYPE_NEG_NUM:
2010 case ARGTYPE_IND_NUM: {
2011 while ((prgm->text[pc2++] & 128) == 0);
2012 break;
2013 }
2014 case ARGTYPE_STK:
2015 case ARGTYPE_IND_STK:
2016 case ARGTYPE_COMMAND:
2017 case ARGTYPE_LCLBL:
2018 pc2++;
2019 break;
2020 case ARGTYPE_STR:
2021 case ARGTYPE_IND_STR: {
2022 pc2 += prgm->text[pc2] + 1;
2023 break;
2024 }
2025 case ARGTYPE_DOUBLE:
2026 pc2 += sizeof(phloat);
2027 break;
2028 case ARGTYPE_XSTR: {
2029 int xl = prgm->text[pc2++];
2030 xl += prgm->text[pc2++] << 8;
2031 pc2 += xl;
2032 break;
2033 }
2034 }
2035 if (have_orig_num)
2036 while (prgm->text[pc2++]);
2037 return pc2 - pc;
2038 }
2039
2040 void get_next_command(int4 *pc, int *command, arg_struct *arg, int find_target, const char **num_str) {
2041 prgm_struct *prgm = prgms + current_prgm;
2042 int i;
2043 int4 target_pc;
2044 int4 orig_pc = *pc;
2045
2046 *command = prgm->text[(*pc)++];
2047 arg->type = prgm->text[(*pc)++];
2048 *command |= (arg->type & 112) << 4;
2049 bool have_orig_num = *command == CMD_NUMBER && (arg->type & 128) != 0;
2050 arg->type &= 15;
2051
2052 if ((*command == CMD_GTO || *command == CMD_XEQ)
2053 && (arg->type == ARGTYPE_NUM
2054 || arg->type == ARGTYPE_LCLBL
2055 || arg->type == ARGTYPE_STK)) {
2056 if (find_target) {
2057 target_pc = 0;
2058 for (i = 0; i < 4; i++)
2059 target_pc = (target_pc << 8) | prgm->text[(*pc)++];
2060 if (target_pc != -1) {
2061 arg->target = target_pc;
2062 find_target = 0;
2063 }
2064 } else
2065 (*pc) += 4;
2066 } else {
2067 find_target = 0;
2068 arg->target = -1;
2069 }
2070
2071 switch (arg->type) {
2072 case ARGTYPE_NUM:
2073 case ARGTYPE_NEG_NUM:
2074 case ARGTYPE_IND_NUM: {
2075 int4 num = 0;
2076 unsigned char c;
2077 do {
2078 c = prgm->text[(*pc)++];
2079 num = (num << 7) | (c & 127);
2080 } while ((c & 128) == 0);
2081 if (arg->type == ARGTYPE_NEG_NUM) {
2082 arg->type = ARGTYPE_NUM;
2083 num = -num;
2084 }
2085 arg->val.num = num;
2086 break;
2087 }
2088 case ARGTYPE_STK:
2089 case ARGTYPE_IND_STK:
2090 arg->val.stk = prgm->text[(*pc)++];
2091 break;
2092 case ARGTYPE_COMMAND:
2093 arg->val.cmd = prgm->text[(*pc)++];
2094 break;
2095 case ARGTYPE_LCLBL:
2096 arg->val.lclbl = prgm->text[(*pc)++];
2097 break;
2098 case ARGTYPE_STR:
2099 case ARGTYPE_IND_STR: {
2100 arg->length = prgm->text[(*pc)++];
2101 for (i = 0; i < arg->length; i++)
2102 arg->val.text[i] = prgm->text[(*pc)++];
2103 break;
2104 }
2105 case ARGTYPE_DOUBLE: {
2106 unsigned char *b = (unsigned char *) &arg->val_d;
2107 for (int i = 0; i < (int) sizeof(phloat); i++)
2108 *b++ = prgm->text[(*pc)++];
2109 break;
2110 }
2111 case ARGTYPE_XSTR: {
2112 int xstr_len = prgm->text[(*pc)++];
2113 xstr_len += prgm->text[(*pc)++] << 8;
2114 arg->length = xstr_len;
2115 arg->val.xstr = (const char *) (prgm->text + *pc);
2116 (*pc) += xstr_len;
2117 break;
2118 }
2119 }
2120
2121 if (*command == CMD_NUMBER) {
2122 if (have_orig_num) {
2123 char *p = (char *) &prgm->text[*pc];
2124 if (num_str != NULL)
2125 *num_str = p;
2126 /* Make sure the decimal stored in the program matches
2127 * the current setting of flag 28.
2128 */
2129 char wrong_dot = flags.f.decimal_point ? ',' : '.';
2130 char right_dot = flags.f.decimal_point ? '.' : ',';
2131 int numlen = 1;
2132 while (*p != 0) {
2133 if (*p == wrong_dot)
2134 *p = right_dot;
2135 p++;
2136 numlen++;
2137 }
2138 *pc += numlen;
2139 } else {
2140 if (num_str != NULL)
2141 *num_str = NULL;
2142 }
2143 if (arg->type != ARGTYPE_DOUBLE) {
2144 /* argtype is ARGTYPE_NUM; convert to phloat */
2145 arg->val_d = arg->val.num;
2146 arg->type = ARGTYPE_DOUBLE;
2147 }
2148 }
2149
2150 if (find_target) {
2151 target_pc = find_local_label(arg);
2152 arg->target = target_pc;
2153 for (i = 5; i >= 2; i--) {
2154 prgm->text[orig_pc + i] = target_pc;
2155 target_pc >>= 8;
2156 }
2157 prgm->lclbl_invalid = false;
2158 }
2159 }
2160
2161 void rebuild_label_table() {
2162 /* TODO -- this is *not* efficient; inserting and deleting ENDs and
2163 * global LBLs should not cause every single program to get rescanned!
2164 * But, I don't feel like dealing with that at the moment, so just
2165 * this ugly brute force approach for now.
2166 */
2167 int prgm_index;
2168 int4 pc;
2169 labels_count = 0;
2170 for (prgm_index = 0; prgm_index < prgms_count; prgm_index++) {
2171 prgm_struct *prgm = prgms + prgm_index;
2172 pc = 0;
2173 while (pc < prgm->size) {
2174 int command = prgm->text[pc];
2175 int argtype = prgm->text[pc + 1];
2176 command |= (argtype & 112) << 4;
2177 argtype &= 15;
2178
2179 if (command == CMD_END
2180 || (command == CMD_LBL && argtype == ARGTYPE_STR)) {
2181 label_struct *newlabel;
2182 if (labels_count == labels_capacity) {
2183 label_struct *newlabels;
2184 int i;
2185 labels_capacity += 50;
2186 newlabels = (label_struct *)
2187 malloc(labels_capacity * sizeof(label_struct));
2188 // TODO - handle memory allocation failure
2189 for (i = 0; i < labels_count; i++)
2190 newlabels[i] = labels[i];
2191 if (labels != NULL)
2192 free(labels);
2193 labels = newlabels;
2194 }
2195 newlabel = labels + labels_count++;
2196 if (command == CMD_END)
2197 newlabel->length = 0;
2198 else {
2199 int i;
2200 newlabel->length = prgm->text[pc + 2];
2201 for (i = 0; i < newlabel->length; i++)
2202 newlabel->name[i] = prgm->text[pc + 3 + i];
2203 }
2204 newlabel->prgm = prgm_index;
2205 newlabel->pc = pc;
2206 }
2207 pc += get_command_length(prgm_index, pc);
2208 }
2209 }
2210 }
2211
2212 static void update_label_table(int prgm, int4 pc, int inserted) {
2213 int i;
2214 for (i = 0; i < labels_count; i++) {
2215 if (labels[i].prgm > prgm)
2216 return;
2217 if (labels[i].prgm == prgm && labels[i].pc >= pc)
2218 labels[i].pc += inserted;
2219 }
2220 }
2221
2222 static void invalidate_lclbls(int prgm_index, bool force) {
2223 prgm_struct *prgm = prgms + prgm_index;
2224 if (force || !prgm->lclbl_invalid) {
2225 int4 pc2 = 0;
2226 while (pc2 < prgm->size) {
2227 int command = prgm->text[pc2];
2228 int argtype = prgm->text[pc2 + 1];
2229 command |= (argtype & 112) << 4;
2230 argtype &= 15;
2231 if ((command == CMD_GTO || command == CMD_XEQ)
2232 && (argtype == ARGTYPE_NUM || argtype == ARGTYPE_STK
2233 || argtype == ARGTYPE_LCLBL)) {
2234 /* A dest_pc value of -1 signals 'unknown',
2235 * -2 means 'nonexistent', and anything else is
2236 * the pc where the destination label is found.
2237 */
2238 int4 pos;
2239 for (pos = pc2 + 2; pos < pc2 + 6; pos++)
2240 prgm->text[pos] = 255;
2241 }
2242 pc2 += get_command_length(prgm_index, pc2);
2243 }
2244 prgm->lclbl_invalid = true;
2245 }
2246 }
2247
2248 void delete_command(int4 pc) {
2249 prgm_struct *prgm = prgms + current_prgm;
2250 int command = prgm->text[pc];
2251 int argtype = prgm->text[pc + 1];
2252 int length = get_command_length(current_prgm, pc);
2253 int4 pos;
2254
2255 command |= (argtype & 112) << 4;
2256 argtype &= 15;
2257
2258 if (command == CMD_END) {
2259 int4 newsize;
2260 prgm_struct *nextprgm;
2261 if (current_prgm == prgms_count - 1)
2262 /* Don't allow deletion of last program's END. */
2263 return;
2264 nextprgm = prgm + 1;
2265 prgm->size -= 2;
2266 newsize = prgm->size + nextprgm->size;
2267 if (newsize > prgm->capacity) {
2268 int4 newcapacity = (newsize + 511) & ~511;
2269 unsigned char *newtext = (unsigned char *) malloc(newcapacity);
2270 // TODO - handle memory allocation failure
2271 for (pos = 0; pos < prgm->size; pos++)
2272 newtext[pos] = prgm->text[pos];
2273 free(prgm->text);
2274 prgm->text = newtext;
2275 prgm->capacity = newcapacity;
2276 }
2277 for (pos = 0; pos < nextprgm->size; pos++)
2278 prgm->text[prgm->size++] = nextprgm->text[pos];
2279 free(nextprgm->text);
2280 for (pos = current_prgm + 1; pos < prgms_count - 1; pos++)
2281 prgms[pos] = prgms[pos + 1];
2282 prgms_count--;
2283 rebuild_label_table();
2284 invalidate_lclbls(current_prgm, true);
2285 clear_all_rtns();
2286 draw_varmenu();
2287 return;
2288 }
2289
2290 for (pos = pc; pos < prgm->size - length; pos++)
2291 prgm->text[pos] = prgm->text[pos + length];
2292 prgm->size -= length;
2293 if (command == CMD_LBL && argtype == ARGTYPE_STR)
2294 rebuild_label_table();
2295 else
2296 update_label_table(current_prgm, pc, -length);
2297 invalidate_lclbls(current_prgm, false);
2298 clear_all_rtns();
2299 draw_varmenu();
2300 }
2301
2302 bool store_command(int4 pc, int command, arg_struct *arg, const char *num_str) {
2303 unsigned char buf[100];
2304 int bufptr = 0;
2305 int xstr_len;
2306 int i;
2307 int4 pos;
2308 prgm_struct *prgm = prgms + current_prgm;
2309
2310 if (flags.f.prgm_mode && prgm->locked) {
2311 display_error(ERR_PROGRAM_LOCKED);
2312 return false;
2313 }
2314
2315 /* We should never be called with pc = -1, but just to be safe... */
2316 if (pc == -1)
2317 pc = 0;
2318
2319 if (arg->type == ARGTYPE_NUM && arg->val.num < 0) {
2320 arg->type = ARGTYPE_NEG_NUM;
2321 arg->val.num = -arg->val.num;
2322 } else if (command == CMD_NUMBER) {
2323 /* Store the string representation of the number, unless it matches
2324 * the canonical representation, or unless the number is zero.
2325 */
2326 if (num_str != NULL) {
2327 if (arg->val_d == 0) {
2328 num_str = NULL;
2329 } else {
2330 const char *ap = phloat2program(arg->val_d);
2331 const char *bp = num_str;
2332 bool equal = true;
2333 while (1) {
2334 char a = *ap++;
2335 char b = *bp++;
2336 if (a == 0) {
2337 if (b != 0)
2338 equal = false;
2339 break;
2340 } else if (b == 0) {
2341 goto notequal;
2342 }
2343 if (a != b) {
2344 if (a == 24) {
2345 if (b != 'E' && b != 'e')
2346 goto notequal;
2347 } else if (a == '.' || a == ',') {
2348 if (b != '.' && b != ',')
2349 goto notequal;
2350 } else {
2351 notequal:
2352 equal = false;
2353 break;
2354 }
2355 }
2356 }
2357 if (equal)
2358 num_str = NULL;
2359 }
2360 }
2361 /* arg.type is always ARGTYPE_DOUBLE for CMD_NUMBER, but for storage
2362 * efficiency, we handle integers specially and store them as
2363 * ARGTYPE_NUM or ARGTYPE_NEG_NUM instead.
2364 */
2365 int4 n = to_int4(arg->val_d);
2366 if (n == arg->val_d && n != (int4) 0x80000000) {
2367 if (n >= 0) {
2368 arg->val.num = n;
2369 arg->type = ARGTYPE_NUM;
2370 } else {
2371 arg->val.num = -n;
2372 arg->type = ARGTYPE_NEG_NUM;
2373 }
2374 }
2375 } else if (arg->type == ARGTYPE_LBLINDEX) {
2376 int li = arg->val.num;
2377 arg->length = labels[li].length;
2378 for (i = 0; i < arg->length; i++)
2379 arg->val.text[i] = labels[li].name[i];
2380 arg->type = ARGTYPE_STR;
2381 }
2382
2383 buf[bufptr++] = command & 255;
2384 buf[bufptr++] = arg->type | ((command & 0x700) >> 4) | (command != CMD_NUMBER || num_str == NULL ? 0 : 128);
2385
2386 /* If the program is nonempty, it must already contain an END,
2387 * since that's the very first thing that gets stored in any new
2388 * program. In this case, we need to split the program.
2389 */
2390 if (command == CMD_END && prgm->size > 0) {
2391 int new_prgm_size = prgm->size - pc;
2392 int new_prgm_capacity = (new_prgm_size + 511) & ~511;
2393 unsigned char *new_prgm_text = (unsigned char *) malloc(new_prgm_capacity);
2394 if (new_prgm_text == NULL) {
2395 display_error(ERR_INSUFFICIENT_MEMORY);
2396 return false;
2397 }
2398 if (prgms_count == prgms_capacity) {
2399 int i;
2400 int new_prgms_capacity = prgms_capacity + 10;
2401 prgm_struct *new_prgms = (prgm_struct *)
2402 malloc(new_prgms_capacity * sizeof(prgm_struct));
2403 if (new_prgms == NULL) {
2404 free(new_prgm_text);
2405 display_error(ERR_INSUFFICIENT_MEMORY);
2406 return false;
2407 }
2408 for (i = 0; i <= current_prgm; i++)
2409 new_prgms[i] = prgms[i];
2410 for (i = current_prgm + 1; i < prgms_count; i++)
2411 new_prgms[i + 1] = prgms[i];
2412 free(prgms);
2413 prgms = new_prgms;
2414 prgms_capacity = new_prgms_capacity;
2415 prgm = prgms + current_prgm;
2416 } else {
2417 for (i = prgms_count - 1; i > current_prgm; i--)
2418 prgms[i + 1] = prgms[i];
2419 }
2420 prgms_count++;
2421 prgm_struct *new_prgm = prgm + 1;
2422 new_prgm->size = new_prgm_size;
2423 new_prgm->capacity = new_prgm_capacity;
2424 new_prgm->locked = false;
2425 new_prgm->text = new_prgm_text;
2426 for (i = pc; i < prgm->size; i++)
2427 new_prgm->text[i - pc] = prgm->text[i];
2428 current_prgm++;
2429
2430 /* Truncate the previously 'current' program and append an END.
2431 * No need to check the size against the capacity and grow the
2432 * program; since it contained an END before, it still has the
2433 * capacity for one now;
2434 */
2435 prgm->size = pc;
2436 prgm->text[prgm->size++] = CMD_END;
2437 prgm->text[prgm->size++] = ARGTYPE_NONE;
2438 if (flags.f.printer_exists && (flags.f.trace_print || flags.f.normal_print))
2439 print_program_line(current_prgm - 1, pc);
2440
2441 rebuild_label_table();
2442 invalidate_lclbls(current_prgm, true);
2443 invalidate_lclbls(current_prgm - 1, true);
2444 clear_all_rtns();
2445 draw_varmenu();
2446 return true;
2447 }
2448
2449 if ((command == CMD_GTO || command == CMD_XEQ)
2450 && (arg->type == ARGTYPE_NUM || arg->type == ARGTYPE_STK
2451 || arg->type == ARGTYPE_LCLBL))
2452 for (i = 0; i < 4; i++)
2453 buf[bufptr++] = 255;
2454 switch (arg->type) {
2455 case ARGTYPE_NUM:
2456 case ARGTYPE_NEG_NUM:
2457 case ARGTYPE_IND_NUM: {
2458 int4 num = arg->val.num;
2459 char tmpbuf[5];
2460 int tmplen = 0;
2461 while (num > 127) {
2462 tmpbuf[tmplen++] = num & 127;
2463 num >>= 7;
2464 }
2465 tmpbuf[tmplen++] = num;
2466 tmpbuf[0] |= 128;
2467 while (--tmplen >= 0)
2468 buf[bufptr++] = tmpbuf[tmplen];
2469 break;
2470 }
2471 case ARGTYPE_STK:
2472 case ARGTYPE_IND_STK:
2473 buf[bufptr++] = arg->val.stk;
2474 break;
2475 case ARGTYPE_STR:
2476 case ARGTYPE_IND_STR: {
2477 buf[bufptr++] = (unsigned char) arg->length;
2478 for (i = 0; i < arg->length; i++)
2479 buf[bufptr++] = arg->val.text[i];
2480 break;
2481 }
2482 case ARGTYPE_LCLBL:
2483 buf[bufptr++] = arg->val.lclbl;
2484 break;
2485 case ARGTYPE_DOUBLE: {
2486 unsigned char *b = (unsigned char *) &arg->val_d;
2487 for (int i = 0; i < (int) sizeof(phloat); i++)
2488 buf[bufptr++] = *b++;
2489 break;
2490 }
2491 case ARGTYPE_XSTR: {
2492 xstr_len = arg->length;
2493 if (xstr_len > 65535)
2494 xstr_len = 65535;
2495 buf[bufptr++] = xstr_len;
2496 buf[bufptr++] = xstr_len >> 8;
2497 // Not storing the text in 'buf' because it may not fit;
2498 // we'll handle that separately when copying the buffer
2499 // into the program.
2500 bufptr += xstr_len;
2501 break;
2502 }
2503 }
2504
2505 if (command == CMD_NUMBER && num_str != NULL) {
2506 const char *p = num_str;
2507 char c;
2508 const char wrong_dot = flags.f.decimal_point ? ',' : '.';
2509 const char right_dot = flags.f.decimal_point ? '.' : ',';
2510 while ((c = *p++) != 0) {
2511 if (c == wrong_dot)
2512 c = right_dot;
2513 else if (c == 'E' || c == 'e')
2514 c = 24;
2515 buf[bufptr++] = c;
2516 }
2517 buf[bufptr++] = 0;
2518 }
2519
2520 if (bufptr + prgm->size > prgm->capacity) {
2521 int new_capacity = prgm->capacity + bufptr + 512;
2522 unsigned char *newtext;
2523 newtext = (unsigned char *) malloc(new_capacity);
2524 if (newtext == NULL) {
2525 display_error(ERR_INSUFFICIENT_MEMORY);
2526 return false;
2527 }
2528 for (pos = 0; pos < pc; pos++)
2529 newtext[pos] = prgm->text[pos];
2530 for (pos = pc; pos < prgm->size; pos++)
2531 newtext[pos + bufptr] = prgm->text[pos];
2532 if (prgm->text != NULL)
2533 free(prgm->text);
2534 prgm->capacity = new_capacity;
2535 prgm->text = newtext;
2536 } else {
2537 for (pos = prgm->size - 1; pos >= pc; pos--)
2538 prgm->text[pos + bufptr] = prgm->text[pos];
2539 }
2540 if (arg->type == ARGTYPE_XSTR) {
2541 int instr_len = bufptr - xstr_len;
2542 memcpy(prgm->text + pc, buf, instr_len);
2543 memcpy(prgm->text + pc + instr_len, arg->val.xstr, xstr_len);
2544 } else {
2545 memcpy(prgm->text + pc, buf, bufptr);
2546 }
2547 prgm->size += bufptr;
2548 if (command != CMD_END && flags.f.printer_exists && (flags.f.trace_print || flags.f.normal_print))
2549 print_program_line(current_prgm, pc);
2550
2551 if (command == CMD_END ||
2552 (command == CMD_LBL && arg->type == ARGTYPE_STR))
2553 rebuild_label_table();
2554 else
2555 update_label_table(current_prgm, pc, bufptr);
2556 invalidate_lclbls(current_prgm, false);
2557 clear_all_rtns();
2558 if (!loading_state)
2559 draw_varmenu();
2560 return true;
2561 }
2562
2563 bool store_command_after(int4 *pc, int command, arg_struct *arg, const char *num_str) {
2564 int4 oldpc = *pc;
2565 if (*pc == -1)
2566 *pc = 0;
2567 else if (!prgms[current_prgm].is_end(*pc))
2568 *pc += get_command_length(current_prgm, *pc);
2569 bool success = store_command(*pc, command, arg, num_str);
2570 if (!success)
2571 *pc = oldpc;
2572 return success;
2573 }
2574
2575 static bool ensure_prgm_space(int n) {
2576 prgm_struct *prgm = prgms + current_prgm;
2577 if (prgm->size + n <= prgm->capacity)
2578 return true;
2579 int4 newcapacity = prgm->size + n;
2580 unsigned char *newtext = (unsigned char *) realloc(prgm->text, newcapacity);
2581 if (newtext == NULL)
2582 return false;
2583 prgm->text = newtext;
2584 prgm->capacity = newcapacity;
2585 return true;
2586 }
2587
2588 int x2line() {
2589 if (prgms[current_prgm].locked)
2590 return ERR_PROGRAM_LOCKED;
2591 switch (stack[sp]->type) {
2592 case TYPE_REAL: {
2593 if (!ensure_prgm_space(2 + sizeof(phloat)))
2594 return ERR_INSUFFICIENT_MEMORY;
2595 vartype_real *r = (vartype_real *) stack[sp];
2596 arg_struct arg;
2597 arg.type = ARGTYPE_DOUBLE;
2598 arg.val_d = r->x;
2599 store_command_after(&pc, CMD_NUMBER, &arg, NULL);
2600 return ERR_NONE;
2601 }
2602 case TYPE_COMPLEX: {
2603 if (!ensure_prgm_space(6 + 2 * sizeof(phloat)))
2604 return ERR_INSUFFICIENT_MEMORY;
2605 vartype_complex *c = (vartype_complex *) stack[sp];
2606 arg_struct arg;
2607 arg.type = ARGTYPE_DOUBLE;
2608 arg.val_d = c->re;
2609 store_command_after(&pc, CMD_NUMBER, &arg, NULL);
2610 arg.type = ARGTYPE_DOUBLE;
2611 arg.val_d = c->im;
2612 store_command_after(&pc, CMD_NUMBER, &arg, NULL);
2613 arg.type = ARGTYPE_NONE;
2614 store_command_after(&pc, CMD_RCOMPLX, &arg, NULL);
2615 return ERR_NONE;
2616 }
2617 case TYPE_STRING: {
2618 vartype_string *s = (vartype_string *) stack[sp];
2619 int len = s->length;
2620 if (len > 65535)
2621 len = 65535;
2622 if (!ensure_prgm_space(4 + len))
2623 return ERR_INSUFFICIENT_MEMORY;
2624 arg_struct arg;
2625 arg.type = ARGTYPE_XSTR;
2626 arg.length = len;
2627 arg.val.xstr = s->txt();
2628 store_command_after(&pc, CMD_XSTR, &arg, NULL);
2629 return ERR_NONE;
2630 }
2631 default:
2632 return ERR_INTERNAL_ERROR;
2633 }
2634 }
2635
2636 int a2line(bool append) {
2637 if (prgms[current_prgm].locked)
2638 return ERR_PROGRAM_LOCKED;
2639 if (reg_alpha_length == 0) {
2640 squeak();
2641 return ERR_NONE;
2642 }
2643 if (!ensure_prgm_space(reg_alpha_length + ((reg_alpha_length - 2) / 14 + 1) * 3))
2644 return ERR_INSUFFICIENT_MEMORY;
2645 const char *p = reg_alpha;
2646 int len = reg_alpha_length;
2647 int maxlen = 15;
2648
2649 arg_struct arg;
2650 if (append) {
2651 maxlen = 14;
2652 } else if (p[0] == 0x7f || (p[0] & 128) != 0) {
2653 arg.type = ARGTYPE_NONE;
2654 store_command_after(&pc, CMD_CLA, &arg, NULL);
2655 maxlen = 14;
2656 }
2657
2658 while (len > 0) {
2659 int len2 = len;
2660 if (len2 > maxlen)
2661 len2 = maxlen;
2662 arg.type = ARGTYPE_STR;
2663 if (maxlen == 15) {
2664 arg.length = len2;
2665 memcpy(arg.val.text, p, len2);
2666 } else {
2667 arg.length = len2 + 1;
2668 arg.val.text[0] = 127;
2669 memcpy(arg.val.text + 1, p, len2);
2670 }
2671 store_command_after(&pc, CMD_STRING, &arg, NULL);
2672 p += len2;
2673 len -= len2;
2674 maxlen = 14;
2675 }
2676 return ERR_NONE;
2677 }
2678
2679 int prgm_lock(bool lock) {
2680 if (!flags.f.prgm_mode)
2681 return ERR_RESTRICTED_OPERATION;
2682 prgms[current_prgm].locked = lock;
2683 return ERR_NONE;
2684 }
2685
2686 static int pc_line_convert(int4 loc, int loc_is_pc) {
2687 int4 pc = 0;
2688 int4 line = 1;
2689 prgm_struct *prgm = prgms + current_prgm;
2690
2691 while (1) {
2692 if (loc_is_pc) {
2693 if (pc >= loc)
2694 return line;
2695 } else {
2696 if (line >= loc)
2697 return pc;
2698 }
2699 if (prgm->is_end(pc))
2700 return loc_is_pc ? line : pc;
2701 pc += get_command_length(current_prgm, pc);
2702 line++;
2703 }
2704 }
2705
2706 int4 pc2line(int4 pc) {
2707 if (pc == -1)
2708 return 0;
2709 else
2710 return pc_line_convert(pc, 1);
2711 }
2712
2713 int4 line2pc(int4 line) {
2714 if (line == 0)
2715 return -1;
2716 else
2717 return pc_line_convert(line, 0);
2718 }
2719
2720 int4 global_pc2line(int prgm, int4 pc) {
2721 int saved_prgm = current_prgm;
2722 current_prgm = prgm;
2723 int4 res = pc2line(pc);
2724 current_prgm = saved_prgm;
2725 return res;
2726 }
2727
2728 int4 global_line2pc(int prgm, int4 line) {
2729 int saved_prgm = current_prgm;
2730 current_prgm = prgm;
2731 int4 res = line2pc(line);
2732 current_prgm = saved_prgm;
2733 return res;
2734 }
2735
2736 int4 find_local_label(const arg_struct *arg) {
2737 int4 orig_pc = pc;
2738 int4 search_pc;
2739 int wrapped = 0;
2740 prgm_struct *prgm = prgms + current_prgm;
2741
2742 if (orig_pc == -1)
2743 orig_pc = 0;
2744 search_pc = orig_pc;
2745
2746 while (!wrapped || search_pc < orig_pc) {
2747 int command, argtype;
2748 if (search_pc >= prgm->size - 2) {
2749 if (orig_pc == 0)
2750 break;
2751 search_pc = 0;
2752 wrapped = 1;
2753 }
2754 command = prgm->text[search_pc];
2755 argtype = prgm->text[search_pc + 1];
2756 command |= (argtype & 112) << 4;
2757 argtype &= 15;
2758 if (command == CMD_LBL && (argtype == arg->type
2759 || argtype == ARGTYPE_STK)) {
2760 if (argtype == ARGTYPE_NUM) {
2761 int num = 0;
2762 unsigned char c;
2763 int pos = search_pc + 2;
2764 do {
2765 c = prgm->text[pos++];
2766 num = (num << 7) | (c & 127);
2767 } while ((c & 128) == 0);
2768 if (num == arg->val.num)
2769 return search_pc;
2770 } else if (argtype == ARGTYPE_STK) {
2771 // Synthetic LBL ST T etc.
2772 // Allow GTO ST T and GTO 112
2773 char stk = prgm->text[search_pc + 2];
2774 if (arg->type == ARGTYPE_STK) {
2775 if (stk = arg->val.stk)
2776 return search_pc;
2777 } else if (arg->type == ARGTYPE_NUM) {
2778 int num = 0;
2779 switch (stk) {
2780 case 'T': num = 112; break;
2781 case 'Z': num = 113; break;
2782 case 'Y': num = 114; break;
2783 case 'X': num = 115; break;
2784 case 'L': num = 116; break;
2785 }
2786 if (num == arg->val.num)
2787 return search_pc;
2788 }
2789 } else {
2790 char lclbl = prgm->text[search_pc + 2];
2791 if (lclbl == arg->val.lclbl)
2792 return search_pc;
2793 }
2794 }
2795 search_pc += get_command_length(current_prgm, search_pc);
2796 }
2797
2798 return -2;
2799 }
2800
2801 static bool find_global_label_2(const arg_struct *arg, int *prgm, int4 *pc, int *idx) {
2802 int i;
2803 const char *name = arg->val.text;
2804 int namelen = arg->length;
2805 for (i = labels_count - 1; i >= 0; i--) {
2806 int j;
2807 char *labelname;
2808 if (labels[i].length != namelen)
2809 continue;
2810 labelname = labels[i].name;
2811 for (j = 0; j < namelen; j++)
2812 if (labelname[j] != name[j])
2813 goto nomatch;
2814 if (prgm != NULL)
2815 *prgm = labels[i].prgm;
2816 if (pc != NULL)
2817 *pc = labels[i].pc;
2818 if (idx != NULL)
2819 *idx = i;
2820 return true;
2821 nomatch:;
2822 }
2823 return false;
2824 }
2825
2826 bool find_global_label(const arg_struct *arg, int *prgm, int4 *pc) {
2827 return find_global_label_2(arg, prgm, pc, NULL);
2828 }
2829
2830 bool find_global_label_index(const arg_struct *arg, int *idx) {
2831 return find_global_label_2(arg, NULL, NULL, idx);
2832 }
2833
2834 int push_rtn_addr(int prgm, int4 pc) {
2835 if (rtn_level == MAX_RTN_LEVEL)
2836 return ERR_RTN_STACK_FULL;
2837 if (rtn_level == rtn_stack_capacity) {
2838 int new_rtn_stack_capacity = rtn_stack_capacity + 16;
2839 rtn_stack_entry *new_rtn_stack = (rtn_stack_entry *) realloc(rtn_stack, new_rtn_stack_capacity * sizeof(rtn_stack_entry));
2840 if (new_rtn_stack == NULL)
2841 return ERR_INSUFFICIENT_MEMORY;
2842 rtn_stack_capacity = new_rtn_stack_capacity;
2843 rtn_stack = new_rtn_stack;
2844 }
2845 rtn_stack[rtn_level].set_prgm(prgm);
2846 rtn_stack[rtn_level].pc = pc;
2847 rtn_level++;
2848 if (prgm == -2)
2849 rtn_solve_active = true;
2850 else if (prgm == -3)
2851 rtn_integ_active = true;
2852 return ERR_NONE;
2853 }
2854
2855 int push_indexed_matrix() {
2856 if (rtn_level == 0 ? rtn_level_0_has_matrix_entry : rtn_stack[rtn_level - 1].has_matrix())
2857 return ERR_NONE;
2858 vartype_list *list = (vartype_list *) new_list(4 + matedit_stack_depth);
2859 if (list == NULL)
2860 return ERR_INSUFFICIENT_MEMORY;
2861 list->array->data[0] = new_string(matedit_name, matedit_length);
2862 list->array->data[1] = new_real(matedit_level);
2863 list->array->data[2] = new_real(matedit_i);
2864 list->array->data[3] = new_real(matedit_is_list ? -1 : matedit_j);
2865 for (int i = 0; i < matedit_stack_depth; i++)
2866 list->array->data[4 + i] = new_real(matedit_stack[i]);
2867 for (int i = 0; i < 4 + matedit_stack_depth; i++)
2868 if (list->array->data[i] == NULL) {
2869 free_vartype((vartype *) list);
2870 return ERR_INSUFFICIENT_MEMORY;
2871 }
2872 store_private_var("MAT", 3, (vartype *) list);
2873 if (rtn_level == 0)
2874 rtn_level_0_has_matrix_entry = true;
2875 else
2876 rtn_stack[rtn_level - 1].set_has_matrix(true);
2877 matedit_mode = 0;
2878 free(matedit_stack);
2879 matedit_stack = NULL;
2880 matedit_stack_depth = 0;
2881 return ERR_NONE;
2882 }
2883
2884 void maybe_pop_indexed_matrix(const char *name, int len) {
2885 if (rtn_level == 0 ? !rtn_level_0_has_matrix_entry : !rtn_stack[rtn_level - 1].has_matrix())
2886 return;
2887 if (!string_equals(matedit_name, matedit_length, name, len))
2888 return;
2889 vartype_list *list = (vartype_list *) recall_and_purge_private_var("MAT", 3);
2890 if (list == NULL)
2891 return;
2892 int newdepth = list->size - 4;
2893 int4 *newstack = newdepth == 0 ? NULL : (int4 *) malloc(newdepth * sizeof(int4));
2894 // TODO Handle memory allocation failure
2895 vartype_string *s = (vartype_string *) list->array->data[0];
2896 string_copy(matedit_name, &matedit_length, s->txt(), s->length);
2897 matedit_level = to_int4(((vartype_real *) list->array->data[1])->x);
2898 matedit_i = to_int4(((vartype_real *) list->array->data[2])->x);
2899 matedit_j = to_int4(((vartype_real *) list->array->data[3])->x);
2900 matedit_is_list = matedit_j == -1;
2901 if (matedit_is_list)
2902 matedit_j = 0;
2903 matedit_stack_depth = newdepth;
2904 free(matedit_stack);
2905 matedit_stack = newstack;
2906 for (int i = 0; i < newdepth; i++)
2907 matedit_stack[i] = to_int4(((vartype_real *) list->array->data[i + 4])->x);
2908 matedit_mode = 1;
2909 free_vartype((vartype *) list);
2910 if (rtn_level == 0)
2911 rtn_level_0_has_matrix_entry = false;
2912 else
2913 rtn_stack[rtn_level - 1].set_has_matrix(false);
2914 }
2915
2916 /* Layout of STK list used to save state for FUNC and LNSTK/L4STK:
2917 * 0: Mode: [0-4][0-4] for FUNC, or -1 for stand-alone LNSTK/L4STK;
2918 * 1: State: "ABCDE<Text>" where A=Caller Big Stack, B=LNSTK/L4STK after FUNC,
2919 * C=CSLD, D=F25, E=ERRNO, <Text>=ERRMSG
2920 * For stand-alone LNSTK/L4STK, only A is present.
2921 * 2: Saved stack. For stand-alone LNSTK/L4STK that didn't have to
2922 * change the actual stack mode, this is empty; for stand-alone
2923 * LNSTK, this is also empty; for all other cases, this is the entire stack.
2924 * 3: Saved LASTX (FUNC only)
2925 */
2926
2927 int push_func_state(int n) {
2928 if (!program_running())
2929 return ERR_RESTRICTED_OPERATION;
2930 int inputs = n / 10;
2931 if (sp + 1 < inputs)
2932 return ERR_TOO_FEW_ARGUMENTS;
2933
2934 vartype *stk = recall_private_var("STK", 3);
2935 if (stk != NULL)
2936 return ERR_INVALID_CONTEXT;
2937 if (!ensure_var_space(1))
2938 return ERR_INSUFFICIENT_MEMORY;
2939
2940 /* Create the STK list */
2941 stk = new_list(4);
2942 if (stk == NULL)
2943 return ERR_INSUFFICIENT_MEMORY;
2944 vartype_list *slist = (vartype_list *) stk;
2945 slist->array->data[0] = new_real(n);
2946 slist->array->data[1] = new_string(NULL, lasterr == -1 ? 5 + lasterr_length : 5);
2947 int i;
2948 for (i = 0; i < 2; i++)
2949 if (slist->array->data[i] == NULL) {
2950 nomem1:
2951 free_vartype(stk);
2952 return ERR_INSUFFICIENT_MEMORY;
2953 }
2954
2955 /* Create the new stack
2956 * Note that we're creating a list here, while the actual stack is just
2957 * a plain array of (vartype *). We'll swap the array in this list with
2958 * the one for the RPN stack once all allocations have been done, and then
2959 * use this list to store the old stack in the STK list.
2960 */
2961 int newdepth = flags.f.big_stack ? inputs : 4;
2962 // Allocating size 4 because the stack must always have capacity >= 4
2963 vartype_list *tlist = (vartype_list *) new_list(4);
2964 tlist->size = newdepth;
2965 if (tlist == NULL)
2966 goto nomem1;
2967 for (i = 0; i < newdepth; i++) {
2968 tlist->array->data[newdepth - 1 - i] = i < inputs ? dup_vartype(stack[sp - i]) : new_real(0);
2969 if (tlist->array->data[newdepth - 1 - i] == NULL) {
2970 nomem2:
2971 free_vartype((vartype *) tlist);
2972 goto nomem1;
2973 }
2974 }
2975
2976 vartype *newlastx = new_real(0);
2977 if (newlastx == NULL)
2978 goto nomem2;
2979
2980 /* OK, we have everything we need. Now move it all into place... */
2981 vartype_string *s = (vartype_string *) slist->array->data[1];
2982 s->txt()[0] = flags.f.big_stack ? '1' : '0';
2983 s->txt()[1] = '0';
2984 s->txt()[2] = sp != -1 && is_csld() ? '1' : '0';
2985 s->txt()[3] = flags.f.error_ignore ? '1' : '0';
2986 s->txt()[4] = (char) lasterr;
2987 if (lasterr == -1)
2988 memcpy(s->txt() + 5, lasterr_text, lasterr_length);
2989 vartype **tmpstk = tlist->array->data;
2990 int4 tmpdepth = tlist->size;
2991 tlist->array->data = stack;
2992 tlist->size = sp + 1;
2993 stack = tmpstk;
2994 stack_capacity = 4;
2995 sp = tmpdepth - 1;
2996 slist->array->data[2] = (vartype *) tlist;
2997 slist->array->data[3] = lastx;
2998 lastx = newlastx;
2999
3000 store_private_var("STK", 3, stk);
3001 flags.f.error_ignore = 0;
3002 lasterr = ERR_NONE;
3003
3004 if (rtn_level == 0)
3005 rtn_level_0_has_func_state = true;
3006 else
3007 rtn_stack[rtn_level - 1].set_has_func(true);
3008 return ERR_NONE;
3009 }
3010
3011 int push_stack_state(bool big) {
3012 vartype *stk = recall_private_var("STK", 3);
3013 if (stk != NULL) {
3014 /* LNSTK/L4STK after FUNC */
3015 vartype_list *slist = (vartype_list *) stk;
3016 vartype_string *s = (vartype_string *) slist->array->data[1];
3017 if (s->length == 1 || s->txt()[1] != '0')
3018 /* LNSTK/L4STK after LNSTK/L4STK: not allowed */
3019 return ERR_INVALID_CONTEXT;
3020 if ((bool) flags.f.big_stack == big) {
3021 /* Nothing to do */
3022 } else if (big) {
3023 /* Assuming we're being called right after FUNC, so
3024 * the stack contains the parameters, padded to depth 4
3025 * with zeros. We just remove the padding now.
3026 */
3027 vartype_real *mode = (vartype_real *) slist->array->data[0];
3028 int inputs = to_int(mode->x) / 10;
3029 int excess = 4 - inputs;
3030 if (excess > 0) {
3031 for (int i = 0; i < excess; i++)
3032 free_vartype(stack[i]);
3033 memmove(stack, stack + excess, inputs * sizeof(vartype *));
3034 sp = inputs - 1;
3035 }
3036 flags.f.big_stack = true;
3037 } else {
3038 /* Just a plain switch to 4STK using truncation to 4 levels.
3039 * FUNC has already taken care of saving the entire stack,
3040 * so there's no need to do that here.
3041 */
3042 int err = docmd_4stk(NULL);
3043 if (err != ERR_NONE)
3044 return err;
3045 }
3046 s->txt()[1] = '1';
3047 return ERR_NONE;
3048 } else {
3049 /* Stand-alone LNSTK/L4STK */
3050
3051 bool save_stk = flags.f.big_stack && !big;
3052
3053 /* Create the STK list */
3054 stk = new_list(3);
3055 if (stk == NULL)
3056 return ERR_INSUFFICIENT_MEMORY;
3057 vartype_list *slist = (vartype_list *) stk;
3058 slist->array->data[0] = new_real(-1);
3059 slist->array->data[1] = new_string(flags.f.big_stack ? "1" : "0", 1);
3060 slist->array->data[2] = NULL;
3061 int i;
3062 for (i = 0; i < 2; i++)
3063 if (slist->array->data[i] == NULL) {
3064 nomem:
3065 free_vartype(stk);
3066 return ERR_INSUFFICIENT_MEMORY;
3067 }
3068
3069 /* When switching from NSTK to 4STK, save the stack */
3070 if (save_stk) {
3071 vartype_list *tlist = (vartype_list *) new_list(4);
3072 if (tlist == NULL)
3073 goto nomem;
3074 for (i = 0; i < 4; i++) {
3075 tlist->array->data[3 - i] = i <= sp ? dup_vartype(stack[sp - i]) : new_real(0);
3076 if (tlist->array->data[3 - i] == NULL) {
3077 free_vartype((vartype *) tlist);
3078 goto nomem;
3079 }
3080 }
3081 vartype **tmpstk = tlist->array->data;
3082 int4 tmpdepth = tlist->size;
3083 tlist->array->data = stack;
3084 tlist->size = sp + 1;
3085 stack = tmpstk;
3086 stack_capacity = tmpdepth;
3087 sp = tmpdepth - 1;
3088 slist->array->data[2] = (vartype *) tlist;
3089 }
3090
3091 store_private_var("STK", 3, stk);
3092 flags.f.big_stack = big;
3093
3094 if (rtn_level == 0)
3095 rtn_level_0_has_func_state = true;
3096 else
3097 rtn_stack[rtn_level - 1].set_has_func(true);
3098 return ERR_NONE;
3099 }
3100 }
3101
3102 /* FUNC and L4STK save the stack in a list, but this has one undesirable
3103 * side effect: if the state is saved and restored, the array inside the
3104 * list will be created without any spare room. This is bad when restoring
3105 * the stack, because there is the assumption that the stack array will
3106 * always have capacity >= 4. This function makes sure the array has that
3107 * capacity.
3108 */
3109 static bool ensure_list_capacity_4(vartype_list *list) {
3110 int4 size = list->size;
3111 if (size < 4) {
3112 vartype **newdata = (vartype **) realloc(list->array->data, 4 * sizeof(vartype *));
3113 if (newdata == NULL)
3114 return false;
3115 list->array->data = newdata;
3116 }
3117 return true;
3118 }
3119
3120 int pop_func_state(bool error) {
3121 if (rtn_level == 0) {
3122 if (!rtn_level_0_has_func_state)
3123 return ERR_NONE;
3124 } else {
3125 if (!rtn_stack[rtn_level - 1].has_func())
3126 return ERR_NONE;
3127 }
3128
3129 vartype_list *stk = (vartype_list *) recall_private_var("STK", 3);
3130 if (stk == NULL)
3131 // Older FD/ST-based FUNC logic, pre-47. Too difficult to deal with, so
3132 // we punt. Note that this can only happen if a user upgrades from <47
3133 // to >=47 while execution is stopped with old FUNC data on the stack.
3134 // That seems like a reasonable scenario to ignore.
3135 return ERR_INVALID_DATA;
3136
3137 vartype **stk_data = stk->array->data;
3138 int n = to_int(((vartype_real *) stk_data[0])->x);
3139 vartype_string *state = (vartype_string *) stk_data[1];
3140 bool big = state->txt()[0] == '1';
3141 if (big && !core_settings.allow_big_stack)
3142 return ERR_BIG_STACK_DISABLED;
3143
3144 int err = ERR_NONE;
3145 if (false) {
3146 error:
3147 free_vartype(lastx);
3148 lastx = stk_data[3];
3149 stk_data[3] = NULL;
3150 goto done;
3151 }
3152
3153 if (n == -1) {
3154 // Stand-alone LNSTK/L4STK
3155 if (big && !flags.f.big_stack && stk_data[2] != NULL) {
3156 // Extend the stack back to its original size, restoring its
3157 // original contents, but only those above level 4.
3158 vartype_list *tlist = (vartype_list *) stk_data[2];
3159 if (!ensure_list_capacity_4(tlist))
3160 return ERR_INSUFFICIENT_MEMORY;
3161 while (tlist->size < 4)
3162 tlist->array->data[tlist->size++] = NULL;
3163 for (int i = 0; i < 4; i++) {
3164 free_vartype(tlist->array->data[tlist->size - 1 - i]);
3165 tlist->array->data[tlist->size - 1 - i] = stack[sp - i];
3166 stack[sp - i] = NULL;
3167 }
3168 vartype **tmpstk = stack;
3169 int tmpsize = sp + 1;
3170 stack = tlist->array->data;
3171 stack_capacity = tlist->size;
3172 sp = stack_capacity - 1;
3173 tlist->array->data = tmpstk;
3174 tlist->size = tmpsize;
3175 } else if (!big && flags.f.big_stack) {
3176 if (sp < 3) {
3177 int extra = 3 - sp;
3178 vartype *zeros[4];
3179 bool nomem = false;
3180 for (int i = 0; i < extra; i++) {
3181 zeros[i] = new_real(0);
3182 if (zeros[i] == NULL)
3183 nomem = true;
3184 }
3185 if (nomem || !ensure_stack_capacity(extra)) {
3186 for (int i = 0; i < extra; i++)
3187 free_vartype(zeros[i]);
3188 big = true; // because the switch back to 4STK has failed
3189 err = ERR_INSUFFICIENT_MEMORY;
3190 } else {
3191 memmove(stack + extra, stack, (sp + 1) * sizeof(vartype *));
3192 for (int i = 0; i < extra; i++)
3193 stack[i] = zeros[i];
3194 sp = 3;
3195 }
3196 } else if (sp > 3) {
3197 int excess = sp - 3;
3198 for (int i = 0; i < excess; i++)
3199 free_vartype(stack[i]);
3200 memmove(stack, stack + excess, 4 * sizeof(vartype *));
3201 sp = 3;
3202 }
3203 }
3204 } else {
3205 // FUNC, with or without LNSTK/L4STK
3206 vartype_list *tlist = (vartype_list *) stk_data[2];
3207 if (!ensure_list_capacity_4(tlist)) {
3208 err = ERR_INSUFFICIENT_MEMORY;
3209 goto error;
3210 }
3211
3212 vartype **tmpstk = stack;
3213 int tmpsize = sp + 1;
3214 stack = tlist->array->data;
3215 stack_capacity = tlist->size;
3216 sp = stack_capacity - 1;
3217 if (stack_capacity < 4)
3218 stack_capacity = 4;
3219 tlist->array->data = tmpstk;
3220 tlist->size = tmpsize;
3221
3222 if (error)
3223 goto error;
3224
3225 int inputs = n / 10;
3226 int outputs = n % 10;
3227 if (tmpsize < outputs) {
3228 // One could make the case that this should be an error.
3229 // I chose to be lenient and pad the result set with zeros instead.
3230 int deficit = outputs - tmpsize;
3231 memmove(tmpstk + deficit, tmpstk, tmpsize * sizeof(vartype *));
3232 bool nomem = false;
3233 for (int i = 0; i < deficit; i++) {
3234 vartype *zero = new_real(0);
3235 tmpstk[i] = zero;
3236 if (zero == NULL)
3237 nomem = true;
3238 }
3239 tmpsize += deficit;
3240 tlist->size = tmpsize;
3241 if (nomem) {
3242 err = ERR_INSUFFICIENT_MEMORY;
3243 goto error;
3244 }
3245 }
3246
3247 bool do_lastx = inputs > 0;
3248 if (n == 1 && state->txt()[2] == '1')
3249 // RCL-like function with stack lift disabled
3250 inputs = 1;
3251 int growth = outputs - inputs;
3252 if (big) {
3253 flags.f.big_stack = true;
3254 if (!ensure_stack_capacity(growth)) {
3255 err = ERR_INSUFFICIENT_MEMORY;
3256 goto error;
3257 }
3258 free_vartype(lastx);
3259 if (do_lastx) {
3260 lastx = stack[sp];
3261 stack[sp] = NULL;
3262 } else {
3263 lastx = stk_data[3];
3264 stk_data[3] = NULL;
3265 }
3266 for (int i = 0; i < inputs; i++) {
3267 free_vartype(stack[sp - i]);
3268 stack[sp - i] = NULL;
3269 }
3270 sp -= inputs;
3271 sp += outputs;
3272 for (int i = 0; i < outputs; i++) {
3273 stack[sp - i] = tmpstk[tmpsize - i - 1];
3274 tmpstk[tmpsize - i - 1] = NULL;
3275 }
3276 } else {
3277 vartype *tdups[4];
3278 int n_tdups = -growth;
3279 for (int i = 0; i < n_tdups; i++) {
3280 tdups[i] = dup_vartype(stack[0]);
3281 if (tdups[i] == NULL) {
3282 for (int j = 0; j < i; j++)
3283 free_vartype(tdups[j]);
3284 err = ERR_INSUFFICIENT_MEMORY;
3285 goto error;
3286 }
3287 }
3288 free_vartype(lastx);
3289 if (do_lastx) {
3290 lastx = stack[sp];
3291 stack[sp] = NULL;
3292 } else {
3293 lastx = stk_data[3];
3294 stk_data[3] = NULL;
3295 }
3296 for (int i = 0; i < inputs; i++) {
3297 free_vartype(stack[sp - i]);
3298 stack[sp - i] = NULL;
3299 }
3300 if (growth > 0) {
3301 for (int i = 0; i < growth; i++)
3302 free_vartype(stack[i]);
3303 memmove(stack, stack + growth, (4 - outputs) * sizeof(vartype *));
3304 } else if (growth < 0) {
3305 int shrinkage = -growth;
3306 memmove(stack + shrinkage, stack, (4 - inputs) * sizeof(vartype *));
3307 for (int i = 0; i < shrinkage; i++)
3308 stack[i] = tdups[i];
3309 }
3310 for (int i = 0; i < outputs; i++) {
3311 stack[sp - i] = tmpstk[tmpsize - i - 1];
3312 tmpstk[tmpsize - i - 1] = NULL;
3313 }
3314 }
3315
3316 flags.f.error_ignore = state->txt()[3] == '1';
3317 lasterr = (signed char) state->txt()[4];
3318 if (lasterr == -1) {
3319 lasterr_length = state->length - 5;
3320 memcpy(lasterr_text, state->txt() + 5, lasterr_length);
3321 }
3322 }
3323
3324 done:
3325 if (rtn_level == 0)
3326 rtn_level_0_has_func_state = false;
3327 else
3328 rtn_stack[rtn_level - 1].set_has_func(false);
3329
3330 flags.f.big_stack = big;
3331 print_trace();
3332 return err;
3333 }
3334
3335 void step_out() {
3336 if (rtn_level > 0)
3337 rtn_stop_level = rtn_level - 1;
3338 }
3339
3340 void step_over() {
3341 if (rtn_level >= 0)
3342 rtn_stop_level = rtn_level;
3343 }
3344
3345 bool should_i_stop_at_this_level() {
3346 bool stop = rtn_stop_level >= rtn_level;
3347 if (stop)
3348 rtn_stop_level = -1;
3349 return stop;
3350 }
3351
3352 static void remove_locals() {
3353 if (matedit_mode == 3 && matedit_level >= rtn_level)
3354 leave_matrix_editor();
3355 int last = -1;
3356 for (int i = vars_count - 1; i >= 0; i--) {
3357 if (vars[i].level == -1)
3358 continue;
3359 if (vars[i].level < rtn_level)
3360 break;
3361 if ((matedit_mode == 1 || matedit_mode == 3)
3362 && vars[i].level == matedit_level
3363 && string_equals(vars[i].name, vars[i].length, matedit_name, matedit_length)) {
3364 if (matedit_mode == 3) {
3365 set_appmenu_exitcallback(0);
3366 set_menu(MENULEVEL_APP, MENU_NONE);
3367 }
3368 matedit_mode = 0;
3369 free(matedit_stack);
3370 matedit_stack = NULL;
3371 matedit_stack_depth = 0;
3372 }
3373 if ((vars[i].flags & VAR_HIDING) != 0) {
3374 for (int j = i - 1; j >= 0; j--)
3375 if ((vars[j].flags & VAR_HIDDEN) != 0 && string_equals(vars[i].name, vars[i].length, vars[j].name, vars[j].length)) {
3376 vars[j].flags &= ~VAR_HIDDEN;
3377 break;
3378 }
3379 }
3380 free_vartype(vars[i].value);
3381 vars[i].length = 100;
3382 last = i;
3383 }
3384 if (last == -1)
3385 return;
3386 int from = last;
3387 int to = last;
3388 while (from < vars_count) {
3389 if (vars[from].length != 100)
3390 vars[to++] = vars[from];
3391 from++;
3392 }
3393 vars_count -= from - to;
3394 update_catalog();
3395 }
3396
3397 int rtn(int err) {
3398 // NOTE: 'err' should be one of ERR_NONE, ERR_YES, or ERR_NO.
3399 // For any actual *error*, i.e. anything that should actually
3400 // stop program execution, use rtn_with_error() instead.
3401 int newprgm;
3402 int4 newpc;
3403 bool stop;
3404 pop_rtn_addr(&newprgm, &newpc, &stop);
3405 if (newprgm == -3) {
3406 return return_to_integ(stop);
3407 } else if (newprgm == -2) {
3408 return return_to_solve(0, stop);
3409 } else if (newprgm == -1) {
3410 if (pc >= prgms[current_prgm].size)
3411 /* It's an END; go to line 0 */
3412 pc = -1;
3413 if (err != ERR_NONE)
3414 display_error(err);
3415 return ERR_STOP;
3416 } else {
3417 current_prgm = newprgm;
3418 pc = newpc;
3419 if (err == ERR_NO) {
3420 int command;
3421 arg_struct arg;
3422 get_next_command(&pc, &command, &arg, 0, NULL);
3423 if (command == CMD_END)
3424 pc = newpc;
3425 }
3426 return stop ? ERR_STOP : ERR_NONE;
3427 }
3428 }
3429
3430 int rtn_with_error(int err) {
3431 bool stop;
3432 if (solve_active() && (err == ERR_OUT_OF_RANGE
3433 || err == ERR_DIVIDE_BY_0
3434 || err == ERR_INVALID_DATA
3435 || err == ERR_STAT_MATH_ERROR)) {
3436 stop = unwind_stack_until_solve();
3437 return return_to_solve(1, stop);
3438 }
3439 int newprgm;
3440 int4 newpc;
3441 pop_rtn_addr(&newprgm, &newpc, &stop);
3442 if (newprgm >= 0) {
3443 // Stop on the calling XEQ, not the RTNERR
3444 current_prgm = newprgm;
3445 int line = pc2line(newpc);
3446 set_old_pc(line2pc(line - 1));
3447 }
3448 return err;
3449 }
3450
3451 void pop_rtn_addr(int *prgm, int4 *pc, bool *stop) {
3452 if (rtn_level == 0 ? rtn_level_0_has_matrix_entry : rtn_stack[rtn_level - 1].has_matrix()) {
3453 vartype_list *list = (vartype_list *) recall_and_purge_private_var("MAT", 3);
3454 if (list != NULL) {
3455 int newdepth = list->size - 4;
3456 int4 *newstack = newdepth == 0 ? NULL : (int4 *) malloc(newdepth * sizeof(vartype *));
3457 // TODO: Handle memory allocation failure
3458 vartype_string *s = (vartype_string *) list->array->data[0];
3459 string_copy(matedit_name, &matedit_length, s->txt(), s->length);
3460 matedit_level = to_int4(((vartype_real *) list->array->data[1])->x);
3461 matedit_i = to_int4(((vartype_real *) list->array->data[2])->x);
3462 matedit_j = to_int4(((vartype_real *) list->array->data[3])->x);
3463 matedit_is_list = matedit_j == -1;
3464 if (matedit_is_list)
3465 matedit_j = 0;
3466 matedit_stack_depth = newdepth;
3467 free(matedit_stack);
3468 matedit_stack = newstack;
3469 for (int i = 0; i < matedit_stack_depth; i++)
3470 matedit_stack[i] = to_int4(((vartype_real *) list->array->data[i + 4])->x);
3471 matedit_mode = 1;
3472 free_vartype((vartype *) list);
3473 }
3474 if (rtn_level == 0)
3475 rtn_level_0_has_matrix_entry = false;
3476 else
3477 rtn_stack[rtn_level - 1].set_has_matrix(false);
3478 }
3479 remove_locals();
3480 if (rtn_level == 0) {
3481 *prgm = -1;
3482 *pc = -1;
3483 rtn_stop_level = -1;
3484 rtn_level_0_has_func_state = false;
3485 } else {
3486 rtn_level--;
3487 *prgm = rtn_stack[rtn_level].get_prgm();
3488 *pc = rtn_stack[rtn_level].pc;
3489 if (rtn_stop_level >= rtn_level) {
3490 *stop = true;
3491 rtn_stop_level = -1;
3492 } else
3493 *stop = false;
3494 if (*prgm == -2)
3495 rtn_solve_active = false;
3496 else if (*prgm == -3)
3497 rtn_integ_active = false;
3498 }
3499 }
3500
3501 static void get_saved_stack_mode(int *m) {
3502 if (rtn_level == 0) {
3503 if (!rtn_level_0_has_func_state)
3504 return;
3505 } else {
3506 if (!rtn_stack[rtn_level - 1].has_func())
3507 return;
3508 }
3509 vartype_list *stk = (vartype_list *) recall_private_var("STK", 3);
3510 if (stk == NULL)
3511 return;
3512 vartype **stk_data = stk->array->data;
3513 *m = ((vartype_string *) stk_data[1])->txt()[0] == '1';
3514 }
3515
3516 void clear_all_rtns() {
3517 int dummy1;
3518 int4 dummy2;
3519 bool dummy3;
3520 int st_mode = -1;
3521 while (rtn_level > 0) {
3522 get_saved_stack_mode(&st_mode);
3523 pop_rtn_addr(&dummy1, &dummy2, &dummy3);
3524 }
3525 get_saved_stack_mode(&st_mode);
3526 pop_rtn_addr(&dummy1, &dummy2, &dummy3);
3527 if (st_mode == 0) {
3528 arg_struct dummy_arg;
3529 docmd_4stk(&dummy_arg);
3530 } else if (st_mode == 1) {
3531 docmd_nstk(NULL);
3532 }
3533 if (mode_plainmenu == MENU_PROGRAMMABLE)
3534 set_menu(MENULEVEL_PLAIN, MENU_NONE);
3535 if (varmenu_role == 3)
3536 varmenu_role = 0;
3537 }
3538
3539 int get_rtn_level() {
3540 return rtn_level;
3541 }
3542
3543 void save_csld() {
3544 if (rtn_level == 0)
3545 mode_caller_stack_lift_disabled = flags.f.stack_lift_disable;
3546 else
3547 rtn_stack[rtn_level - 1].set_csld();
3548 }
3549
3550 bool is_csld() {
3551 if (rtn_level == 0)
3552 return mode_caller_stack_lift_disabled;
3553 else
3554 return rtn_stack[rtn_level - 1].is_csld();
3555 }
3556
3557 bool solve_active() {
3558 return rtn_solve_active;
3559 }
3560
3561 bool integ_active() {
3562 return rtn_integ_active;
3563 }
3564
3565 bool unwind_stack_until_solve() {
3566 int prgm;
3567 int4 pc;
3568 bool stop;
3569 int st_mode = -1;
3570 do {
3571 get_saved_stack_mode(&st_mode);
3572 pop_rtn_addr(&prgm, &pc, &stop);
3573 } while (prgm != -2);
3574 if (st_mode == 0) {
3575 arg_struct dummy_arg;
3576 docmd_4stk(&dummy_arg);
3577 } else if (st_mode == 1) {
3578 docmd_nstk(NULL);
3579 }
3580 return stop;
3581 }
3582
3583 bool read_bool(bool *b) {
3584 return read_char((char *) b);
3585 }
3586
3587 bool write_bool(bool b) {
3588 return fputc((char) b, gfile) != EOF;
3589 }
3590
3591 bool read_char(char *c) {
3592 int i = fgetc(gfile);
3593 *c = (char) i;
3594 return i != EOF;
3595 }
3596
3597 bool write_char(char c) {
3598 return fputc(c, gfile) != EOF;
3599 }
3600
3601 bool read_int(int *n) {
3602 int4 m;
3603 if (!read_int4(&m))
3604 return false;
3605 *n = (int) m;
3606 return true;
3607 }
3608
3609 bool write_int(int n) {
3610 return write_int4(n);
3611 }
3612
3613 bool read_int2(int2 *n) {
3614 #ifdef F42_BIG_ENDIAN
3615 char buf[2];
3616 if (fread(buf, 1, 2, gfile) != 2)
3617 return false;
3618 char *dst = (char *) n;
3619 for (int i = 0; i < 2; i++)
3620 dst[i] = buf[1 - i];
3621 return true;
3622 #else
3623 return fread(n, 1, 2, gfile) == 2;
3624 #endif
3625 }
3626
3627 bool write_int2(int2 n) {
3628 #ifdef F42_BIG_ENDIAN
3629 char buf[2];
3630 char *src = (char *) &n;
3631 for (int i = 0; i < 2; i++)
3632 buf[i] = src[1 - i];
3633 return fwrite(buf, 1, 2, gfile) == 2;
3634 #else
3635 return fwrite(&n, 1, 2, gfile) == 2;
3636 #endif
3637 }
3638
3639 bool read_int4(int4 *n) {
3640 #ifdef F42_BIG_ENDIAN
3641 char buf[4];
3642 if (fread(buf, 1, 4, gfile) != 4)
3643 return false;
3644 char *dst = (char *) n;
3645 for (int i = 0; i < 4; i++)
3646 dst[i] = buf[3 - i];
3647 return true;
3648 #else
3649 return fread(n, 1, 4, gfile) == 4;
3650 #endif
3651 }
3652
3653 bool write_int4(int4 n) {
3654 #ifdef F42_BIG_ENDIAN
3655 char buf[4];
3656 char *src = (char *) &n;
3657 for (int i = 0; i < 4; i++)
3658 buf[i] = src[3 - i];
3659 return fwrite(buf, 1, 4, gfile) == 4;
3660 #else
3661 return fwrite(&n, 1, 4, gfile) == 4;
3662 #endif
3663 }
3664
3665 bool read_int8(int8 *n) {
3666 #ifdef F42_BIG_ENDIAN
3667 char buf[8];
3668 if (fread(buf, 1, 8, gfile) != 8)
3669 return false;
3670 char *dst = (char *) n;
3671 for (int i = 0; i < 8; i++)
3672 dst[i] = buf[7 - i];
3673 return true;
3674 #else
3675 return fread(n, 1, 8, gfile) == 8;
3676 #endif
3677 }
3678
3679 bool write_int8(int8 n) {
3680 #ifdef F42_BIG_ENDIAN
3681 char buf[8];
3682 char *src = (char *) &n;
3683 for (int i = 0; i < 8; i++)
3684 buf[i] = src[7 - i];
3685 return fwrite(buf, 1, 8, gfile) == 8;
3686 #else
3687 return fwrite(&n, 1, 8, gfile) == 8;
3688 #endif
3689 }
3690
3691 bool read_phloat(phloat *d) {
3692 if (bin_dec_mode_switch()) {
3693 #ifdef F42_BIG_ENDIAN
3694 #ifdef BCD_MATH
3695 char buf[8];
3696 if (fread(buf, 1, 8, gfile) != 8)
3697 return false;
3698 double dbl;
3699 char *dst = (char *) &dbl;
3700 for (int i = 0; i < 8; i++)
3701 dst[i] = buf[7 - i];
3702 d->assign17digits(dbl);
3703 return true;
3704 #else
3705 char buf[16], data[16];
3706 if (fread(buf, 1, 16, gfile) != 16)
3707 return false;
3708 for (int i = 0; i < 16; i++)
3709 data[i] = buf[15 - i];
3710 *d = decimal2double(data);
3711 return true;
3712 #endif
3713 #else
3714 #ifdef BCD_MATH
3715 double dbl;
3716 if (fread(&dbl, 1, 8, gfile) != 8)
3717 return false;
3718 d->assign17digits(dbl);
3719 return true;
3720 #else
3721 char data[16];
3722 if (fread(data, 1, 16, gfile) != 16)
3723 return false;
3724 *d = decimal2double(data);
3725 return true;
3726 #endif
3727 #endif
3728 } else {
3729 #ifdef F42_BIG_ENDIAN
3730 #ifdef BCD_MATH
3731 char buf[16];
3732 if (fread(buf, 1, 16, gfile) != 16)
3733 return false;
3734 char *dst = (char *) d;
3735 for (int i = 0; i < 16; i++)
3736 dst[i] = buf[15 - i];
3737 return true;
3738 #else
3739 char buf[8];
3740 if (fread(buf, 1, 8, gfile) != 8)
3741 return false;
3742 char *dst = (char *) d;
3743 for (int i = 0; i < 8; i++)
3744 dst[i] = buf[7 - i];
3745 return true;
3746 #endif
3747 #else
3748 if (fread(d, 1, sizeof(phloat), gfile) != sizeof(phloat))
3749 return false;
3750 return true;
3751 #endif
3752 }
3753 }
3754
3755 bool write_phloat(phloat d) {
3756 #ifdef F42_BIG_ENDIAN
3757 #ifdef BCD_MATH
3758 char buf[16];
3759 char *src = (char *) &d;
3760 for (int i = 0; i < 16; i++)
3761 buf[i] = src[15 - i];
3762 return fwrite(buf, 1, 16, gfile) == 16;
3763 #else
3764 char buf[8];
3765 char *src = (char *) &d;
3766 for (int i = 0; i < 8; i++)
3767 buf[i] = src[7 - i];
3768 return fwrite(buf, 1, 8, gfile) == 8;
3769 #endif
3770 #else
3771 return fwrite(&d, 1, sizeof(phloat), gfile) == sizeof(phloat);
3772 #endif
3773 }
3774
3775 bool read_arg(arg_struct *arg) {
3776 if (!read_char((char *) &arg->type))
3777 return false;
3778 char c;
3779 switch (arg->type) {
3780 case ARGTYPE_NONE:
3781 return true;
3782 case ARGTYPE_NUM:
3783 case ARGTYPE_NEG_NUM:
3784 case ARGTYPE_IND_NUM:
3785 case ARGTYPE_LBLINDEX:
3786 return read_int4(&arg->val.num);
3787 case ARGTYPE_STK:
3788 case ARGTYPE_IND_STK:
3789 return read_char(&arg->val.stk);
3790 case ARGTYPE_STR:
3791 case ARGTYPE_IND_STR:
3792 // Serializing 'length' as a char for backward compatibility.
3793 // Values > 255 only happen for XSTR, and those are never
3794 // serialized.
3795 if (!read_char(&c))
3796 return false;
3797 arg->length = c & 255;
3798 return fread(arg->val.text, 1, arg->length, gfile) == arg->length;
3799 case ARGTYPE_COMMAND:
3800 return read_int(&arg->val.cmd);
3801 case ARGTYPE_LCLBL:
3802 return read_char(&arg->val.lclbl);
3803 case ARGTYPE_DOUBLE:
3804 return read_phloat(&arg->val_d);
3805 default:
3806 // Should never happen
3807 return false;
3808 }
3809 }
3810
3811 bool write_arg(const arg_struct *arg) {
3812 int type = arg->type;
3813 if (type == ARGTYPE_XSTR)
3814 // This type is always used immediately, so no need to persist it;
3815 // also, persisting it would be difficult, since this variant uses
3816 // a pointer to the actual text, which is context-dependent and
3817 // would be impossible to restore.
3818 type = ARGTYPE_NONE;
3819
3820 if (!write_char(type))
3821 return false;
3822 switch (type) {
3823 case ARGTYPE_NONE:
3824 return true;
3825 case ARGTYPE_NUM:
3826 case ARGTYPE_NEG_NUM:
3827 case ARGTYPE_IND_NUM:
3828 case ARGTYPE_LBLINDEX:
3829 return write_int4(arg->val.num);
3830 case ARGTYPE_STK:
3831 case ARGTYPE_IND_STK:
3832 return write_char(arg->val.stk);
3833 case ARGTYPE_STR:
3834 case ARGTYPE_IND_STR:
3835 return write_char((char) arg->length)
3836 && fwrite(arg->val.text, 1, arg->length, gfile) == arg->length;
3837 case ARGTYPE_COMMAND:
3838 return write_int(arg->val.cmd);
3839 case ARGTYPE_LCLBL:
3840 return write_char(arg->val.lclbl);
3841 case ARGTYPE_DOUBLE:
3842 return write_phloat(arg->val_d);
3843 default:
3844 // Should never happen
3845 return false;
3846 }
3847 }
3848
3849 static void menu_adjust(int from1, int to1, int offset1,
3850 int from2 = INT_MIN, int to2 = INT_MIN, int offset2 = INT_MIN,
3851 int from3 = INT_MIN, int to3 = INT_MIN, int offset3 = INT_MIN) {
3852 static int *menuptr[] = { &mode_appmenu, &mode_plainmenu, &mode_transientmenu, &mode_alphamenu, &mode_commandmenu };
3853 for (int i = 0; i < 5; i++) {
3854 int *menu = menuptr[i];
3855 if (*menu >= from1 && *menu <= to1)
3856 *menu += offset1;
3857 else if (*menu >= from2 && *menu <= to2)
3858 *menu += offset2;
3859 else if (*menu >= from3 && *menu <= to3)
3860 *menu += offset3;
3861 }
3862 }
3863
3864 static bool load_state2(bool *clear, bool *too_new) {
3865 int4 magic;
3866 int4 version;
3867 *clear = false;
3868 *too_new = false;
3869
3870 /* The shell has verified the initial magic and version numbers,
3871 * and loaded the shell state, before we got called.
3872 */
3873
3874 if (!read_int4(&magic))
3875 return false;
3876 if (magic != FREE42_MAGIC)
3877 return false;
3878 if (!read_int4(&ver)) {
3879 // A state file containing nothing after the magic number
3880 // is considered empty, and results in a hard reset. This
3881 // is *not* an error condition; such state files are used
3882 // when creating a new state in the States window.
3883 *clear = true;
3884 return false;
3885 }
3886
3887 if (ver > FREE42_VERSION) {
3888 *too_new = true;
3889 return false;
3890 }
3891 if (ver < 26)
3892 // Pre-2.5, non-portable state. Not supported any more.
3893 return false;
3894
3895 if (bug_mode == 0 && ver == 26)
3896 bug_mode = 1;
3897
3898 if (ver >= 28) {
3899 // Embedded version information. No need to read this; it's just
3900 // there for troubleshooting purposes. All we need to do here is
3901 // skip it.
3902 while (true) {
3903 char c;
3904 if (!read_char(&c))
3905 return false;
3906 if (c == 0)
3907 break;
3908 }
3909 }
3910
3911 bool state_is_decimal;
3912 if (!read_bool(&state_is_decimal)) return false;
3913 if (!state_is_decimal)
3914 state_file_number_format = NUMBER_FORMAT_BINARY;
3915 else
3916 state_file_number_format = NUMBER_FORMAT_BID128;
3917
3918 bool bdummy;
3919 if (!read_bool(&bdummy)) return false;
3920 if (!read_bool(&bdummy)) return false;
3921 if (!read_bool(&bdummy)) return false;
3922
3923 if (!read_bool(&mode_clall)) return false;
3924 if (!read_bool(&mode_command_entry)) return false;
3925 if (!read_char(&mode_number_entry)) return false;
3926 if (!read_bool(&mode_alpha_entry)) return false;
3927 if (!read_bool(&mode_shift)) return false;
3928 if (!read_int(&mode_appmenu)) return false;
3929 if (!read_int(&mode_plainmenu)) return false;
3930 if (!read_bool(&mode_plainmenu_sticky)) return false;
3931 if (!read_int(&mode_transientmenu)) return false;
3932 if (!read_int(&mode_alphamenu)) return false;
3933 if (!read_int(&mode_commandmenu)) return false;
3934 if (ver < 33)
3935 menu_adjust(24, INT_MAX, 2);
3936 if (ver < 50)
3937 menu_adjust(24, 24, 37, 25, 61, -1);
3938 if (ver < 51)
3939 menu_adjust(61, 61, 7, 62, 63, 3, 64, INT_MAX, 7);
3940 if (ver < 53)
3941 menu_adjust(26, INT_MAX, 1);
3942 if (ver < 54)
3943 menu_adjust(55, INT_MAX, 3);
3944 if (!read_bool(&mode_running)) return false;
3945 if (ver < 46)
3946 mode_caller_stack_lift_disabled = false;
3947 else if (!read_bool(&mode_caller_stack_lift_disabled))
3948 return false;
3949 if (!read_bool(&mode_varmenu)) return false;
3950 if (!read_bool(&mode_updown)) return false;
3951
3952 if (!read_bool(&mode_getkey))
3953 return false;
3954
3955 if (!read_phloat(&entered_number)) return false;
3956 if (!read_int(&entered_string_length)) return false;
3957 if (fread(entered_string, 1, 15, gfile) != 15) return false;
3958
3959 if (!read_int(&pending_command)) return false;
3960 if (!read_arg(&pending_command_arg)) return false;
3961 if (!read_int(&xeq_invisible)) return false;
3962
3963 if (!read_int(&incomplete_command)) return false;
3964 if (ver < 35) {
3965 int temp;
3966 if (!read_int(&temp)) return false;
3967 incomplete_ind = temp != 0;
3968 if (!read_int(&temp)) return false;
3969 incomplete_alpha = temp != 0;
3970 } else {
3971 if (!read_bool(&incomplete_ind)) return false;
3972 if (!read_bool(&incomplete_alpha)) return false;
3973 }
3974 if (!read_int(&incomplete_length)) return false;
3975 if (!read_int(&incomplete_maxdigits)) return false;
3976 if (!read_int(&incomplete_argtype)) return false;
3977 if (!read_int(&incomplete_num)) return false;
3978 int isl = ver < 40 ? 7 : 22;
3979 if (fread(incomplete_str, 1, isl, gfile) != isl) return false;
3980 if (!read_int4(&incomplete_saved_pc)) return false;
3981 if (!read_int4(&incomplete_saved_highlight_row)) return false;
3982
3983 if (fread(cmdline, 1, 100, gfile) != 100) return false;
3984 if (!read_int(&cmdline_length)) return false;
3985 if (!read_int(&cmdline_row)) return false;
3986
3987 if (!read_int(&matedit_mode)) return false;
3988 if (ver < 47)
3989 matedit_level = -2; // This is handled later in this function
3990 else
3991 if (!read_int(&matedit_level)) return false;
3992 if (fread(matedit_name, 1, 7, gfile) != 7) return false;
3993 if (!read_int(&matedit_length)) return false;
3994 if (!unpersist_vartype(&matedit_x)) return false;
3995 if (!read_int4(&matedit_i)) return false;
3996 if (!read_int4(&matedit_j)) return false;
3997 if (!read_int(&matedit_prev_appmenu)) return false;
3998 if (ver < 48) {
3999 matedit_stack = NULL;
4000 matedit_stack_depth = 0;
4001 matedit_is_list = false;
4002 } else {
4003 if (!read_int(&matedit_stack_depth)) return false;
4004 if (matedit_stack_depth == 0) {
4005 matedit_stack = NULL;
4006 } else {
4007 matedit_stack = (int4 *) malloc(matedit_stack_depth * sizeof(int4));
4008 if (matedit_stack == NULL) {
4009 matedit_stack_depth = 0;
4010 return false;
4011 }
4012 for (int i = 0; i < matedit_stack_depth; i++)
4013 if (!read_int4(matedit_stack + i)) {
4014 free(matedit_stack);
4015 matedit_stack = NULL;
4016 matedit_stack_depth = 0;
4017 return false;
4018 }
4019 }
4020 if (!read_bool(&matedit_is_list)) return false;
4021 }
4022
4023 if (fread(input_name, 1, 11, gfile) != 11) return false;
4024 if (!read_int(&input_length)) return false;
4025 if (!read_arg(&input_arg)) return false;
4026
4027 if (ver < 39) {
4028 lasterr = 0;
4029 } else {
4030 if (!read_int(&lasterr)) return false;
4031 if (!read_int(&lasterr_length)) return false;
4032 if (fread(lasterr_text, 1, 22, gfile) != 22) return false;
4033 }
4034
4035 if (!read_int(&baseapp)) return false;
4036
4037 if (!read_int8(&random_number_low)) return false;
4038 if (!read_int8(&random_number_high)) return false;
4039
4040 if (!read_int(&deferred_print)) return false;
4041
4042 if (!read_int(&keybuf_head)) return false;
4043 if (!read_int(&keybuf_tail)) return false;
4044 for (int i = 0; i < 16; i++)
4045 if (!read_int(&keybuf[i]))
4046 return false;
4047
4048 if (!unpersist_display(ver))
4049 return false;
4050 if (!unpersist_globals())
4051 return false;
4052
4053 /* When loading older states, figure out matedit_level.
4054 * This is needed for EDITN and INDEX to keep working across
4055 * upgrades. Note that we're not dealing with saved matrix
4056 * info on the RTN stack, though, so this provides only
4057 * partial compatibility, covering only the most common
4058 * scenarios.
4059 */
4060 if (matedit_level == -2 && (matedit_mode == 1 || matedit_mode == 3)) {
4061 matedit_level = -1;
4062 for (int i = vars_count - 1; i >= 0; i--) {
4063 var_struct *vs = vars + i;
4064 if ((vs->flags & (VAR_HIDDEN | VAR_PRIVATE)) != 0)
4065 continue;
4066 if (string_equals(matedit_name, matedit_length, vs->name, vs->length)) {
4067 matedit_level = vs->level;
4068 break;
4069 }
4070 }
4071 }
4072
4073 if (!unpersist_math(ver))
4074 return false;
4075
4076 if (!read_int4(&magic)) return false;
4077 if (magic != FREE42_MAGIC)
4078 return false;
4079 if (!read_int4(&version)) return false;
4080 if (version != ver)
4081 return false;
4082
4083 return true;
4084 }
4085
4086 // See the comment for bug_mode at its declaration...
4087
4088 bool load_state(int4 ver_p, bool *clear, bool *too_new) {
4089 bug_mode = 0;
4090 ver = ver_p;
4091 long fpos = ftell(gfile);
4092 if (load_state2(clear, too_new))
4093 return true;
4094 if (bug_mode != 3)
4095 return false;
4096 // bug_mode == 3 is the signal that the file looks screwy
4097 // in the way caused by the buggy string-in-matrix writing
4098 // in version 2.5
4099 core_cleanup();
4100 fseek(gfile, fpos, SEEK_SET);
4101 bug_mode = 2;
4102 return load_state2(clear, too_new);
4103 }
4104
4105 void save_state(bool *success) {
4106 *success = false;
4107 if (!write_int4(FREE42_MAGIC) || !write_int4(FREE42_VERSION))
4108 return;
4109
4110 // Write app version and platform, for troubleshooting purposes
4111 const char *platform = shell_platform();
4112 char c;
4113 do {
4114 c = *platform++;
4115 write_char(c);
4116 } while (c != 0);
4117
4118 #ifdef BCD_MATH
4119 if (!write_bool(true)) return;
4120 #else
4121 if (!write_bool(false)) return;
4122 #endif
4123 if (!write_bool(core_settings.matrix_singularmatrix)) return;
4124 if (!write_bool(core_settings.matrix_outofrange)) return;
4125 if (!write_bool(core_settings.auto_repeat)) return;
4126 if (!write_bool(mode_clall)) return;
4127 if (!write_bool(mode_command_entry)) return;
4128 if (!write_char(mode_number_entry)) return;
4129 if (!write_bool(mode_alpha_entry)) return;
4130 if (!write_bool(mode_shift)) return;
4131 if (!write_int(mode_appmenu)) return;
4132 if (!write_int(mode_plainmenu)) return;
4133 if (!write_bool(mode_plainmenu_sticky)) return;
4134 if (!write_int(mode_transientmenu)) return;
4135 if (!write_int(mode_alphamenu)) return;
4136 if (!write_int(mode_commandmenu)) return;
4137 if (!write_bool(mode_running)) return;
4138 if (!write_bool(mode_caller_stack_lift_disabled)) return;
4139 if (!write_bool(mode_varmenu)) return;
4140 if (!write_bool(mode_updown)) return;
4141 if (!write_bool(mode_getkey)) return;
4142
4143 if (!write_phloat(entered_number)) return;
4144 if (!write_int(entered_string_length)) return;
4145 if (fwrite(entered_string, 1, 15, gfile) != 15) return;
4146
4147 if (!write_int(pending_command)) return;
4148 if (!write_arg(&pending_command_arg)) return;
4149 if (!write_int(xeq_invisible)) return;
4150
4151 if (!write_int(incomplete_command)) return;
4152 if (!write_bool(incomplete_ind)) return;
4153 if (!write_bool(incomplete_alpha)) return;
4154 if (!write_int(incomplete_length)) return;
4155 if (!write_int(incomplete_maxdigits)) return;
4156 if (!write_int(incomplete_argtype)) return;
4157 if (!write_int(incomplete_num)) return;
4158 if (fwrite(incomplete_str, 1, 22, gfile) != 22) return;
4159 if (!write_int4(pc2line(incomplete_saved_pc))) return;
4160 if (!write_int4(incomplete_saved_highlight_row)) return;
4161
4162 if (fwrite(cmdline, 1, 100, gfile) != 100) return;
4163 if (!write_int(cmdline_length)) return;
4164 if (!write_int(cmdline_row)) return;
4165
4166 if (!write_int(matedit_mode)) return;
4167 if (!write_int(matedit_level)) return;
4168 if (fwrite(matedit_name, 1, 7, gfile) != 7) return;
4169 if (!write_int(matedit_length)) return;
4170 if (!persist_vartype(matedit_x)) return;
4171 if (!write_int4(matedit_i)) return;
4172 if (!write_int4(matedit_j)) return;
4173 if (!write_int(matedit_prev_appmenu)) return;
4174 if (!write_int(matedit_stack_depth)) return;
4175 for (int i = 0; i < matedit_stack_depth; i++)
4176 if (!write_int4(matedit_stack[i])) return;
4177 if (!write_bool(matedit_is_list)) return;
4178
4179 if (fwrite(input_name, 1, 11, gfile) != 11) return;
4180 if (!write_int(input_length)) return;
4181 if (!write_arg(&input_arg)) return;
4182
4183 if (!write_int(lasterr)) return;
4184 if (!write_int(lasterr_length)) return;
4185 if (fwrite(lasterr_text, 1, 22, gfile) != 22) return;
4186
4187 if (!write_int(baseapp)) return;
4188
4189 if (!write_int8(random_number_low)) return;
4190 if (!write_int8(random_number_high)) return;
4191
4192 if (!write_int(deferred_print)) return;
4193
4194 if (!write_int(keybuf_head)) return;
4195 if (!write_int(keybuf_tail)) return;
4196 for (int i = 0; i < 16; i++)
4197 if (!write_int(keybuf[i]))
4198 return;
4199
4200 if (!persist_display())
4201 return;
4202 if (!persist_globals())
4203 return;
4204 if (!persist_math())
4205 return;
4206
4207 if (!write_int4(FREE42_MAGIC)) return;
4208 if (!write_int4(FREE42_VERSION)) return;
4209 *success = true;
4210 }
4211
4212 // Reason:
4213 // 0 = Memory Clear
4214 // 1 = State File Corrupt
4215 // 2 = State File Too New
4216 void hard_reset(int reason) {
4217 vartype *regs;
4218
4219 /* Clear stack */
4220 for (int i = 0; i <= sp; i++)
4221 free_vartype(stack[i]);
4222 free(stack);
4223 free_vartype(lastx);
4224 sp = 3;
4225 stack_capacity = 4;
4226 stack = (vartype **) malloc(stack_capacity * sizeof(vartype *));
4227 for (int i = 0; i <= sp; i++)
4228 stack[i] = new_real(0);
4229 lastx = new_real(0);
4230
4231 /* Clear alpha */
4232 reg_alpha_length = 0;
4233
4234 /* Clear variables */
4235 purge_all_vars();
4236 regs = new_realmatrix(25, 1);
4237 store_var("REGS", 4, regs);
4238
4239 /* Clear RTN stack */
4240 if (rtn_stack != NULL)
4241 free(rtn_stack);
4242 rtn_stack_capacity = 16;
4243 rtn_stack = (rtn_stack_entry *) malloc(rtn_stack_capacity * sizeof(rtn_stack_entry));
4244 rtn_level = 0;
4245 rtn_stop_level = -1;
4246 rtn_solve_active = false;
4247 rtn_integ_active = false;
4248
4249 /* Clear programs */
4250 if (prgms != NULL) {
4251 free(prgms);
4252 prgms = NULL;
4253 prgms_capacity = 0;
4254 prgms_count = 0;
4255 }
4256 if (labels != NULL) {
4257 free(labels);
4258 labels = NULL;
4259 labels_capacity = 0;
4260 labels_count = 0;
4261 }
4262 goto_dot_dot(false);
4263
4264 pending_command = CMD_NONE;
4265
4266 matedit_mode = 0;
4267 matedit_stack_depth = 0;
4268 free(matedit_stack);
4269 matedit_stack = NULL;
4270 input_length = 0;
4271 baseapp = 0;
4272 random_number_low = 0;
4273 random_number_high = 0;
4274
4275 flags.f.f00 = flags.f.f01 = flags.f.f02 = flags.f.f03 = flags.f.f04 = 0;
4276 flags.f.f05 = flags.f.f06 = flags.f.f07 = flags.f.f08 = flags.f.f09 = 0;
4277 flags.f.f10 = 0;
4278 flags.f.auto_exec = 0;
4279 flags.f.double_wide_print = 0;
4280 flags.f.lowercase_print = 0;
4281 flags.f.f14 = 0;
4282 flags.f.trace_print = 0;
4283 flags.f.normal_print = 0;
4284 flags.f.f17 = flags.f.f18 = flags.f.f19 = flags.f.f20 = 0;
4285 flags.f.printer_enable = 0;
4286 flags.f.numeric_data_input = 0;
4287 flags.f.alpha_data_input = 0;
4288 flags.f.range_error_ignore = 0;
4289 flags.f.error_ignore = 0;
4290 flags.f.audio_enable = 1;
4291 /* flags.f.VIRTUAL_custom_menu = 0; */
4292 flags.f.decimal_point = number_format()[0] != ','; // HP-42S sets this to 1 on hard reset
4293 flags.f.thousands_separators = 1;
4294 flags.f.stack_lift_disable = 0;
4295 int df = shell_date_format();
4296 flags.f.dmy = df == 1;
4297 flags.f.f32 = flags.f.f33 = 0;
4298 flags.f.agraph_control1 = 0;
4299 flags.f.agraph_control0 = 0;
4300 flags.f.digits_bit3 = 0;
4301 flags.f.digits_bit2 = 1;
4302 flags.f.digits_bit1 = 0;
4303 flags.f.digits_bit0 = 0;
4304 flags.f.fix_or_all = 1;
4305 flags.f.eng_or_all = 0;
4306 flags.f.grad = 0;
4307 flags.f.rad = 0;
4308 /* flags.f.VIRTUAL_continuous_on = 0; */
4309 /* flags.f.VIRTUAL_solving = 0; */
4310 /* flags.f.VIRTUAL_integrating = 0; */
4311 /* flags.f.VIRTUAL_variable_menu = 0; */
4312 /* flags.f.VIRTUAL_alpha_mode = 0; */
4313 /* flags.f.VIRTUAL_low_battery = 0; */
4314 flags.f.message = 1;
4315 flags.f.two_line_message = 0;
4316 flags.f.prgm_mode = 0;
4317 /* flags.f.VIRTUAL_input = 0; */
4318 flags.f.f54 = 0;
4319 flags.f.printer_exists = 0;
4320 flags.f.lin_fit = 1;
4321 flags.f.log_fit = 0;
4322 flags.f.exp_fit = 0;
4323 flags.f.pwr_fit = 0;
4324 flags.f.all_sigma = 1;
4325 flags.f.log_fit_invalid = 0;
4326 flags.f.exp_fit_invalid = 0;
4327 flags.f.pwr_fit_invalid = 0;
4328 flags.f.shift_state = 0;
4329 /* flags.f.VIRTUAL_matrix_editor = 0; */
4330 flags.f.grow = 0;
4331 flags.f.ymd = df == 2;
4332 flags.f.base_bit0 = 0;
4333 flags.f.base_bit1 = 0;
4334 flags.f.base_bit2 = 0;
4335 flags.f.base_bit3 = 0;
4336 flags.f.local_label = 0;
4337 flags.f.polar = 0;
4338 flags.f.real_result_only = 0;
4339 /* flags.f.VIRTUAL_programmable_menu = 0; */
4340 flags.f.matrix_edge_wrap = 0;
4341 flags.f.matrix_end_wrap = 0;
4342 flags.f.base_signed = 1;
4343 flags.f.base_wrap = 0;
4344 flags.f.big_stack = 0;
4345 flags.f.f81 = flags.f.f82 = flags.f.f83 = flags.f.f84 = 0;
4346 flags.f.f85 = flags.f.f86 = flags.f.f87 = flags.f.f88 = flags.f.f89 = 0;
4347 flags.f.f90 = flags.f.f91 = flags.f.f92 = flags.f.f93 = flags.f.f94 = 0;
4348 flags.f.f95 = flags.f.f96 = flags.f.f97 = flags.f.f98 = flags.f.f99 = 0;
4349
4350 mode_clall = false;
4351 mode_command_entry = false;
4352 mode_number_entry = false;
4353 mode_alpha_entry = false;
4354 mode_shift = false;
4355 mode_commandmenu = MENU_NONE;
4356 mode_alphamenu = MENU_NONE;
4357 mode_transientmenu = MENU_NONE;
4358 mode_plainmenu = MENU_NONE;
4359 mode_appmenu = MENU_NONE;
4360 mode_running = false;
4361 mode_getkey = false;
4362 mode_pause = false;
4363 mode_caller_stack_lift_disabled = false;
4364 mode_varmenu = false;
4365 prgm_highlight_row = 0;
4366 varmenu_length = 0;
4367 mode_updown = false;
4368 mode_sigma_reg = 11;
4369 mode_goose = -1;
4370 mode_time_clktd = false;
4371 mode_time_clk24 = shell_clk24();
4372 mode_wsize = 36;
4373 mode_carry = false;
4374 mode_dec_int = false;
4375 mode_bin_sep = false;
4376 mode_oct_sep = false;
4377 mode_dec_sep = false;
4378 mode_hex_sep = false;
4379 mode_menu_caps = false;
4380 mode_menu_static = false;
4381
4382 reset_math();
4383
4384 clear_display();
4385 clear_custom_menu();
4386 clear_prgm_menu();
4387 switch (reason) {
4388 case 0:
4389 draw_string(0, 0, "Memory Clear", 12);
4390 break;
4391 case 1:
4392 draw_string(0, 0, "State File Corrupt", 18);
4393 break;
4394 case 2:
4395 draw_string(0, 0, "State File Too New", 18);
4396 break;
4397 }
4398 display_x(1);
4399 flush_display();
4400 }
4401
4402 #ifdef IPHONE
4403 bool off_enabled() {
4404 if (off_enable_flag)
4405 return true;
4406 if (sp == -1 || stack[sp]->type != TYPE_STRING)
4407 return false;
4408 vartype_string *str = (vartype_string *) stack[sp];
4409 off_enable_flag = string_equals(str->txt(), str->length, "YESOFF", 6);
4410 return off_enable_flag;
4411 }
4412 #endif