2010-08-10 06:39:43 +08:00
|
|
|
/*
|
|
|
|
* test-line-buffer.c: code to exercise the svn importer's input helper
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "git-compat-util.h"
|
2011-01-03 11:05:46 +08:00
|
|
|
#include "strbuf.h"
|
2010-08-10 06:39:43 +08:00
|
|
|
#include "vcs-svn/line_buffer.h"
|
|
|
|
|
|
|
|
static uint32_t strtouint32(const char *s)
|
|
|
|
{
|
|
|
|
char *end;
|
|
|
|
uintmax_t n = strtoumax(s, &end, 10);
|
|
|
|
if (*s == '\0' || *end != '\0')
|
|
|
|
die("invalid count: %s", s);
|
|
|
|
return (uint32_t) n;
|
|
|
|
}
|
|
|
|
|
2011-01-03 08:50:16 +08:00
|
|
|
static void handle_command(const char *command, const char *arg, struct line_buffer *buf)
|
|
|
|
{
|
|
|
|
switch (*command) {
|
2011-01-03 11:05:46 +08:00
|
|
|
case 'b':
|
2013-12-01 04:55:40 +08:00
|
|
|
if (starts_with(command, "binary ")) {
|
2011-01-03 11:05:46 +08:00
|
|
|
struct strbuf sb = STRBUF_INIT;
|
|
|
|
strbuf_addch(&sb, '>');
|
|
|
|
buffer_read_binary(buf, &sb, strtouint32(arg));
|
|
|
|
fwrite(sb.buf, 1, sb.len, stdout);
|
|
|
|
strbuf_release(&sb);
|
|
|
|
return;
|
|
|
|
}
|
2011-01-03 08:50:16 +08:00
|
|
|
case 'c':
|
2013-12-01 04:55:40 +08:00
|
|
|
if (starts_with(command, "copy ")) {
|
2011-01-03 08:52:28 +08:00
|
|
|
buffer_copy_bytes(buf, strtouint32(arg));
|
2011-01-03 08:50:16 +08:00
|
|
|
return;
|
|
|
|
}
|
2011-01-03 08:52:28 +08:00
|
|
|
case 's':
|
2013-12-01 04:55:40 +08:00
|
|
|
if (starts_with(command, "skip ")) {
|
2011-01-03 08:52:28 +08:00
|
|
|
buffer_skip_bytes(buf, strtouint32(arg));
|
2011-01-03 08:50:16 +08:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2010-08-10 06:39:43 +08:00
|
|
|
int main(int argc, char *argv[])
|
|
|
|
{
|
2011-01-03 08:50:16 +08:00
|
|
|
struct line_buffer stdin_buf = LINE_BUFFER_INIT;
|
2011-01-03 09:07:16 +08:00
|
|
|
struct line_buffer file_buf = LINE_BUFFER_INIT;
|
|
|
|
struct line_buffer *input = &stdin_buf;
|
|
|
|
const char *filename;
|
2010-08-10 06:39:43 +08:00
|
|
|
char *s;
|
|
|
|
|
2011-01-03 09:07:16 +08:00
|
|
|
if (argc == 1)
|
|
|
|
filename = NULL;
|
|
|
|
else if (argc == 2)
|
|
|
|
filename = argv[1];
|
|
|
|
else
|
2011-01-03 11:09:38 +08:00
|
|
|
usage("test-line-buffer [file | &fd] < script");
|
2011-01-03 08:50:16 +08:00
|
|
|
|
|
|
|
if (buffer_init(&stdin_buf, NULL))
|
2010-08-10 06:39:43 +08:00
|
|
|
die_errno("open error");
|
2011-01-03 09:07:16 +08:00
|
|
|
if (filename) {
|
2011-01-03 11:09:38 +08:00
|
|
|
if (*filename == '&') {
|
|
|
|
if (buffer_fdinit(&file_buf, strtouint32(filename + 1)))
|
|
|
|
die_errno("error opening fd %s", filename + 1);
|
|
|
|
} else {
|
|
|
|
if (buffer_init(&file_buf, filename))
|
|
|
|
die_errno("error opening %s", filename);
|
|
|
|
}
|
2011-01-03 09:07:16 +08:00
|
|
|
input = &file_buf;
|
|
|
|
}
|
|
|
|
|
2011-01-03 08:50:16 +08:00
|
|
|
while ((s = buffer_read_line(&stdin_buf)))
|
2011-01-03 09:07:16 +08:00
|
|
|
handle_line(s, input);
|
|
|
|
|
|
|
|
if (filename && buffer_deinit(&file_buf))
|
|
|
|
die("error reading from %s", filename);
|
2011-01-03 08:50:16 +08:00
|
|
|
if (buffer_deinit(&stdin_buf))
|
2010-08-10 06:39:43 +08:00
|
|
|
die("input error");
|
|
|
|
if (ferror(stdout))
|
|
|
|
die("output error");
|
|
|
|
return 0;
|
|
|
|
}
|