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