-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
74 lines (62 loc) · 1.73 KB
/
main.c
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
#include <errno.h>
#if !defined(__CYGWIN__) && !defined(__MINGW32__)
#include <execinfo.h>
#endif /* !defined(__CYGWIN__) && !defined(__MINGW32__) */
#include <getopt.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include "game.h"
static struct option longopts[] =
{
{"seed", required_argument, NULL, 's'},
{"help", no_argument, NULL, 'h'},
{NULL, 0, NULL, 0}
};
static void usage()
{
const char *usage_str =
"-s --seed : Set the game seed. Default: time based seed\n"
"-h --help : Print this usage text\n";
printf("%s", usage_str);
}
#if !defined(__CYGWIN__) && !defined(__MINGW32__)
static void crash_handler(int signal)
{
void *callstack[16];
int frames = 0;
frames = backtrace(callstack, 16);
backtrace_symbols_fd(callstack, frames, STDERR_FILENO);
exit(EXIT_FAILURE);
}
#endif /* !defined(__CYGWIN__) && !defined(__MINGW32__) */
int main(int argc, char **argv)
{
struct game_state game;
int seed = 0;
int ch;
#if !defined(__CYGWIN__) && !defined(__MINGW32__)
signal(SIGSEGV, crash_handler);
signal(SIGABRT, crash_handler);
#endif /* !defined(__CYGWIN__) && !defined(__MINGW32__) */
while ((ch = getopt_long(argc, argv, "hs:", longopts, NULL)) != -1) {
switch (ch) {
case 's':
if (!game_strtol(optarg, &seed)) {
fprintf(stderr, "Invalid seed value\n");
exit(EXIT_FAILURE);
}
break;
case 'h':
usage();
exit(EXIT_SUCCESS);
default:
usage();
exit(EXIT_FAILURE);
}
}
init_game_state(&game, seed);
game_loop(&game);
return 0;
}