diff options
-rw-r--r-- | doc.h | 8 | ||||
-rw-r--r-- | main.c | 6 | ||||
-rw-r--r-- | nav.c | 5 | ||||
-rw-r--r-- | nav.h | 1 | ||||
-rw-r--r-- | net.c | 59 | ||||
-rw-r--r-- | net.h | 19 | ||||
-rw-r--r-- | parse.h | 1 |
7 files changed, 64 insertions, 35 deletions
diff --git a/doc.h b/doc.h index fccd676..496b631 100644 --- a/doc.h +++ b/doc.h @@ -2,7 +2,6 @@ #define DOC_H #include "buf.h" -#include "net.h" #define DOC_LINK_NONE 0xffff @@ -11,6 +10,13 @@ struct doc_line { char txt[]; }; +enum doc_type { + DOC_UNKNOWN, + DOC_GOPHERDOC, + DOC_GEMTEXT, + DOC_PLAIN, +}; + struct doc { buf_t txt, lnk; size_t latest; diff --git a/main.c b/main.c index 738a3e9..2ac6d21 100644 --- a/main.c +++ b/main.c @@ -79,10 +79,14 @@ int cmd_get(char *buf, size_t n) { return !!fgets(buf, n, stdin); } -int main(void) { +int main(int argc, const char **argv) { struct nav_state ns; char cmd_buf[1024]; nav_init(&ns); + if (argc == 2) { + if (nav_to(&ns, argv[1])) perr("navigation failure"); + } + nav_redraw(&ns); while (cmd_get(cmd_buf, sizeof cmd_buf)) { cmd_trim(cmd_buf, sizeof cmd_buf); if (cmd_do(cmd_buf, &ns)) { diff --git a/nav.c b/nav.c index 59fdca6..8f1d699 100644 --- a/nav.c +++ b/nav.c @@ -98,17 +98,18 @@ void nav_redraw(struct nav_state *ns) { /* network */ int nav_to(struct nav_state *ns, const char *url) { + enum doc_type doct; struct addr adr; buf_t buf; if (net_addr(url, &adr, ns->prot_default)) { return -1; } ns->prot_default = adr.prot; - if (net_fetch(&adr, &buf)) { + if (net_fetch(&adr, &buf, &doct)) { return -1; } struct doc d; - if (parse_doc(adr.type, &d, &buf)) { + if (parse_doc(doct, &d, &buf)) { buf_free(&buf); return -1; } diff --git a/nav.h b/nav.h index 69f666a..0afcfaf 100644 --- a/nav.h +++ b/nav.h @@ -2,6 +2,7 @@ #define NAV_H #include "doc.h" +#include "net.h" #define HIST_MAX 32 struct nav_state { diff --git a/net.c b/net.c index b826e96..9a21fb2 100644 --- a/net.c +++ b/net.c @@ -3,6 +3,7 @@ #include "net.h" #include "err.h" +#include "doc.h" int net_addr(const char *url, struct addr *adr, enum protocol prot_default) { char *prot_mark; @@ -29,38 +30,62 @@ int net_addr(const char *url, struct addr *adr, enum protocol prot_default) { } if (adr->prot == PROT_FILE) { - adr->type = DOC_PLAIN; adr->host_len = 0; size_t n = strlen(url); if (n >= PATH_MAX) return -1; adr->path_len = n; memcpy(adr->path, url, n + 1); } else { + adr->host_len = 0; + while (*url && *url != '/' && adr->host_len < HOST_MAX) { + adr->host[adr->host_len++] = *url++; + } + adr->host[adr->host_len] = 0; + if (*url && *url != '/') { + perr("hostname too long"); + return -1; + } + if (*url) url++; + adr->path_len = 0; + while (*url && adr->path_len < HOST_MAX) { + adr->path[adr->path_len++] = *url++; + } + adr->path[adr->path_len] = 0; + if (*url) { + perr("path too long"); + return -1; + } + puts(adr->host); + puts(adr->path); return -1; } return 0; } -int net_fetch(const struct addr *adr, struct buf *buf) { +static int file_fetch(const struct addr *adr, struct buf *buf, enum doc_type *doct) { + FILE *f = fopen(adr->path, "r/o"); + if (!f) { + perr("file not found"); + return -1; + } + buf_init(buf, 1024); + char b[256]; + while (fgets(b, sizeof b, f)) { + buf_cat(buf, b, strlen(b)); + } + buf_catc(buf, 0); + fclose(f); + *doct = DOC_PLAIN; + return 0; +} + +int net_fetch(const struct addr *adr, struct buf *buf, enum doc_type *doct) { switch (adr->prot) { case PROT_FILE: - { - FILE *f = fopen(adr->path, "r/o"); - if (!f) { - perr("file not found"); - return -1; - } - buf_init(buf, 1024); - char b[256]; - while (fgets(b, sizeof b, f)) { - buf_cat(buf, b, strlen(b)); - } - buf_catc(buf, 0); - fclose(f); - } - return 0; + return file_fetch(adr, buf, doct); default: + perr("unsupported protocol"); return -1; } } diff --git a/net.h b/net.h index c3e12ef..64048be 100644 --- a/net.h +++ b/net.h @@ -2,9 +2,10 @@ #define NET_H #include "buf.h" +#include "doc.h" -#define HOST_MAX 256 -#define PATH_MAX 256 +#define HOST_MAX 255 +#define PATH_MAX 255 enum protocol { PROT_UNKNOWN, @@ -13,22 +14,14 @@ enum protocol { PROT_GEMINI, }; -enum doc_type { - DOC_UNKNOWN, - DOC_GOPHERDOC, - DOC_GEMTEXT, - DOC_PLAIN, -}; - struct addr { - char host[HOST_MAX]; - char path[PATH_MAX]; + char host[HOST_MAX + 1]; + char path[PATH_MAX + 1]; size_t host_len, path_len; enum protocol prot; - enum doc_type type; }; int net_addr(const char *url, struct addr *adr, enum protocol prot_default); -int net_fetch(const struct addr *adr, struct buf *buf); +int net_fetch(const struct addr *adr, struct buf *buf, enum doc_type *doct); #endif diff --git a/parse.h b/parse.h index 4f19285..460c275 100644 --- a/parse.h +++ b/parse.h @@ -1,7 +1,6 @@ #ifndef PARSE_H #define PARSE_H -#include "net.h" #include "doc.h" #include "buf.h" |