aboutsummaryrefslogtreecommitdiff
path: root/capture.c
blob: 4d2c04c5c5150386251c72bbab0002a520cdccbc (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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#include "capture.h"
#include <stdlib.h>

static void
call_monitor(SV *cv, const char *ptr, size_t sz)
{
	dSP;

	ENTER;
	SAVETMPS;

	PUSHMARK(SP);
	XPUSHs(sv_2mortal(newSVpv(ptr, sz)));
	PUTBACK;

	call_sv(cv, G_DISCARD);

	FREETMPS;
	LEAVE;
}

static void
line_monitor(const char *ptr, size_t sz, void *closure)
{
	struct line_closure *lc = closure;

	if (lc->len || ptr[sz-1] != '\n') {
		size_t newsz = lc->len + sz + 1;

		if (newsz > lc->size) {
			lc->str = realloc(lc->str, newsz);
			if (!lc->str)
				croak("Out of memory");
			lc->size = newsz;
		}
		memcpy(lc->str + lc->len, ptr, sz);
		lc->len += sz;
		lc->str[lc->len] = 0;

		if (lc->str[lc->len - 1] == '\n') {
			call_monitor(lc->cv, lc->str, lc->len);
			lc->len = 0;
		}
	} else
		call_monitor(lc->cv, ptr, sz);
}

struct capture *
capture_new(AV *av, unsigned timeout, SV *cb[2])
{
	struct capture *cp;
	I32 i, n;
	char **argv;

	cp = malloc(sizeof *cp);
	if (!cp)
		goto nomem;
	memset(cp, 0, sizeof *cp);
	n = av_len(av);
	if (n == -1)
		return cp;
	argv = calloc(n + 2, sizeof *argv);
	if (!argv)
		goto nomem;
	for (i = 0; i <= n; i++) {
		SV *sv, **psv = av_fetch(av, i, 0);
		if (!psv)
			croak("element %d doesn't exist", i);
		sv = *psv;
		if (SvROK(sv)) {
			croak("argument #%d is not a scalar", i);
		} else {
			char *s = SvPV_nolen(sv);
			if ((argv[i] = strdup(s)) == NULL)
				goto nomem;
		}
	}
	argv[i] = NULL;

	cp->rc.rc_argv = argv;

	if (timeout) {
		cp->rc.rc_timeout = timeout;
		cp->flags |= RCF_TIMEOUT;
	}

	cp->closure[0].cv = cb[0];
	if (cb[0] != &PL_sv_undef) {
		SvREFCNT_inc(cb[0]);
		cp->rc.rc_cap[RUNCAP_STDOUT].sc_linemon = line_monitor;
		cp->rc.rc_cap[RUNCAP_STDOUT].sc_monarg = &cp->closure[0];
		cp->flags |= RCF_STDOUT_LINEMON;
	}

	cp->closure[1].cv = cb[1];
	if (cb[1] != &PL_sv_undef) {
		SvREFCNT_inc(cb[1]);
		cp->closure[1].cv = cb[1];
		cp->rc.rc_cap[RUNCAP_STDERR].sc_linemon = line_monitor;
		cp->rc.rc_cap[RUNCAP_STDERR].sc_monarg = &cp->closure[1];
		cp->flags |= RCF_STDOUT_LINEMON;
	}
	
	return cp;
  nomem:
	return NULL;
}

void
capture_DESTROY(struct capture *cp)
{
//	printf("DESTROY\n");
	if (cp->rc.rc_argv) {
		size_t i;
		for (i = 0; cp->rc.rc_argv[i]; i++) {
			free(cp->rc.rc_argv[i]);
		}
		free(cp->rc.rc_argv);
	}
	runcap_free(&cp->rc);

	free(cp->closure[0].str);
	if (cp->closure[0].cv != &PL_sv_undef)
		SvREFCNT_dec(cp->closure[0].cv);

	free(cp->closure[1].str);
	if (cp->closure[1].cv != &PL_sv_undef)
		SvREFCNT_dec(cp->closure[1].cv);
	free(cp);
}	

char *
capture_next_line(struct capture *cp, int fd)
{
	char *buf = NULL;
        size_t sz = 0;
        ssize_t n;
	
	if (fd != RUNCAP_STDOUT && fd != RUNCAP_STDERR) {
		croak("invalid stream number: %d", fd);
	}
        n = runcap_getline(&cp->rc, fd, &buf, &sz);
        if (n == -1)
		croak("error getting line: %s", strerror(errno));
        if (n == 0)
		return NULL;
	return realloc(buf, n + 1);
}

int
capture_run(struct capture *cp)
{
	int res;
	
	if (!cp->rc.rc_argv)
		croak("no command line given");
	
	res = runcap(&cp->rc, cp->flags);

	if (cp->flags & RCF_STDOUT_LINEMON && cp->closure[0].len) {
		call_monitor(cp->closure[0].cv,
			     cp->closure[0].str,
			     cp->closure[0].len);
		cp->closure[0].len = 0;
	}

	if (cp->flags & RCF_STDERR_LINEMON && cp->closure[1].len) {
		call_monitor(cp->closure[1].cv,
			     cp->closure[1].str,
			     cp->closure[1].len);
		cp->closure[1].len = 0;
	}

	return res;
}

Return to:

Send suggestions and report system problems to the System administrator.