Sync with 2.38.4
[git.git] / t / helper / test-pkt-line.c
blobc5e052e537805075682f16204f1ab16991a8e0fe
1 #include "cache.h"
2 #include "test-tool.h"
3 #include "pkt-line.h"
5 static void pack_line(const char *line)
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);
15 static void pack(int argc, const char **argv)
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);
29 static void pack_raw_stdin(void)
31 struct strbuf sb = STRBUF_INIT;
33 if (strbuf_read(&sb, 0, 0) < 0)
34 die_errno("failed to read from stdin");
35 packet_write(1, sb.buf, sb.len);
36 strbuf_release(&sb);
39 static void unpack(void)
41 struct packet_reader reader;
42 packet_reader_init(&reader, 0, NULL, 0,
43 PACKET_READ_GENTLE_ON_EOF |
44 PACKET_READ_CHOMP_NEWLINE);
46 while (packet_reader_read(&reader) != PACKET_READ_EOF) {
47 switch (reader.status) {
48 case PACKET_READ_EOF:
49 break;
50 case PACKET_READ_NORMAL:
51 printf("%s\n", reader.line);
52 break;
53 case PACKET_READ_FLUSH:
54 printf("0000\n");
55 break;
56 case PACKET_READ_DELIM:
57 printf("0001\n");
58 break;
59 case PACKET_READ_RESPONSE_END:
60 printf("0002\n");
61 break;
66 static void unpack_sideband(void)
68 struct packet_reader reader;
69 packet_reader_init(&reader, 0, NULL, 0,
70 PACKET_READ_GENTLE_ON_EOF |
71 PACKET_READ_CHOMP_NEWLINE);
73 while (packet_reader_read(&reader) != PACKET_READ_EOF) {
74 int band;
75 int fd;
77 switch (reader.status) {
78 case PACKET_READ_EOF:
79 break;
80 case PACKET_READ_NORMAL:
81 band = reader.line[0] & 0xff;
82 if (band < 1 || band > 2)
83 continue; /* skip non-sideband packets */
84 fd = band;
86 write_or_die(fd, reader.line + 1, reader.pktlen - 1);
87 break;
88 case PACKET_READ_FLUSH:
89 return;
90 case PACKET_READ_DELIM:
91 case PACKET_READ_RESPONSE_END:
92 break;
97 static int send_split_sideband(void)
99 const char *part1 = "Hello,";
100 const char *primary = "\001primary: regular output\n";
101 const char *part2 = " world!\n";
103 send_sideband(1, 2, part1, strlen(part1), LARGE_PACKET_MAX);
104 packet_write(1, primary, strlen(primary));
105 send_sideband(1, 2, part2, strlen(part2), LARGE_PACKET_MAX);
106 packet_response_end(1);
108 return 0;
111 static int receive_sideband(void)
113 return recv_sideband("sideband", 0, 1);
116 int cmd__pkt_line(int argc, const char **argv)
118 if (argc < 2)
119 die("too few arguments");
121 if (!strcmp(argv[1], "pack"))
122 pack(argc - 2, argv + 2);
123 else if (!strcmp(argv[1], "pack-raw-stdin"))
124 pack_raw_stdin();
125 else if (!strcmp(argv[1], "unpack"))
126 unpack();
127 else if (!strcmp(argv[1], "unpack-sideband"))
128 unpack_sideband();
129 else if (!strcmp(argv[1], "send-split-sideband"))
130 send_split_sideband();
131 else if (!strcmp(argv[1], "receive-sideband"))
132 receive_sideband();
133 else
134 die("invalid argument '%s'", argv[1]);
136 return 0;