<commit_before>#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <sys/queue.h>
#include <stddef.h>
#include <fcntl.h>
#include <ev.h>
#include "xfer.h"
#include "setup.h"

struct emulator {

    int ser_fd;    /* represent serial port */

    /* represent controlling tty */
    int ifd;     /* read from this fd */
    int ofd;     /* write to this fd */

    ev_io ser_w;   /* serial port watcher */
    ev_io tty_w;   /* controling tty watcher */

    SLIST_ENTRY(emulator) node;
};


static SLIST_HEAD(,emulator) em_head;
static struct ev_loop *loop;

#define container_of(ptr, type, member) ({                      \
        const typeof( ((type *)0)->member ) *__mptr = (ptr);    \
        (type *)( (char *)__mptr - offsetof(type,member) );})

/* read  from serial port and then write to controlling tty */
static void
ser_read (EV_P_ struct ev_io *w, int revents)
{
    int len;
    char buf[1024];
    struct emulator *em = container_of(w, struct emulator, ser_w);

    len = read(em->ser_fd, buf, sizeof buf);
    if( len <= 0) {
        perror(ttyname(em->ser_fd));
        ev_io_stop(EV_A_ &em->ser_w);
        ev_io_stop(EV_A_ &em->tty_w);
    }
    write(em->ofd, buf, len);
}

/* read from controlling tty and then write to serial port */<commit_msg>xfer: silent when serial device is removed<commit_after>#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <sys/queue.h>
#include <stddef.h>
#include <fcntl.h>
#include <ev.h>
#include "xfer.h"
#include "setup.h"

struct emulator {

    int ser_fd;    /* represent serial port */

    /* represent controlling tty */
    int ifd;     /* read from this fd */
    int ofd;     /* write to this fd */

    ev_io ser_w;   /* serial port watcher */
    ev_io tty_w;   /* controling tty watcher */

    SLIST_ENTRY(emulator) node;
};


static SLIST_HEAD(,emulator) em_head;
static struct ev_loop *loop;

#define container_of(ptr, type, member) ({                      \
        const typeof( ((type *)0)->member ) *__mptr = (ptr);    \
        (type *)( (char *)__mptr - offsetof(type,member) );})

/* read  from serial port and then write to controlling tty */
static void
ser_read (EV_P_ struct ev_io *w, int revents)
{
    int len;
    char buf[1024];
    struct emulator *em = container_of(w, struct emulator, ser_w);

    len = read(em->ser_fd, buf, sizeof buf);
    if( len <= 0) {
        ev_io_stop(EV_A_ &em->ser_w);
        ev_io_stop(EV_A_ &em->tty_w);
    }
    write(em->ofd, buf, len);
}

/* read from controlling tty and then write to serial port */
{'fraction_non_alphanumeric': 0.1415168100078186, 'fraction_numerical': 0.004691164972634871, 'mean_word_length': 2.746705710102489, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 28, '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': '6216875', 'n_tokens_mistral': 943, 'n_tokens_neox': 916, 'n_words': 263}