t / helper / test-line-buffer.con commit Merge branch 'sg/index-pack-progress' (da924b5)
   1/*
   2 * test-line-buffer.c: code to exercise the svn importer's input helper
   3 */
   4
   5#include "git-compat-util.h"
   6#include "strbuf.h"
   7#include "vcs-svn/line_buffer.h"
   8
   9static 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
  18static void handle_command(const char *command, const char *arg, struct line_buffer *buf)
  19{
  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 {
  31                die("unrecognized command: %s", command);
  32        }
  33}
  34
  35static 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
  43int cmd_main(int argc, const char **argv)
  44{
  45        struct line_buffer stdin_buf = LINE_BUFFER_INIT;
  46        struct line_buffer file_buf = LINE_BUFFER_INIT;
  47        struct line_buffer *input = &stdin_buf;
  48        const char *filename;
  49        char *s;
  50
  51        if (argc == 1)
  52                filename = NULL;
  53        else if (argc == 2)
  54                filename = argv[1];
  55        else
  56                usage("test-line-buffer [file | &fd] < script");
  57
  58        if (buffer_init(&stdin_buf, NULL))
  59                die_errno("open error");
  60        if (filename) {
  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                }
  68                input = &file_buf;
  69        }
  70
  71        while ((s = buffer_read_line(&stdin_buf)))
  72                handle_line(s, input);
  73
  74        if (filename && buffer_deinit(&file_buf))
  75                die("error reading from %s", filename);
  76        if (buffer_deinit(&stdin_buf))
  77                die("input error");
  78        if (ferror(stdout))
  79                die("output error");
  80        return 0;
  81}