This repository was archived by the owner on Aug 31, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 227
Expand file tree
/
Copy pathdispatch.cpp
More file actions
2834 lines (2399 loc) · 81.2 KB
/
dispatch.cpp
File metadata and controls
2834 lines (2399 loc) · 81.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* Copyright (C) 2003-2015 LiveCode Ltd.
This file is part of LiveCode.
LiveCode is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License v3 as published by the Free
Software Foundation.
LiveCode is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
You should have received a copy of the GNU General Public License
along with LiveCode. If not see <http://www.gnu.org/licenses/>. */
#include "prefix.h"
#include "globdefs.h"
#include "filedefs.h"
#include "objdefs.h"
#include "parsedef.h"
#include "mcio.h"
#include "dispatch.h"
#include "stack.h"
#include "tooltip.h"
#include "card.h"
#include "field.h"
#include "button.h"
#include "image.h"
#include "aclip.h"
#include "vclip.h"
#include "stacklst.h"
#include "mcerror.h"
#include "hc.h"
#include "util.h"
#include "param.h"
#include "debug.h"
#include "statemnt.h"
#include "funcs.h"
#include "magnify.h"
#include "sellst.h"
#include "undolst.h"
#include "styledtext.h"
#include "external.h"
#include "osspec.h"
#include "flst.h"
#include "globals.h"
#include "license.h"
#include "mode.h"
#include "redraw.h"
#include "printer.h"
#include "font.h"
#include "stacksecurity.h"
#include "scriptpt.h"
#include "widget-events.h"
#include "parentscript.h"
#include "exec.h"
#include "exec-interface.h"
#include "graphics_util.h"
#include "stackfileformat.h"
#define UNLICENSED_TIME 6.0
#ifdef _DEBUG_MALLOC_INC
#define LICENSED_TIME 1.0
#else
#define LICENSED_TIME 3.0
#endif
MCImage *MCDispatch::imagecache;
////////////////////////////////////////////////////////////////////////////////
MCPropertyInfo MCDispatch::kProperties[] =
{
DEFINE_RO_OBJ_PROPERTY(P_TEXT_FONT, String, MCDispatch, DefaultTextFont)
DEFINE_RO_OBJ_PROPERTY(P_TEXT_SIZE, UInt32, MCDispatch, DefaultTextSize)
DEFINE_RO_OBJ_ENUM_PROPERTY(P_TEXT_ALIGN, InterfaceTextAlign, MCDispatch, DefaultTextAlign)
DEFINE_RO_OBJ_CUSTOM_PROPERTY(P_TEXT_STYLE, InterfaceTextStyle, MCDispatch, DefaultTextStyle)
DEFINE_RO_OBJ_PROPERTY(P_TEXT_HEIGHT, UInt32, MCDispatch, DefaultTextHeight)
DEFINE_RO_OBJ_PROPERTY(P_FORE_PIXEL, UInt32, MCDispatch, DefaultForePixel)
DEFINE_RO_OBJ_PROPERTY(P_HILITE_PIXEL, UInt32, MCDispatch, DefaultForePixel)
DEFINE_RO_OBJ_PROPERTY(P_BORDER_PIXEL, UInt32, MCDispatch, DefaultForePixel)
DEFINE_RO_OBJ_PROPERTY(P_BOTTOM_PIXEL, UInt32, MCDispatch, DefaultForePixel)
DEFINE_RO_OBJ_PROPERTY(P_SHADOW_PIXEL, UInt32, MCDispatch, DefaultForePixel)
DEFINE_RO_OBJ_PROPERTY(P_FOCUS_PIXEL, UInt32, MCDispatch, DefaultForePixel)
DEFINE_RO_OBJ_PROPERTY(P_BACK_PIXEL, UInt32, MCDispatch, DefaultBackPixel)
DEFINE_RO_OBJ_PROPERTY(P_TOP_PIXEL, UInt32, MCDispatch, DefaultTopPixel)
DEFINE_RO_OBJ_CUSTOM_PROPERTY(P_FORE_COLOR, InterfaceNamedColor, MCDispatch, DefaultForeColor)
DEFINE_RO_OBJ_CUSTOM_PROPERTY(P_BORDER_COLOR, InterfaceNamedColor, MCDispatch, DefaultForeColor)
DEFINE_RO_OBJ_CUSTOM_PROPERTY(P_TOP_COLOR, InterfaceNamedColor, MCDispatch, DefaultForeColor)
DEFINE_RO_OBJ_CUSTOM_PROPERTY(P_BOTTOM_COLOR, InterfaceNamedColor, MCDispatch, DefaultForeColor)
DEFINE_RO_OBJ_CUSTOM_PROPERTY(P_SHADOW_COLOR, InterfaceNamedColor, MCDispatch, DefaultForeColor)
DEFINE_RO_OBJ_CUSTOM_PROPERTY(P_FOCUS_COLOR, InterfaceNamedColor, MCDispatch, DefaultForeColor)
DEFINE_RO_OBJ_CUSTOM_PROPERTY(P_BACK_COLOR, InterfaceNamedColor, MCDispatch, DefaultBackColor)
DEFINE_RO_OBJ_CUSTOM_PROPERTY(P_HILITE_COLOR, InterfaceNamedColor, MCDispatch, DefaultBackColor)
DEFINE_RO_OBJ_PROPERTY(P_FORE_PATTERN, OptionalUInt32, MCDispatch, DefaultPattern)
DEFINE_RO_OBJ_PROPERTY(P_BACK_PATTERN, OptionalUInt32, MCDispatch, DefaultPattern)
DEFINE_RO_OBJ_PROPERTY(P_HILITE_PATTERN, OptionalUInt32, MCDispatch, DefaultPattern)
DEFINE_RO_OBJ_PROPERTY(P_BORDER_PATTERN, OptionalUInt32, MCDispatch, DefaultPattern)
DEFINE_RO_OBJ_PROPERTY(P_TOP_PATTERN, OptionalUInt32, MCDispatch, DefaultPattern)
DEFINE_RO_OBJ_PROPERTY(P_BOTTOM_PATTERN, OptionalUInt32, MCDispatch, DefaultPattern)
DEFINE_RO_OBJ_PROPERTY(P_SHADOW_PATTERN, OptionalUInt32, MCDispatch, DefaultPattern)
DEFINE_RO_OBJ_PROPERTY(P_FOCUS_PATTERN, OptionalUInt32, MCDispatch, DefaultPattern)
};
MCObjectPropertyTable MCDispatch::kPropertyTable =
{
&MCObject::kPropertyTable,
sizeof(kProperties) / sizeof(kProperties[0]),
&kProperties[0],
};
////////////////////////////////////////////////////////////////////////////////
MCDispatch::MCDispatch()
{
license = NULL;
stacks = NULL;
fonts = NULL;
setname_cstring("dispatch");
handling = False;
menu = NULL;
panels = NULL;
startdir = NULL;
enginedir = NULL;
flags = 0;
m_drag_source = false;
m_drag_target = false;
m_drag_end_sent = false;
m_showing_mnemonic_underline = false;
m_externals = nil;
m_transient_stacks = nil;
// AL-2015-02-10: [[ Standalone Inclusions ]] Add resource mapping array to MCDispatch. This stores
// any universal name / relative path pairs included in a standalone executable for locating included
// resources.
/* UNCHECKED */ MCArrayCreateMutable(m_library_mapping);
}
MCDispatch::~MCDispatch()
{
delete license;
while (stacks != NULL)
{
MCStack *sptr = stacks->prev()->remove(stacks);
delete sptr;
}
while (imagecache != NULL)
{
MCImage *iptr = imagecache->remove(imagecache);
delete iptr;
}
delete fonts;
MCMemoryDeleteArray(startdir); /* Allocated by MCStringConvertToCString() */
MCMemoryDeleteArray(enginedir); /* Allocated by MCStringConvertToCString() */
delete m_externals;
// AL-2015-02-10: [[ Standalone Inclusions ]] Delete library mapping
MCValueRelease(m_library_mapping);
}
bool MCDispatch::visit_self(MCObjectVisitor* p_visitor)
{
return p_visitor -> OnObject(this);
}
bool MCDispatch::isdragsource(void)
{
return m_drag_source;
}
bool MCDispatch::isdragtarget(void)
{
return m_drag_target;
}
// bogus "cut" call actually checks license
Boolean MCDispatch::cut(Boolean home)
{
if (home)
return True;
return MCnoui || (flags & F_WAS_LICENSED) != 0;
}
Exec_stat MCDispatch::handle(Handler_type htype, MCNameRef mess, MCParameter *params, MCObject *pass_from)
{
Exec_stat stat = ES_NOT_HANDLED;
bool t_has_passed;
t_has_passed = false;
if (MCcheckstack && MCU_abs(MCstackbottom - (char *)&stat) > MCrecursionlimit)
{
MCeerror->add(EE_RECURSION_LIMIT, 0, 0);
MCerrorptr = stacks;
return ES_ERROR;
}
// MW-2011-06-30: Move handling of library stacks from MCStack::handle.
if (MCnusing > 0)
{
for (uint32_t i = MCnusing; i > 0 && (stat == ES_PASS || stat == ES_NOT_HANDLED); i -= 1)
{
stat = MCusing[i - 1]->handle(htype, mess, params, nil);
// MW-2011-08-22: [[ Bug 9686 ]] Make sure we exit as soon as the
// message is handled.
if (stat != ES_NOT_HANDLED && stat != ES_PASS)
return stat;
if (stat == ES_PASS)
t_has_passed = true;
}
if (t_has_passed && stat == ES_NOT_HANDLED)
stat = ES_PASS;
}
if ((stat == ES_NOT_HANDLED || stat == ES_PASS) && MCbackscripts != NULL)
{
MCObjectList *optr = MCbackscripts;
do
{
if (!optr->getremoved())
{
stat = optr->getobject()->handle(htype, mess, params, nil);
if (stat != ES_NOT_HANDLED && stat != ES_PASS)
return stat;
if (stat == ES_PASS)
t_has_passed = true;
}
optr = optr->next();
}
while (optr != MCbackscripts);
}
if ((stat == ES_NOT_HANDLED || stat == ES_PASS) && m_externals != nil)
{
// TODO[19681]: This can be removed when all engine messages are sent with
// target.
bool t_target_was_valid = MCtargetptr.IsValid();
Exec_stat oldstat = stat;
stat = m_externals -> Handle(this, htype, mess, params);
// MW-2011-08-22: [[ Bug 9686 ]] Make sure we exit as soon as the
// message is handled.
if (stat != ES_NOT_HANDLED && stat != ES_PASS)
return stat;
if (oldstat == ES_PASS && stat == ES_NOT_HANDLED)
stat = ES_PASS;
if (stat == ES_PASS || stat == ES_NOT_HANDLED)
{
if (t_target_was_valid && !MCtargetptr.IsValid())
{
stat = ES_NORMAL;
t_has_passed = false;
}
}
}
//#ifdef TARGET_SUBPLATFORM_IPHONE
// extern Exec_stat MCIPhoneHandleMessage(MCNameRef message, MCParameter *params);
// if (stat == ES_NOT_HANDLED || stat == ES_PASS)
// {
// stat = MCIPhoneHandleMessage(mess, params);
//
// if (stat != ES_NOT_HANDLED && stat != ES_PASS)
// return stat;
// }
//#endif
//
//#ifdef _MOBILE
// if (stat == ES_NOT_HANDLED || stat == ES_PASS)
// {
// stat = MCHandlePlatformMessage(htype, MCNameGetOldString(mess), params);
//
// // MW-2011-08-22: [[ Bug 9686 ]] Make sure we exit as soon as the
// // message is handled.
// if (stat != ES_NOT_HANDLED && stat != ES_PASS)
// return stat;
// }
//#endif
if ((stat == ES_NOT_HANDLED || stat == ES_PASS))
{
// TODO[19681]: This can be removed when all engine messages are sent with
// target.
bool t_target_was_valid = MCtargetptr.IsValid();
extern Exec_stat MCEngineHandleLibraryMessage(MCNameRef name, MCParameter *params);
stat = MCEngineHandleLibraryMessage(mess, params);
if (stat == ES_PASS || stat == ES_NOT_HANDLED)
{
if (t_target_was_valid && !MCtargetptr.IsValid())
{
stat = ES_NORMAL;
t_has_passed = false;
}
}
}
if (MCmessagemessages && stat != ES_PASS && MCtargetptr)
MCtargetptr -> sendmessage(htype, mess, False);
if (t_has_passed)
return ES_PASS;
return stat;
}
bool MCDispatch::getmainstacknames(MCListRef& r_list)
{
MCAutoListRef t_list;
if (!MCListCreateMutable('\n', &t_list))
return false;
MCStack *tstk = stacks;
do
{
MCAutoValueRef t_string;
if (!tstk->names(P_SHORT_NAME, &t_string))
return false;
if (!MCListAppend(*t_list, (MCStringRef)*t_string))
return false;
tstk = (MCStack *)tstk->next();
}
while (tstk != stacks);
return MCListCopy(*t_list, r_list);
}
void MCDispatch::appendstack(MCStack *sptr)
{
sptr->appendto(stacks);
// MW-2013-03-20: [[ MainStacksChanged ]]
MCmainstackschanged = True;
}
void MCDispatch::removestack(MCStack *sptr)
{
sptr->remove(stacks);
// MW-2013-03-20: [[ MainStacksChanged ]]
MCmainstackschanged = True;
}
void MCDispatch::destroystack(MCStack *sptr, Boolean needremove)
{
/* Make sure no messages are sent when destroying the given stack as this
* destruction method is only ever used for stacks which are not-yet-alive
* (e.g. failed to deserialize) or in restricted contexts (e.g. licensing
* dialog on startup). */
Boolean oldstate = MClockmessages;
MClockmessages = True;
if (needremove)
{
MCStack *t_substacks = sptr -> getsubstacks();
while(t_substacks != nullptr)
{
/* The MCStack::dodel() method removes the stack from its mainstack
* so we must explicitly delete it explicitly. Note that there is
* no need to scheduledelete() in this case as destroystack() is
* only called when it is known that no script is running from the
* stack. */
t_substacks -> dodel();
delete t_substacks;
/* Refetch the substacks list - the substack we just processed will
* have been removed from it. */
t_substacks = sptr -> getsubstacks();
}
/* Release any references to the mainstack */
sptr -> dodel();
}
if (sptr == MCstaticdefaultstackptr)
MCstaticdefaultstackptr = stacks;
if (sptr == MCdefaultstackptr)
MCdefaultstackptr = MCstaticdefaultstackptr;
if (MCacptr && MCacptr->getmessagestack() == sptr)
MCacptr->setmessagestack(NULL);
/* Delete the stack explicitly. Note that there is no need to use
* scheduledelete here as destroystack() is only called when it is known
* that no script is running from the stack. */
delete sptr;
/* Restore the previous message lock state. */
MClockmessages = oldstate;
}
static bool attempt_to_loadfile(IO_handle& r_stream, MCStringRef& r_path, const char *p_path_format, ...)
{
MCAutoStringRef t_trial_path;
va_list t_args;
va_start(t_args, p_path_format);
/* UNCHECKED */ MCStringFormatV(&t_trial_path, p_path_format, t_args);
va_end(t_args);
IO_handle t_trial_stream;
t_trial_stream = MCS_open(*t_trial_path, kMCOpenFileModeRead, True, False, 0);
if (t_trial_stream != nil)
{
r_path = (MCStringRef)MCValueRetain(*t_trial_path);
r_stream = t_trial_stream;
return true;
}
return false;
}
Boolean MCDispatch::openstartup(MCStringRef sname, MCStringRef& outpath, IO_handle &stream)
{
if (enginedir == nil)
return False;
if (attempt_to_loadfile(stream, outpath, "%s/%@", startdir, sname))
return True;
if (attempt_to_loadfile(stream, outpath, "%s/%@", enginedir, sname))
return True;
return False;
}
Boolean MCDispatch::openenv(MCStringRef sname, MCStringRef env,
MCStringRef& outpath, IO_handle &stream, uint4 offset)
{
MCAutoStringRef t_env;
if (!MCS_getenv(env, &t_env))
return False;
bool t_found;
t_found = false;
MCStringRef t_rest_of_env;
t_rest_of_env = MCValueRetain(env);
while(!t_found && !MCStringIsEmpty(t_rest_of_env))
{
MCAutoStringRef t_env_path;
MCStringRef t_next_rest_of_env;
/* UNCHECKED */ MCStringDivideAtChar(t_rest_of_env, ENV_SEPARATOR, kMCStringOptionCompareExact, &t_env_path, t_next_rest_of_env);
if (attempt_to_loadfile(stream, outpath, "%@/%@", *t_env_path, sname))
t_found = true;
MCValueRelease(t_rest_of_env);
t_rest_of_env = t_next_rest_of_env;
}
MCValueRelease(t_rest_of_env);
return t_found;
}
IO_stat readheader(IO_handle& stream, uint32_t& r_version)
{
char tnewheader[kMCStackFileVersionStringLength + 1];
if (IO_read(tnewheader, kMCStackFileVersionStringLength, stream) != IO_NORMAL)
return IO_ERROR;
tnewheader[kMCStackFileVersionStringLength] = '\0'; /* nul-terminate */
// AL-2014-10-27: [[ Bug 12558 ]] Check for valid header prefix
if (!MCStackFileParseVersionNumber(tnewheader, r_version))
{
char theader[kMCStackFileMetaCardVersionStringLength + 1];
theader[kMCStackFileMetaCardVersionStringLength] = '\0';
uint4 offset;
strncpy(theader, tnewheader, kMCStackFileVersionStringLength);
if (IO_read(theader + kMCStackFileVersionStringLength, kMCStackFileMetaCardVersionStringLength - kMCStackFileVersionStringLength, stream) == IO_NORMAL
&& MCU_offset(kMCStackFileMetaCardSignature, theader, offset))
{
if (theader[offset - 1] != '\n' || theader[offset - 2] == '\r')
{
MCresult->sets("stack was corrupted by a non-binary file transfer");
return IO_ERROR;
}
r_version = (theader[offset + kMCStackFileMetaCardSignatureLength] - '0') * 1000;
r_version += (theader[offset + kMCStackFileMetaCardSignatureLength + 2] - '0') * 100;
}
else
return IO_ERROR;
}
return IO_NORMAL;
}
bool MCDispatch::streamstackisscriptonly(IO_handle stream)
{
uint32_t t_version;
return readheader(stream, t_version) != IO_NORMAL;
}
// This method reads a stack from the given stream. The stack is set to
// have parent MCDispatch, and filename MCcmd. It is designed to be used
// for embedded stacks/deployed stacks/revlet stacks.
IO_stat MCDispatch::readstartupstack(IO_handle stream, MCStack*& r_stack)
{
MCAutoStringRef t_filename;
// MM-2013-10-30: [[ Bug 11333 ]] Set the filename of android mainstack to apk/mainstack (previously was just apk).
// This solves relative file path referencing issues.
#ifdef TARGET_SUBPLATFORM_ANDROID
/* UNCHECKED */ MCStringFormat(&t_filename, "%@/mainstack", MCcmd);
#else
t_filename = MCcmd;
#endif
const char* t_result = nullptr;
MCStack* t_stack = nullptr;
if (trytoreadbinarystack(*t_filename, kMCEmptyString, stream, this,
t_stack, t_result) != IO_NORMAL ||
t_stack == nullptr)
{
return IO_ERROR;
}
// We are reading the startup stack, so this becomes the root of the
// stack list. This must happen prior to resolving parent scripts
// because otherwise there are no mainstacks, which can cause a
// crash when searching substacks.
stacks = t_stack;
// Mark the stack as needed parentscript resolution. This is done after
// aux stacks have been loaded.
if (s_loaded_parent_script_reference)
t_stack -> setextendedstate(True, ECS_USES_PARENTSCRIPTS);
r_stack = t_stack;
return IO_NORMAL;
}
IO_stat MCDispatch::readscriptonlystartupstack(IO_handle stream, uindex_t p_length, MCStack*& r_stack)
{
MCAutoStringRef t_filename;
// MM-2013-10-30: [[ Bug 11333 ]] Set the filename of android mainstack to apk/mainstack (previously was just apk).
// This solves relative file path referencing issues.
#ifdef TARGET_SUBPLATFORM_ANDROID
/* UNCHECKED */ MCStringFormat(&t_filename, "%@/mainstack", MCcmd);
#else
t_filename = MCcmd;
#endif
const char* t_result = nullptr;
MCStack* t_stack = nullptr;
// Read a script-only stack from the stream
if (trytoreadscriptonlystackofsize(*t_filename, stream,
p_length, this,
t_stack, t_result) != IO_NORMAL
|| t_stack == nullptr)
return IO_ERROR;
// We are reading the startup stack, so this becomes the root of the
// stack list.
stacks = t_stack;
// Mark the stack as needed parentscript resolution. This is done after
// aux stacks have been loaded.
if (s_loaded_parent_script_reference)
t_stack -> setextendedstate(True, ECS_USES_PARENTSCRIPTS);
r_stack = t_stack;
return IO_NORMAL;
}
// MW-2012-02-17: [[ LogFonts ]] Load a stack file, ensuring we clear up any
// font table afterwards - regardless of errors.
IO_stat MCDispatch::readfile(MCStringRef p_openpath, MCStringRef p_name, IO_handle &stream, MCStack *&sptr)
{
// Various places like to call this function with the first two parameters as NULL
if (p_openpath == nil)
p_openpath = kMCEmptyString;
if (p_name == nil)
p_name = kMCEmptyString;
IO_stat stat;
stat = doreadfile(p_openpath, p_name, stream, sptr);
MCLogicalFontTableFinish();
return stat;
}
IO_stat MCDispatch::trytoreadbinarystack(MCStringRef p_openpath,
MCStringRef p_name,
IO_handle &x_stream,
MCObject* p_parent,
MCStack* &r_stack,
const char* &r_result)
{
uint32_t t_version;
if (readheader(x_stream, t_version) != IO_NORMAL)
{
return IO_NORMAL;
}
if (t_version > kMCStackFileFormatCurrentVersion)
{
r_result = "stack was produced by a newer version";
return checkloadstat(IO_ERROR);
}
// MW-2008-10-20: [[ ParentScripts ]] Set the boolean flag that tells us whether
// parentscript resolution is required to false.
s_loaded_parent_script_reference = false;
// MW-2013-11-19: [[ UnicodeFileFormat ]] newsf is no longer used.
uint1 charset, type;
if (IO_read_uint1(&charset, x_stream) != IO_NORMAL
|| IO_read_uint1(&type, x_stream) != IO_NORMAL
|| IO_discard_cstring_legacy(x_stream, 2) != IO_NORMAL)
{
r_result = "stack is corrupted, check for ~ backup file";
return checkloadstat(IO_ERROR);
}
MCtranslatechars = charset != CHARSET;
MCStack *t_stack;
if (!MCStackSecurityCreateStack(t_stack))
{
r_result = "couldn't create stack";
return checkloadstat(IO_ERROR);
}
if (p_parent != nullptr)
t_stack -> setparent(p_parent);
else if (stacks != nullptr)
t_stack->setparent(stacks);
else
t_stack->setparent(this);
t_stack->setfilename(p_openpath);
if (MCModeCanLoadHome() && type == OT_HOME)
{
// MW-2013-11-19: [[ UnicodeFileFormat ]] These strings are never written out, so
// legacy.
if (IO_discard_cstring_legacy(x_stream, 2) != IO_NORMAL
|| IO_discard_cstring_legacy(x_stream, 2) != IO_NORMAL)
{
r_result = "stack is corrupted, check for ~ backup file";
return checkloadstat(IO_ERROR);
}
}
if (IO_read_uint1(&type, x_stream) != IO_NORMAL
|| (type != OT_STACK && type != OT_ENCRYPT_STACK)
|| t_stack->load(x_stream, t_version, type) != IO_NORMAL)
{
r_result = "stack is corrupted, check for ~ backup file";
destroystack(t_stack, False);
return checkloadstat(IO_ERROR);
}
// MW-2011-08-09: [[ Groups ]] Make sure F_GROUP_SHARED is set
// appropriately.
t_stack -> checksharedgroups();
if (t_stack->load_substacks(x_stream, t_version) != IO_NORMAL
|| IO_read_uint1(&type, x_stream) != IO_NORMAL
|| type != OT_END)
{
r_result = "stack is corrupted, check for ~ backup file";
destroystack(t_stack, False);
return checkloadstat(IO_ERROR);
}
r_stack = t_stack;
return IO_NORMAL;
}
static MCStack* script_only_stack_from_bytes(uint8_t *p_bytes,
uindex_t p_size,
MCStringEncoding p_encoding)
{
MCAutoStringRef t_raw_script_string, t_lc_script_string;
MCStringLineEndingStyle t_line_encoding_style;
if (!MCStringCreateWithBytes(p_bytes, p_size, p_encoding, false,
&t_raw_script_string) ||
!MCStringNormalizeLineEndings(*t_raw_script_string,
kMCStringLineEndingStyleLF,
kMCStringLineEndingOptionNormalizePSToLineEnding |
kMCStringLineEndingOptionNormalizeLSToVT,
&t_lc_script_string,
&t_line_encoding_style))
{
return nullptr;
}
// Now attempt to parse the header line:
// 'script' <string>
MCScriptPoint sp(*t_lc_script_string);
// Parse 'script' token.
if (sp . skip_token(SP_FACTOR, TT_PROPERTY, P_SCRIPT) != PS_NORMAL)
{
return nullptr;
}
// Parse <string> token.
Symbol_type t_type;
if (sp . next(t_type) != PS_NORMAL || t_type != ST_LIT)
{
return nullptr;
}
MCNewAutoNameRef t_script_name = sp.gettoken_nameref();
// If 'with' is next then parse the behavior reference.
MCNewAutoNameRef t_behavior_name;
if (sp.skip_token(SP_REPEAT, TT_UNDEFINED, RF_WITH) == PS_NORMAL)
{
// Ensure 'behavior' is next
if (sp.skip_token(SP_FACTOR, TT_PROPERTY, P_PARENT_SCRIPT) != PS_NORMAL)
{
return nullptr;
}
// Read the behavior name
if (sp.next(t_type) != PS_NORMAL || t_type != ST_LIT)
{
return nullptr;
}
t_behavior_name = sp.gettoken_nameref();
}
// Parse end of line.
Parse_stat t_stat;
t_stat = sp . next(t_type);
if (t_stat != PS_EOL && t_stat != PS_EOF)
return nullptr;
// MW-2014-10-23: [[ Bug ]] Make sure we trim the correct number of lines.
// SN-2014-10-16: [[ Merge-6.7.0-rc-3 ]] Update to StringRef
// Now trim the ep down to the remainder of the script.
// Trim the header.
uint32_t t_lines = sp.getline();
uint32_t t_index = 0;
// Jump over the possible lines before the string token
while (MCStringFirstIndexOfChar(*t_lc_script_string, '\n', t_index,
kMCStringOptionCompareExact, t_index)
&& t_lines > 0)
{
t_lines -= 1;
}
// Add one to the index so we include the LF
t_index += 1;
// t_line now has the last LineFeed of the token
MCAutoStringRef t_lc_script_body;
// We copy the body of the stack script
if (!MCStringCopySubstring(*t_lc_script_string,
MCRangeMake(t_index,
MCStringGetLength(*t_lc_script_string)
- t_index), &t_lc_script_body))
{
return nullptr;
}
// Create a stack.
MCStack *t_stack;
if (!MCStackSecurityCreateStack(t_stack))
return nullptr;
// Set it up as script only.
t_stack -> setasscriptonly(*t_lc_script_body);
// Set its name.
t_stack -> setname(*t_script_name);
// Save line endings from raw script string to restore when saving file.
t_stack -> setlineencodingstyle(t_line_encoding_style);
// If we parsed a behavior reference, then set it.
if (*t_behavior_name != nullptr)
{
t_stack->setparentscript_onload(0, *t_behavior_name);
}
return t_stack;
}
IO_stat MCDispatch::trytoreadscriptonlystackofsize(MCStringRef p_openpath,
IO_handle &x_stream,
uindex_t p_size,
MCObject* p_parent,
MCStack* &r_stack,
const char* &r_result)
{
MCAutoPointer<byte_t> t_bytes = new byte_t[p_size];
if (IO_read(*t_bytes, p_size, x_stream) == IO_ERROR)
{
return checkloadstat(IO_ERROR);
}
uindex_t t_bom_size = 0;
MCFileEncodingType t_file_encoding =
MCS_resolve_BOM_from_bytes(*t_bytes, p_size, t_bom_size);
MCStringEncoding t_string_encoding;
switch (t_file_encoding)
{
case kMCFileEncodingUTF8:
t_string_encoding = kMCStringEncodingUTF8;
break;
case kMCFileEncodingUTF16:
t_string_encoding = kMCStringEncodingUTF16;
break;
case kMCFileEncodingUTF16BE:
t_string_encoding = kMCStringEncodingUTF16BE;
break;
case kMCFileEncodingUTF16LE:
t_string_encoding = kMCStringEncodingUTF16LE;
break;
default:
// Assume native
t_string_encoding = kMCStringEncodingNative;
break;
}
MCStack *t_stack = script_only_stack_from_bytes(*t_bytes + t_bom_size,
p_size - t_bom_size,
t_string_encoding);
if (t_stack == nullptr)
return IO_NORMAL;
// Set its parent.
if (p_parent != nullptr)
t_stack -> setparent(p_parent);
else if (stacks != nullptr)
t_stack->setparent(stacks);
else
t_stack->setparent(this);
// Set its filename.
t_stack->setfilename(p_openpath);
// Make it invisible
t_stack -> setflag(False, F_VISIBLE);
r_stack = t_stack;
return IO_NORMAL;
}
IO_stat MCDispatch::trytoreadscriptonlystack(MCStringRef p_openpath,
IO_handle &x_stream,
MCObject* p_parent,
MCStack* &r_stack,
const char* &r_result)
{
// Load the file into memory - we need to process a byteorder mark and any
// line endings.
uindex_t t_size = static_cast<uindex_t>(MCS_fsize(x_stream));
if (trytoreadscriptonlystackofsize(p_openpath, x_stream, t_size,
p_parent, r_stack, r_result)
!= IO_NORMAL)
{
r_result = "failed to load script only stack";
return checkloadstat(IO_ERROR);
}
return IO_NORMAL;
}
void MCDispatch::processstack(MCStringRef p_openpath, MCStack* &x_stack)
{
if (stacks != NULL)
{
MCStack *tstk = stacks;
do
{
if (x_stack->hasname(tstk->getname()))
{
MCNewAutoNameRef t_stack_name = x_stack->getname();
delete x_stack;
x_stack = nullptr;
if (MCStringIsEqualTo(tstk -> getfilename(), p_openpath, kMCStringOptionCompareCaseless))
x_stack = tstk;
else
{
MCdefaultstackptr->getcard()->message_with_valueref_args(MCM_reload_stack, tstk->getname(), p_openpath);
tstk = stacks;
do
{
if (MCNameIsEqualToCaseless(*t_stack_name, tstk->getname()))
{
x_stack = tstk;
break;
}
tstk = (MCStack *)tstk->next();
}
while (tstk != stacks);
}
return;
}
tstk = (MCStack *)tstk->next();
}
while (tstk != stacks);
}
appendstack(x_stack);
x_stack->extraopen(false);
// MW-2008-10-28: [[ ParentScript ]]
// We just loaded a stackfile, so check to see if parentScript resolution
// is required and if so do it.
// MW-2009-01-28: [[ Inherited parentScripts ]]
// Resolving parentScripts may allocate memory, so 'resolveparentscripts'
// will return false if it fails to allocate what it needs. At some point
// this needs to be dealt with by deleting the stack and returning an error,
// *However* at the time of writing, 'readfile' isn't designed to handle
// this - so we just ignore the result for now (note that all the 'load'
// methods *fail* to check for no-memory errors!).
if (s_loaded_parent_script_reference)
{
x_stack -> resolveparentscripts();
x_stack -> setextendedstate(True, ECS_USES_PARENTSCRIPTS);
}
}
// MW-2012-02-17: [[ LogFonts ]] Actually load the stack file (wrapped by readfile
// to handle font table cleanup).
IO_stat MCDispatch::doreadfile(MCStringRef p_openpath, MCStringRef p_name, IO_handle &stream, MCStack* &r_stack)
{
MCresult -> clear();
const char* t_result = nullptr;
MCStack *t_stack = nullptr;
if (trytoreadbinarystack(p_openpath, p_name, stream, nullptr,
t_stack, t_result) != IO_NORMAL)
{
/* UNCHECKED */ MCresult -> setvalueref(MCSTR(t_result));
return IO_ERROR;
}
// If there was no IO error but it wasn't a binary stack then try as script-only
if (t_stack == nullptr)
{
// Reset to position 0.
MCS_seek_set(stream, 0);
if (trytoreadscriptonlystack(p_openpath, stream, nullptr,
t_stack, t_result) != IO_NORMAL)
{
/* UNCHECKED */ MCresult -> setvalueref(MCSTR(t_result));
return IO_ERROR;
}
}
// MW-2014-09-30: [[ ScriptOnlyStack ]] If we managed to load a stack as either binary
// or script, then do the normal processing.
if (t_stack != nullptr)
{
processstack(p_openpath, t_stack);
r_stack = t_stack;
return IO_NORMAL;
}
// MW-2014-09-30: [[ ScriptOnlyStack ]] Finally attempt to load the script in legacy
// modes - either as a single script, or as a HyperCard conversion.
MCS_seek_set(stream, 0);
if (stacks == NULL)
{