| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
#include <unistd.h> |
| 23 |
|
| 24 |
#include <gio/gio.h> |
| 25 |
#include <gio/gunixinputstream.h> |
| 26 |
#include <gio/gunixoutputstream.h> |
| 27 |
|
| 28 |
#include "cat-util.h" |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
static DexFuture * |
| 41 |
cat_read_fiber (gpointer user_data) |
| 42 |
{ |
| 43 |
Cat *cat = user_data; |
| 44 |
GInputStream *stream = g_unix_input_stream_new (cat->read_fd, FALSE); |
| 45 |
Buffer *buffer = NULL; |
| 46 |
|
| 47 |
for (;;) |
| 48 |
{ |
| 49 |
Buffer *next; |
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
if (buffer != NULL) |
| 55 |
dex_await (dex_channel_send (cat->channel, |
| 56 |
dex_future_new_for_pointer (g_steal_pointer (&buffer))), |
| 57 |
NULL); |
| 58 |
|
| 59 |
|
| 60 |
next = cat_pop_buffer (cat); |
| 61 |
|
| 62 |
|
| 63 |
next->length = dex_await_int64 (dex_input_stream_read (stream, |
| 64 |
next->data, |
| 65 |
next->capacity, |
| 66 |
G_PRIORITY_DEFAULT), |
| 67 |
NULL); |
| 68 |
|
| 69 |
|
| 70 |
if (next->length <= 0) |
| 71 |
{ |
| 72 |
dex_channel_close_send (cat->channel); |
| 73 |
cat_push_buffer (cat, next); |
| 74 |
break; |
| 75 |
} |
| 76 |
|
| 77 |
buffer = next; |
| 78 |
} |
| 79 |
|
| 80 |
|
| 81 |
dex_await (dex_input_stream_close (stream, 0), NULL); |
| 82 |
|
| 83 |
g_object_unref (stream); |
| 84 |
|
| 85 |
return dex_future_new_for_boolean (TRUE); |
| 86 |
} |
| 87 |
|
| 88 |
static DexFuture * |
| 89 |
cat_write_fiber (gpointer user_data) |
| 90 |
{ |
| 91 |
Cat *cat = user_data; |
| 92 |
GOutputStream *stream = g_unix_output_stream_new (cat->write_fd, FALSE); |
| 93 |
|
| 94 |
for (;;) |
| 95 |
{ |
| 96 |
Buffer *buffer; |
| 97 |
gssize len; |
| 98 |
|
| 99 |
|
| 100 |
if (!(buffer = dex_await_pointer (dex_channel_receive (cat->channel), NULL))) |
| 101 |
break; |
| 102 |
|
| 103 |
|
| 104 |
len = dex_await_int64 (dex_output_stream_write (G_OUTPUT_STREAM (stream), |
| 105 |
buffer->data, |
| 106 |
buffer->length, |
| 107 |
G_PRIORITY_DEFAULT), |
| 108 |
NULL); |
| 109 |
|
| 110 |
|
| 111 |
cat_push_buffer (cat, buffer); |
| 112 |
|
| 113 |
|
| 114 |
if (len <= 0) |
| 115 |
break; |
| 116 |
} |
| 117 |
|
| 118 |
|
| 119 |
dex_await (dex_output_stream_close (stream, 0), NULL); |
| 120 |
|
| 121 |
|
| 122 |
g_object_unref (stream); |
| 123 |
|
| 124 |
return dex_future_new_for_boolean (TRUE); |
| 125 |
} |
| 126 |
|
| 127 |
int |
| 128 |
main (int argc, |
| 129 |
char *argv[]) |
| 130 |
{ |
| 131 |
GError *error = NULL; |
| 132 |
Cat cat; |
| 133 |
int ret = EXIT_SUCCESS; |
| 134 |
|
| 135 |
dex_init (); |
| 136 |
|
| 137 |
if (!cat_init (&cat, &argc, &argv, &error) || |
| 138 |
!cat_run (&cat, |
| 139 |
dex_scheduler_spawn (NULL, 0, cat_read_fiber, &cat, NULL), |
| 140 |
dex_scheduler_spawn (NULL, 0, cat_write_fiber, &cat, NULL), |
| 141 |
&error)) |
| 142 |
{ |
| 143 |
g_printerr ("cat: %s\n", error->message); |
| 144 |
g_clear_error (&error); |
| 145 |
ret = EXIT_FAILURE; |
| 146 |
} |
| 147 |
|
| 148 |
cat_clear (&cat); |
| 149 |
|
| 150 |
return ret; |
| 151 |
} |