383f35bba6422e29099745f7e9c1f5d2b35e5cc7
   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 "vcs-svn/line_buffer.h"
   7
   8static uint32_t strtouint32(const char *s)
   9{
  10        char *end;
  11        uintmax_t n = strtoumax(s, &end, 10);
  12        if (*s == '\0' || *end != '\0')
  13                die("invalid count: %s", s);
  14        return (uint32_t) n;
  15}
  16
  17static void handle_command(const char *command, const char *arg, struct line_buffer *buf)
  18{
  19        switch (*command) {
  20        case 'c':
  21                if (!prefixcmp(command, "copy ")) {
  22                        buffer_copy_bytes(buf, strtouint32(arg) + 1);
  23                        return;
  24                }
  25        case 'r':
  26                if (!prefixcmp(command, "read ")) {
  27                        const char *s = buffer_read_string(buf, strtouint32(arg));
  28                        printf("%s\n", s);
  29                        buffer_skip_bytes(buf, 1);      /* consume newline */
  30                        return;
  31                }
  32        default:
  33                die("unrecognized command: %s", command);
  34        }
  35}
  36
  37static void handle_line(const char *line, struct line_buffer *stdin_buf)
  38{
  39        const char *arg = strchr(line, ' ');
  40        if (!arg)
  41                die("no argument in line: %s", line);
  42        handle_command(line, arg + 1, stdin_buf);
  43}
  44
  45int main(int argc, char *argv[])
  46{
  47        struct line_buffer stdin_buf = LINE_BUFFER_INIT;
  48        char *s;
  49
  50        if (argc != 1)
  51                usage("test-line-buffer < script");
  52
  53        if (buffer_init(&stdin_buf, NULL))
  54                die_errno("open error");
  55        while ((s = buffer_read_line(&stdin_buf)))
  56                handle_line(s, &stdin_buf);
  57        if (buffer_deinit(&stdin_buf))
  58                die("input error");
  59        if (ferror(stdout))
  60                die("output error");
  61        buffer_reset(&stdin_buf);
  62        return 0;
  63}