<commit_before>#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/un.h>
#include <fcntl.h>
#include <stdio.h>

#include "activity.h"
#include "watch.h"
#include "command.h"

static int Command_sock = -1;
static color_t Command_color;

static void command_color_set(color_t s)
{
	color_t p = {}, m = {};
	unsigned c;
	for_color (c)
		if (s[c] > Command_color[c])
			p[c] = s[c] - Command_color[c];
		else
			m[c] = Command_color[c] - s[c];
	base_rm(m);
	base_add(p);
	color_cpy(Command_color, s);
}

static void command_sequence(struct segment *seq, unsigned n)
{
	unsigned i;
	struct activity_then *a;
	a = calloc(n, sizeof(*a));
	if (!a)
	{
		fprintf(stderr, "command_sequence(%u): %m\n", n);
		return;
	}
	a[0].act.seg = seq[n-1];
	a[0].act.fun = activity_free;
	for (i = 1; i < n; i ++)
	{
		a[i].act.seg = seq[n-i-1];
		a[i].act.fun = &activity_then;
		a[i].then = &a[i-1].act;
	}
	activity_add(&a[n-1].act);
}

static void command_run(struct watch *w, uint8_t events)
{
	union {
		char buf[256];
		enum command cmd;
		struct command_color cmd_color;
		struct command_sequence cmd_seq;
	} cmdbuf;

	ssize_t z = recv(Command_sock, cmdbuf.buf, sizeof(cmdbuf), 0);
	if (z < 0)
	{
		fprintf(stderr, "command recv: %m\n");
		return;
	}
#define CHECK_LEN(T) \
	if (z < sizeof(cmdbuf.T)) \
	{ \
		fprintf(stderr, "command len: %zd for %s\n", z, #T); \
		return; \
	}
	CHECK_LEN(cmd);
	unsigned c;
	color_t ct = {};
	if (z >= sizeof(cmdbuf.cmd_color))
		color_cpy(ct, cmdbuf.cmd_color.color);
	switch (cmdbuf.cmd)
	{
		case COMMAND_NULL:
			break;

		case COMMAND_COLOR_SET:
			command_color_set(ct);
			break;

		case COMMAND_COLOR_ADD:
			CHECK_LEN(cmd_color);
			for_color (c)
				ct[c] = Command_color[c] + ct[c];
			command_color_set(ct);
			break;

		case COMMAND_COLOR_SUB:
			CHECK_LEN(cmd_color);
			for_color (c)
				ct[c] = Command_color[c] - ct[c];
			command_color_set(ct);
			break;

		case COMMAND_SEQUENCE:
			command_sequence(cmdbuf.cmd_seq.seq, (z - sizeof(cmdbuf.cmd_seq))/sizeof(*cmdbuf.cmd_seq.seq));
			break;
	}
#undef CHECK_LEN
}

static struct watch Command_watch = { .events = WATCH(IN), .fun = &command_run };<commit_msg>fix command_sequence length bug<commit_after>#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/un.h>
#include <fcntl.h>
#include <stdio.h>

#include "activity.h"
#include "watch.h"
#include "command.h"

static int Command_sock = -1;
static color_t Command_color;

static void command_color_set(color_t s)
{
	color_t p = {}, m = {};
	unsigned c;
	for_color (c)
		if (s[c] > Command_color[c])
			p[c] = s[c] - Command_color[c];
		else
			m[c] = Command_color[c] - s[c];
	base_rm(m);
	base_add(p);
	color_cpy(Command_color, s);
}

static void command_sequence(struct segment *seq, unsigned n)
{
	unsigned i;
	struct activity_then *a;
	a = calloc(n, sizeof(*a));
	if (!a)
	{
		fprintf(stderr, "command_sequence(%u): %m\n", n);
		return;
	}
	a[0].act.seg = seq[n-1];
	a[0].act.fun = activity_free;
	for (i = 1; i < n; i ++)
	{
		a[i].act.seg = seq[n-i-1];
		a[i].act.fun = &activity_then;
		a[i].then = &a[i-1].act;
	}
	activity_add(&a[n-1].act);
}

static void command_run(struct watch *w, uint8_t events)
{
	union {
		char buf[256];
		enum command cmd;
		struct command_color cmd_color;
		struct command_sequence cmd_seq;
	} cmdbuf;

	ssize_t z = recv(Command_sock, cmdbuf.buf, sizeof(cmdbuf), 0);
	if (z < 0)
	{
		fprintf(stderr, "command recv: %m\n");
		return;
	}
#define CHECK_LEN(T) \
	if (z < sizeof(cmdbuf.T)) \
	{ \
		fprintf(stderr, "command len: %zd for %s\n", z, #T); \
		return; \
	}
	CHECK_LEN(cmd);
	unsigned c;
	color_t ct = {};
	if (z >= sizeof(cmdbuf.cmd_color))
		color_cpy(ct, cmdbuf.cmd_color.color);
	switch (cmdbuf.cmd)
	{
		case COMMAND_NULL:
			break;

		case COMMAND_COLOR_SET:
			command_color_set(ct);
			break;

		case COMMAND_COLOR_ADD:
			CHECK_LEN(cmd_color);
			for_color (c)
				ct[c] = Command_color[c] + ct[c];
			command_color_set(ct);
			break;

		case COMMAND_COLOR_SUB:
			CHECK_LEN(cmd_color);
			for_color (c)
				ct[c] = Command_color[c] - ct[c];
			command_color_set(ct);
			break;

		case COMMAND_SEQUENCE:
			command_sequence(cmdbuf.cmd_seq.seq, (z - offsetof(struct command_sequence, seq))/sizeof(*cmdbuf.cmd_seq.seq));
			break;
	}
#undef CHECK_LEN
}

static struct watch Command_watch = { .events = WATCH(IN), .fun = &command_run };
{'fraction_non_alphanumeric': 0.16793551276309898, 'fraction_numerical': 0.006269592476489028, 'mean_word_length': 3.9250275633958105, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 23, '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': '438185', 'n_tokens_mistral': 2053, 'n_tokens_neox': 1911, 'n_words': 397}