Expand Up
@@ -14,15 +14,16 @@
// Make sure these are included first so they don't conflict with the system.
#include < limits.h>
#include " shared/rpc.h"
#include " llvmlibc_rpc_server.h"
#include " src/__support/RPC/rpc .h"
#include " include/llvm-libc-types/rpc_opcodes_t .h"
#include " src/__support/arg_list.h"
#include " src/stdio/printf_core/converter.h"
#include " src/stdio/printf_core/parser.h"
#include " src/stdio/printf_core/writer.h"
#include " src/stdio/gpu/file.h"
#include < algorithm>
#include < atomic>
#include < cstdio>
Expand Down
Expand Up
@@ -53,6 +54,26 @@ struct TempStorage {
};
} // namespace
enum Stream {
File = 0 ,
Stdin = 1 ,
Stdout = 2 ,
Stderr = 3 ,
};
// Get the associated stream out of an encoded number.
LIBC_INLINE ::FILE *to_stream (uintptr_t f) {
::FILE *stream = reinterpret_cast <FILE *>(f & ~0x3ull );
Stream type = static_cast <Stream>(f & 0x3ull );
if (type == Stdin)
return stdin;
if (type == Stdout)
return stdout;
if (type == Stderr)
return stderr;
return stream;
}
template <bool packed, uint32_t lane_size>
static void handle_printf (rpc::Server::Port &port, TempStorage &temp_storage) {
FILE *files[lane_size] = {nullptr };
Expand Down
Expand Up
@@ -260,7 +281,7 @@ rpc_status_t handle_server_impl(
port->recv ([&](rpc::Buffer *buffer, uint32_t id) {
data[id] = temp_storage.alloc (buffer->data [0 ]);
sizes[id] =
fread (data[id], 1 , buffer->data [0 ], file:: to_stream (buffer->data [1 ]));
fread (data[id], 1 , buffer->data [0 ], to_stream (buffer->data [1 ]));
});
port->send_n (data, sizes);
port->send ([&](rpc::Buffer *buffer, uint32_t id) {
Expand All
@@ -273,9 +294,8 @@ rpc_status_t handle_server_impl(
void *data[lane_size] = {nullptr };
port->recv ([&](rpc::Buffer *buffer, uint32_t id) {
data[id] = temp_storage.alloc (buffer->data [0 ]);
const char *str =
fgets (reinterpret_cast <char *>(data[id]), buffer->data [0 ],
file::to_stream (buffer->data [1 ]));
const char *str = fgets (reinterpret_cast <char *>(data[id]),
buffer->data [0 ], to_stream (buffer->data [1 ]));
sizes[id] = !str ? 0 : std::strlen (str) + 1 ;
});
port->send_n (data, sizes);
Expand Down
Expand Up
@@ -335,46 +355,46 @@ rpc_status_t handle_server_impl(
}
case RPC_FEOF: {
port->recv_and_send ([](rpc::Buffer *buffer, uint32_t ) {
buffer->data [0 ] = feof (file:: to_stream (buffer->data [0 ]));
buffer->data [0 ] = feof (to_stream (buffer->data [0 ]));
});
break ;
}
case RPC_FERROR: {
port->recv_and_send ([](rpc::Buffer *buffer, uint32_t ) {
buffer->data [0 ] = ferror (file:: to_stream (buffer->data [0 ]));
buffer->data [0 ] = ferror (to_stream (buffer->data [0 ]));
});
break ;
}
case RPC_CLEARERR: {
port->recv_and_send ([](rpc::Buffer *buffer, uint32_t ) {
clearerr (file:: to_stream (buffer->data [0 ]));
clearerr (to_stream (buffer->data [0 ]));
});
break ;
}
case RPC_FSEEK: {
port->recv_and_send ([](rpc::Buffer *buffer, uint32_t ) {
buffer->data [0 ] = fseek ( file::to_stream (buffer-> data [ 0 ]),
static_cast <long >(buffer->data [1 ]),
static_cast <int >(buffer->data [2 ]));
buffer->data [0 ] =
fseek ( to_stream (buffer-> data [ 0 ]), static_cast <long >(buffer->data [1 ]),
static_cast <int >(buffer->data [2 ]));
});
break ;
}
case RPC_FTELL: {
port->recv_and_send ([](rpc::Buffer *buffer, uint32_t ) {
buffer->data [0 ] = ftell (file:: to_stream (buffer->data [0 ]));
buffer->data [0 ] = ftell (to_stream (buffer->data [0 ]));
});
break ;
}
case RPC_FFLUSH: {
port->recv_and_send ([](rpc::Buffer *buffer, uint32_t ) {
buffer->data [0 ] = fflush (file:: to_stream (buffer->data [0 ]));
buffer->data [0 ] = fflush (to_stream (buffer->data [0 ]));
});
break ;
}
case RPC_UNGETC: {
port->recv_and_send ([](rpc::Buffer *buffer, uint32_t ) {
buffer->data [0 ] = ungetc ( static_cast < int >(buffer-> data [ 0 ]),
file:: to_stream (buffer->data [1 ]));
buffer->data [0 ] =
ungetc ( static_cast < int >(buffer-> data [ 0 ]), to_stream (buffer->data [1 ]));
});
break ;
}
Expand Down