]>
Commit | Line | Data |
---|---|---|
3bbaec00 DB |
1 | /* |
2 | * test-line-buffer.c: code to exercise the svn importer's input helper | |
3bbaec00 DB |
3 | */ |
4 | ||
5 | #include "git-compat-util.h" | |
e832f43c | 6 | #include "strbuf.h" |
3bbaec00 DB |
7 | #include "vcs-svn/line_buffer.h" |
8 | ||
9 | static uint32_t strtouint32(const char *s) | |
10 | { | |
11 | char *end; | |
12 | uintmax_t n = strtoumax(s, &end, 10); | |
13 | if (*s == '\0' || *end != '\0') | |
14 | die("invalid count: %s", s); | |
15 | return (uint32_t) n; | |
16 | } | |
17 | ||
850c5ea4 JN |
18 | static void handle_command(const char *command, const char *arg, struct line_buffer *buf) |
19 | { | |
8968b7b0 JK |
20 | if (starts_with(command, "binary ")) { |
21 | struct strbuf sb = STRBUF_INIT; | |
22 | strbuf_addch(&sb, '>'); | |
23 | buffer_read_binary(buf, &sb, strtouint32(arg)); | |
24 | fwrite(sb.buf, 1, sb.len, stdout); | |
25 | strbuf_release(&sb); | |
26 | } else if (starts_with(command, "copy ")) { | |
27 | buffer_copy_bytes(buf, strtouint32(arg)); | |
28 | } else if (starts_with(command, "skip ")) { | |
29 | buffer_skip_bytes(buf, strtouint32(arg)); | |
30 | } else { | |
850c5ea4 JN |
31 | die("unrecognized command: %s", command); |
32 | } | |
33 | } | |
34 | ||
35 | static void handle_line(const char *line, struct line_buffer *stdin_buf) | |
36 | { | |
37 | const char *arg = strchr(line, ' '); | |
38 | if (!arg) | |
39 | die("no argument in line: %s", line); | |
40 | handle_command(line, arg + 1, stdin_buf); | |
41 | } | |
42 | ||
3f2e2297 | 43 | int cmd_main(int argc, const char **argv) |
3bbaec00 | 44 | { |
850c5ea4 | 45 | struct line_buffer stdin_buf = LINE_BUFFER_INIT; |
d280f683 JN |
46 | struct line_buffer file_buf = LINE_BUFFER_INIT; |
47 | struct line_buffer *input = &stdin_buf; | |
48 | const char *filename; | |
3bbaec00 DB |
49 | char *s; |
50 | ||
d280f683 JN |
51 | if (argc == 1) |
52 | filename = NULL; | |
53 | else if (argc == 2) | |
54 | filename = argv[1]; | |
55 | else | |
cb3f87cf | 56 | usage("test-line-buffer [file | &fd] < script"); |
850c5ea4 JN |
57 | |
58 | if (buffer_init(&stdin_buf, NULL)) | |
3bbaec00 | 59 | die_errno("open error"); |
d280f683 | 60 | if (filename) { |
cb3f87cf JN |
61 | if (*filename == '&') { |
62 | if (buffer_fdinit(&file_buf, strtouint32(filename + 1))) | |
63 | die_errno("error opening fd %s", filename + 1); | |
64 | } else { | |
65 | if (buffer_init(&file_buf, filename)) | |
66 | die_errno("error opening %s", filename); | |
67 | } | |
d280f683 JN |
68 | input = &file_buf; |
69 | } | |
70 | ||
850c5ea4 | 71 | while ((s = buffer_read_line(&stdin_buf))) |
d280f683 JN |
72 | handle_line(s, input); |
73 | ||
74 | if (filename && buffer_deinit(&file_buf)) | |
75 | die("error reading from %s", filename); | |
850c5ea4 | 76 | if (buffer_deinit(&stdin_buf)) |
3bbaec00 DB |
77 | die("input error"); |
78 | if (ferror(stdout)) | |
79 | die("output error"); | |
3bbaec00 DB |
80 | return 0; |
81 | } |