aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSergey Poznyakoff <gray@gnu.org.ua>2019-08-17 11:14:43 +0300
committerSergey Poznyakoff <gray@gnu.org.ua>2019-08-17 11:15:18 +0300
commit9faece352b572fd65e4579e178c4df8fc0c53c35 (patch)
tree0630ccb0804719a174de7c95e4e0c6972b3c34ea
parent12c5eac517cb62728b5525cab25aff245086525c (diff)
downloadruncap-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.am3
-rw-r--r--read.c69
-rw-r--r--runcap.h1
-rw-r--r--t/Makefile.am5
-rw-r--r--t/nocap.at (renamed from t/nocap00.at)15
-rw-r--r--t/nocap.inc78
-rw-r--r--t/nocap01.at22
-rw-r--r--t/read.at65
-rw-r--r--t/rt.c92
-rw-r--r--t/testsuite.at4
10 files changed, 221 insertions, 133 deletions
diff --git a/Make.am b/Make.am
index 7d5f0e6..32c35c9 100644
--- a/Make.am
+++ b/Make.am
@@ -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 @@
32RUNCAP_SRC = \ 32RUNCAP_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
diff --git a/read.c b/read.c
new file mode 100644
index 0000000..a57d9b9
--- /dev/null
+++ b/read.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
23ssize_t
24runcap_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}
diff --git a/runcap.h b/runcap.h
index efafdf3..94ee2e5 100644
--- a/runcap.h
+++ b/runcap.h
@@ -87,6 +87,7 @@ runcap_get_capture(struct runcap *rc, int stream)
87 return fp; 87 return fp;
88} 88}
89 89
90ssize_t runcap_read(struct runcap *rc, int sd, char *buf, size_t size);
90int runcap_getc(struct runcap *rc, int stream, char *cp); 91int runcap_getc(struct runcap *rc, int stream, char *cp);
91ssize_t runcap_getline(struct runcap *rc, int stream, char **pstr, size_t *psize); 92ssize_t runcap_getline(struct runcap *rc, int stream, char **pstr, size_t *psize);
92off_t runcap_tell(struct runcap *rc, int stream); 93off_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
17AT_SETUP([disable capture: size 0]) 17AT_SETUP([disable capture])
18AT_KEYWORDS([nocap]) 18AT_KEYWORDS([nocap])
19m4_include([nocap.inc]) 19# Create expout for the two tests that follow.
20AT_CHECK([cat $INPUT > expout
21cat >> expout <<\EOF
22res=0
23exit code: 0
24stdout: 0 lines, 0 bytes
25stderr: 0 lines, 0 bytes
26EOF
27])
28# Method 1: set buffer size to 0.
20AT_CHECK([rt -S stdout -s 0 -- genout $INPUT],[0],[expout]) 29AT_CHECK([rt -S stdout -s 0 -- genout $INPUT],[0],[expout])
30# Method 2: use the RCF_STDOUT_NOCAP flag.
31AT_CHECK([rt -S stdout -N -- genout $INPUT],[0],[expout])
21AT_CLEANUP 32AT_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 @@
1AT_DATA([expout],
2[CHAPTER I. Down the Rabbit-Hole
3
4Alice was beginning to get very tired of sitting by her sister on the
5bank, and of having nothing to do: once or twice she had peeped into the
6book her sister was reading, but it had no pictures or conversations
7in it, 'and what is the use of a book,' thought Alice 'without
8pictures or conversations?'
9
10So she was considering in her own mind (as well as she could, for the
11hot day made her feel very sleepy and stupid), whether the pleasure of
12making a daisy-chain would be worth the trouble of getting up and picking
13the daisies, when suddenly a White Rabbit with pink eyes ran close by her.
14
15There was nothing so very remarkable in that; nor did Alice think it
16so very much out of the way to hear the Rabbit say to itself, 'Oh
17dear! Oh dear! I shall be late!' (when she thought it over afterwards,
18it occurred to her that she ought to have wondered at this, but at the
19time it all seemed quite natural); but when the Rabbit actually took a
20watch out of its waistcoat-pocket, and looked at it, and then hurried on,
21Alice started to her feet, for it flashed across her mind that she had
22never before seen a rabbit with either a waistcoat-pocket, or a watch
23to take out of it, and burning with curiosity, she ran across the field
24after it, and fortunately was just in time to see it pop down a large
25rabbit-hole under the hedge.
26
27In another moment down went Alice after it, never once considering how
28in the world she was to get out again.
29
30The rabbit-hole went straight on like a tunnel for some way, and then
31dipped suddenly down, so suddenly that Alice had not a moment to think
32about stopping herself before she found herself falling down a very
33deep well.
34
35Either the well was very deep, or she fell very slowly, for she had plenty
36of time as she went down to look about her and to wonder what was going
37to happen next. First, she tried to look down and make out what she was
38coming to, but it was too dark to see anything; then she looked at the
39sides of the well, and noticed that they were filled with cupboards
40and book-shelves; here and there she saw maps and pictures hung upon
41pegs. She took down a jar from one of the shelves as she passed; it was
42labelled 'ORANGE MARMALADE', but to her great disappointment it was
43empty: she did not like to drop the jar for fear of killing somebody,
44so 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,
47I shall think nothing of tumbling down stairs! How brave they'll all
48think me at home! Why, I wouldn't say anything about it, even if I
49fell off the top of the house!' (Which was very likely true.)
50
51Down, down, down. Would the fall never come to an end! 'I wonder how
52many miles I've fallen by this time?' she said aloud. 'I must be
53getting somewhere near the centre of the earth. Let me see: that would be
54four thousand miles down, I think--' (for, you see, Alice had learnt
55several things of this sort in her lessons in the schoolroom, and though
56this was not a very good opportunity for showing off her knowledge,
57as there was no one to listen to her, still it was good practice to
58say it over) '--yes, that's about the right distance--but then I
59wonder what Latitude or Longitude I've got to?' (Alice had no idea
60what Latitude was, or Longitude either, but thought they were nice grand
61words to say.)
62
63Presently she began again. 'I wonder if I shall fall right through
64the earth! How funny it'll seem to come out among the people that
65walk with their heads downward! The Antipathies, I think--' (she was
66rather glad there was no one listening, this time, as it didn't sound
67at all the right word) '--but I shall have to ask them what the name
68of the country is, you know. Please, Ma'am, is this New Zealand or
69Australia?' (and she tried to curtsey as she spoke--fancy curtseying
70as you're falling through the air! Do you think you could manage
71it?) 'And what an ignorant little girl she'll think me for asking! No,
72it'll never do to ask: perhaps I shall see it written up somewhere.'
73res=0
74exit code: 0