<commit_before>#include "common.h"
#include "ctl.h"
#include "str.h"

#include <stdio.h>
#include <sys/socket.h>
#include <unistd.h>

#include "../argz/argz.h"

static int
gt_path_status(int fd)
{
    struct ctl_msg req = {
        .type = CTL_PATH_STATUS,
    }, res = {0};

    if (send(fd, &req, sizeof(struct ctl_msg), 0) == -1)
        return -1;

    int term = isatty(1);

    do {
        if (recv(fd, &res, sizeof(struct ctl_msg), 0) == -1)
            return -1;

        if (res.type != req.type)
            return -2;

        if (!res.ret)
            return 0;

        char bindstr[INET6_ADDRSTRLEN];
        char publstr[INET6_ADDRSTRLEN];
        char peerstr[INET6_ADDRSTRLEN];

        gt_toaddr(bindstr, sizeof(bindstr),
                  (struct sockaddr *)&res.path_status.local_addr);
        gt_toaddr(publstr, sizeof(publstr),
                  (struct sockaddr *)&res.path_status.r_addr);
        gt_toaddr(peerstr, sizeof(peerstr),
                  (struct sockaddr *)&res.path_status.addr);

        const char *statestr = NULL;

        switch (res.path_status.state) {
            case MUD_UP:     statestr = "UP";     break;
            case MUD_BACKUP: statestr = "BACKUP"; break;
            case MUD_DOWN:   statestr = "DOWN";   break;
            default:         return -2;
        }

        const char *statusstr = "DEGRADED";

        if (res.path_status.ok)
            statusstr = "OK";

        printf(term ? "path %s\n"
                      "  status:   %s\n"
                      "  bind:     %s port %"PRIu16"\n"
                      "  public:   %s port %"PRIu16"\n"
                      "  peer:     %s port %"PRIu16"\n"
                      "  mtu:      %zu bytes\n"
                      "  rtt:      %.3f ms\n"
                      "  rttvar:   %.3f ms\n"
                      "  upload:   %"PRIu64" bytes/s (max: %"PRIu64")\n"
                      "  download: %"PRIu64" bytes/s (max: %"PRIu64")\n"
                      "  output:   %"PRIu64" packets\n"
                      "  input:    %"PRIu64" packets\n"
                    : "path %s %s"
                      " %s %"PRIu16
                      " %s %"PRIu16
                      " %s %"PRIu16
                      " %zu"
                      " %.3f %.3f"
                      " %"PRIu64
                      " %"PRIu64
                      " %"PRIu64
                      " %"PRIu64
                      " %"PRIu64
                      " %"PRIu64
                      "\n",
            statestr,
            statusstr,
            bindstr[0] ? bindstr : "-",<commit_msg>Code cleanup<commit_after>#include "common.h"
#include "ctl.h"
#include "str.h"

#include <stdio.h>
#include <sys/socket.h>
#include <unistd.h>

#include "../argz/argz.h"

static int
gt_path_status(int fd)
{
    struct ctl_msg req = {
        .type = CTL_PATH_STATUS,
    }, res = {0};

    if (send(fd, &req, sizeof(struct ctl_msg), 0) == -1)
        return -1;

    int term = isatty(1);

    do {
        if (recv(fd, &res, sizeof(struct ctl_msg), 0) == -1)
            return -1;

        if (res.type != req.type)
            return -2;

        if (!res.ret)
            return 0;

        char bindstr[INET6_ADDRSTRLEN];
        char publstr[INET6_ADDRSTRLEN];
        char peerstr[INET6_ADDRSTRLEN];

        gt_toaddr(bindstr, sizeof(bindstr),
                  (struct sockaddr *)&res.path_status.local_addr);
        gt_toaddr(publstr, sizeof(publstr),
                  (struct sockaddr *)&res.path_status.r_addr);
        gt_toaddr(peerstr, sizeof(peerstr),
                  (struct sockaddr *)&res.path_status.addr);

        const char *statestr = NULL;

        switch (res.path_status.state) {
            case MUD_UP:     statestr = "UP";     break;
            case MUD_BACKUP: statestr = "BACKUP"; break;
            case MUD_DOWN:   statestr = "DOWN";   break;
            default:         return -2;
        }

        const char *statusstr = res.path_status.ok ? "OK" : "DEGRADED";

        printf(term ? "path %s\n"
                      "  status:   %s\n"
                      "  bind:     %s port %"PRIu16"\n"
                      "  public:   %s port %"PRIu16"\n"
                      "  peer:     %s port %"PRIu16"\n"
                      "  mtu:      %zu bytes\n"
                      "  rtt:      %.3f ms\n"
                      "  rttvar:   %.3f ms\n"
                      "  upload:   %"PRIu64" bytes/s (max: %"PRIu64")\n"
                      "  download: %"PRIu64" bytes/s (max: %"PRIu64")\n"
                      "  output:   %"PRIu64" packets\n"
                      "  input:    %"PRIu64" packets\n"
                    : "path %s %s"
                      " %s %"PRIu16
                      " %s %"PRIu16
                      " %s %"PRIu16
                      " %zu"
                      " %.3f %.3f"
                      " %"PRIu64
                      " %"PRIu64
                      " %"PRIu64
                      " %"PRIu64
                      " %"PRIu64
                      " %"PRIu64
                      "\n",
            statestr,
            statusstr,
            bindstr[0] ? bindstr : "-",
{'fraction_non_alphanumeric': 0.1316298877274487, 'fraction_numerical': 0.02129307007355788, 'mean_word_length': 1.1422056384742951, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4277377', 'n_tokens_mistral': 1789, 'n_tokens_neox': 1705, 'n_words': 347}