aboutsummaryrefslogtreecommitdiff
path: root/Capture.xs
blob: df47544fdeb450a75b89e1e2a1c9d1635d68d59a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#include "capture.h"

MODULE = POSIX::Run::Capture		PACKAGE = POSIX::Run::Capture PREFIX = capture_
PROTOTYPES: ENABLE

=head2 new POSIX::Run::Capture

   new POSIX::Run::Capture([ $command, @args ]);

   new POSIX::Run::Capture(argv => [ $command, @args ],
			   stdout => sub { ... },
			   stderr => sub { ... },
			   timeout => N)
			   

=cut
	
POSIX::Run::Capture
capture_new(package, ...)
	char *package;
  PREINIT:
        AV *argv = NULL;
        unsigned timeout = 0;
        SV *cb[2] = { &PL_sv_undef, &PL_sv_undef };
  CODE:
        if (items == 2) {
		SV *sv = ST(1);
		if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVAV) {
			argv = (AV*) SvRV(sv);
		} else
			croak("single argument must be an array ref");
	} else if (items % 2 == 0)
		croak("Bad number of arguments");
        else {
		int i;
		for (i = 1; i < items; i += 2) {
			char *kw;
			SV *sv = ST(i);
			SV *val = ST(i+1);
			
			if (!SvPOK(sv))
				croak("bad arguments near #%d", i);
			kw = SvPV_nolen(sv);
			if (strcmp(kw, "argv") == 0) {
				if (SvROK(val)
				    && SvTYPE(SvRV(val)) == SVt_PVAV) {
					argv = (AV*) SvRV(val);
				} else
					croak("argv must be an array ref");
			} else if (strcmp(kw, "stdout") == 0
				   || strcmp(kw, "stderr") == 0) {
				if (SvROK(val)
				    && SvTYPE(SvRV(val)) == SVt_PVCV) {
					cb[kw[3] == 'o' ? 0 : 1] = SvRV(val);
				} else
					croak("%s must be a code ref", kw);
			} else if (strcmp(kw, "timeout") == 0) {
				if (SvIOK(val)) {
					timeout = SvUV(val);
				} else
					croak("timeout must be a number of seconds");
			} else
				croak("unknown keyword argument %s", kw);
		}
	}
        if (!argv)
		croak("argv not defined");
        RETVAL = capture_new(argv, timeout, cb);
        if (!RETVAL)
		croak("Out of memory!");
  OUTPUT:
        RETVAL

void
capture_DESTROY(cp)
	POSIX::Run::Capture cp;

int
capture_run(cp)
	POSIX::Run::Capture cp;

int
capture_status(cp)
	POSIX::Run::Capture cp;
  CODE:
	RETVAL = cp->rc.rc_status;
  OUTPUT:
        RETVAL

int
capture_errno(cp)
	POSIX::Run::Capture cp;
  CODE:
	RETVAL = cp->rc.rc_errno;
  OUTPUT:
        RETVAL

size_t
capture_nlines(cp, n)
	POSIX::Run::Capture cp;
	int n;
  CODE:
	if (n != RUNCAP_STDOUT && n != RUNCAP_STDERR) {
		croak("invalid stream number: %d", n);
	}
        RETVAL = cp->rc.rc_cap[n].sc_nlines;
  OUTPUT:
        RETVAL

size_t
capture_length(cp, n)
	POSIX::Run::Capture cp;
	int n;
  CODE:
	if (n != RUNCAP_STDOUT && n != RUNCAP_STDERR) {
		croak("invalid stream number: %d", n);
	}
        RETVAL = cp->rc.rc_cap[n].sc_leng;
  OUTPUT:
        RETVAL
	
char *
capture_next_line(cp, n)
	POSIX::Run::Capture cp;
	int n;

void
capture_rewind(cp, n)
	POSIX::Run::Capture cp;
	int n;
   CODE:
        runcap_rewind(&cp->rc, n);

Return to:

Send suggestions and report system problems to the System administrator.