]> git.ipfire.org Git - thirdparty/git.git/blob - t/helper/test-pkt-line.c
pkt-line: do not chomp newlines for sideband messages
[thirdparty/git.git] / t / helper / test-pkt-line.c
1 #include "git-compat-util.h"
2 #include "test-tool.h"
3 #include "pkt-line.h"
4 #include "write-or-die.h"
5 #include "parse-options.h"
6
7 static void pack_line(const char *line)
8 {
9 if (!strcmp(line, "0000") || !strcmp(line, "0000\n"))
10 packet_flush(1);
11 else if (!strcmp(line, "0001") || !strcmp(line, "0001\n"))
12 packet_delim(1);
13 else
14 packet_write_fmt(1, "%s", line);
15 }
16
17 static void pack(int argc, const char **argv)
18 {
19 if (argc) { /* read from argv */
20 int i;
21 for (i = 0; i < argc; i++)
22 pack_line(argv[i]);
23 } else { /* read from stdin */
24 char line[LARGE_PACKET_MAX];
25 while (fgets(line, sizeof(line), stdin)) {
26 pack_line(line);
27 }
28 }
29 }
30
31 static void pack_raw_stdin(void)
32 {
33 struct strbuf sb = STRBUF_INIT;
34
35 if (strbuf_read(&sb, 0, 0) < 0)
36 die_errno("failed to read from stdin");
37 packet_write(1, sb.buf, sb.len);
38 strbuf_release(&sb);
39 }
40
41 static void unpack(void)
42 {
43 struct packet_reader reader;
44 packet_reader_init(&reader, 0, NULL, 0,
45 PACKET_READ_GENTLE_ON_EOF |
46 PACKET_READ_CHOMP_NEWLINE);
47
48 while (packet_reader_read(&reader) != PACKET_READ_EOF) {
49 switch (reader.status) {
50 case PACKET_READ_EOF:
51 break;
52 case PACKET_READ_NORMAL:
53 printf("%s\n", reader.line);
54 break;
55 case PACKET_READ_FLUSH:
56 printf("0000\n");
57 break;
58 case PACKET_READ_DELIM:
59 printf("0001\n");
60 break;
61 case PACKET_READ_RESPONSE_END:
62 printf("0002\n");
63 break;
64 }
65 }
66 }
67
68 static void unpack_sideband(int argc, const char **argv)
69 {
70 struct packet_reader reader;
71 int options = PACKET_READ_GENTLE_ON_EOF;
72 int chomp_newline = 1;
73 int reader_use_sideband = 0;
74 const char *const unpack_sideband_usage[] = {
75 "test_tool unpack_sideband [options...]", NULL
76 };
77 struct option cmd_options[] = {
78 OPT_BOOL(0, "reader-use-sideband", &reader_use_sideband,
79 "set use_sideband bit for packet reader (Default: off)"),
80 OPT_BOOL(0, "chomp-newline", &chomp_newline,
81 "chomp newline in packet (Default: on)"),
82 OPT_END()
83 };
84
85 argc = parse_options(argc, argv, "", cmd_options, unpack_sideband_usage,
86 0);
87 if (argc > 0)
88 usage_msg_opt(_("too many arguments"), unpack_sideband_usage,
89 cmd_options);
90
91 if (chomp_newline)
92 options |= PACKET_READ_CHOMP_NEWLINE;
93 packet_reader_init(&reader, 0, NULL, 0, options);
94 reader.use_sideband = reader_use_sideband;
95
96 while (packet_reader_read(&reader) != PACKET_READ_EOF) {
97 int band;
98 int fd;
99
100 switch (reader.status) {
101 case PACKET_READ_EOF:
102 break;
103 case PACKET_READ_NORMAL:
104 /*
105 * When the "use_sideband" field of the reader is turned
106 * on, sideband packets other than the payload have been
107 * parsed and consumed in packet_reader_read(), and only
108 * the payload arrives here.
109 */
110 if (reader.use_sideband) {
111 write_or_die(1, reader.line, reader.pktlen - 1);
112 break;
113 }
114
115 band = reader.line[0] & 0xff;
116 if (band < 1 || band > 2)
117 continue; /* skip non-sideband packets */
118 fd = band;
119
120 write_or_die(fd, reader.line + 1, reader.pktlen - 1);
121 break;
122 case PACKET_READ_FLUSH:
123 return;
124 case PACKET_READ_DELIM:
125 case PACKET_READ_RESPONSE_END:
126 break;
127 }
128 }
129 }
130
131 static int send_split_sideband(void)
132 {
133 const char *foo = "Foo.\n";
134 const char *bar = "Bar.\n";
135 const char *part1 = "Hello,";
136 const char *primary = "\001primary: regular output\n";
137 const char *part2 = " world!\n";
138
139 /* Each sideband message has a trailing newline character. */
140 send_sideband(1, 2, foo, strlen(foo), LARGE_PACKET_MAX);
141 send_sideband(1, 2, bar, strlen(bar), LARGE_PACKET_MAX);
142
143 /*
144 * One sideband message is divided into part1 and part2
145 * by the primary message.
146 */
147 send_sideband(1, 2, part1, strlen(part1), LARGE_PACKET_MAX);
148 packet_write(1, primary, strlen(primary));
149 send_sideband(1, 2, part2, strlen(part2), LARGE_PACKET_MAX);
150 packet_response_end(1);
151
152 /*
153 * We use unpack_sideband() to consume packets. A flush packet
154 * is required to end parsing.
155 */
156 packet_flush(1);
157
158 return 0;
159 }
160
161 static int receive_sideband(void)
162 {
163 return recv_sideband("sideband", 0, 1);
164 }
165
166 int cmd__pkt_line(int argc, const char **argv)
167 {
168 if (argc < 2)
169 die("too few arguments");
170
171 if (!strcmp(argv[1], "pack"))
172 pack(argc - 2, argv + 2);
173 else if (!strcmp(argv[1], "pack-raw-stdin"))
174 pack_raw_stdin();
175 else if (!strcmp(argv[1], "unpack"))
176 unpack();
177 else if (!strcmp(argv[1], "unpack-sideband"))
178 unpack_sideband(argc - 1, argv + 1);
179 else if (!strcmp(argv[1], "send-split-sideband"))
180 send_split_sideband();
181 else if (!strcmp(argv[1], "receive-sideband"))
182 receive_sideband();
183 else
184 die("invalid argument '%s'", argv[1]);
185
186 return 0;
187 }