ObjFW  Diff

Differences From Artifact [bde88848cd]:

To Artifact [594a3c0f6e]:


1
2

3
4
5
6
7
8
9
1

2
3
4
5
6
7
8
9

-
+







/*
 * Copyright (c) 2008-2022 Jonathan Schleifer <js@nil.im>
 * Copyright (c) 2008-2023 Jonathan Schleifer <js@nil.im>
 *
 * All rights reserved.
 *
 * This file is part of ObjFW. It may be distributed under the terms of the
 * Q Public License 1.0, which can be found in the file LICENSE.QPL included in
 * the packaging of this file.
 *
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
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







+





+



-








+


+







#endif
#import "OFLocale.h"
#import "OFNotificationCenter.h"
#import "OFPair.h"
#import "OFRunLoop+Private.h"
#import "OFRunLoop.h"
#import "OFSandbox.h"
#import "OFStdIOStream.h"
#import "OFString.h"
#import "OFSystemInfo.h"
#import "OFThread+Private.h"
#import "OFThread.h"

#import "OFActivateSandboxFailedException.h"
#import "OFInvalidArgumentException.h"
#import "OFOutOfMemoryException.h"
#import "OFOutOfRangeException.h"
#import "OFSandboxActivationFailedException.h"

#if defined(OF_MACOS)
# include <crt_externs.h>
#elif defined(OF_WINDOWS)
# include <windows.h>
extern int _CRT_glob;
extern void __wgetmainargs(int *, wchar_t ***, wchar_t ***, int, int *);
#elif defined(OF_AMIGAOS)
# define Class IntuitionClass
# include <proto/exec.h>
# include <proto/dos.h>
# undef Class
#elif !defined(OF_IOS)
extern char **environ;
#endif

#ifdef OF_PSP
# include <pspkerneltypes.h>
# include <psploadexec.h>
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
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







+
+








-
-
-
+
+


-
-
+
+


+
+







	  andArgumentValues: (char **[])argv
       andWideArgumentCount: (int)wargc
      andWideArgumentValues: (wchar_t *[])wargv;
#endif
- (void)of_run;
@end

const OFNotificationName OFApplicationDidFinishLaunchingNotification =
    @"OFApplicationDidFinishLaunchingNotification";
const OFNotificationName OFApplicationWillTerminateNotification =
    @"OFApplicationWillTerminateNotification";
static OFApplication *app = nil;

static void
atexitHandler(void)
{
	id <OFApplicationDelegate> delegate = app.delegate;

	[[OFNotificationCenter defaultCenter]
	    postNotificationName: OFApplicationWillTerminateNotification
	OFNotification *notification = [OFNotification
	    notificationWithName: OFApplicationWillTerminateNotification
			  object: app];

	if ([delegate respondsToSelector: @selector(applicationWillTerminate)])
		[delegate applicationWillTerminate];
	if ([delegate respondsToSelector: @selector(applicationWillTerminate:)])
		[delegate applicationWillTerminate: notification];

	[delegate release];

	[[OFNotificationCenter defaultCenter] postNotification: notification];

#if defined(OF_HAVE_THREADS) && defined(OF_HAVE_SOCKETS) && \
    defined(OF_AMIGAOS) && !defined(OF_MORPHOS)
	OFSocketDeinit();
#endif
}

256
257
258
259
260
261
262
263
264
265


266
267
268
269
270
271
272
262
263
264
265
266
267
268



269
270
271
272
273
274
275
276
277







-
-
-
+
+







				if ([tmp hasPrefix: @"="]) {
					objc_autoreleasePoolPop(pool);
					continue;
				}

				pos = [tmp rangeOfString: @"="].location;
				if (pos == OFNotFound) {
					fprintf(stderr,
					    "Warning: Invalid environment "
					    "variable: %s\n", tmp.UTF8String);
					OFLog(@"Warning: Invalid environment "
					    "variable: %@", tmp);
					continue;
				}

				key = [tmp substringToIndex: pos];
				value = [tmp substringFromIndex: pos + 1];
				[_environment setObject: value forKey: key];

298
299
300
301
302
303
304
305
306
307


308
309
310
311
312
313
314
303
304
305
306
307
308
309



310
311
312
313
314
315
316
317
318







-
-
-
+
+







				if ([tmp hasPrefix: @"="]) {
					objc_autoreleasePoolPop(pool);
					continue;
				}

				pos = [tmp rangeOfString: @"="].location;
				if (pos == OFNotFound) {
					fprintf(stderr,
					    "Warning: Invalid environment "
					    "variable: %s\n", tmp.UTF8String);
					OFLog(@"Warning: Invalid environment "
					    "variable: %@", tmp);
					continue;
				}

				key = [tmp substringToIndex: pos];
				value = [tmp substringFromIndex: pos + 1];
				[_environment setObject: value forKey: key];

392
393
394
395
396
397
398
399
400


401
402
403
404
405
406
407
396
397
398
399
400
401
402


403
404
405
406
407
408
409
410
411







-
-
+
+








			for (; *env != NULL; env++) {
				void *pool = objc_autoreleasePoolPush();
				OFString *key, *value;
				char *sep;

				if ((sep = strchr(*env, '=')) == NULL) {
					fprintf(stderr, "Warning: Invalid "
					    "environment variable: %s\n", *env);
					OFLog(@"Warning: Invalid environment "
					    "variable: %s", *env);
					continue;
				}

				key = [OFString
				    stringWithCString: *env
					     encoding: encoding
					       length: sep - *env];
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
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







+



















+
+
+
+
+
+
+
-
+
+







#undef REGISTER_SIGNAL
}

- (void)of_run
{
	void *pool = objc_autoreleasePoolPush();
	OFRunLoop *runLoop;
	OFNotification *notification;

#ifdef OF_HAVE_THREADS
	[OFThread of_createMainThread];
	runLoop = [OFRunLoop currentRunLoop];
#else
	runLoop = [[[OFRunLoop alloc] init] autorelease];
#endif

	[OFRunLoop of_setMainRunLoop: runLoop];

	objc_autoreleasePoolPop(pool);

	/*
	 * Note: runLoop is still valid after the release of the pool, as
	 * of_setMainRunLoop: retained it. However, we only have a weak
	 * reference to it now, whereas we had a strong reference before.
	 */

	pool = objc_autoreleasePoolPush();

	notification = [OFNotification
	    notificationWithName: OFApplicationDidFinishLaunchingNotification
			  object: app];

	[[OFNotificationCenter defaultCenter] postNotification: notification];

	[_delegate applicationDidFinishLaunching];
	[_delegate applicationDidFinishLaunching: notification];

	objc_autoreleasePoolPop(pool);

	[runLoop run];
}

- (void)terminate
{
650
651
652
653
654
655
656
657

658
659
660
661
662
663
664
663
664
665
666
667
668
669

670
671
672
673
674
675
676
677







-
+







	}

	sandbox->_unveiledPathsIndex = unveiledPathsCount;

	promises = [sandbox.pledgeString cStringWithEncoding: encoding];

	if (pledge(promises, NULL) != 0)
		@throw [OFSandboxActivationFailedException
		@throw [OFActivateSandboxFailedException
		    exceptionWithSandbox: sandbox
				   errNo: errno];

	objc_autoreleasePoolPop(pool);

	if (_activeSandbox == nil)
		_activeSandbox = [sandbox retain];
678
679
680
681
682
683
684
685

686
687
688
689
690
691
692
693
694
695
696
691
692
693
694
695
696
697

698
699
700
701
702
703
704
705
706
707
708
709







-
+











	if (sandbox.unveiledPaths.count != 0)
		@throw [OFInvalidArgumentException exception];

	promises = [sandbox.pledgeString
	    cStringWithEncoding: [OFLocale encoding]];

	if (pledge(NULL, promises) != 0)
		@throw [OFSandboxActivationFailedException
		@throw [OFActivateSandboxFailedException
		    exceptionWithSandbox: sandbox
				   errNo: errno];

	objc_autoreleasePoolPop(pool);

	if (_activeSandboxForChildProcesses == nil)
		_activeSandboxForChildProcesses = [sandbox retain];
# endif
}
#endif
@end