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 char * f_name = "main()";
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";
37 /* Parse command line arguments. */
38 obey_argv(argc, argv);
40 /* So error exits also go through the client's cleanup() function. */
41 set_cleanup_func(cleanup);
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.");
47 /* Initialize the whole interface. */
48 world.winDB.t_screen = initscr();
49 set_cleanup_flag(CLEANUP_NCURSES);
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 world.focus_each_turn = 1;
57 /* Set handler for terminal window resizing. */
59 memset(&act, 0, sizeof(act));
60 act.sa_handler = &winch_called;
61 exit_trouble(sigaction(SIGWINCH, &act, NULL), f_name, "sigaction()");
63 /* Open file streams for communicating with the server. */
64 exit_err(access(path_server_in, F_OK), "No server input file found.");
65 world.file_server_in = try_fopen(path_server_in, "a", f_name);
66 set_cleanup_flag(CLEANUP_SERVER_IN);
67 world.file_server_out = try_fopen(path_server_out, "r", f_name);
68 set_cleanup_flag(CLEANUP_SERVER_OUT);
70 /* This is where most everything happens. */
71 char * quit_msg = io_loop();
75 exit_trouble(printf("%s\n", quit_msg) < 0, f_name, "printf()");