#include #include #include #include #include /* errors */ void perr(const char *s) { fprintf(stderr, "%s\n", s); } /* misc */ int running; int quit(const char *_) { puts("goodbye!"); running = 0; return 0; } /* documents */ #define TXT_BUF_SIZE (1024 * 1024) struct { char buf[TXT_BUF_SIZE]; size_t n; } text; struct doc { struct doc *prev, *next; struct { size_t ofs, len, idx; } txt; }; #define DOC_MAX 32 struct doc docv[DOC_MAX]; size_t docn = 0, doci = 0; int doc_new(void) { if (doci < docn) { text.n = docv[doci].txt.ofs; docn = doci; } if (docn >= DOC_MAX) return -1; memset(&docv[docn], 0, sizeof(struct doc)); docv[docn].txt.ofs = text.n; docv[docn].txt.idx = text.n; doci = docn++; return 0; } int doc_add(char *buf, size_t n) { if (text.n + n > TXT_BUF_SIZE) { return -1; } memcpy(&text.buf[text.n], buf, n); text.n += n; return 0; } void doc_prev(void) { if (doci > 0) doci--; } void doc_next(void) { if (doci < docn) doci++; } /* pagination */ size_t pg_lines() { return 24; } int pg_down(const char *_) { if (doci >= docn) return -1; size_t lines = pg_lines(); size_t i = docv[doci].txt.idx; size_t end = docv[doci].txt.ofs + docv[doci].txt.len; while (lines > 0 && i < end) { putchar(text.buf[i]); if (text.buf[i] == '\n') lines--; i++; } return 0; } int pg_up(const char *_) { if (doci >= docn) return -1; size_t lines = pg_lines() << 1; size_t i = docv[doci].txt.idx; while (lines > 0 && i > 0) { putchar(text.buf[i]); if (text.buf[i] == '\n') lines--; i--; } pg_down(_); return 0; } /* navigation */ #define HOST_MAX 255 #define PATH_MAX 255 enum doc_type { TYPE_GOPHERDOC, TYPE_GEMTEXT, TYPE_PLAIN, TYPE_UNKNOWN }; enum protocol { PROT_FILE, PROT_GOPHER, PROT_GEMINI, PROT_UNKNOWN, }; struct addr { char host[HOST_MAX]; char path[PATH_MAX]; size_t host_len, path_len; enum protocol prot; enum doc_type type; }; int url_to_addr(const char *url, struct addr *adr) { char *colon = strchr(url, ':'); adr->prot = PROT_UNKNOWN; if (colon) { } return -1; } int nav_to(const char *url) { return 0; } int nav_link_nr(unsigned long link_nr) { return 0; } /* commands */ struct cmd { char ch; int (*fn)(const char *); }; struct cmd cmd_tbl[] = { { 'g', nav_to }, { '\n', pg_down }, { '\b', pg_up }, { 'q', quit } }; /* cmd is mutated when trimming strings */ void cmd_do(char *cmd) { if (isdigit(*cmd)) { errno = 0; unsigned long n = strtoul(cmd, NULL, 10); if (errno) { perr("invalid link number"); } else if (nav_link_nr(n)) { perr("navigation failure"); } } else { for (size_t i = 0; i < sizeof cmd_tbl / sizeof(struct cmd); i++) { if (cmd_tbl[i].ch == *cmd) { while (isspace(*++cmd)); for (int j = strlen(cmd) - 1; j > 0 && isspace(cmd[j]); j--) { cmd[j] = 0; } if (cmd_tbl[i].fn(cmd)) { perr("failed"); } goto found; } } perr("?"); found: ; } } int cmd_get(char *buf, size_t n) { fputs("* ", stdout); return !!fgets(buf, n, stdin); } int main(void) { char cmd_buf[1024]; running = 1; while (running && cmd_get(cmd_buf, sizeof cmd_buf)) { cmd_do(cmd_buf); } return 0; }