Factorize URI formatting into sacc.c - sacc - sacc(omys), simple console gopher client
git clone git://bitreich.org/sacc/ git://enlrupgkhuxnvlhsf6lc3fziv5h2hhfrinws65d7roiv6bfj7d652fid.onion/sacc/
Log
Files
Refs
Tags
LICENSE
---
commit 89d3a0bd06d2345ffa98675e356a17be24a61210
parent ed632c2c8f6b6c64acda2883a04a2739ebf3cf0f
Author: Quentin Rameau 
Date:   Mon,  8 Nov 2021 23:21:44 +0100

Factorize URI formatting into sacc.c

Diffstat:
  M common.h                            |       2 ++
  M sacc.c                              |      37 +++++++++++++++++++++++++++++++
  M ui_ti.c                             |      32 ++-----------------------------
  M ui_txt.c                            |      47 ++-----------------------------

4 files changed, 43 insertions(+), 75 deletions(-)
---
diff --git a/common.h b/common.h
@@ -45,12 +45,14 @@ struct dir {
 };
 
 extern void (*diag)(char *fmt, ...);
+
 void die(const char *fmt, ...);
 size_t mbsprint(const char *s, size_t len);
 #ifdef NEED_STRCASESTR
 char *strcasestr(const char *h, const char *n);
 #endif /* NEED_STRCASESTR */
 const char *typedisplay(char t);
+int itemuri(Item *, char *, size_t);
 void uicleanup(void);
 void uidisplay(Item *entry);
 char *uiprompt(char *fmt, ...);
diff --git a/sacc.c b/sacc.c
@@ -265,6 +265,43 @@ typedisplay(char t)
         }
 }
 
+int
+itemuri(Item *item, char *buf, size_t bsz)
+{
+        int n;
+
+        switch (item->type) {
+        case '8':
+                n = snprintf(buf, bsz, "telnet://%s@%s:%s",
+                             item->selector, item->host, item->port);
+                break;
+        case 'T':
+                n = snprintf(buf, bsz, "tn3270://%s@%s:%s",
+                             item->selector, item->host, item->port);
+                break;
+        case 'h':
+                n = snprintf(buf, bsz, "%s", item->selector +
+                             (strncmp(item->selector, "URL:", 4) ? 0 : 4));
+                break;
+        default:
+                n = snprintf(buf, bsz, "gopher://%s", item->host);
+
+                if (n < bsz-1 && strcmp(item->port, "70"))
+                        n += snprintf(buf+n, bsz-n, ":%s", item->port);
+                if (n < bsz-1) {
+                        n += snprintf(buf+n, bsz-n, "/%c%s",
+                                      item->type, item->selector);
+                }
+                if (n < bsz-1 && item->type == '7' && item->tag) {
+                        n += snprintf(buf+n, bsz-n, "%%09%s",
+                                      item->tag + strlen(item->selector));
+                }
+                break;
+        }
+
+        return n;
+}
+
 static void
 printdir(Item *item)
 {
diff --git a/ui_ti.c b/ui_ti.c
@@ -219,36 +219,8 @@ displayuri(Item *item)
 
         putp(tparm(cursor_address, lines-1, 0, 0, 0, 0, 0, 0, 0, 0));
         putp(tparm(enter_standout_mode, 0, 0, 0, 0, 0, 0, 0, 0, 0));
-        switch (item->type) {
-        case '8':
-                n = snprintf(bufout, sizeof(bufout), "telnet://%s@%s:%s",
-                             item->selector, item->host, item->port);
-                break;
-        case 'h':
-                n = snprintf(bufout, sizeof(bufout), "%s",
-                             item->selector);
-                break;
-        case 'T':
-                n = snprintf(bufout, sizeof(bufout), "tn3270://%s@%s:%s",
-                             item->selector, item->host, item->port);
-                break;
-        default:
-                n = snprintf(bufout, sizeof(bufout), "gopher://%s", item->host);
-
-                if (n < sizeof(bufout) && strcmp(item->port, "70")) {
-                        n += snprintf(bufout+n, sizeof(bufout)-n, ":%s",
-                                      item->port);
-                }
-                if (n < sizeof(bufout)) {
-                        n += snprintf(bufout+n, sizeof(bufout)-n, "/%c%s",
-                                      item->type, item->selector);
-                }
-                if (n < sizeof(bufout) && item->type == '7' && item->tag) {
-                        n += snprintf(bufout+n, sizeof(bufout)-n, "%%09%s",
-                                      item->tag + strlen(item->selector));
-                }
-                break;
-        }
+
+        itemuri(item, bufout, sizeof(bufout));
 
         if (n >= sizeof(bufout))
                 bufout[sizeof(bufout)-1] = '\0';
diff --git a/ui_txt.c b/ui_txt.c
@@ -175,53 +175,10 @@ printuri(Item *item, size_t i)
 {
         int n;
 
-        if (!item)
+        if (!item || item->type == 0 || item->type == 'i')
                 return;
 
-        switch (item->type) {
-        case 0:
-                return;
-        case '8':
-                n = snprintf(bufout, sizeof(bufout), "telnet://%s@%s:%s",
-                             item->selector, item->host, item->port);
-                break;
-        case 'i':
-                n = snprintf(bufout, sizeof(bufout), "%zu: %s",
-                             i, item->username);
-                break;
-        case 'h':
-                n = snprintf(bufout, sizeof(bufout), "%zu: %s: %s",
-                         i, item->username, item->selector);
-                break;
-        case 'T':
-                n = snprintf(bufout, sizeof(bufout), "tn3270://%s@%s:%s",
-                             item->selector, item->host, item->port);
-                break;
-        default:
-                n = snprintf(bufout, sizeof(bufout), "%zu: ", i);
-
-                if (n < sizeof(bufout) && *item->username) {
-                        n += snprintf(bufout+n, sizeof(bufout)-n, "%s: ",
-                                      item->username);
-                }
-                if (n < sizeof(bufout)) {
-                        n += snprintf(bufout+n, sizeof(bufout)-n, "gopher://%s",
-                                      item->host);
-                }
-                if (n < sizeof(bufout) && strcmp(item->port, "70")) {
-                        n += snprintf(bufout+n, sizeof(bufout)-n, ":%s",
-                                      item->port);
-                }
-                if (n < sizeof(bufout)) {
-                        n += snprintf(bufout+n, sizeof(bufout)-n, "/%c%s",
-                                      item->type, item->selector);
-                }
-                if (n < sizeof(bufout) && item->type == '7' && item->tag) {
-                        n += snprintf(bufout+n, sizeof(bufout)-n, "%%09%s",
-                                      item->tag + strlen(item->selector));
-                }
-                break;
-        }
+        n = itemuri(item, bufout, sizeof(bufout));
 
         if (n >= sizeof(bufout))
                 bufout[sizeof(bufout)-1] = '\0';