- length = strtoul(command_buf.buf + 5, NULL, 10);
- buffer = xmalloc(length);
-
- while (n < length) {
- size_t s = fread((char*)buffer + n, 1, length - n, stdin);
- if (!s && feof(stdin))
- die("EOF in data (%lu bytes remaining)", length - n);
- n += s;
+ if (!strncmp("<<", command_buf.buf + 5, 2)) {
+ char *term = xstrdup(command_buf.buf + 5 + 2);
+ size_t sz = 8192, term_len = command_buf.len - 5 - 2;
+ length = 0;
+ buffer = xmalloc(sz);
+ for (;;) {
+ read_next_command();
+ if (command_buf.eof)
+ die("EOF in data (terminator '%s' not found)", term);
+ if (term_len == command_buf.len
+ && !strcmp(term, command_buf.buf))
+ break;
+ if (sz < (length + command_buf.len)) {
+ sz = sz * 3 / 2 + 16;
+ if (sz < (length + command_buf.len))
+ sz = length + command_buf.len;
+ buffer = xrealloc(buffer, sz);
+ }
+ memcpy(buffer + length,
+ command_buf.buf,
+ command_buf.len - 1);
+ length += command_buf.len - 1;
+ buffer[length++] = '\n';
+ }
+ free(term);
+ }
+ else {
+ size_t n = 0;
+ length = strtoul(command_buf.buf + 5, NULL, 10);
+ buffer = xmalloc(length);
+ while (n < length) {
+ size_t s = fread(buffer + n, 1, length - n, stdin);
+ if (!s && feof(stdin))
+ die("EOF in data (%lu bytes remaining)", length - n);
+ n += s;
+ }