50
# a new mark directive with the old idnum.
51
#
52
mark ::= 'mark' sp idnum lf;
53
+ data ::= (delimited_data | exact_data)
54
+ lf;
55
+
56
+ # note: delim may be any string but must not contain lf.
57
+ # data_line may contain any data but must not be exactly
58
+ # delim.
59
+ delimited_data ::= 'data' sp '<<' delim lf
60
+ (data_line lf)*
61
+ delim lf;
62
63
# note: declen indicates the length of binary_data in bytes.
55
- # declen does not include the lf preceeding or trailing the
56
- # binary data.
64
+ # declen does not include the lf preceeding the binary data.
65
#
58
- data ::= 'data' sp declen lf
59
- binary_data
60
- lf;
66
+ exact_data ::= 'data' sp declen lf
67
+ binary_data;
68
69
# note: quoted strings are C-style quoting supporting \c for
70
# common escapes of 'c' (e..g \n, \t, \\, \") or \nnn where nnn
1341
1342
static void* cmd_data (size_t *size)
1343
{
1337
- size_t n = 0;
1338
- void *buffer;
1344
size_t length;
1345
+ char *buffer;
1346
1347
if (strncmp("data ", command_buf.buf, 5))
1348
die("Expected 'data n' command, found: %s", command_buf.buf);
1349
1344
- length = strtoul(command_buf.buf + 5, NULL, 10);
1345
- buffer = xmalloc(length);
1346
-
1347
- while (n < length) {
1348
- size_t s = fread((char*)buffer + n, 1, length - n, stdin);
1349
- if (!s && feof(stdin))
1350
- die("EOF in data (%lu bytes remaining)", length - n);
1351
- n += s;
1350
+ if (!strncmp("<<", command_buf.buf + 5, 2)) {
1351
+ char *term = xstrdup(command_buf.buf + 5 + 2);
1352
+ size_t sz = 8192, term_len = command_buf.len - 5 - 2;
1353
+ length = 0;
1354
+ buffer = xmalloc(sz);
1355
+ for (;;) {
1356
+ read_next_command();
1357
+ if (command_buf.eof)
1358
+ die("EOF in data (terminator '%s' not found)", term);
1359
+ if (term_len == command_buf.len
1360
+ && !strcmp(term, command_buf.buf))
1361
+ break;
1362
+ if (sz < (length + command_buf.len)) {
1363
+ sz = sz * 3 / 2 + 16;
1364
+ if (sz < (length + command_buf.len))
1365
+ sz = length + command_buf.len;
1366
+ buffer = xrealloc(buffer, sz);
1367
+ }
1368
+ memcpy(buffer + length,
1369
+ command_buf.buf,
1370
+ command_buf.len - 1);
1371
+ length += command_buf.len - 1;
1372
+ buffer[length++] = '\n';
1373
+ }
1374
+ free(term);
1375
+ }
1376
+ else {
1377
+ size_t n = 0;
1378
+ length = strtoul(command_buf.buf + 5, NULL, 10);
1379
+ buffer = xmalloc(length);
1380
+ while (n < length) {
1381
+ size_t s = fread(buffer + n, 1, length - n, stdin);
1382
+ if (!s && feof(stdin))
1383
+ die("EOF in data (%lu bytes remaining)", length - n);
1384
+ n += s;
1385
+ }
1386
}
1387
1388
if (fgetc(stdin) != '\n')