Recently Written · git

sacc

Mirror of git://bitreich.org/sacc, the sacc gopher client from bitreich.org.

git clone https://github.com/equwal/sacc

Log | Files | Refs


commit 89d3a0bd06d2345ffa98675e356a17be24a61210
Quentin Rameau <quinq@fifth.space>
2021-11-08 23:21:44 +0100

Factorize URI formatting into sacc.c

 common.h |  2 ++
 sacc.c   | 37 +++++++++++++++++++++++++++++++++++++
 ui_ti.c  | 32 ++------------------------------
 ui_txt.c | 47 ++---------------------------------------------
 4 files changed, 43 insertions(+), 75 deletions(-)
diff --git a/common.h b/common.h
index 7a9e866..78c1a11 100644
--- 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
index 31b123e..43cd26f 100644
--- 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
index 50c5466..87ffc60 100644
--- 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
index b326d38..14258e9 100644
--- 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';