diff options
author | Sergey Poznyakoff <gray@gnu.org.ua> | 2019-08-17 11:14:43 +0300 |
---|---|---|
committer | Sergey Poznyakoff <gray@gnu.org.ua> | 2019-08-17 11:15:18 +0300 |
commit | 9faece352b572fd65e4579e178c4df8fc0c53c35 (patch) | |
tree | 0630ccb0804719a174de7c95e4e0c6972b3c34ea | |
parent | 12c5eac517cb62728b5525cab25aff245086525c (diff) | |
download | runcap-9faece352b572fd65e4579e178c4df8fc0c53c35.tar.gz runcap-9faece352b572fd65e4579e178c4df8fc0c53c35.tar.bz2 |
Implement runcap_read functionv1.2
* Make.am (RUNCAP_SRC): Add read.c
* read.c: New file
* runcap.h (runcap_read): New proto.
* t/rt: Expand read request to include a flag for selecting
* t/read.at: New file.
* t/nocap.at: New file
* t/nocap00.at: Remove.
* t/nocap01.at: Remove.
* t/Makefile.am (TESTSUITE_AT): Add new test. Merge two nocap tests
to one.
* t/testsuite.at: Likewise.
-rw-r--r-- | Make.am | 3 | ||||
-rw-r--r-- | read.c | 69 | ||||
-rw-r--r-- | runcap.h | 1 | ||||
-rw-r--r-- | t/Makefile.am | 5 | ||||
-rw-r--r-- | t/nocap.at (renamed from t/nocap00.at) | 15 | ||||
-rw-r--r-- | t/nocap.inc | 78 | ||||
-rw-r--r-- | t/nocap01.at | 22 | ||||
-rw-r--r-- | t/read.at | 65 | ||||
-rw-r--r-- | t/rt.c | 92 | ||||
-rw-r--r-- | t/testsuite.at | 4 |
10 files changed, 221 insertions, 133 deletions
@@ -1,5 +1,5 @@ | |||
1 | # Main Makefile.am source for runcap | 1 | # Main Makefile.am source for runcap |
2 | # Copyright (C) 2017 Sergey Poznyakoff | 2 | # Copyright (C) 2017-2019 Sergey Poznyakoff |
3 | # | 3 | # |
4 | # Runcap is free software; you can redistribute it and/or modify it | 4 | # Runcap is free software; you can redistribute it and/or modify it |
5 | # under the terms of the GNU General Public License as published by the | 5 | # under the terms of the GNU General Public License as published by the |
@@ -32,6 +32,7 @@ | |||
32 | RUNCAP_SRC = \ | 32 | RUNCAP_SRC = \ |
33 | getc.c\ | 33 | getc.c\ |
34 | getl.c\ | 34 | getl.c\ |
35 | read.c\ | ||
35 | runcap.c\ | 36 | runcap.c\ |
36 | seek.c\ | 37 | seek.c\ |
37 | tell.c | 38 | tell.c |
@@ -0,0 +1,69 @@ | |||
1 | /* runcap - run program and capture its output | ||
2 | Copyright (C) 2019 Sergey Poznyakoff | ||
3 | |||
4 | Runcap is free software; you can redistribute it and/or modify it | ||
5 | under the terms of the GNU General Public License as published by the | ||
6 | Free Software Foundation; either version 3 of the License, or (at your | ||
7 | option) any later version. | ||
8 | |||
9 | Runcap is distributed in the hope that it will be useful, | ||
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
12 | GNU General Public License for more details. | ||
13 | |||
14 | You should have received a copy of the GNU General Public License along | ||
15 | with Runcap. If not, see <http://www.gnu.org/licenses/>. */ | ||
16 | |||
17 | #include <stdlib.h> | ||
18 | #include <unistd.h> | ||
19 | #include <errno.h> | ||
20 | #include <string.h> | ||
21 | #include "runcap.h" | ||
22 | |||
23 | ssize_t | ||
24 | runcap_read(struct runcap *rc, int sd, char *buf, size_t size) | ||
25 | { | ||
26 | struct stream_capture *cap; | ||
27 | ssize_t nread = 0; | ||
28 | |||
29 | if (!buf) { | ||
30 | errno = EINVAL; | ||
31 | return -1; | ||
32 | } | ||
33 | if (size == 0) | ||
34 | return 0; | ||
35 | |||
36 | cap = runcap_get_capture(rc, sd); | ||
37 | if (!cap) | ||
38 | return -1; | ||
39 | |||
40 | while (size) { | ||
41 | size_t avail = cap->sc_level - cap->sc_cur; | ||
42 | if (avail == 0) { | ||
43 | if (cap->sc_storfd != -1) { | ||
44 | ssize_t r = read(cap->sc_storfd, | ||
45 | cap->sc_base, | ||
46 | cap->sc_size); | ||
47 | if (r < 0) | ||
48 | return -1; | ||
49 | else if (r == 0) | ||
50 | break; | ||
51 | avail = r; | ||
52 | cap->sc_level = r; | ||
53 | cap->sc_cur = 0; | ||
54 | } else { | ||
55 | break; | ||
56 | } | ||
57 | } | ||
58 | |||
59 | if (avail > size) | ||
60 | avail = size; | ||
61 | memcpy(buf + nread, cap->sc_base + cap->sc_cur, avail); | ||
62 | |||
63 | cap->sc_cur += avail; | ||
64 | nread += avail; | ||
65 | size -= avail; | ||
66 | } | ||
67 | |||
68 | return nread; | ||
69 | } | ||
@@ -87,6 +87,7 @@ runcap_get_capture(struct runcap *rc, int stream) | |||
87 | return fp; | 87 | return fp; |
88 | } | 88 | } |
89 | 89 | ||
90 | ssize_t runcap_read(struct runcap *rc, int sd, char *buf, size_t size); | ||
90 | int runcap_getc(struct runcap *rc, int stream, char *cp); | 91 | int runcap_getc(struct runcap *rc, int stream, char *cp); |
91 | ssize_t runcap_getline(struct runcap *rc, int stream, char **pstr, size_t *psize); | 92 | ssize_t runcap_getline(struct runcap *rc, int stream, char **pstr, size_t *psize); |
92 | off_t runcap_tell(struct runcap *rc, int stream); | 93 | off_t runcap_tell(struct runcap *rc, int stream); |
diff --git a/t/Makefile.am b/t/Makefile.am index 4e44f05..0a16429 100644 --- a/t/Makefile.am +++ b/t/Makefile.am | |||
@@ -47,13 +47,12 @@ TESTSUITE_AT = \ | |||
47 | longout.at\ | 47 | longout.at\ |
48 | stdin.at\ | 48 | stdin.at\ |
49 | pipe.at\ | 49 | pipe.at\ |
50 | read.at\ | ||
50 | linemon00.at\ | 51 | linemon00.at\ |
51 | linemon01.at\ | 52 | linemon01.at\ |
52 | linemon02.at\ | 53 | linemon02.at\ |
53 | linemon03.at\ | 54 | linemon03.at\ |
54 | nocap.inc\ | 55 | nocap.at\ |
55 | nocap00.at\ | ||
56 | nocap01.at\ | ||
57 | seek00.at\ | 56 | seek00.at\ |
58 | seek01.at | 57 | seek01.at |
59 | # Add more files here | 58 | # Add more files here |
diff --git a/t/nocap00.at b/t/nocap.at index 9323936..45abd6c 100644 --- a/t/nocap00.at +++ b/t/nocap.at | |||
@@ -14,9 +14,20 @@ | |||
14 | # You should have received a copy of the GNU General Public License along | 14 | # You should have received a copy of the GNU General Public License along |
15 | # with Runcap. If not, see <http://www.gnu.org/licenses/>. | 15 | # with Runcap. If not, see <http://www.gnu.org/licenses/>. |
16 | 16 | ||
17 | AT_SETUP([disable capture: size 0]) | 17 | AT_SETUP([disable capture]) |
18 | AT_KEYWORDS([nocap]) | 18 | AT_KEYWORDS([nocap]) |
19 | m4_include([nocap.inc]) | 19 | # Create expout for the two tests that follow. |
20 | AT_CHECK([cat $INPUT > expout | ||
21 | cat >> expout <<\EOF | ||
22 | res=0 | ||
23 | exit code: 0 | ||
24 | stdout: 0 lines, 0 bytes | ||
25 | stderr: 0 lines, 0 bytes | ||
26 | EOF | ||
27 | ]) | ||
28 | # Method 1: set buffer size to 0. | ||
20 | AT_CHECK([rt -S stdout -s 0 -- genout $INPUT],[0],[expout]) | 29 | AT_CHECK([rt -S stdout -s 0 -- genout $INPUT],[0],[expout]) |
30 | # Method 2: use the RCF_STDOUT_NOCAP flag. | ||
31 | AT_CHECK([rt -S stdout -N -- genout $INPUT],[0],[expout]) | ||
21 | AT_CLEANUP | 32 | AT_CLEANUP |
22 | 33 | ||
diff --git a/t/nocap.inc b/t/nocap.inc deleted file mode 100644 index fb1e20c..0000000 --- a/t/nocap.inc +++ /dev/null | |||
@@ -1,78 +0,0 @@ | |||
1 | AT_DATA([expout], | ||
2 | [CHAPTER I. Down the Rabbit-Hole | ||
3 | |||
4 | Alice was beginning to get very tired of sitting by her sister on the | ||
5 | bank, and of having nothing to do: once or twice she had peeped into the | ||
6 | book her sister was reading, but it had no pictures or conversations | ||
7 | in it, 'and what is the use of a book,' thought Alice 'without | ||
8 | pictures or conversations?' | ||
9 | |||
10 | So she was considering in her own mind (as well as she could, for the | ||
11 | hot day made her feel very sleepy and stupid), whether the pleasure of | ||
12 | making a daisy-chain would be worth the trouble of getting up and picking | ||
13 | the daisies, when suddenly a White Rabbit with pink eyes ran close by her. | ||
14 | |||
15 | There was nothing so very remarkable in that; nor did Alice think it | ||
16 | so very much out of the way to hear the Rabbit say to itself, 'Oh | ||
17 | dear! Oh dear! I shall be late!' (when she thought it over afterwards, | ||
18 | it occurred to her that she ought to have wondered at this, but at the | ||
19 | time it all seemed quite natural); but when the Rabbit actually took a | ||
20 | watch out of its waistcoat-pocket, and looked at it, and then hurried on, | ||
21 | Alice started to her feet, for it flashed across her mind that she had | ||
22 | never before seen a rabbit with either a waistcoat-pocket, or a watch | ||
23 | to take out of it, and burning with curiosity, she ran across the field | ||
24 | after it, and fortunately was just in time to see it pop down a large | ||
25 | rabbit-hole under the hedge. | ||
26 | |||
27 | In another moment down went Alice after it, never once considering how | ||
28 | in the world she was to get out again. | ||
29 | |||
30 | The rabbit-hole went straight on like a tunnel for some way, and then | ||
31 | dipped suddenly down, so suddenly that Alice had not a moment to think | ||
32 | about stopping herself before she found herself falling down a very | ||
33 | deep well. | ||
34 | |||
35 | Either the well was very deep, or she fell very slowly, for she had plenty | ||
36 | of time as she went down to look about her and to wonder what was going | ||
37 | to happen next. First, she tried to look down and make out what she was | ||
38 | coming to, but it was too dark to see anything; then she looked at the | ||
39 | sides of the well, and noticed that they were filled with cupboards | ||
40 | and book-shelves; here and there she saw maps and pictures hung upon | ||
41 | pegs. She took down a jar from one of the shelves as she passed; it was | ||
42 | labelled 'ORANGE MARMALADE', but to her great disappointment it was | ||
43 | empty: she did not like to drop the jar for fear of killing somebody, | ||
44 | so managed to put it into one of the cupboards as she fell past it. | ||
45 | |||
46 | 'Well!' thought Alice to herself, 'after such a fall as this, | ||
47 | I shall think nothing of tumbling down stairs! How brave they'll all | ||
48 | think me at home! Why, I wouldn't say anything about it, even if I | ||
49 | fell off the top of the house!' (Which was very likely true.) | ||
50 | |||
51 | Down, down, down. Would the fall never come to an end! 'I wonder how | ||
52 | many miles I've fallen by this time?' she said aloud. 'I must be | ||
53 | getting somewhere near the centre of the earth. Let me see: that would be | ||
54 | four thousand miles down, I think--' (for, you see, Alice had learnt | ||
55 | several things of this sort in her lessons in the schoolroom, and though | ||
56 | this was not a very good opportunity for showing off her knowledge, | ||
57 | as there was no one to listen to her, still it was good practice to | ||
58 | say it over) '--yes, that's about the right distance--but then I | ||
59 | wonder what Latitude or Longitude I've got to?' (Alice had no idea | ||
60 | what Latitude was, or Longitude either, but thought they were nice grand | ||
61 | words to say.) | ||
62 | |||
63 | Presently she began again. 'I wonder if I shall fall right through | ||
64 | the earth! How funny it'll seem to come out among the people that | ||
65 | walk with their heads downward! The Antipathies, I think--' (she was | ||
66 | rather glad there was no one listening, this time, as it didn't sound | ||
67 | at all the right word) '--but I shall have to ask them what the name | ||
68 | of the country is, you know. Please, Ma'am, is this New Zealand or | ||
69 | Australia?' (and she tried to curtsey as she spoke--fancy curtseying | ||
70 | as you're falling through the air! Do you think you could manage | ||
71 | it?) 'And what an ignorant little girl she'll think me for asking! No, | ||
72 | it'll never do to ask: perhaps I shall see it written up somewhere.' | ||
73 | res=0 | ||
74 | exit code: 0 |