#include "lab.h" static int control(struct backend *backend, const struct command *command, struct reply *reply) { if (command->operation == 'A' && command_has(command, 1) && command->argument[0] >= 0 && command->argument[0] < BANKS) { backend->selected = command->argument[0]; return 1; } if (command->operation == 'F' && command_has(command, 3) && command->argument[0] >= 0 && command->argument[0] < 8 && command->argument[1] >= 0 && command->argument[2] >= 0) { fault_set(backend, command->argument[0], command->argument[1], command->argument[2]); return 1; } if (command->operation == 'Z') { backend_control(backend, command, reply); return 1; } return 0; } void client_snapshot(const struct client *client) { size_t bank; size_t index; printf("C %d", client->selected); for (bank = 0; bank < BANKS; bank++) { for (index = 0; index < 8; index++) { printf(" %d", client->value[bank][index]); } } putchar('\n'); } void backend_snapshot(const struct backend *backend) { size_t bank; size_t index; printf("B %d %lu", backend->selected, backend->trace.total); for (bank = 0; bank < BANKS; bank++) { for (index = 0; index < 8; index++) { printf(" %d", backend->value[bank][index]); } printf(" %u", byte_sum(backend->storage[bank], 256)); } for (index = 0; index < 8; index++) { printf(" %d %u", backend->faults[index].error, backend->faults[index].remaining); } putchar('\n'); } int main(void) { struct command command; struct reply reply; struct backend backend; struct client client; unsigned long sequence = 0; int read_status; backend_init(&backend); client_init(&client, &backend); while ((read_status = command_read(stdin, &command)) > 0) { sequence++; reply_reset(&reply); trace_begin(&backend.trace); if (!control(&backend, &command, &reply)) { client_apply(&client, &command, &reply); } if (command.operation == 'A' && reply.result == 0) { client.selected = backend.selected; } printf("N %lu\n", sequence); trace_print(&backend.trace); reply_print(&reply); client_snapshot(&client); backend_snapshot(&backend); } if (read_status < 0) { fputs("invalid console input\n", stderr); return 2; } return 0; }