-
Notifications
You must be signed in to change notification settings - Fork 0
/
pipe.c
82 lines (71 loc) · 2.32 KB
/
pipe.c
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
#include "terminal.h"
#include <gtk/gtk.h>
#define FIFO_STREAM_PATH "/tmp/it_fifo_stream_"
#define FIFO_INPUT_PATH "/tmp/it_fifo_input_"
#define FIFO_OUTPUT_PATH "/tmp/it_fifo_output_"
#define FIFO_EVENT_PATH "/tmp/it_fifo_event_"
static FILE *handle_stream = NULL;
static FILE *handle_input = NULL;
static FILE *handle_output = NULL;
static FILE *handle_event = NULL;
static FILE *pipe_open(char *name, char *suffix, char *filemode) {
GString *buff = g_string_new(NULL);
g_string_printf(buff, "%s%s", name, suffix);
FILE *handle = fopen(buff->str, filemode);
if (handle == NULL) {
perror("pipe failed");
exit(EXIT_FAILURE);
}
g_string_free(buff, TRUE);
return handle;
}
void pipe_init(char *pipe_suffix) {
handle_output = pipe_open(FIFO_OUTPUT_PATH, pipe_suffix, "w");
handle_event = pipe_open(FIFO_EVENT_PATH, pipe_suffix, "w");
handle_input = pipe_open(FIFO_INPUT_PATH, pipe_suffix, "r");
handle_stream = pipe_open(FIFO_STREAM_PATH, pipe_suffix, "r");
io_input_start(handle_input);
io_stream_start(handle_stream);
}
void pipe_done() {
if (fclose(handle_input) != 0) {
perror("pipe close failed (i)");
exit(EXIT_FAILURE);
}
if (fclose(handle_stream) != 0) {
perror("pipe close failed (s)");
exit(EXIT_FAILURE);
}
if (fclose(handle_output) != 0) {
perror("pipe close failed (o)");
exit(EXIT_FAILURE);
}
if (fclose(handle_event) != 0) {
perror("pipe close failed (e)");
exit(EXIT_FAILURE);
}
}
void pipe_output_write(const void *data, const int length) {
if (fwrite(data, length, 1, handle_output) == 0) {
perror("pipe write (o)");
exit(EXIT_FAILURE);
}
}
void pipe_output_write_string(const char *data) {
int32_t length = strlen(data);
pipe_output_write(&length, sizeof length);
pipe_output_write(data, length);
}
void pipe_output_flush() { fflush(handle_output); }
void pipe_event_write(const void *data, const int length) {
if (fwrite(data, length, 1, handle_event) == 0) {
perror("pipe write (e)");
exit(EXIT_FAILURE);
}
}
void pipe_event_write_string(const char *data) {
int32_t length = strlen(data);
pipe_event_write(&length, sizeof length);
pipe_event_write(data, length);
}
void pipe_event_flush() { fflush(handle_event); }