#include <k/io.h>
#include <k/core.h>
#include <k/def.h>
/* send.c - kiosend() "send to channel"
 * ~ lexi hale <lexi@hale.su>
 * kiosend() writes to a channel with an open out stream
 */

/* we define all platform functions here,
 * whether or not they're for the correct
 * platform - only the ones actually called
 * by the generated code will be linked */
#include <posix/posix.h>
#include <error_table.h>

kiocond kiosend(kiochan target, ksraw string, sz* len) {
	if (target.out.kind == kiostream_closed) return kiocond_fail_closed_stream;

#	ifdef KFenv_posix
		/* issue the write syscall here and now so we can
		 * retrieve errno and report it if necessary */

		k_platform_syscall_arg args[] = {
			target.out.platform_fd, (k_platform_syscall_arg)string.ptr, string.size };

		struct k_platform_syscall_answer a = k_platform_syscall
			(k_platform_syscall_write,3,args);

		sz size = a.ret;
		if (size == -1) switch(a.error) {
			case k_platform_error_EPERM:  return kiocond_fail_forbidden;
			case k_platform_error_EINVAL: return kiocond_fail_invalid;
			case k_platform_error_EBADF:  return kiocond_fail_invalid;
			case k_platform_error_EFAULT: return kiocond_fail_pfault;
			case k_platform_error_ENOSPC: return kiocond_fail_no_space;
			case k_platform_error_EDQUOT: return kiocond_fail_over_quota;
			case k_platform_error_EIO:    return kiocond_fail_io;
			case k_platform_error_EAGAIN: return kiocond_poll;
			case k_platform_error_EFBIG:  return kiocond_fail_too_big;
			case k_platform_error_EINTR:  return kiocond_interrupt;
			case k_platform_error_EDESTADDRREQ: return kiocond_fail_no_peer;
			default: return kiocond_fail;
		}
#	else
		Knoimpl(kiosend,KVos);
#		error missing implementation // boring error for plebs
#	endif

	if (len != null) *len = size;
	return kiocond_ok;
}
