]>
Commit | Line | Data |
---|---|---|
1 | #include <stdio.h> | |
2 | #include <sys/types.h> | |
3 | #include <sys/stat.h> | |
4 | #include <fcntl.h> | |
5 | #include <stdlib.h> | |
6 | #include <poll.h> | |
7 | ||
8 | void dowrite(char *s, int len) | |
9 | { | |
10 | int i; | |
11 | for (i=0; i<len; i++) | |
12 | { | |
13 | write(1, s+i, 1); | |
14 | } | |
15 | } | |
16 | ||
17 | int waitchar(int timeout) | |
18 | { | |
19 | struct pollfd pfd; | |
20 | ||
21 | pfd.fd = 0; | |
22 | pfd.events = POLLIN; | |
23 | return poll(&pfd, 1, timeout) == 1; | |
24 | } | |
25 | ||
26 | void expect(char *s, int len) | |
27 | { | |
28 | int i; | |
29 | char c; | |
30 | for (i=0; i < len; i++) | |
31 | { | |
32 | if (waitchar(100) == 0) | |
33 | { | |
34 | fprintf(stderr, "Timeout reached in expect (expected %c)\n", s[i]); | |
35 | return; | |
36 | } | |
37 | while (read(0, &c, 1) == 0) | |
38 | fprintf(stderr, "Short read...\n"); | |
39 | if (c != s[i]) | |
40 | fprintf(stderr, "Expect failed: expected %d, got %d (pos %d)\n", s[i], c, i); | |
41 | } | |
42 | } | |
43 | ||
44 | void expect_no_chars() | |
45 | { | |
46 | int cs = 0; | |
47 | ||
48 | while (waitchar(100) == 1) | |
49 | { | |
50 | char c; | |
51 | if (read(0, &c, 1) == 0) | |
52 | fprintf(stderr, "enc Short read...\n"); | |
53 | cs++; | |
54 | fprintf(stderr, "Warning: expected no chars, got %d\n", c); | |
55 | } | |
56 | if (cs) | |
57 | fprintf(stderr, "Expect no chars failed: got %d chars\n", cs); | |
58 | ||
59 | ||
60 | } | |
61 | ||
62 | void main(int argc, char **argv) | |
63 | { | |
64 | unsigned char buf[259]; | |
65 | int sz; | |
66 | int rfd; | |
67 | ||
68 | if (argc < 2) | |
69 | { | |
70 | fprintf(stderr, "Usage: %s [filename]\n", argv[0]); | |
71 | exit(1); | |
72 | } | |
73 | rfd = open(argv[1], O_RDONLY); | |
74 | if (rfd < 0) | |
75 | { | |
76 | perror("open"); | |
77 | exit(1); | |
78 | } | |
79 | ||
80 | dowrite("\x1B" "A\x00\x00\x00", 5); | |
81 | fprintf(stderr, "Address sent\n"); | |
82 | expect("A", 1); | |
83 | expect_no_chars(); | |
84 | while ((sz = read(rfd, buf+3, 255)) > 0) | |
85 | { | |
86 | buf[0] = 0x1B; | |
87 | buf[1] = 'D'; | |
88 | buf[2] = sz+1; | |
89 | dowrite(buf, sz + 3); | |
90 | fprintf(stderr, "Data sent\n"); | |
91 | expect("D", 1); | |
92 | expect_no_chars(); | |
93 | } | |
94 | exit(0); | |
95 | } |