1 #include "git-compat-util.h"
5 #include "write-or-die.h"
6 #include "parse-options.h"
8 static void pack_line(const char *line
)
10 if (!strcmp(line
, "0000") || !strcmp(line
, "0000\n"))
12 else if (!strcmp(line
, "0001") || !strcmp(line
, "0001\n"))
15 packet_write_fmt(1, "%s", line
);
18 static void pack(int argc
, const char **argv
)
20 if (argc
) { /* read from argv */
22 for (i
= 0; i
< argc
; i
++)
24 } else { /* read from stdin */
25 char line
[LARGE_PACKET_MAX
];
26 while (fgets(line
, sizeof(line
), stdin
)) {
32 static void pack_raw_stdin(void)
34 struct strbuf sb
= STRBUF_INIT
;
36 if (strbuf_read(&sb
, 0, 0) < 0)
37 die_errno("failed to read from stdin");
38 packet_write(1, sb
.buf
, sb
.len
);
42 static void unpack(void)
44 struct packet_reader reader
;
45 packet_reader_init(&reader
, 0, NULL
, 0,
46 PACKET_READ_GENTLE_ON_EOF
|
47 PACKET_READ_CHOMP_NEWLINE
);
49 while (packet_reader_read(&reader
) != PACKET_READ_EOF
) {
50 switch (reader
.status
) {
53 case PACKET_READ_NORMAL
:
54 printf("%s\n", reader
.line
);
56 case PACKET_READ_FLUSH
:
59 case PACKET_READ_DELIM
:
62 case PACKET_READ_RESPONSE_END
:
69 static void unpack_sideband(int argc
, const char **argv
)
71 struct packet_reader reader
;
72 int options
= PACKET_READ_GENTLE_ON_EOF
;
73 int chomp_newline
= 1;
74 int reader_use_sideband
= 0;
75 const char *const unpack_sideband_usage
[] = {
76 "test_tool unpack_sideband [options...]", NULL
78 struct option cmd_options
[] = {
79 OPT_BOOL(0, "reader-use-sideband", &reader_use_sideband
,
80 "set use_sideband bit for packet reader (Default: off)"),
81 OPT_BOOL(0, "chomp-newline", &chomp_newline
,
82 "chomp newline in packet (Default: on)"),
86 argc
= parse_options(argc
, argv
, "", cmd_options
, unpack_sideband_usage
,
89 usage_msg_opt(_("too many arguments"), unpack_sideband_usage
,
93 options
|= PACKET_READ_CHOMP_NEWLINE
;
94 packet_reader_init(&reader
, 0, NULL
, 0, options
);
95 reader
.use_sideband
= reader_use_sideband
;
97 while (packet_reader_read(&reader
) != PACKET_READ_EOF
) {
101 switch (reader
.status
) {
102 case PACKET_READ_EOF
:
104 case PACKET_READ_NORMAL
:
106 * When the "use_sideband" field of the reader is turned
107 * on, sideband packets other than the payload have been
108 * parsed and consumed in packet_reader_read(), and only
109 * the payload arrives here.
111 if (reader
.use_sideband
) {
112 write_or_die(1, reader
.line
, reader
.pktlen
- 1);
116 band
= reader
.line
[0] & 0xff;
117 if (band
< 1 || band
> 2)
118 continue; /* skip non-sideband packets */
121 write_or_die(fd
, reader
.line
+ 1, reader
.pktlen
- 1);
123 case PACKET_READ_FLUSH
:
125 case PACKET_READ_DELIM
:
126 case PACKET_READ_RESPONSE_END
:
132 static int send_split_sideband(void)
134 const char *foo
= "Foo.\n";
135 const char *bar
= "Bar.\n";
136 const char *part1
= "Hello,";
137 const char *primary
= "\001primary: regular output\n";
138 const char *part2
= " world!\n";
140 /* Each sideband message has a trailing newline character. */
141 send_sideband(1, 2, foo
, strlen(foo
), LARGE_PACKET_MAX
);
142 send_sideband(1, 2, bar
, strlen(bar
), LARGE_PACKET_MAX
);
145 * One sideband message is divided into part1 and part2
146 * by the primary message.
148 send_sideband(1, 2, part1
, strlen(part1
), LARGE_PACKET_MAX
);
149 packet_write(1, primary
, strlen(primary
));
150 send_sideband(1, 2, part2
, strlen(part2
), LARGE_PACKET_MAX
);
151 packet_response_end(1);
154 * We use unpack_sideband() to consume packets. A flush packet
155 * is required to end parsing.
162 static int receive_sideband(void)
164 return recv_sideband("sideband", 0, 1);
167 int cmd__pkt_line(int argc
, const char **argv
)
170 die("too few arguments");
172 if (!strcmp(argv
[1], "pack"))
173 pack(argc
- 2, argv
+ 2);
174 else if (!strcmp(argv
[1], "pack-raw-stdin"))
176 else if (!strcmp(argv
[1], "unpack"))
178 else if (!strcmp(argv
[1], "unpack-sideband"))
179 unpack_sideband(argc
- 1, argv
+ 1);
180 else if (!strcmp(argv
[1], "send-split-sideband"))
181 send_split_sideband();
182 else if (!strcmp(argv
[1], "receive-sideband"))
185 die("invalid argument '%s'", argv
[1]);