Skip to content

Commit cb3f87c

Browse files
committed
vcs-svn: allow input from file descriptor
Based-on-patch-by: David Barr <[email protected]> Signed-off-by: Jonathan Nieder <[email protected]>
1 parent cc193f1 commit cb3f87c

File tree

5 files changed

+31
-7
lines changed

5 files changed

+31
-7
lines changed

t/t0081-line-buffer.sh

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,15 @@ test_expect_success PIPE,EXPENSIVE 'longer read (around 65536 bytes)' '
131131
long_read_test 65536
132132
'
133133

134+
test_expect_success 'read from file descriptor' '
135+
rm -f input &&
136+
echo hello >expect &&
137+
echo hello >input &&
138+
echo copy 6 |
139+
test-line-buffer "&4" 4<input >actual &&
140+
test_cmp expect actual
141+
'
142+
134143
test_expect_success 'buffer_read_string copes with null byte' '
135144
>expect &&
136145
q_to_nul <<-\EOF | test-line-buffer >actual &&

test-line-buffer.c

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,13 +69,18 @@ int main(int argc, char *argv[])
6969
else if (argc == 2)
7070
filename = argv[1];
7171
else
72-
usage("test-line-buffer [file] < script");
72+
usage("test-line-buffer [file | &fd] < script");
7373

7474
if (buffer_init(&stdin_buf, NULL))
7575
die_errno("open error");
7676
if (filename) {
77-
if (buffer_init(&file_buf, filename))
78-
die_errno("error opening %s", filename);
77+
if (*filename == '&') {
78+
if (buffer_fdinit(&file_buf, strtouint32(filename + 1)))
79+
die_errno("error opening fd %s", filename + 1);
80+
} else {
81+
if (buffer_init(&file_buf, filename))
82+
die_errno("error opening %s", filename);
83+
}
7984
input = &file_buf;
8085
}
8186

vcs-svn/line_buffer.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,14 @@ int buffer_init(struct line_buffer *buf, const char *filename)
1717
return 0;
1818
}
1919

20+
int buffer_fdinit(struct line_buffer *buf, int fd)
21+
{
22+
buf->infile = fdopen(fd, "r");
23+
if (!buf->infile)
24+
return -1;
25+
return 0;
26+
}
27+
2028
int buffer_deinit(struct line_buffer *buf)
2129
{
2230
int err;

vcs-svn/line_buffer.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ struct line_buffer {
1313
#define LINE_BUFFER_INIT {"", STRBUF_INIT, NULL}
1414

1515
int buffer_init(struct line_buffer *buf, const char *filename);
16+
int buffer_fdinit(struct line_buffer *buf, int fd);
1617
int buffer_deinit(struct line_buffer *buf);
1718
char *buffer_read_line(struct line_buffer *buf);
1819
char *buffer_read_string(struct line_buffer *buf, uint32_t len);

vcs-svn/line_buffer.txt

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,11 @@ resources.
2727
Functions
2828
---------
2929

30-
`buffer_init`::
31-
Open the named file for input. If filename is NULL,
32-
start reading from stdin. On failure, returns -1 (with
33-
errno indicating the nature of the failure).
30+
`buffer_init`, `buffer_fdinit`::
31+
Open the named file or file descriptor for input.
32+
buffer_init(buf, NULL) prepares to read from stdin.
33+
On failure, returns -1 (with errno indicating the nature
34+
of the failure).
3435

3536
`buffer_deinit`::
3637
Stop reading from the current file (closing it unless

0 commit comments

Comments
 (0)