<commit_before>#include <fudge.h>
#include "call.h"
#include "file.h"
#include "channel.h"

static void dispatch(struct channel *channel, struct message_header *header, struct message_data *data)
{

    if (header->type < EVENTS)
    {

        if (channel->signals[header->type].callback)
            channel->signals[header->type].callback(channel, header->source, data->buffer, message_datasize(header));

        if (channel->signals[EVENT_ANY].callback)
            channel->signals[EVENT_ANY].callback(channel, header->source, data->buffer, message_datasize(header));

    }

}

static unsigned int poll(struct channel *channel, struct message_header *header, struct message_data *data)
{

    while (channel->poll)
    {

        if (call_pick(header, data))
            return header->type;

    }

    return 0;

}

unsigned int channel_place(struct channel *channel, unsigned int id, unsigned int type, unsigned int count, void *data)
{

    struct message_header header;

    message_initheader(&header, type, count);

    if (type < EVENTS)
    {

        if (channel->signals[type].redirect)
            id = channel->signals[type].redirect;

        return call_place(id, &header, data);

    }

    return 0;

}

unsigned int channel_poll(struct channel *channel, unsigned int source, unsigned int type, struct message_header *header, struct message_data *data)
{

    while (poll(channel, header, data))
    {

        if (header->source == source && header->type == type)
            return header->type;

        dispatch(channel, header, data);

    }

    return 0;

}

unsigned int channel_polltype(struct channel *channel, unsigned int type, struct message_header *header, struct message_data *data)
{

    while (poll(channel, header, data))
    {

        if (header->type == type)
            return header->type;

        dispatch(channel, header, data);

    }

    return 0;

}

unsigned int channel_pollsource(struct channel *channel, unsigned int source, struct message_header *header, struct message_data *data)
{

    while (poll(channel, header, data))
    {

        if (header->source == source)
            return header->type;

        dispatch(channel, header, data);

    }

    return 0;

}

void channel_listen(struct channel *channel, void (*oninit)(struct channel *channel), void (*onexit)(struct channel *channel))
{

    struct message_header header;
    struct message_data data;

    if (oninit)
        oninit(channel);

    channel->poll = 1;

    while (poll(channel, &header, &data))
        dispatch(channel, &header, &data);

    if (onexit)
        onexit(channel);

}

void channel_close(struct channel *channel)
{

    channel->poll = 0;

    if (channel->signals[EVENT_CLOSE].redirect)
        channel_place(channel, channel->signals[EVENT_CLOSE].redirect, EVENT_CLOSE, 0, 0);

}

void channel_setredirect(struct channel *channel, unsigned int type, unsigned int mode, unsigned int id, unsigned int source)
{

    switch (mode)
    {

    case 1:
        channel->signals[type].redirect = id;

        break;

    case 2:
        channel->signals[type].redirect = source;

        break;

    default:
        channel->signals[type].redirect = 0;

        break;

    }

}

static void onmain(struct channel *channel, unsigned int source, void *mdata, unsigned int msize)
{

    channel_close(channel);

}

static void onredirect(struct channel *channel, unsigned int source, void *mdata, unsigned int msize)
{

    struct event_redirect *redirect = mdata;

    channel_setredirect(channel, redirect->type, redirect->mode, redirect->id, source);

}

void channel_setsignal(struct channel *channel, unsigned int type, void (*callback)(struct channel *channel, unsigned int source, void *mdata, unsigned int msize))
{

    channel->signals[type].callback = callback;

}

void channel_init(struct channel *channel)
{

    unsigned int i;

    channel->poll = 0;

    for (i = 0; i < EVENTS; i++)
    {

        channel_setsignal(channel, i, 0);
        channel_setredirect(channel, i, 0, 0, 0);

    }

    channel_setsignal(channel, EVENT_MAIN, onmain);
    channel_setsignal(channel, EVENT_REDIRECT, onredirect);

}<commit_msg>more<commit_after>#include <fudge.h>
#include "call.h"
#include "file.h"
#include "channel.h"

static void dispatch(struct channel *channel, struct message_header *header, struct message_data *data)
{

    if (header->type < EVENTS)
    {

        if (channel->signals[header->type].callback)
            channel->signals[header->type].callback(channel, header->source, data->buffer, message_datasize(header));

        if (channel->signals[EVENT_ANY].callback)
            channel->signals[EVENT_ANY].callback(channel, header->source, data->buffer, message_datasize(header));

    }

}

static unsigned int poll(struct channel *channel, struct message_header *header, struct message_data *data)
{

    while (channel->poll)
    {

        if (call_pick(header, data))
            return header->type;

    }

    return 0;

}

static void onmain(struct channel *channel, unsigned int source, void *mdata, unsigned int msize)
{

    channel_close(channel);

}

static void onredirect(struct channel *channel, unsigned int source, void *mdata, unsigned int msize)
{

    struct event_redirect *redirect = mdata;

    channel_setredirect(channel, redirect->type, redirect->mode, redirect->id, source);

}

unsigned int channel_place(struct channel *channel, unsigned int id, unsigned int type, unsigned int count, void *data)
{

    struct message_header header;

    message_initheader(&header, type, count);

    if (type < EVENTS)
    {

        if (channel->signals[type].redirect)
            id = channel->signals[type].redirect;

        return call_place(id, &header, data);

    }

    return 0;

}

unsigned int channel_poll(struct channel *channel, unsigned int source, unsigned int type, struct message_header *header, struct message_data *data)
{

    while (poll(channel, header, data))
    {

        if (header->source == source && header->type == type)
            return header->type;

        dispatch(channel, header, data);

    }

    return 0;

}

unsigned int channel_polltype(struct channel *channel, unsigned int type, struct message_header *header, struct message_data *data)
{

    while (poll(channel, header, data))
    {

        if (header->type == type)
            return header->type;

        dispatch(channel, header, data);

    }

    return 0;

}

unsigned int channel_pollsource(struct channel *channel, unsigned int source, struct message_header *header, struct message_data *data)
{

    while (poll(channel, header, data))
    {

        if (header->source == source)
            return header->type;

        dispatch(channel, header, data);

    }

    return 0;

}

void channel_listen(struct channel *channel, void (*oninit)(struct channel *channel), void (*onexit)(struct channel *channel))
{

    struct message_header header;
    struct message_data data;

    if (oninit)
        oninit(channel);

    channel->poll = 1;

    while (poll(channel, &header, &data))
        dispatch(channel, &header, &data);

    if (onexit)
        onexit(channel);

}

void channel_close(struct channel *channel)
{

    channel->poll = 0;

    if (channel->signals[EVENT_CLOSE].redirect)
        channel_place(channel, channel->signals[EVENT_CLOSE].redirect, EVENT_CLOSE, 0, 0);

}

void channel_setredirect(struct channel *channel, unsigned int type, unsigned int mode, unsigned int id, unsigned int source)
{

    switch (mode)
    {

    case 1:
        channel->signals[type].redirect = id;

        break;

    case 2:
        channel->signals[type].redirect = source;

        break;

    default:
        channel->signals[type].redirect = 0;

        break;

    }

}

void channel_setsignal(struct channel *channel, unsigned int type, void (*callback)(struct channel *channel, unsigned int source, void *mdata, unsigned int msize))
{

    channel->signals[type].callback = callback;

}

void channel_init(struct channel *channel)
{

    unsigned int i;

    channel->poll = 0;

    for (i = 0; i < EVENTS; i++)
    {

        channel_setsignal(channel, i, 0);
        channel_setredirect(channel, i, 0, 0, 0);

    }

    channel_setsignal(channel, EVENT_MAIN, onmain);
    channel_setsignal(channel, EVENT_REDIRECT, onredirect);

}
{'fraction_non_alphanumeric': 0.11511307885604882, 'fraction_numerical': 0.004307765944717004, 'mean_word_length': 3.1727408886669997, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 73, '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': '574969', 'n_tokens_mistral': 2561, 'n_tokens_neox': 2411, 'n_words': 686}