1 #include "git-compat-util.h"
4 #include "write-or-die.h"
5 #include "parse-options.h"
7 static void pack_line(const char *line
)
9 if (!strcmp(line
, "0000") || !strcmp(line
, "0000\n"))
11 else if (!strcmp(line
, "0001") || !strcmp(line
, "0001\n"))
14 packet_write_fmt(1, "%s", line
);
17 static void pack(int argc
, const char **argv
)
19 if (argc
) { /* read from argv */
21 for (i
= 0; i
< argc
; i
++)
23 } else { /* read from stdin */
24 char line
[LARGE_PACKET_MAX
];
25 while (fgets(line
, sizeof(line
), stdin
)) {
31 static void pack_raw_stdin(void)
33 struct strbuf sb
= STRBUF_INIT
;
35 if (strbuf_read(&sb
, 0, 0) < 0)
36 die_errno("failed to read from stdin");
37 packet_write(1, sb
.buf
, sb
.len
);
41 static void unpack(void)
43 struct packet_reader reader
;
44 packet_reader_init(&reader
, 0, NULL
, 0,
45 PACKET_READ_GENTLE_ON_EOF
|
46 PACKET_READ_CHOMP_NEWLINE
);
48 while (packet_reader_read(&reader
) != PACKET_READ_EOF
) {
49 switch (reader
.status
) {
52 case PACKET_READ_NORMAL
:
53 printf("%s\n", reader
.line
);
55 case PACKET_READ_FLUSH
:
58 case PACKET_READ_DELIM
:
61 case PACKET_READ_RESPONSE_END
:
68 static void unpack_sideband(int argc
, const char **argv
)
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
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)"),
85 argc
= parse_options(argc
, argv
, "", cmd_options
, unpack_sideband_usage
,
88 usage_msg_opt(_("too many arguments"), unpack_sideband_usage
,
92 options
|= PACKET_READ_CHOMP_NEWLINE
;
93 packet_reader_init(&reader
, 0, NULL
, 0, options
);
94 reader
.use_sideband
= reader_use_sideband
;
96 while (packet_reader_read(&reader
) != PACKET_READ_EOF
) {
100 switch (reader
.status
) {
101 case PACKET_READ_EOF
:
103 case PACKET_READ_NORMAL
:
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.
110 if (reader
.use_sideband
) {
111 write_or_die(1, reader
.line
, reader
.pktlen
- 1);
115 band
= reader
.line
[0] & 0xff;
116 if (band
< 1 || band
> 2)
117 continue; /* skip non-sideband packets */
120 write_or_die(fd
, reader
.line
+ 1, reader
.pktlen
- 1);
122 case PACKET_READ_FLUSH
:
124 case PACKET_READ_DELIM
:
125 case PACKET_READ_RESPONSE_END
:
131 static int send_split_sideband(void)
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";
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
);
144 * One sideband message is divided into part1 and part2
145 * by the primary message.
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);
153 * We use unpack_sideband() to consume packets. A flush packet
154 * is required to end parsing.
161 static int receive_sideband(void)
163 return recv_sideband("sideband", 0, 1);
166 int cmd__pkt_line(int argc
, const char **argv
)
169 die("too few arguments");
171 if (!strcmp(argv
[1], "pack"))
172 pack(argc
- 2, argv
+ 2);
173 else if (!strcmp(argv
[1], "pack-raw-stdin"))
175 else if (!strcmp(argv
[1], "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"))
184 die("invalid argument '%s'", argv
[1]);