+static void handle_command(const char *command, const char *arg, struct line_buffer *buf)
+{
+ switch (*command) {
+ case 'c':
+ if (!prefixcmp(command, "copy ")) {
+ buffer_copy_bytes(buf, strtouint32(arg) + 1);
+ return;
+ }
+ case 'r':
+ if (!prefixcmp(command, "read ")) {
+ const char *s = buffer_read_string(buf, strtouint32(arg));
+ printf("%s\n", s);
+ buffer_skip_bytes(buf, 1); /* consume newline */
+ return;
+ }
+ default:
+ die("unrecognized command: %s", command);
+ }
+}
+
+static void handle_line(const char *line, struct line_buffer *stdin_buf)
+{
+ const char *arg = strchr(line, ' ');
+ if (!arg)
+ die("no argument in line: %s", line);
+ handle_command(line, arg + 1, stdin_buf);
+}
+