Repository: valkey-io/valkey
File: src/cli_common.c (C)

Code before:
        case '\n': s = sdscatlen(s, "\\n", 2); break;
        case '\f': s = sdscatlen(s, "\\f", 2); break;
        case '\r': s = sdscatlen(s, "\\r", 2); break;
        case '\t': s = sdscatlen(s, "\\t", 2); break;
        case '\b': s = sdscatlen(s, "\\b", 2); break;
        default: s = sdscatprintf(s, *(unsigned char *)p <= 0x1f ? "\\u%04x" : "%c", *p);
        }
        p++;
    }
    return sdscatlen(s, "\"", 1);
}

sds cliVersion(void) {
    sds version = sdscatprintf(sdsempty(), "%s", VALKEY_VERSION);

    /* Add git commit and working tree status when available. */
    if (strtoll(serverGitSHA1(), NULL, 16)) {
        version = sdscatprintf(version, " (git:%s", serverGitSHA1());
        if (strtoll(serverGitDirty(), NULL, 10)) version = sdscatprintf(version, "-dirty");
        version = sdscat(version, ")");
    }
    return version;
}

/* This is a wrapper to call valkeyConnect or valkeyConnectWithTimeout. */
valkeyContext *valkeyConnectWrapper(const char *ip, int port, const struct timeval tv, int nonblock, int rdma) {
    valkeyOptions options = {0};
    if (rdma) {
#ifdef USE_RDMA
        VALKEY_OPTIONS_SET_RDMA(&options, ip, port);
#endif
    } else {
        VALKEY_OPTIONS_SET_TCP(&options, ip, port);
    }

    if (tv.tv_sec || tv.tv_usec) {
        options.connect_timeout = &tv;
    }

    if (nonblock) {
        options.options |= VALKEY_OPT_NONBLOCK;
    }

    return valkeyConnectWithOptions(&options);
}

/* This is a wrapper to call valkeyConnectUnix or valkeyConnectUnixWithTimeout. */
valkeyContext *valkeyConnectUnixWrapper(const char *path, const struct timeval tv, int nonblock) {
    valkeyOptions options = {0};
    VALKEY_OPTIONS_SET_UNIX(&options, path);


Review comment (suggestion):
For Unix domain sockets, there is a special function to connect: valkeyConnectUnixWrapper. It's not nice structure that two kinds of connection use one function and unix sockets use another.

Let's do one of these alternatives:

1. Merge valkeyConnectUnixWrapper with valkeyConnectWrapper and let it take a `type` argument that is TCP, UNIX or RDMA. (We can use ip = path and port = 0 for unix sockets.)
2. Make all three separate functions: valkeyConnectWrapper, valkeyConnectUnixWrapper, valkeyConnectRdmaWrapper. The valkeyConnectRdmaWrapper should not exist if compiled without RDMA.

Code after:
        case '\n': s = sdscatlen(s, "\\n", 2); break;
        case '\f': s = sdscatlen(s, "\\f", 2); break;
        case '\r': s = sdscatlen(s, "\\r", 2); break;
        case '\t': s = sdscatlen(s, "\\t", 2); break;
        case '\b': s = sdscatlen(s, "\\b", 2); break;
        default: s = sdscatprintf(s, *(unsigned char *)p <= 0x1f ? "\\u%04x" : "%c", *p);
        }
        p++;
    }
    return sdscatlen(s, "\"", 1);
}

sds cliVersion(void) {
    sds version = sdscatprintf(sdsempty(), "%s", VALKEY_VERSION);

    /* Add git commit and working tree status when available. */
    if (strtoll(serverGitSHA1(), NULL, 16)) {
        version = sdscatprintf(version, " (git:%s", serverGitSHA1());
        if (strtoll(serverGitDirty(), NULL, 10)) version = sdscatprintf(version, "-dirty");
        version = sdscat(version, ")");
    }
    return version;
}

/* This is a wrapper to call valkeyConnect or valkeyConnectWithTimeout. */
valkeyContext *valkeyConnectWrapper(enum valkeyConnectionType ct, const char *ip_or_path, int port, const struct timeval tv, int nonblock) {
    valkeyOptions options = {0};

    switch (ct) {
    case VALKEY_CONN_TCP:
        VALKEY_OPTIONS_SET_TCP(&options, ip_or_path, port);
        break;

    case VALKEY_CONN_UNIX:
        VALKEY_OPTIONS_SET_UNIX(&options, ip_or_path);
        break;

    case VALKEY_CONN_RDMA:
#ifdef USE_RDMA
        VALKEY_OPTIONS_SET_RDMA(&options, ip_or_path, port);
        break;
#else
        assert(0); /* requesting RDMA connection without RDMA support??? */
#endif

    default:
        assert(0); /* this should not happen */
    }

    if (tv.tv_sec || tv.tv_usec) {
        options.connect_timeout = &tv;