<commit_before>/* Copyright (C) 2009 Dovecot authors, see the included COPYING file */

#include "common.h"
#include "llist.h"
#include "istream.h"
#include "ostream.h"
#include "master-service.h"
#include "master-interface.h"
#include "connect-limit.h"
#include "anvil-connection.h"

#include <stdlib.h>
#include <unistd.h>

#define MAX_INBUF_SIZE 1024

#define ANVIL_CLIENT_PROTOCOL_MAJOR_VERSION 1
#define ANVIL_CLIENT_PROTOCOL_MINOR_VERSION 0

struct anvil_connection {
	struct anvil_connection *prev, *next;

	int fd;
	struct istream *input;
	struct ostream *output;
	struct io *io;
	unsigned char *fifo_inbuf;
	size_t fifo_inbuf_size;

	unsigned int version_received:1;
	unsigned int handshaked:1;
	unsigned int master:1;
};

struct anvil_connection *anvil_connections = NULL;

static const char *
anvil_connection_fifo_read_line(struct anvil_connection *conn)
{
	ssize_t ret;

	ret = read(conn->fd, conn->fifo_inbuf, conn->fifo_inbuf_size);
	if (ret > 0) {
		if (conn->fifo_inbuf[ret-1] != '\n') {
			i_error("BUG: Client packet didn't end with LF");
			return NULL;
		}
		conn->fifo_inbuf[ret-1] = '\0';
		return (const char *)conn->fifo_inbuf;
	}
	if (ret == 0) {
		/* disconnected */
	} else {<commit_msg>anvil: Fixed reading from FIFOs.<commit_after>/* Copyright (C) 2009 Dovecot authors, see the included COPYING file */

#include "common.h"
#include "llist.h"
#include "istream.h"
#include "ostream.h"
#include "master-service.h"
#include "master-interface.h"
#include "connect-limit.h"
#include "anvil-connection.h"

#include <stdlib.h>
#include <unistd.h>

#define MAX_INBUF_SIZE 1024

#define ANVIL_CLIENT_PROTOCOL_MAJOR_VERSION 1
#define ANVIL_CLIENT_PROTOCOL_MINOR_VERSION 0

struct anvil_connection {
	struct anvil_connection *prev, *next;

	int fd;
	struct istream *input;
	struct ostream *output;
	struct io *io;
	unsigned char *fifo_inbuf;
	size_t fifo_inbuf_size, fifo_inbuf_next;

	unsigned int version_received:1;
	unsigned int handshaked:1;
	unsigned int master:1;
};

struct anvil_connection *anvil_connections = NULL;

static const char *
anvil_connection_get_next_line(struct anvil_connection *conn)
{
	char *line, *p;

	line = (char *)conn->fifo_inbuf + conn->fifo_inbuf_next;

	p = strchr(line, '\n');
	if (p == NULL)
		conn->fifo_inbuf_next = 0;
	else {
		*p = '\0';
		conn->fifo_inbuf_next = p - line + 1;
	}
	return line;
}

static const char *
anvil_connection_fifo_read_line(struct anvil_connection *conn)
{
	ssize_t ret;

	if (conn->fifo_inbuf_next > 0)
		return anvil_connection_get_next_line(conn);

	ret = read(conn->fd, conn->fifo_inbuf, conn->fifo_inbuf_size);
	if (ret > 0) {
		if (conn->fifo_inbuf[ret-1] != '\n') {
			i_error("BUG: Client packet didn't end with LF");
			return NULL;
		}
		conn->fifo_inbuf[ret-1] = '\0';
		return anvil_connection_get_next_line(conn);
	}

	if (ret == 0) {
		/* disconnected */
	} else {
{'fraction_non_alphanumeric': 0.12879581151832462, 'fraction_numerical': 0.013961605584642234, 'mean_word_length': 4.885010266940451, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 26, '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': '1436480', 'n_tokens_mistral': 1180, 'n_tokens_neox': 1112, 'n_words': 264}