<commit_before>/**
 * @file
 * @brief Pseudo TTY master-side and slave driver.
 *
 * @date 25.03.13
 * @author <NAME>
 */

#include <drivers/pty.h>

#include <string.h>

#include <kernel/thread/sched.h>
#include <kernel/thread/event.h>
#include <kernel/work.h>

static void pty_out_wake(struct tty *t) {
	struct pty *pt = pty_from_tty(t);

	event_notify(&pt->read_event);
}

size_t pty_read(struct pty *pt, char *buff, size_t size) {
	struct tty *t = pty_to_tty(pt);
	size_t saved_size = size;
	size_t block_size;
	int rc;

	for (;;) {
		work_disable(&t->rx_work);
		while ((block_size = ring_can_read(&t->o_ring, TTY_IO_BUFF_SZ, size))) {
			const char *block = t->o_buff + t->o_ring.tail;

			work_enable(&t->rx_work);

			memcpy(buff, block, block_size);
			buff += block_size;
			size -= block_size;

			work_disable(&t->rx_work);

			ring_just_read(&t->o_ring, TTY_IO_BUFF_SZ, block_size);
		}
		work_enable(&t->rx_work);

		if (!size)
			break;

		rc = event_wait_ms(&pt->read_event, SCHED_TIMEOUT_INFINITE);
		if (rc)
			break;
	}

	return saved_size - size;
}

size_t pty_write(struct pty *pt, const char *buff, size_t size) {
	return 0;
}

const struct tty_ops pty_ops = {<commit_msg>pty: dirty implementation of pty_write (master) using tty_rx_putc<commit_after>/**
 * @file
 * @brief Pseudo TTY master-side and slave driver.
 *
 * @date 25.03.13
 * @author <NAME>
 */

#include <drivers/pty.h>

#include <string.h>

#include <kernel/thread/sched.h>
#include <kernel/thread/event.h>
#include <kernel/work.h>

static void pty_out_wake(struct tty *t) {
	struct pty *pt = pty_from_tty(t);

	event_notify(&pt->read_event);
}

size_t pty_read(struct pty *pt, char *buff, size_t size) {
	struct tty *t = pty_to_tty(pt);
	const char *saved_buff = buff;
	size_t block_size;
	int rc;

	for (;;) {
		work_disable(&t->rx_work);
		while ((block_size = ring_can_read(&t->o_ring, TTY_IO_BUFF_SZ, size))) {
			const char *block = t->o_buff + t->o_ring.tail;

			work_enable(&t->rx_work);

			memcpy(buff, block, block_size);
			buff += block_size;
			size -= block_size;

			work_disable(&t->rx_work);

			ring_just_read(&t->o_ring, TTY_IO_BUFF_SZ, block_size);
		}
		work_enable(&t->rx_work);

		if (!size)
			break;

		rc = event_wait_ms(&pt->read_event, SCHED_TIMEOUT_INFINITE);
		if (rc)
			break;
	}

	return buff - saved_buff;
}

size_t pty_write(struct pty *pt, const char *buff, size_t size) {
	struct tty *t = pty_to_tty(pt);
	const char *saved_buff = buff;

	while (size--)
		tty_rx_putc(t, *buff++, 0);

	return buff - saved_buff;
}

const struct tty_ops pty_ops = {
{'fraction_non_alphanumeric': 0.15124416796267495, 'fraction_numerical': 0.005443234836702955, 'mean_word_length': 4.08498023715415, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 35, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7210200', 'n_tokens_mistral': 1156, 'n_tokens_neox': 1073, 'n_words': 230}