4 #include <errno.h> /* global errno */
5 #include <fcntl.h> /* open() */
6 #include <limits.h> /* PIPE_BUF */
7 #include <ncurses.h> /* halfdelay(), getch() */
8 #include <stddef.h> /* NULL */
9 #include <stdint.h> /* uint8_t, uint16_t, uint32_t */
10 #include <stdio.h> /* FILE, sprintf(), fseek() */
11 #include <string.h> /* strcmp(), strlen(), memcpy() */
12 #include <stdlib.h> /* free(), atoi() */
13 #include <sys/stat.h> /* stat() */
14 #include <unistd.h> /* access(), write() */
15 #include "../common/try_malloc.h" /* try_malloc() */
16 #include "../common/rexit.h" /* exit_trouble(), exit_err() */
17 #include "../common/readwrite.h" /* try_fopen(), try_fclose(), try_fgets(),
20 #include "control.h" /* try_key() */
21 #include "map.h" /* map_center() */
22 #include "misc.h" /* reset_windows() */
23 #include "windows.h" /* reset_windows_on_winch(), draw_all_wins() */
24 #include "world.h" /* world global */
28 /* Read next lines of "file" up to (and excluding) a line "%\n" into the
29 * world.player_inventory string.
31 static void read_inventory(char * read_buf, uint32_t linemax, FILE * file);
33 /* Read the next characters in "file" into world.map.cells. In detail: Read
34 * world.map.size.y times world.map.size.x characters, followed by one ignored
35 * character (that we assume is a newline).
37 static void read_map_cells(FILE * file);
39 /* Repeatedly use try_fgets() with given arguments to read the remaining lines
40 * of "file" into the world.log string.
42 static void read_log(char * read_buf, uint32_t linemax, FILE * file);
44 /* Return value seen by atoi() in next line of "file" when passed to try_fgets()
45 * with the given arguments.
47 static uint16_t read_value_from_line(char * read_buf, uint32_t linemax,
50 /* If the server's out file has changed since the last read_world(), return a
51 * pointer to its file descriptor; else, return NULL.
53 * Two tests are performed to check for a file change. The file's last data
54 * modification time in seconds via stat() is compared against world.last_update
55 * (and if it is changed, world.last_update is re-set to it). If this does not
56 * verify a change, the first bytes of the file are read to compare the game
57 * turn number described therein to the last read turn number in world.turn.
59 * The stat() check is mostly useless, for it only detects file updates once a
60 * second. But the turn check fails if a new world is generated from turn 1 on:
61 * the new world also starts in turn 1, not signifying any world change to the
62 * turn check. The stat() check detects this change with at most 1 second delay.
64 static FILE * changed_server_out_file(char * path);
66 /* Attempt to read the server's out file as representation of the game world in
67 * a hard-coded serialization format. Returns 1 on success and 0 if the out file
68 * wasn't read for supposedly not having changed since a last read_world() call.
70 * map_center() is triggered by the first successful read_world() or on turn 1,
71 * so the client focuses the map window on the player on client and world start.
73 static uint8_t read_world();
77 static void read_inventory(char * read_buf, uint32_t linemax, FILE * file)
79 char * f_name = "read_inventory()";
80 char * delimiter = "%\n";
81 free(world.player_inventory);
82 world.player_inventory = NULL; /* Avoids illegal strlen() below. */
85 try_fgets(read_buf, linemax + 1, file, f_name);
86 if (!(strcmp(read_buf, delimiter)))
91 if (NULL != world.player_inventory)
93 old_size = strlen(world.player_inventory);
95 int new_size = strlen(read_buf);
96 char * new_inventory = try_malloc(old_size + new_size + 1, f_name);
97 memcpy(new_inventory, world.player_inventory, old_size);
98 sprintf(new_inventory + old_size, "%s", read_buf);
99 free(world.player_inventory);
100 world.player_inventory = new_inventory;
102 world.player_inventory[strlen(world.player_inventory) - 1] = '\0';
103 world.player_inventory_select = 0;
108 static void read_map_cells(FILE * file)
110 char * f_name = "read_map_cells()";
111 free(world.map.cells);
112 world.map.cells = try_malloc(world.map.size.y * world.map.size.x, f_name);
114 for (y = 0; y < world.map.size.y; y++)
116 for (x = 0; x < world.map.size.x; x++)
118 char c = try_fgetc(file, f_name);
119 world.map.cells[(y * world.map.size.x) + x] = c;
121 try_fgetc(file, f_name);
127 static void read_log(char * read_buf, uint32_t linemax, FILE * file)
129 char * f_name = "read_log()";
132 while (try_fgets(read_buf, linemax + 1, file, f_name))
135 if (NULL != world.log)
137 old_size = strlen(world.log);
139 int new_size = strlen(read_buf);
140 char * new_log = try_malloc(old_size + new_size + 1, f_name);
141 memcpy(new_log, world.log, old_size);
142 sprintf(new_log + old_size, "%s", read_buf);
150 static uint16_t read_value_from_line(char * read_buf, uint32_t linemax,
153 char * f_name = "read_value_from_line()";
154 try_fgets(read_buf, linemax + 1, file, f_name);
155 return atoi(read_buf);
160 static FILE * changed_server_out_file(char * path)
162 char * f_name = "changed_server_out_file()";
163 struct stat stat_buf;
164 exit_trouble(stat(path, &stat_buf), f_name, "stat()");
165 if (stat_buf.st_mtime != world.last_update)
167 world.last_update = stat_buf.st_mtime;
168 return try_fopen(path, "r", f_name);
170 FILE * file = try_fopen(path, "r", f_name);
172 try_fgets(turn_string, 6, file, f_name);
173 if (world.turn == atoi(turn_string))
175 try_fclose(file, f_name);
178 exit_trouble(fseek(file, 0, SEEK_SET), f_name, "fseek()");
184 static uint8_t read_world()
186 char * f_name = "read_world()";
187 char * path = "server/out";
188 char * quit_msg = "No server out file found to read. Server may be down.";
189 static uint8_t first_read = 1;
190 exit_err(access(path, F_OK), quit_msg);
191 FILE * file = changed_server_out_file(path);
196 uint32_t linemax = textfile_sizes(file, NULL);
197 char * read_buf = try_malloc(linemax + 1, f_name);
198 world.turn = read_value_from_line(read_buf, linemax, file);
199 world.player_score = read_value_from_line(read_buf, linemax, file);
200 world.player_lifepoints = read_value_from_line(read_buf, linemax, file);
201 read_inventory(read_buf, linemax, file);
202 world.player_pos.y = read_value_from_line(read_buf, linemax, file);
203 world.player_pos.x = read_value_from_line(read_buf, linemax, file);
204 if (1 == world.turn || first_read)
209 world.map.size.y = read_value_from_line(read_buf, linemax, file);
210 world.map.size.x = read_value_from_line(read_buf, linemax, file);
211 read_map_cells(file);
212 read_log(read_buf, linemax, file);
214 try_fclose(file, f_name);
220 extern void try_send(char * msg)
222 char * f_name = "try_send()";
223 uint32_t msg_size = strlen(msg) + 1;
224 char * err = "try_send() tries to send message larger than PIPE_BUF bytes.";
225 exit_err(msg_size > PIPE_BUF, err);
230 fd_out = open(world.path_server_in, O_WRONLY | O_NONBLOCK);
235 exit_err(-1 == fd_out && ENXIO != errno, "Server fifo not found.");
238 exit_err(0 == j, "Failed to open server fifo for writing.");
242 int test = write(fd_out, msg, msg_size);
249 exit_err(0 == j, "Failed to write to server fifo.");
250 exit_trouble(-1 == close(fd_out), f_name, "close()");
255 extern char * io_loop()
257 world.halfdelay = 1; /* Ensures read_world() is only called 10 */
258 halfdelay(world.halfdelay); /* times a second during user inactivity. */
259 uint8_t change_in_client = 0;
264 reset_windows_on_winch();
268 if (read_world() || change_in_client)
272 change_in_client = 0;
276 change_in_client = try_key((uint16_t) key);
277 if (2 == change_in_client)
284 return "Sent QUIT to server.";