Merge branch 'pb/doc-modules-git-work-tree-typofix'
[git] / t / helper / test-pkt-line.c
1 #include "cache.h"
2 #include "test-tool.h"
3 #include "pkt-line.h"
4
5 static void pack_line(const char *line)
6 {
7         if (!strcmp(line, "0000") || !strcmp(line, "0000\n"))
8                 packet_flush(1);
9         else if (!strcmp(line, "0001") || !strcmp(line, "0001\n"))
10                 packet_delim(1);
11         else
12                 packet_write_fmt(1, "%s", line);
13 }
14
15 static void pack(int argc, const char **argv)
16 {
17         if (argc) { /* read from argv */
18                 int i;
19                 for (i = 0; i < argc; i++)
20                         pack_line(argv[i]);
21         } else { /* read from stdin */
22                 char line[LARGE_PACKET_MAX];
23                 while (fgets(line, sizeof(line), stdin)) {
24                         pack_line(line);
25                 }
26         }
27 }
28
29 static void unpack(void)
30 {
31         struct packet_reader reader;
32         packet_reader_init(&reader, 0, NULL, 0,
33                            PACKET_READ_GENTLE_ON_EOF |
34                            PACKET_READ_CHOMP_NEWLINE);
35
36         while (packet_reader_read(&reader) != PACKET_READ_EOF) {
37                 switch (reader.status) {
38                 case PACKET_READ_EOF:
39                         break;
40                 case PACKET_READ_NORMAL:
41                         printf("%s\n", reader.line);
42                         break;
43                 case PACKET_READ_FLUSH:
44                         printf("0000\n");
45                         break;
46                 case PACKET_READ_DELIM:
47                         printf("0001\n");
48                         break;
49                 case PACKET_READ_RESPONSE_END:
50                         printf("0002\n");
51                         break;
52                 }
53         }
54 }
55
56 static void unpack_sideband(void)
57 {
58         struct packet_reader reader;
59         packet_reader_init(&reader, 0, NULL, 0,
60                            PACKET_READ_GENTLE_ON_EOF |
61                            PACKET_READ_CHOMP_NEWLINE);
62
63         while (packet_reader_read(&reader) != PACKET_READ_EOF) {
64                 int band;
65                 int fd;
66
67                 switch (reader.status) {
68                 case PACKET_READ_EOF:
69                         break;
70                 case PACKET_READ_NORMAL:
71                         band = reader.line[0] & 0xff;
72                         if (band < 1 || band > 2)
73                                 continue; /* skip non-sideband packets */
74                         fd = band;
75
76                         write_or_die(fd, reader.line + 1, reader.pktlen - 1);
77                         break;
78                 case PACKET_READ_FLUSH:
79                         return;
80                 case PACKET_READ_DELIM:
81                 case PACKET_READ_RESPONSE_END:
82                         break;
83                 }
84         }
85 }
86
87 static int send_split_sideband(void)
88 {
89         const char *part1 = "Hello,";
90         const char *primary = "\001primary: regular output\n";
91         const char *part2 = " world!\n";
92
93         send_sideband(1, 2, part1, strlen(part1), LARGE_PACKET_MAX);
94         packet_write(1, primary, strlen(primary));
95         send_sideband(1, 2, part2, strlen(part2), LARGE_PACKET_MAX);
96         packet_response_end(1);
97
98         return 0;
99 }
100
101 static int receive_sideband(void)
102 {
103         return recv_sideband("sideband", 0, 1);
104 }
105
106 int cmd__pkt_line(int argc, const char **argv)
107 {
108         if (argc < 2)
109                 die("too few arguments");
110
111         if (!strcmp(argv[1], "pack"))
112                 pack(argc - 2, argv + 2);
113         else if (!strcmp(argv[1], "unpack"))
114                 unpack();
115         else if (!strcmp(argv[1], "unpack-sideband"))
116                 unpack_sideband();
117         else if (!strcmp(argv[1], "send-split-sideband"))
118                 send_split_sideband();
119         else if (!strcmp(argv[1], "receive-sideband"))
120                 receive_sideband();
121         else
122                 die("invalid argument '%s'", argv[1]);
123
124         return 0;
125 }