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