aboutsummaryrefslogtreecommitdiff
path: root/test/src/test/output.c
blob: 663db4baaf3bf333a1c23cd7c77c5902c683c587 (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
/*
 * ecgen, tool for generating Elliptic curve domain parameters
 * Copyright (C) 2017 J08nY
 */
#include "output.h"
#include "io/output.h"
#include "misc/types.h"

FILE *read_out = NULL;
FILE *read_err = NULL;
FILE *read_verbose = NULL;

static void setup_stream(FILE **original_out, FILE **redirected_out) {
	int fd[2];
	pipe(fd);

	*redirected_out = fdopen(fd[0], "r");
	setlinebuf(*redirected_out);
	*original_out = fdopen(fd[1], "w");
	setlinebuf(*original_out);
}

void output_setup(void) {
	config_t cfg;
	memset(&cfg, 0, sizeof(cfg));
	output_init(&cfg);

	int in_fd[2];
	pipe(in_fd);

	setup_stream(&out, &read_out);
	setup_stream(&err, &read_err);
	setup_stream(&verbose, &read_verbose);
}

void output_teardown(void) {
	if (read_out) {
		fclose(out);
		fclose(read_out);
	}
	if (read_err) {
		fclose(err);
		fclose(read_err);
	}
	if (read_verbose) {
		fclose(verbose);
		fclose(read_verbose);
	}
}