1#include "pkt-line.h"
2
3static void pack_line(const char *line)
4{
5 if (!strcmp(line, "0000") || !strcmp(line, "0000\n"))
6 packet_flush(1);
7 else if (!strcmp(line, "0001") || !strcmp(line, "0001\n"))
8 packet_delim(1);
9 else
10 packet_write_fmt(1, "%s", line);
11}
12
13static void pack(int argc, const char **argv)
14{
15 if (argc) { /* read from argv */
16 int i;
17 for (i = 0; i < argc; i++)
18 pack_line(argv[i]);
19 } else { /* read from stdin */
20 char line[LARGE_PACKET_MAX];
21 while (fgets(line, sizeof(line), stdin)) {
22 pack_line(line);
23 }
24 }
25}
26
27static void unpack(void)
28{
29 struct packet_reader reader;
30 packet_reader_init(&reader, 0, NULL, 0,
31 PACKET_READ_GENTLE_ON_EOF |
32 PACKET_READ_CHOMP_NEWLINE);
33
34 while (packet_reader_read(&reader) != PACKET_READ_EOF) {
35 switch (reader.status) {
36 case PACKET_READ_EOF:
37 break;
38 case PACKET_READ_NORMAL:
39 printf("%s\n", reader.line);
40 break;
41 case PACKET_READ_FLUSH:
42 printf("0000\n");
43 break;
44 case PACKET_READ_DELIM:
45 printf("0001\n");
46 break;
47 }
48 }
49}
50
51int cmd_main(int argc, const char **argv)
52{
53 if (argc < 2)
54 die("too few arguments");
55
56 if (!strcmp(argv[1], "pack"))
57 pack(argc - 2, argv + 2);
58 else if (!strcmp(argv[1], "unpack"))
59 unpack();
60 else
61 die("invalid argument '%s'", argv[1]);
62
63 return 0;
64}