/** Copy bytes from a buffer into a memory stream. * * @param data mem stream * @param buf buffer * @param n number of bytes to copy */ static void mem_put(MemData *data, const char *buf, size_t n){ char *start = mem_hi(data); char *end = mem_end(data); if(start + n < end){ memcpy(start, buf, n); } else { int k = end - start; memcpy(start, buf, k); memcpy(data->buf, buf + k, n - k); } }
/** Copy bytes from a memory stream into a buffer. * * @param data mem stream * @param buf buffer * @param n number of bytes to copy */ static void mem_get(MemData *data, char *buf, size_t n){ char *start = mem_lo(data); char *end = mem_end(data); if (start + n < end) { memcpy(buf, start, n); } else { int k = end - start; memcpy(buf, start, k); memcpy(buf + k, data->buf, n - k); } }
bool process_next_command() { char command[COMMAND_SIZE + 1]; int pid, address; printf("\nrequest: > "); fgets(command, sizeof command, stdin); // process the entered command if (sscanf(command, "begin p%d", &pid) == 1) { mem_begin(pid); } else if (sscanf(command, "read p%d %d", &pid, &address) == 2) { mem_read(pid, address); } else if (sscanf(command, "write p%d %d", &pid, &address) == 2) { mem_write(pid, address); } else if (sscanf(command, "end p%d", &pid) == 1) { mem_end(pid); } else if (strncmp(command, "view", 4) == 0) { view_memory_system(); } else if (strncmp(command, "warnings", 8) == 0) { if (messages) { printf(" sys !> warnings have been turned off\n"); } else { printf(" sys !> warnings are on\n"); } messages = !messages; } else if (strncmp(command, "stop", 4) == 0) { return false; } else { display_warning("main: unrecognized command"); } return true; }