t / helper / test-pkt-line.con commit Merge branch 'es/test-lint-one-shot-export' (d1cd220)
   1#include "cache.h"
   2#include "pkt-line.h"
   3
   4static void pack_line(const char *line)
   5{
   6        if (!strcmp(line, "0000") || !strcmp(line, "0000\n"))
   7                packet_flush(1);
   8        else if (!strcmp(line, "0001") || !strcmp(line, "0001\n"))
   9                packet_delim(1);
  10        else
  11                packet_write_fmt(1, "%s", line);
  12}
  13
  14static void pack(int argc, const char **argv)
  15{
  16        if (argc) { /* read from argv */
  17                int i;
  18                for (i = 0; i < argc; i++)
  19                        pack_line(argv[i]);
  20        } else { /* read from stdin */
  21                char line[LARGE_PACKET_MAX];
  22                while (fgets(line, sizeof(line), stdin)) {
  23                        pack_line(line);
  24                }
  25        }
  26}
  27
  28static void unpack(void)
  29{
  30        struct packet_reader reader;
  31        packet_reader_init(&reader, 0, NULL, 0,
  32                           PACKET_READ_GENTLE_ON_EOF |
  33                           PACKET_READ_CHOMP_NEWLINE);
  34
  35        while (packet_reader_read(&reader) != PACKET_READ_EOF) {
  36                switch (reader.status) {
  37                case PACKET_READ_EOF:
  38                        break;
  39                case PACKET_READ_NORMAL:
  40                        printf("%s\n", reader.line);
  41                        break;
  42                case PACKET_READ_FLUSH:
  43                        printf("0000\n");
  44                        break;
  45                case PACKET_READ_DELIM:
  46                        printf("0001\n");
  47                        break;
  48                }
  49        }
  50}
  51
  52static void unpack_sideband(void)
  53{
  54        struct packet_reader reader;
  55        packet_reader_init(&reader, 0, NULL, 0,
  56                           PACKET_READ_GENTLE_ON_EOF |
  57                           PACKET_READ_CHOMP_NEWLINE);
  58
  59        while (packet_reader_read(&reader) != PACKET_READ_EOF) {
  60                int band;
  61                int fd;
  62
  63                switch (reader.status) {
  64                case PACKET_READ_EOF:
  65                        break;
  66                case PACKET_READ_NORMAL:
  67                        band = reader.line[0] & 0xff;
  68                        if (band < 1 || band > 2)
  69                                die("unexpected side band %d", band);
  70                        fd = band;
  71
  72                        write_or_die(fd, reader.line + 1, reader.pktlen - 1);
  73                        break;
  74                case PACKET_READ_FLUSH:
  75                        return;
  76                case PACKET_READ_DELIM:
  77                        break;
  78                }
  79        }
  80}
  81
  82int cmd_main(int argc, const char **argv)
  83{
  84        if (argc < 2)
  85                die("too few arguments");
  86
  87        if (!strcmp(argv[1], "pack"))
  88                pack(argc - 2, argv + 2);
  89        else if (!strcmp(argv[1], "unpack"))
  90                unpack();
  91        else if (!strcmp(argv[1], "unpack-sideband"))
  92                unpack_sideband();
  93        else
  94                die("invalid argument '%s'", argv[1]);
  95
  96        return 0;
  97}