io: Use defines instead of constants - sacc - sacc(omys), simple console gopher client
 (HTM) git clone git://bitreich.org/sacc/ git://enlrupgkhuxnvlhsf6lc3fziv5h2hhfrinws65d7roiv6bfj7d652fid.onion/sacc/
 (DIR) Log
 (DIR) Files
 (DIR) Refs
 (DIR) Tags
 (DIR) LICENSE
       ---
 (DIR) commit 392dcb24732c88f500266b923bafdda8eb631220
 (DIR) parent e8930420e7c31a3cea0484314bd38aba5197fa39
 (HTM) Author: Quentin Rameau <quinq@fifth.space>
       Date:   Tue, 20 Sep 2022 23:30:55 +0200
       
       io: Use defines instead of constants
       
       Diffstat:
         M io.h                                |       5 +++++
         M io_tls.c                            |      39 +++++++++++++++++--------------
         M sacc.c                              |       5 ++---
         M ui_ti.c                             |       4 ++--
       
       4 files changed, 31 insertions(+), 22 deletions(-)
       ---
 (DIR) diff --git a/io.h b/io.h
       @@ -1,5 +1,10 @@
        #include <netdb.h>
        
       +#define CONN_VALID 0
       +#define CONN_ERROR -1
       +#define CONN_RETRY -2
       +#define CONN_ABORT -3
       +
        struct cnx {
        #ifdef USE_TLS
                struct tls *tls;
 (DIR) diff --git a/io_tls.c b/io_tls.c
       @@ -12,6 +12,10 @@
        #include "common.h"
        #include "io.h"
        
       +#define TLS_OFF 0
       +#define TLS_ON  1
       +#define TLS_PEM 2
       +
        int tls;
        
        static int
       @@ -19,7 +23,7 @@ close_tls(struct cnx *c)
        {
                int r;
        
       -        if (tls && c->tls) {
       +        if (tls != TLS_OFF && c->tls) {
                        do {
                                r = tls_close(c->tls);
                        } while (r == TLS_WANT_POLLIN || r == TLS_WANT_POLLOUT);
       @@ -71,9 +75,10 @@ connect_tls(struct cnx *c, struct addrinfo *ai, const char *host)
                if ((t = tls_client()) == NULL)
                        return -1;
        
       +/* XXX: construct path from getuid home path */
                snprintf(pempath, sizeof(pempath), "/home/quinq/share/sacc/%s.pem", host);
                switch (tls) {
       -        case 1:
       +        case TLS_ON:
                        /* check if there is a local certificate for target */
                        if (access(pempath, R_OK) == 0) {
                                if ((tc = tls_config_new()) == NULL)
       @@ -84,7 +89,7 @@ connect_tls(struct cnx *c, struct addrinfo *ai, const char *host)
                                        goto end;
                        }
                        break;
       -        case 2:
       +        case TLS_PEM:
                        /* save target certificate to file */
                        if ((tc = tls_config_new()) == NULL)
                                goto end;
       @@ -103,12 +108,12 @@ connect_tls(struct cnx *c, struct addrinfo *ai, const char *host)
        
                if (r == 0) {
                        switch (tls) {
       -                case 1:
       +                case TLS_ON:
                                c->tls = t;
                                break;
       -                case 2:
       -                        r = savepem(t, pempath) == 0 ? -2 : -1;
       -                        tls = 1;
       +                case TLS_PEM:
       +                        r = savepem(t, pempath) == 0 ? CONN_RETRY : CONN_ERROR;
       +                        tls = TLS_ON;
                                break;
                        }
                } else {
       @@ -119,8 +124,8 @@ connect_tls(struct cnx *c, struct addrinfo *ai, const char *host)
                        switch (*s) {
                        case 'Y':
                        case 'y':
       -                        tls = 2;
       -                        r = -2;
       +                        tls = TLS_PEM;
       +                        r = CONN_RETRY;
                                goto end;
                        }
        
       @@ -129,17 +134,17 @@ connect_tls(struct cnx *c, struct addrinfo *ai, const char *host)
                        case 'Y':
                        case 'y':
                        case '\0':
       -                        tls = 0;
       -                        r = -2;
       +                        tls = TLS_OFF;
       +                        r = CONN_RETRY;
                                break;
                        default:
       -                        r = -3;
       +                        r = CONN_ABORT;
                        }
                }
        end:
                free(s);
                tls_config_free(tc);
       -        if (r != 0)
       +        if (r != CONN_VALID)
                        tls_free(t);
        
                return r;
       @@ -151,7 +156,7 @@ connerr_tls(struct cnx *c, const char *host, const char *port, int err)
                if (c->sock == -1) {
                        diag("Can't open socket: %s", strerror(err));
                } else {
       -                if (tls && c->tls) {
       +                if (tls != TLS_OFF && c->tls) {
                                diag("Can't establish TLS with \"%s\": %s",
                                     host, tls_error(c->tls));
                        } else {
       @@ -170,9 +175,9 @@ parseurl_tls(char *url)
                        if (!strncmp(url, "gopher", p - url)) {
                                if (tls)
                                        diag("Switching from gophers to gopher");
       -                        tls = 0;
       +                        tls = TLS_OFF;
                        } else if (!strncmp(url, "gophers", p - url)) {
       -                        tls = 1;
       +                        tls = TLS_ON;
                        } else {
                                die("Protocol not supported: %.*s", p - url, url);
                        }
       @@ -187,7 +192,7 @@ read_tls(struct cnx *c, void *buf, size_t bs)
        {
                ssize_t n;
        
       -        if (tls && c->tls) {
       +        if (tls != TLS_OFF && c->tls) {
                        do {
                                n = tls_read(c->tls, buf, bs);
                        } while (n == TLS_WANT_POLLIN || n == TLS_WANT_POLLOUT);
 (DIR) diff --git a/sacc.c b/sacc.c
       @@ -581,13 +581,12 @@ connectto(const char *host, const char *port, struct cnx *c)
                                        err = errno;
                                        ioclose(c);
                                }
       -                /* retry */
       -                } while (r == -2);
       +                } while (r == CONN_RETRY);
                }
        
                freeaddrinfo(addrs);
        
       -        if (r == -1)
       +        if (r == CONN_ERROR)
                        ioconnerr(c, host, port, err);
        err:
                sigprocmask(SIG_SETMASK, &oset, NULL);
 (DIR) diff --git a/ui_ti.c b/ui_ti.c
       @@ -501,7 +501,7 @@ uiselectitem(Item *entry)
                                continue;
                        case _key_end:
                        end:
       -                        jumptoline(entry, dir->nitems, 0);
       +                        jumptoline(entry, dir->nitems, 1);
                                continue;
                        case _key_lnup:
                        lnup:
       @@ -516,7 +516,7 @@ uiselectitem(Item *entry)
                                continue;
                        case _key_home:
                        home:
       -                        jumptoline(entry, 0, 0);
       +                        jumptoline(entry, 0, 1);
                                continue;
                        case _key_search:
                                free(searchstr);