David Barr | 3bbaec0 | 2010-08-09 17:39:43 -0500 | [diff] [blame] | 1 | /* |
| 2 | * test-line-buffer.c: code to exercise the svn importer's input helper |
David Barr | 3bbaec0 | 2010-08-09 17:39:43 -0500 | [diff] [blame] | 3 | */ |
| 4 | |
| 5 | #include "git-compat-util.h" |
Jonathan Nieder | e832f43 | 2011-01-02 21:05:46 -0600 | [diff] [blame] | 6 | #include "strbuf.h" |
David Barr | 3bbaec0 | 2010-08-09 17:39:43 -0500 | [diff] [blame] | 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 | |
Jonathan Nieder | 850c5ea | 2011-01-02 18:50:16 -0600 | [diff] [blame] | 18 | static void handle_command(const char *command, const char *arg, struct line_buffer *buf) |
| 19 | { |
| 20 | switch (*command) { |
Jonathan Nieder | e832f43 | 2011-01-02 21:05:46 -0600 | [diff] [blame] | 21 | case 'b': |
| 22 | if (!prefixcmp(command, "binary ")) { |
| 23 | struct strbuf sb = STRBUF_INIT; |
| 24 | strbuf_addch(&sb, '>'); |
| 25 | buffer_read_binary(buf, &sb, strtouint32(arg)); |
| 26 | fwrite(sb.buf, 1, sb.len, stdout); |
| 27 | strbuf_release(&sb); |
| 28 | return; |
| 29 | } |
Jonathan Nieder | 850c5ea | 2011-01-02 18:50:16 -0600 | [diff] [blame] | 30 | case 'c': |
| 31 | if (!prefixcmp(command, "copy ")) { |
Jonathan Nieder | 7b990c9 | 2011-01-02 18:52:28 -0600 | [diff] [blame] | 32 | buffer_copy_bytes(buf, strtouint32(arg)); |
Jonathan Nieder | 850c5ea | 2011-01-02 18:50:16 -0600 | [diff] [blame] | 33 | return; |
| 34 | } |
Jonathan Nieder | 7b990c9 | 2011-01-02 18:52:28 -0600 | [diff] [blame] | 35 | case 's': |
| 36 | if (!prefixcmp(command, "skip ")) { |
| 37 | buffer_skip_bytes(buf, strtouint32(arg)); |
Jonathan Nieder | 850c5ea | 2011-01-02 18:50:16 -0600 | [diff] [blame] | 38 | return; |
| 39 | } |
| 40 | default: |
| 41 | die("unrecognized command: %s", command); |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | static void handle_line(const char *line, struct line_buffer *stdin_buf) |
| 46 | { |
| 47 | const char *arg = strchr(line, ' '); |
| 48 | if (!arg) |
| 49 | die("no argument in line: %s", line); |
| 50 | handle_command(line, arg + 1, stdin_buf); |
| 51 | } |
| 52 | |
David Barr | 3bbaec0 | 2010-08-09 17:39:43 -0500 | [diff] [blame] | 53 | int main(int argc, char *argv[]) |
| 54 | { |
Jonathan Nieder | 850c5ea | 2011-01-02 18:50:16 -0600 | [diff] [blame] | 55 | struct line_buffer stdin_buf = LINE_BUFFER_INIT; |
Jonathan Nieder | d280f68 | 2011-01-02 19:07:16 -0600 | [diff] [blame] | 56 | struct line_buffer file_buf = LINE_BUFFER_INIT; |
| 57 | struct line_buffer *input = &stdin_buf; |
| 58 | const char *filename; |
David Barr | 3bbaec0 | 2010-08-09 17:39:43 -0500 | [diff] [blame] | 59 | char *s; |
| 60 | |
Jonathan Nieder | d280f68 | 2011-01-02 19:07:16 -0600 | [diff] [blame] | 61 | if (argc == 1) |
| 62 | filename = NULL; |
| 63 | else if (argc == 2) |
| 64 | filename = argv[1]; |
| 65 | else |
Jonathan Nieder | cb3f87c | 2011-01-02 21:09:38 -0600 | [diff] [blame] | 66 | usage("test-line-buffer [file | &fd] < script"); |
Jonathan Nieder | 850c5ea | 2011-01-02 18:50:16 -0600 | [diff] [blame] | 67 | |
| 68 | if (buffer_init(&stdin_buf, NULL)) |
David Barr | 3bbaec0 | 2010-08-09 17:39:43 -0500 | [diff] [blame] | 69 | die_errno("open error"); |
Jonathan Nieder | d280f68 | 2011-01-02 19:07:16 -0600 | [diff] [blame] | 70 | if (filename) { |
Jonathan Nieder | cb3f87c | 2011-01-02 21:09:38 -0600 | [diff] [blame] | 71 | if (*filename == '&') { |
| 72 | if (buffer_fdinit(&file_buf, strtouint32(filename + 1))) |
| 73 | die_errno("error opening fd %s", filename + 1); |
| 74 | } else { |
| 75 | if (buffer_init(&file_buf, filename)) |
| 76 | die_errno("error opening %s", filename); |
| 77 | } |
Jonathan Nieder | d280f68 | 2011-01-02 19:07:16 -0600 | [diff] [blame] | 78 | input = &file_buf; |
| 79 | } |
| 80 | |
Jonathan Nieder | 850c5ea | 2011-01-02 18:50:16 -0600 | [diff] [blame] | 81 | while ((s = buffer_read_line(&stdin_buf))) |
Jonathan Nieder | d280f68 | 2011-01-02 19:07:16 -0600 | [diff] [blame] | 82 | handle_line(s, input); |
| 83 | |
| 84 | if (filename && buffer_deinit(&file_buf)) |
| 85 | die("error reading from %s", filename); |
Jonathan Nieder | 850c5ea | 2011-01-02 18:50:16 -0600 | [diff] [blame] | 86 | if (buffer_deinit(&stdin_buf)) |
David Barr | 3bbaec0 | 2010-08-09 17:39:43 -0500 | [diff] [blame] | 87 | die("input error"); |
| 88 | if (ferror(stdout)) |
| 89 | die("output error"); |
David Barr | 3bbaec0 | 2010-08-09 17:39:43 -0500 | [diff] [blame] | 90 | return 0; |
| 91 | } |