forked from livecode/livecode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdispatch.cpp
More file actions
2682 lines (2281 loc) · 74.8 KB
/
dispatch.cpp
File metadata and controls
2682 lines (2281 loc) · 74.8 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 "execpt.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 "exec.h"
#include "exec-interface.h"
#include "graphics_util.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;
#define VERSION_OFFSET 11
#define HEADERSIZE 255
static char header[HEADERSIZE] = "#!/bin/sh\n# MetaCard 2.4 stack\n# The following is not ASCII text,\n# so now would be a good time to q out of more\f\nexec mc $0 \"$@\"\n";
#define NEWHEADERSIZE 8
#define HEADERPREFIXSIZE 4
static const char *newheader = "REVO2700";
static const char *newheader5500 = "REVO5500";
static const char *newheader7000 = "REVO7000";
static const char *newheader8000 = "REVO8000";
#define MAX_STACKFILE_VERSION 8000
////////////////////////////////////////////////////////////////////////////////
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;
delete startdir;
delete enginedir;
delete m_externals;
// AL-2015-02-10: [[ Standalone Inclusions ]] Delete library mapping
MCValueRelease(m_library_mapping);
}
bool MCDispatch::isdragsource(void)
{
return m_drag_source;
}
bool MCDispatch::isdragtarget(void)
{
return m_drag_target;
}
#ifdef LEGACY_EXEC
Exec_stat MCDispatch::getprop_legacy(uint4 parid, Properties which, MCExecPoint &ep, Boolean effective, bool recursive)
{
switch (which)
{
#ifdef /* MCDispatch::getprop */ LEGACY_EXEC
case P_BACK_PIXEL:
//ep.setint(MCscreen->background_pixel.pixel & 0xFFFFFF);
return ES_NOT_HANDLED;
case P_TOP_PIXEL:
//ep.setint(MCscreen->white_pixel.pixel & 0xFFFFFF);
return ES_NOT_HANDLED;
case P_HILITE_PIXEL:
case P_FORE_PIXEL:
case P_BORDER_PIXEL:
case P_BOTTOM_PIXEL:
case P_SHADOW_PIXEL:
case P_FOCUS_PIXEL:
//ep.setint(MCscreen->black_pixel.pixel & 0xFFFFFF);
return ES_NOT_HANDLED;
case P_BACK_COLOR:
case P_HILITE_COLOR:
//ep.setstaticcstring("white");
return ES_NOT_HANDLED;
case P_FORE_COLOR:
case P_BORDER_COLOR:
case P_TOP_COLOR:
case P_BOTTOM_COLOR:
case P_SHADOW_COLOR:
case P_FOCUS_COLOR:
//ep.setstaticcstring("black");
return ES_NOT_HANDLED;
case P_FORE_PATTERN:
case P_BACK_PATTERN:
case P_HILITE_PATTERN:
case P_BORDER_PATTERN:
case P_TOP_PATTERN:
case P_BOTTOM_PATTERN:
case P_SHADOW_PATTERN:
case P_FOCUS_PATTERN:
ep.clear();
return ES_NORMAL;
case P_TEXT_ALIGN:
ep.setstaticcstring(MCleftstring);
return ES_NORMAL;
case P_TEXT_FONT:
//ep.setstaticcstring(DEFAULT_TEXT_FONT);
return ES_NOT_HANDLED;
case P_TEXT_HEIGHT:
//ep.setint(heightfromsize(DEFAULT_TEXT_SIZE));
return ES_NOT_HANDLED;
case P_TEXT_SIZE:
//ep.setint(DEFAULT_TEXT_SIZE);
return ES_NOT_HANDLED;
case P_TEXT_STYLE:
ep.setstaticcstring(MCplainstring);
return ES_NORMAL;
#endif /* MCDispatch::getprop */
default:
MCeerror->add(EE_OBJECT_GETNOPROP, 0, 0);
return ES_ERROR;
}
}
#endif
#ifdef LEGACY_EXEC
Exec_stat MCDispatch::setprop_legacy(uint4 parid, Properties which, MCExecPoint &ep, Boolean effective)
{
#ifdef /* MCDispatch::setprop */ LEGACY_EXEC
return ES_NORMAL;
#endif /* MCDispatch::setprop */
return ES_NORMAL;
}
#endif
// 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)
{
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;
}
//#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))
{
extern Exec_stat MCEngineHandleLibraryMessage(MCNameRef name, MCParameter *params);
stat = MCEngineHandleLibraryMessage(mess, params);
}
if (MCmessagemessages && stat != ES_PASS)
MCtargetptr . object -> 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)
{
if (needremove)
removestack(sptr);
if (sptr == MCstaticdefaultstackptr)
MCstaticdefaultstackptr = stacks;
if (sptr == MCdefaultstackptr)
MCdefaultstackptr = MCstaticdefaultstackptr;
if (MCacptr != NULL && MCacptr->getmessagestack() == sptr)
MCacptr->setmessagestack(NULL);
Boolean oldstate = MClockmessages;
MClockmessages = True;
delete sptr;
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[NEWHEADERSIZE];
if (IO_read(tnewheader, NEWHEADERSIZE, stream) == IO_NORMAL)
{
// AL-2014-10-27: [[ Bug 12558 ]] Check for valid header prefix
if (strncmp(tnewheader, "REVO", HEADERPREFIXSIZE) == 0)
{
// The header version can now consist of any alphanumeric characters
// They map to numbers as follows:
// 0-9 -> 0-9
// A-Z -> 10-35
// a-z -> 36-61
uint1 versionnum[4];
for (uint1 i = 0; i < 4; i++)
{
char t_char = tnewheader[i + 4];
if ('0' <= t_char && t_char <= '9')
versionnum[i] = (uint1)(t_char - '0');
else if ('A' <= t_char && t_char <= 'Z')
versionnum[i] = (uint1)(t_char - 'A' + 10);
else if ('a' <= t_char && t_char <= 'z')
versionnum[i] = (uint1)(t_char - 'a' + 36);
else
return IO_ERROR;
}
// Future file format versions will always still compare greater than MAX_STACKFILE_VERSION,
// so it is ok that r_version does not accurately reflect future version values.
// TODO: change this, and comparisons for version >= 7000 / 5500, etc
r_version = versionnum[0] * 1000;
r_version += versionnum[1] * 100;
r_version += versionnum[2] * 10;
r_version += versionnum[3];
}
else
{
char theader[HEADERSIZE + 1];
theader[HEADERSIZE] = '\0';
uint4 offset;
strncpy(theader, tnewheader, NEWHEADERSIZE);
if (IO_read(theader + NEWHEADERSIZE, HEADERSIZE - NEWHEADERSIZE, stream) == IO_NORMAL
&& MCU_offset(SIGNATURE, 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 + VERSION_OFFSET] - '0') * 1000;
r_version += (theader[offset + VERSION_OFFSET + 2] - '0') * 100;
}
else
return IO_ERROR;
}
}
else
{
// Could not read header
return IO_ERROR;
}
return 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)
{
uint32_t version;
uint1 charset, type;
char *newsf;
// MW-2013-11-19: [[ UnicodeFileFormat ]] newsf is no longer used.
if (readheader(stream, version) != IO_NORMAL
|| IO_read_uint1(&charset, stream) != IO_NORMAL
|| IO_read_uint1(&type, stream) != IO_NORMAL
|| IO_read_cstring_legacy(newsf, stream, 2) != IO_NORMAL)
return 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;
MCtranslatechars = charset != CHARSET;
delete newsf; // stackfiles is obsolete
MCStack *t_stack = nil;
/* UNCHECKED */ MCStackSecurityCreateStack(t_stack);
t_stack -> setparent(this);
// 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
MCAutoStringRef t_filename;
/* UNCHECKED */ MCStringFormat(&t_filename, "%@/mainstack", MCcmd);
t_stack -> setfilename(*t_filename);
#else
t_stack -> setfilename(MCcmd);
#endif
if (IO_read_uint1(&type, stream) != IO_NORMAL
|| (type != OT_STACK && type != OT_ENCRYPT_STACK)
|| t_stack->load(stream, version, type) != IO_NORMAL)
{
delete t_stack;
return IO_ERROR;
}
if (t_stack->load_substacks(stream, version) != IO_NORMAL
|| IO_read_uint1(&type, stream) != IO_NORMAL
|| type != OT_END)
{
delete t_stack;
return IO_ERROR;
}
// We are reading the startup stack, so this becomes the root of the
// stack list.
stacks = t_stack;
r_stack = t_stack;
#ifndef _MOBILE
// Make sure parent script references are up to date.
if (s_loaded_parent_script_reference)
t_stack -> resolveparentscripts();
#else
// 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);
#endif
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;
}
// 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 *&sptr)
{
uint32_t version;
sptr = NULL;
// MW-2014-09-30: [[ ScriptOnlyStack ]] First see if it is a binary stack.
if (readheader(stream, version) == IO_NORMAL)
{
if (version > MAX_STACKFILE_VERSION)
{
MCresult->sets("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;
char *newsf;
if (IO_read_uint1(&charset, stream) != IO_NORMAL
|| IO_read_uint1(&type, stream) != IO_NORMAL
|| IO_read_cstring_legacy(newsf, stream, 2) != IO_NORMAL)
{
MCresult->sets("stack is corrupted, check for ~ backup file");
return checkloadstat(IO_ERROR);
}
delete newsf; // stackfiles is obsolete
MCtranslatechars = charset != CHARSET;
sptr = nil;
/* UNCHECKED */ MCStackSecurityCreateStack(sptr);
if (stacks == NULL)
sptr->setparent(this);
else
sptr->setparent(stacks);
sptr->setfilename(p_openpath);
if (MCModeCanLoadHome() && type == OT_HOME)
{
// MW-2013-11-19: [[ UnicodeFileFormat ]] These strings are never written out, so
// legacy.
char *lstring = NULL;
char *cstring = NULL;
IO_read_cstring_legacy(lstring, stream, 2);
IO_read_cstring_legacy(cstring, stream, 2);
delete lstring;
delete cstring;
}
MCresult -> clear();
if (IO_read_uint1(&type, stream) != IO_NORMAL
|| (type != OT_STACK && type != OT_ENCRYPT_STACK)
|| sptr->load(stream, version, type) != IO_NORMAL)
{
if (MCresult -> isclear())
MCresult->sets("stack is corrupted, check for ~ backup file");
destroystack(sptr, False);
sptr = NULL;
return checkloadstat(IO_ERROR);
}
// MW-2011-08-09: [[ Groups ]] Make sure F_GROUP_SHARED is set
// appropriately.
sptr -> checksharedgroups();
if (sptr->load_substacks(stream, version) != IO_NORMAL
|| IO_read_uint1(&type, stream) != IO_NORMAL
|| type != OT_END)
{
if (MCresult -> isclear())
MCresult->sets("stack is corrupted, check for ~ backup file");
destroystack(sptr, False);
sptr = NULL;
return checkloadstat(IO_ERROR);
}
}
// MW-2014-09-30: [[ ScriptOnlyStack ]] If we failed to load a stack from that step
// then check to see if it is a script file stack.
if (sptr == NULL)
{
// Clear the error return.
MCresult -> clear();
// Reset to position 0.
MCS_seek_set(stream, 0);
// Load the file into memory - we need to process a byteorder mark and any
// line endings.
int64_t t_size;
t_size = MCS_fsize(stream);
uint8_t *t_script;
/* UNCHECKED */ MCMemoryAllocate(t_size, t_script);
if (IO_read(t_script, t_size, stream) == IO_ERROR)
{
MCresult -> sets("unable to read file");
return checkloadstat(IO_ERROR);
}
// SN-2014-10-16: [[ Merge-6.7.0-rc-3 ]] Update to StringRef
MCFileEncodingType t_file_encoding = MCS_resolve_BOM(stream);
MCStringEncoding t_string_encoding;
MCAutoStringRef t_raw_script_string, t_LC_script_string;
uint32_t t_BOM_offset;
switch (t_file_encoding)
{
case kMCFileEncodingUTF8:
t_BOM_offset = 3;
t_string_encoding = kMCStringEncodingUTF8;
break;
case kMCFileEncodingUTF16:
t_string_encoding = kMCStringEncodingUTF16;
t_BOM_offset = 2;
break;
case kMCFileEncodingUTF16BE:
t_string_encoding = kMCStringEncodingUTF16BE;
t_BOM_offset = 2;
break;
case kMCFileEncodingUTF16LE:
t_string_encoding = kMCStringEncodingUTF16LE;
t_BOM_offset = 2;
break;
default:
// Assume native
t_string_encoding = kMCStringEncodingNative;
t_BOM_offset = 0;
break;
}
/* UNCHECKED */ MCStringCreateWithBytes(t_script + t_BOM_offset, t_size - t_BOM_offset, t_string_encoding, false, &t_raw_script_string);
/* UNCHECKED */ MCStringConvertLineEndingsToLiveCode(*t_raw_script_string, &t_LC_script_string);
MCMemoryDeallocate(t_script);
// 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)
{
// Parse <string> token.
Symbol_type t_type;
if (sp . next(t_type) == PS_NORMAL &&
t_type == ST_LIT)
{
MCNewAutoNameRef t_script_name;
MCNameClone(sp . gettoken_nameref(), &t_script_name);
// Parse end of line.
Parse_stat t_stat;
t_stat = sp . next(t_type);
if (t_stat == PS_EOL || t_stat == PS_EOF)
{
// 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
MCStringCopySubstring(*t_LC_script_string, MCRangeMake(t_index, MCStringGetLength(*t_LC_script_string) - t_index), &t_LC_script_body);
// Create a stack.
/* UNCHECKED */ MCStackSecurityCreateStack(sptr);
// Set its parent.
if (stacks == NULL)
sptr->setparent(this);
else
sptr->setparent(stacks);
// Set its filename.
sptr->setfilename(p_openpath);
// Set its name.
sptr -> setname(*t_script_name);
// Make it invisible
sptr -> setflag(False, F_VISIBLE);
// Set it up as script only.
sptr -> setasscriptonly(*t_LC_script_body);
}
}
}
}
// MW-2014-09-30: [[ ScriptOnlyStack ]] If we managed to load a stack as either binary
// or script, then do the normal processing.
if (sptr != NULL)
{
if (stacks != NULL)
{
MCStack *tstk = stacks;
do
{
if (sptr->hasname(tstk->getname()))
{
MCAutoNameRef t_stack_name;
/* UNCHECKED */ t_stack_name . Clone(sptr -> getname());
delete sptr;
sptr = NULL;
if (MCStringIsEqualTo(tstk -> getfilename(), p_openpath, kMCStringOptionCompareCaseless))
sptr = tstk;
else
{
MCdefaultstackptr->getcard()->message_with_valueref_args(MCM_reload_stack, tstk->getname(), p_openpath);
tstk = stacks;
do
{
if (MCNameIsEqualTo(t_stack_name, tstk->getname(), kMCCompareCaseless))
{
sptr = tstk;
break;
}
tstk = (MCStack *)tstk->next();
}
while (tstk != stacks);
}
return IO_NORMAL;
}
tstk = (MCStack *)tstk->next();
}
while (tstk != stacks);
}
appendstack(sptr);
sptr->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)
sptr -> resolveparentscripts();
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)
{
MCnoui = True;
MCscreen = new MCUIDC;
/* UNCHECKED */ MCStackSecurityCreateStack(stacks);
MCdefaultstackptr = MCstaticdefaultstackptr = stacks;
stacks->setparent(this);
stacks->setname_cstring("revScript");
uint4 size = (uint4)MCS_fsize(stream);
MCAutoPointer<char> script;
script = new char[size + 2];
(*script)[size] = '\n';
(*script)[size + 1] = '\0';
if (IO_read(*script, size, stream) != IO_NORMAL)
return IO_ERROR;
MCAutoStringRef t_script_str;
/* UNCHECKED */ MCStringCreateWithCString(*script, &t_script_str);
if (!stacks -> setscript_from_commandline(*t_script_str))
return IO_ERROR;
}
else
{
// MW-2008-06-12: [[ Bug 6476 ]] Media won't open HC stacks
if (!MCdispatcher->cut(True) || hc_import(p_name, stream, sptr) != IO_NORMAL)
{
MCresult->sets("file is not a stack");
return IO_ERROR;
}
}
return IO_NORMAL;
}
IO_stat MCDispatch::loadfile(MCStringRef p_name, MCStack *&sptr)
{
IO_handle stream;
MCAutoStringRef t_open_path;
bool t_found;
t_found = false;
if (!t_found)
{
if ((stream = MCS_open(p_name, kMCOpenFileModeRead, True, False, 0)) != NULL)
{
// SN-20015-06-01: [[ Bug 15432 ]] We want to use MCS_resolvepath to
// keep consistency and let '~' be resolved as it is in MCS_open
// MCS_resolve_path leaves a backslash-delimited path on Windows,
// and MCS_get_canonical_path is made to cope with this.
// In 7.0, MCS_resolvepath does not return a native path.
t_found = MCS_resolvepath(p_name, &t_open_path);
}
}
if (!t_found)
{
// SN-2014-11-18: [[ Bug 14043 ]] If p_path is was not correct, we then use the leaf, and append it to different locations
// in all the next steps.
MCAutoStringRef t_leaf_name;
uindex_t t_leaf_index;
if (MCStringLastIndexOfChar(p_name, PATH_SEPARATOR, UINDEX_MAX, kMCStringOptionCompareExact, t_leaf_index))
/* UNCHECKED */ MCStringCopySubstring(p_name, MCRangeMake(t_leaf_index + 1, MCStringGetLength(p_name) - (t_leaf_index + 1)), &t_leaf_name);
else
t_leaf_name = p_name;