home · contact · privacy
569d6f6ccfa2e92627eba39fcbcfad767d9d5cfa
[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/err_try_fgets.h" /* set_err_try_fgets_delim() */
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     world.delim            = "%\n";
35     set_err_try_fgets_delim(world.delim);
36     char * path_server_in  = "server/in";
37     char * path_server_out = "server/out";
38
39     /* Parse command line arguments. */
40     obey_argv(argc, argv);
41
42     /* So error exits also go through the client's cleanup() function. */
43     set_cleanup_func(cleanup);
44
45     /* Check existence of config files. */
46     exit_err(access(world.path_commands, F_OK), "No commands config file.");
47     exit_err(access(world.path_interface, F_OK), "No interface config file.");
48
49     /* Initialize the whole interface. */
50     world.winDB.t_screen = initscr();
51     set_cleanup_flag(CLEANUP_NCURSES);
52     noecho();
53     curs_set(0);
54     keypad(world.winDB.t_screen, TRUE);
55     init_command_db();      /* The command DB needs to be initialized before  */
56     load_interface_conf();  /* the interface, whose keybindings depend on it. */
57
58     /* Set handler for terminal window resizing. */
59     struct sigaction act;
60     memset(&act, 0, sizeof(act));
61     act.sa_handler = &winch_called;
62     exit_trouble(sigaction(SIGWINCH, &act, NULL), f_name, "sigaction()");
63
64     /* Open file streams for communicating with the server. */
65     exit_err(access(path_server_in, F_OK), "No server input file found.");
66     world.file_server_in = try_fopen(path_server_in, "a", f_name);
67     set_cleanup_flag(CLEANUP_SERVER_IN);
68     world.file_server_out = try_fopen(path_server_out, "r", f_name);
69     set_cleanup_flag(CLEANUP_SERVER_OUT);
70
71     /* This is where most everything happens. */
72     char * quit_msg = io_loop();
73
74     /* Leave properly. */
75     cleanup();
76     printf("%s\n", quit_msg);
77     exit(EXIT_SUCCESS);
78 }