4 #include <errno.h> /* global errno */
5 #include <fcntl.h> /* open(), O_RDONLY, O_NONBLOCK */
6 #include <limits.h> /* PIPE_BUF */
7 #include <stddef.h> /* size_t, NULL */
8 #include <stdint.h> /* uint8_t, uint16_t, uint32_t */
9 #include <stdio.h> /* define FILE, sprintf() */
10 #include <stdlib.h> /* free() */
11 #include <string.h> /* strlen(), memset(), memcpy() */
12 #include <unistd.h> /* read(), close() */
13 #include "../common/readwrite.h" /* try_fopen(), try_fclose_unlink_rename(),
14 * try_fwrite(), try_fputc()
16 #include "../common/rexit.h" /* exit_trouble() */
17 #include "../common/try_malloc.h" /* try_malloc() */
18 #include "cleanup.h" /* set_cleanup_flag() */
19 #include "map_objects.h" /* structs MapObj, MapObjDef, get_map_obj_def() */
20 #include "world.h" /* global world */
24 /* Cut out and return first \0-terminated string from world.queue and
25 * appropriately reduce world.queue_size. Return NULL if queue is empty.
26 * Superfluous \0 bytes after the string are also cut out. Should the queue
27 * start with \0 bytes, those are cut out, but NULL is returned instead of "".
29 static char * get_message_from_queue();
31 /* Read fifo input to put into queue. Only stop reading when bytes were received
32 * and the receiving has stopped. Read max. PIPE_BUF-sized chunks for atomicity.
34 static void read_fifo_into_queue();
36 /* Write to output file the world state as it is to be visible to clients. */
37 static void update_out_file();
39 /* Write "value" to new \n-delimited line of "file". */
40 static void write_value_as_line(uint32_t value, FILE * file);
42 /* Write to "file" player's inventory, one item name per line. End in "%\n". */
43 static void write_inventory(struct MapObj * player, FILE * file);
45 /* Write to "file" game map, with map objects super-imposed. Write one row per
46 * \n-delimited line. Super-impose animated objects over inanimate objects.
48 static void write_map(FILE * file);
52 static char * get_message_from_queue()
54 char * f_name = "get_message_from_queue()";
55 char * message = NULL;
56 size_t cutout_len = strlen(world.queue);
60 message = try_malloc(cutout_len, f_name);
61 memcpy(message, world.queue, cutout_len);
64 cutout_len != world.queue_size && '\0' == world.queue[cutout_len];
66 world.queue_size = world.queue_size - cutout_len;
67 if (0 == world.queue_size)
69 free(world.queue); /* NULL so read_fifo_into_queue() may free() this */
70 world.queue = NULL; /* every time, even when it's un-allocated first. */
74 char * new_queue = try_malloc(world.queue_size, f_name);
75 memcpy(new_queue, &(world.queue[cutout_len]), world.queue_size);
77 world.queue = new_queue;
84 static void read_fifo_into_queue()
86 char * f_name = "read_fifo_into_queue()";
87 uint32_t buf_size = PIPE_BUF;
88 int fdesc_infile = open(world.path_in, O_RDONLY | O_NONBLOCK);
89 exit_trouble(-1 == fdesc_infile, "open()", f_name);
91 memset(buf, 0, buf_size);
93 uint8_t read_state = 0; /* 0: waiting for input. 1: started receiving it. */
96 bytes_read = read(fdesc_infile, buf, buf_size);
100 uint32_t old_queue_size = world.queue_size;
101 world.queue_size = world.queue_size + bytes_read;
102 char * new_queue = try_malloc(world.queue_size, f_name);
103 memcpy(new_queue, world.queue, old_queue_size);
104 memcpy(&(new_queue[old_queue_size]), buf, bytes_read);
106 world.queue = new_queue;
107 memset(buf, 0, buf_size);
110 exit_trouble(-1 == bytes_read && errno != EAGAIN, "read()", f_name);
116 exit_trouble(close(fdesc_infile), f_name, "close()");
121 static void update_out_file()
123 char * f_name = "update_out_file()";
124 char path_tmp[strlen(world.path_out) + strlen(world.tmp_suffix) + 1];
125 sprintf(path_tmp, "%s%s", world.path_out, world.tmp_suffix);
126 FILE * file = try_fopen(path_tmp, "w", f_name);
127 struct MapObj * player = get_player();
128 write_value_as_line(world.turn, file);
129 write_value_as_line(world.score, file);
130 write_value_as_line(player->lifepoints, file);
131 write_inventory(player, file);
132 write_value_as_line(player->pos.y, file);
133 write_value_as_line(player->pos.x, file);
134 write_value_as_line(world.map.size.y, file);
135 write_value_as_line(world.map.size.x, file);
139 try_fwrite(world.log, strlen(world.log), 1, file, f_name);
141 try_fclose_unlink_rename(file, path_tmp, world.path_out, f_name);
142 set_cleanup_flag(CLEANUP_OUTFILE);
147 static void write_value_as_line(uint32_t value, FILE * file)
149 char * f_name = "write_value_as_line()";
150 char write_buf[12]; /* Holds 10 digits of uint32_t maximum + \n + \0. */
151 sprintf(write_buf, "%u\n", value);
152 try_fwrite(write_buf, strlen(write_buf), 1, file, f_name);
157 static void write_inventory(struct MapObj * player, FILE * file)
159 char * f_name = "write_inventory()";
160 struct MapObj * owned = player->owns;
163 char * empty = "(none)\n";
164 try_fwrite(empty, strlen(empty), 1, file, f_name);
169 for (q = 0; NULL != owned; q++)
171 struct MapObjDef * mod = get_map_object_def(owned->type);
172 try_fwrite(mod->name, strlen(mod->name), 1, file, f_name);
173 try_fputc('\n', file, f_name);
177 try_fputc('%', file, f_name);
178 try_fputc('\n', file, f_name);
183 static void write_map(FILE * file)
185 char * f_name = "write_map()";
186 uint32_t map_size = world.map.size.y * world.map.size.x;
187 char visible_map[map_size];
188 memcpy(visible_map, world.map.cells, map_size);
190 struct MapObjDef * d;
193 for (i = 0; i < 2; i++)
195 for (o = world.map_objs; o != 0; o = o->next)
197 if (( (0 == i && 0 == o->lifepoints)
198 || (1 == i && 0 < o->lifepoints)))
200 d = get_map_object_def(o->type);
202 visible_map[(o->pos.y * world.map.size.x) + o->pos.x] = c;
207 for (y = 0; y < world.map.size.y; y++)
209 for (x = 0; x < world.map.size.x; x++)
211 try_fputc(visible_map[(y * world.map.size.x) + x], file, f_name);
213 try_fputc('\n', file, f_name);
219 extern char * io_round()
221 char * f_name = "io_round()";
222 if (0 < world.queue_size)
224 return get_message_from_queue();
226 if (world.turn != world.last_update_turn)
229 world.last_update_turn = world.turn;
231 read_fifo_into_queue();
232 if ('\0' != world.queue[world.queue_size - 1])
234 char * new_queue = try_malloc(world.queue_size + 1, f_name);
235 memcpy(new_queue, world.queue, world.queue_size);
236 new_queue[world.queue_size] = '\0';
239 world.queue = new_queue;
241 return get_message_from_queue();