annotate libmlk-core/mlk/core/panic.c @ 645:83781cc87fca

core: rework game stack state mechanism The current model was fundamentally broken as the state could continue its execution when calling mlk_game_pop from itself (e.g. in update). The current model uses a sjlj mechanism with mlk_game_push/pop being disallowed in special state function like end, finish, suspend.
author David Demelier <markand@malikania.fr>
date Sun, 04 Feb 2024 15:24:00 +0100
parents 4349b591c3ac
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
88
44de3c528b63 core: implement basic panic mechanism, continue #2484 @1h
David Demelier <markand@malikania.fr>
parents:
diff changeset
1 /*
44de3c528b63 core: implement basic panic mechanism, continue #2484 @1h
David Demelier <markand@malikania.fr>
parents:
diff changeset
2 * panic.c -- unrecoverable error handling
44de3c528b63 core: implement basic panic mechanism, continue #2484 @1h
David Demelier <markand@malikania.fr>
parents:
diff changeset
3 *
445
773a082f0b91 misc: update copyright years
David Demelier <markand@malikania.fr>
parents: 431
diff changeset
4 * Copyright (c) 2020-2023 David Demelier <markand@malikania.fr>
88
44de3c528b63 core: implement basic panic mechanism, continue #2484 @1h
David Demelier <markand@malikania.fr>
parents:
diff changeset
5 *
44de3c528b63 core: implement basic panic mechanism, continue #2484 @1h
David Demelier <markand@malikania.fr>
parents:
diff changeset
6 * Permission to use, copy, modify, and/or distribute this software for any
44de3c528b63 core: implement basic panic mechanism, continue #2484 @1h
David Demelier <markand@malikania.fr>
parents:
diff changeset
7 * purpose with or without fee is hereby granted, provided that the above
44de3c528b63 core: implement basic panic mechanism, continue #2484 @1h
David Demelier <markand@malikania.fr>
parents:
diff changeset
8 * copyright notice and this permission notice appear in all copies.
44de3c528b63 core: implement basic panic mechanism, continue #2484 @1h
David Demelier <markand@malikania.fr>
parents:
diff changeset
9 *
44de3c528b63 core: implement basic panic mechanism, continue #2484 @1h
David Demelier <markand@malikania.fr>
parents:
diff changeset
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
44de3c528b63 core: implement basic panic mechanism, continue #2484 @1h
David Demelier <markand@malikania.fr>
parents:
diff changeset
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
44de3c528b63 core: implement basic panic mechanism, continue #2484 @1h
David Demelier <markand@malikania.fr>
parents:
diff changeset
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
44de3c528b63 core: implement basic panic mechanism, continue #2484 @1h
David Demelier <markand@malikania.fr>
parents:
diff changeset
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
44de3c528b63 core: implement basic panic mechanism, continue #2484 @1h
David Demelier <markand@malikania.fr>
parents:
diff changeset
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
44de3c528b63 core: implement basic panic mechanism, continue #2484 @1h
David Demelier <markand@malikania.fr>
parents:
diff changeset
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
44de3c528b63 core: implement basic panic mechanism, continue #2484 @1h
David Demelier <markand@malikania.fr>
parents:
diff changeset
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
44de3c528b63 core: implement basic panic mechanism, continue #2484 @1h
David Demelier <markand@malikania.fr>
parents:
diff changeset
17 */
44de3c528b63 core: implement basic panic mechanism, continue #2484 @1h
David Demelier <markand@malikania.fr>
parents:
diff changeset
18
44de3c528b63 core: implement basic panic mechanism, continue #2484 @1h
David Demelier <markand@malikania.fr>
parents:
diff changeset
19 #include <assert.h>
95
e82eca4f8606 core: simplify error/panic routines
David Demelier <markand@malikania.fr>
parents: 88
diff changeset
20 #include <stdio.h>
e82eca4f8606 core: simplify error/panic routines
David Demelier <markand@malikania.fr>
parents: 88
diff changeset
21 #include <stdlib.h>
88
44de3c528b63 core: implement basic panic mechanism, continue #2484 @1h
David Demelier <markand@malikania.fr>
parents:
diff changeset
22
586
4349b591c3ac cmake: put NLS back
David Demelier <markand@malikania.fr>
parents: 547
diff changeset
23 #include "core_p.h"
486
d6757c30658e core: rework errors
David Demelier <markand@malikania.fr>
parents: 475
diff changeset
24 #include "err.h"
88
44de3c528b63 core: implement basic panic mechanism, continue #2484 @1h
David Demelier <markand@malikania.fr>
parents:
diff changeset
25 #include "panic.h"
44de3c528b63 core: implement basic panic mechanism, continue #2484 @1h
David Demelier <markand@malikania.fr>
parents:
diff changeset
26
229
e71039d820a7 doc: improve documentation
David Demelier <markand@malikania.fr>
parents: 168
diff changeset
27 static void
486
d6757c30658e core: rework errors
David Demelier <markand@malikania.fr>
parents: 475
diff changeset
28 terminate(const char *what)
95
e82eca4f8606 core: simplify error/panic routines
David Demelier <markand@malikania.fr>
parents: 88
diff changeset
29 {
586
4349b591c3ac cmake: put NLS back
David Demelier <markand@malikania.fr>
parents: 547
diff changeset
30 fprintf(stderr, _("abort: %s\n"), what);
162
629f55f3961e core: rework states
David Demelier <markand@malikania.fr>
parents: 121
diff changeset
31 abort();
95
e82eca4f8606 core: simplify error/panic routines
David Demelier <markand@malikania.fr>
parents: 88
diff changeset
32 exit(1);
e82eca4f8606 core: simplify error/panic routines
David Demelier <markand@malikania.fr>
parents: 88
diff changeset
33 }
88
44de3c528b63 core: implement basic panic mechanism, continue #2484 @1h
David Demelier <markand@malikania.fr>
parents:
diff changeset
34
486
d6757c30658e core: rework errors
David Demelier <markand@malikania.fr>
parents: 475
diff changeset
35 void (*mlk_panic_handler)(const char *) = terminate;
d6757c30658e core: rework errors
David Demelier <markand@malikania.fr>
parents: 475
diff changeset
36
d6757c30658e core: rework errors
David Demelier <markand@malikania.fr>
parents: 475
diff changeset
37 static void
d6757c30658e core: rework errors
David Demelier <markand@malikania.fr>
parents: 475
diff changeset
38 alive(void)
d6757c30658e core: rework errors
David Demelier <markand@malikania.fr>
parents: 475
diff changeset
39 {
d6757c30658e core: rework errors
David Demelier <markand@malikania.fr>
parents: 475
diff changeset
40 /*
d6757c30658e core: rework errors
David Demelier <markand@malikania.fr>
parents: 475
diff changeset
41 * This should not happen, if it does it means the user did not fully
d6757c30658e core: rework errors
David Demelier <markand@malikania.fr>
parents: 475
diff changeset
42 * satisfied the constraint of mlk_panic_handler.
d6757c30658e core: rework errors
David Demelier <markand@malikania.fr>
parents: 475
diff changeset
43 */
586
4349b591c3ac cmake: put NLS back
David Demelier <markand@malikania.fr>
parents: 547
diff changeset
44 fprintf(stderr, _("abort: panic handler returned\n"));
486
d6757c30658e core: rework errors
David Demelier <markand@malikania.fr>
parents: 475
diff changeset
45 exit(1);
d6757c30658e core: rework errors
David Demelier <markand@malikania.fr>
parents: 475
diff changeset
46 }
95
e82eca4f8606 core: simplify error/panic routines
David Demelier <markand@malikania.fr>
parents: 88
diff changeset
47
229
e71039d820a7 doc: improve documentation
David Demelier <markand@malikania.fr>
parents: 168
diff changeset
48 void
465
01f5580e43d1 core: panic -> mlk_panic
David Demelier <markand@malikania.fr>
parents: 445
diff changeset
49 mlk_panicf(const char *fmt, ...)
88
44de3c528b63 core: implement basic panic mechanism, continue #2484 @1h
David Demelier <markand@malikania.fr>
parents:
diff changeset
50 {
44de3c528b63 core: implement basic panic mechanism, continue #2484 @1h
David Demelier <markand@malikania.fr>
parents:
diff changeset
51 assert(fmt);
44de3c528b63 core: implement basic panic mechanism, continue #2484 @1h
David Demelier <markand@malikania.fr>
parents:
diff changeset
52
44de3c528b63 core: implement basic panic mechanism, continue #2484 @1h
David Demelier <markand@malikania.fr>
parents:
diff changeset
53 va_list ap;
486
d6757c30658e core: rework errors
David Demelier <markand@malikania.fr>
parents: 475
diff changeset
54 char buf[PANIC_LINE_MAX];
88
44de3c528b63 core: implement basic panic mechanism, continue #2484 @1h
David Demelier <markand@malikania.fr>
parents:
diff changeset
55
95
e82eca4f8606 core: simplify error/panic routines
David Demelier <markand@malikania.fr>
parents: 88
diff changeset
56 /*
486
d6757c30658e core: rework errors
David Demelier <markand@malikania.fr>
parents: 475
diff changeset
57 * We can't use mlk_panicva directly because we won't be able to call
d6757c30658e core: rework errors
David Demelier <markand@malikania.fr>
parents: 475
diff changeset
58 * va_end then.
95
e82eca4f8606 core: simplify error/panic routines
David Demelier <markand@malikania.fr>
parents: 88
diff changeset
59 */
88
44de3c528b63 core: implement basic panic mechanism, continue #2484 @1h
David Demelier <markand@malikania.fr>
parents:
diff changeset
60 va_start(ap, fmt);
486
d6757c30658e core: rework errors
David Demelier <markand@malikania.fr>
parents: 475
diff changeset
61 vsnprintf(buf, sizeof (buf), fmt, ap);
88
44de3c528b63 core: implement basic panic mechanism, continue #2484 @1h
David Demelier <markand@malikania.fr>
parents:
diff changeset
62 va_end(ap);
95
e82eca4f8606 core: simplify error/panic routines
David Demelier <markand@malikania.fr>
parents: 88
diff changeset
63
486
d6757c30658e core: rework errors
David Demelier <markand@malikania.fr>
parents: 475
diff changeset
64 mlk_panic_handler(buf);
d6757c30658e core: rework errors
David Demelier <markand@malikania.fr>
parents: 475
diff changeset
65 alive();
88
44de3c528b63 core: implement basic panic mechanism, continue #2484 @1h
David Demelier <markand@malikania.fr>
parents:
diff changeset
66 }
44de3c528b63 core: implement basic panic mechanism, continue #2484 @1h
David Demelier <markand@malikania.fr>
parents:
diff changeset
67
229
e71039d820a7 doc: improve documentation
David Demelier <markand@malikania.fr>
parents: 168
diff changeset
68 void
465
01f5580e43d1 core: panic -> mlk_panic
David Demelier <markand@malikania.fr>
parents: 445
diff changeset
69 mlk_panicva(const char *fmt, va_list ap)
88
44de3c528b63 core: implement basic panic mechanism, continue #2484 @1h
David Demelier <markand@malikania.fr>
parents:
diff changeset
70 {
44de3c528b63 core: implement basic panic mechanism, continue #2484 @1h
David Demelier <markand@malikania.fr>
parents:
diff changeset
71 assert(fmt);
475
1a1265445157 core: forgot panic_handler
David Demelier <markand@malikania.fr>
parents: 465
diff changeset
72 assert(mlk_panic_handler);
88
44de3c528b63 core: implement basic panic mechanism, continue #2484 @1h
David Demelier <markand@malikania.fr>
parents:
diff changeset
73
486
d6757c30658e core: rework errors
David Demelier <markand@malikania.fr>
parents: 475
diff changeset
74 char buf[PANIC_LINE_MAX];
d6757c30658e core: rework errors
David Demelier <markand@malikania.fr>
parents: 475
diff changeset
75
d6757c30658e core: rework errors
David Demelier <markand@malikania.fr>
parents: 475
diff changeset
76 vsnprintf(buf, sizeof (buf), fmt, ap);
d6757c30658e core: rework errors
David Demelier <markand@malikania.fr>
parents: 475
diff changeset
77 mlk_panic_handler(buf);
d6757c30658e core: rework errors
David Demelier <markand@malikania.fr>
parents: 475
diff changeset
78 alive();
95
e82eca4f8606 core: simplify error/panic routines
David Demelier <markand@malikania.fr>
parents: 88
diff changeset
79 }
e82eca4f8606 core: simplify error/panic routines
David Demelier <markand@malikania.fr>
parents: 88
diff changeset
80
229
e71039d820a7 doc: improve documentation
David Demelier <markand@malikania.fr>
parents: 168
diff changeset
81 void
547
c7664b679a95 misc: remove error codes for now
David Demelier <markand@malikania.fr>
parents: 486
diff changeset
82 mlk_panic(void)
95
e82eca4f8606 core: simplify error/panic routines
David Demelier <markand@malikania.fr>
parents: 88
diff changeset
83 {
547
c7664b679a95 misc: remove error codes for now
David Demelier <markand@malikania.fr>
parents: 486
diff changeset
84 mlk_panicf("%s", mlk_err());
88
44de3c528b63 core: implement basic panic mechanism, continue #2484 @1h
David Demelier <markand@malikania.fr>
parents:
diff changeset
85 }