home · contact · privacy
Client: Handle SIGWINCH on OS X systems.
[plomrogue] / src / client / main.c
1 /* main.c */
2
3 #define _POSIX_C_SOURCE 1 /* sigaction, sigaction() */
4 #define _DARWIN_C_SOURCE 1 /* SIGWINCH on OS X */
5 #include <ncurses.h> /* keypad() */
6 #include <signal.h> /* SIGWINCH, sigaction, sigaction() */
7 #include <stddef.h> /* NULL */
8 #include <stdlib.h> /* exit() */
9 #include <string.h> /* memset() */
10 #include <unistd.h> /* access() */
11 #include "../common/readwrite.h" /* try_fopen() */
12 #include "../common/rexit.h" /* set_cleanup_func(), exit_trouble(),exit_err() */
13 #include "cleanup.h" /* cleanup(), set_cleanup_flag() */
14 #include "command_db.h" /* init_command_db() */
15 #include "interface_conf.h" /* load_interface_conf(), obey_argv() */
16 #include "io.h" /* io_loop() */
17 #include "windows.h" /* winch_called() */
18 #include "world.h" /* struct World */
19
20
21
22 struct World world;
23
24
25
26 int main(int argc, char * argv[])
27 {
28     char * f_name = "main()";
29
30     /* Declare hard-coded paths and values here. */
31     world.path_commands    = "confclient/commands";
32     world.path_interface   = "confclient/interface_conf";
33     world.winDB.legal_ids  = "012ciklm";
34     char * path_server_in  = "server/in";
35     char * path_server_out = "server/out";
36
37     /* Parse command line arguments. */
38     obey_argv(argc, argv);
39
40     /* So error exits also go through the client's cleanup() function. */
41     set_cleanup_func(cleanup);
42
43     /* Check existence of config files. */
44     exit_err(access(world.path_commands, F_OK), "No commands config file.");
45     exit_err(access(world.path_interface, F_OK), "No interface config file.");
46
47     /* Initialize the whole interface. */
48     world.winDB.t_screen = initscr();
49     set_cleanup_flag(CLEANUP_NCURSES);
50     noecho();
51     curs_set(0);
52     keypad(world.winDB.t_screen, TRUE);
53     init_command_db();      /* The command DB needs to be initialized before  */
54     load_interface_conf();  /* the interface, whose keybindings depend on it. */
55
56     /* Set handler for terminal window resizing. */
57     struct sigaction act;
58     memset(&act, 0, sizeof(act));
59     act.sa_handler = &winch_called;
60     exit_trouble(sigaction(SIGWINCH, &act, NULL), f_name, "sigaction()");
61
62     /* Open file streams for communicating with the server. */
63     exit_err(access(path_server_in, F_OK), "No server input file found.");
64     world.file_server_in = try_fopen(path_server_in, "a", f_name);
65     set_cleanup_flag(CLEANUP_SERVER_IN);
66     world.file_server_out = try_fopen(path_server_out, "r", f_name);
67     set_cleanup_flag(CLEANUP_SERVER_OUT);
68
69     /* This is where most everything happens. */
70     char * quit_msg = io_loop();
71
72     /* Leave properly. */
73     cleanup();
74     printf("%s\n", quit_msg);
75     exit(EXIT_SUCCESS);
76 }