Move navigation handling into its own function - sacc - sacc (saccomys): simple gopher client.
Log
Files
Refs
LICENSE
---
commit 2025a3c168bbfe7cb7c0f2cfb6b7d6f88e1403f6
parent a7cbd6c2d6b4c9d207b91532734915745c2a038f
Author: Quentin Rameau 
Date:   Wed, 21 Jun 2017 19:52:00 +0200

Move navigation handling into its own function

Diffstat:
  sacc.c                              |      71 +++++++++++++++++--------------

1 file changed, 38 insertions(+), 33 deletions(-)
---
diff --git a/sacc.c b/sacc.c
@@ -306,6 +306,42 @@ dig(Item *entry, Item *item)
         return 1;
 }
 
+void
+delve(Item *hole)
+{
+        char buf[BUFSIZ];
+        Item *entry = NULL;
+        int n, itm;
+
+        for (;;) {
+                if (dig(entry, hole)) {
+                        n = display(hole);
+                } else {
+                        n = 0;
+                        fprintf(stderr, "Couldn't get %s:%s/%c%s\n",
+                                hole->host, hole->port,
+                                hole->type, hole->selector);
+                }
+                do {
+                        printf("%d items, visit (0: back, ^D or q: quit): ", n);
+                        if (!fgets(buf, sizeof(buf), stdin)) {
+                                putchar('\n');
+                                return;
+                        }
+                        if (!strcmp(buf, "q\n"))
+                                return;
+                        if (sscanf(buf, "%d", &itm) != 1)
+                                continue;
+                } while (itm < 0 || itm > n);
+                if (itm) {
+                        entry = hole;
+                        hole = ((Item **)hole->target)[itm-1];
+                } else if (hole->entry) {
+                        hole = hole->entry;
+                }
+        }
+}
+
 Item *
 parseurl(const char *URL)
 {
@@ -371,45 +407,14 @@ parseurl(const char *URL)
 int
 main(int argc, char *argv[])
 {
-        char buf[BUFSIZ];
-        Item *entry, *hole;
-        int n, itm;
+        Item *hole;
 
         if (argc != 2)
                 usage();
 
-        entry = NULL;
         hole = parseurl(argv[1]);
 
-        for (;;) {
-                if (dig(entry, hole)) {
-                        n = display(hole);
-                } else {
-                        n = 0;
-                        fprintf(stderr, "Couldn't get %s:%s/%c%s\n",
-                                hole->host, hole->port,
-                                hole->type, hole->selector);
-                }
-                do {
-                        printf("%d items, visit (0: back, ^D or q: quit): ", n);
-                        if (!fgets(buf, sizeof(buf), stdin)) {
-                                putchar('\n');
-                                goto quit;
-                        }
-                        if (!strcmp(buf, "q\n"))
-                                goto quit;
-                        if (sscanf(buf, "%d", &itm) != 1)
-                                continue;
-                } while (itm < 0 || itm > n);
-                if (itm) {
-                        entry = hole;
-                        hole = ((Item **)hole->target)[itm-1];
-                } else if (hole->entry) {
-                        hole = hole->entry;
-                }
-        }
-
-quit:
+        delve(hole);
         free(hole); /* TODO free all tree recursively */
 
         return 0;