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 */
26 int main(int argc, char * argv[])
28 /* Declare hard-coded paths and values here. */
29 world.path_commands = "confclient/commands";
30 world.path_interface = "confclient/interface_conf";
31 world.winDB.legal_ids = "012ciklm";
32 char * path_server_in = "server/in";
33 char * path_server_out = "server/out";
35 /* Parse command line arguments. */
36 obey_argv(argc, argv);
38 /* So error exits also go through the client's cleanup() function. */
39 set_cleanup_func(cleanup);
41 /* Check existence of config files. */
42 exit_err(access(world.path_commands, F_OK), "No commands config file.");
43 exit_err(access(world.path_interface, F_OK), "No interface config file.");
45 /* Initialize the whole interface. */
46 world.winDB.t_screen = initscr();
47 set_cleanup_flag(CLEANUP_NCURSES);
50 keypad(world.winDB.t_screen, TRUE);
51 init_command_db(); /* The command DB needs to be initialized before */
52 load_interface_conf(); /* the interface, whose keybindings depend on it. */
53 world.focus_each_turn = 1;
55 /* Set handler for terminal window resizing. */
57 memset(&act, 0, sizeof(act));
58 act.sa_handler = &winch_called;
59 exit_trouble(sigaction(SIGWINCH, &act, NULL), __func__, "sigaction");
61 /* Open file streams for communicating with the server. */
62 exit_err(access(path_server_in, F_OK), "No server input file found.");
63 world.file_server_in = try_fopen(path_server_in, "a", __func__);
64 set_cleanup_flag(CLEANUP_SERVER_IN);
65 world.file_server_out = try_fopen(path_server_out, "r", __func__);
66 set_cleanup_flag(CLEANUP_SERVER_OUT);
68 /* This is where most everything happens. */
69 char * quit_msg = io_loop();
73 exit_trouble(printf("%s\n", quit_msg) < 0, __func__, "printf");