Skip to content

Commit

Permalink
read gophermap until EOF
Browse files Browse the repository at this point in the history
  • Loading branch information
dimkr committed Apr 13, 2023
1 parent 8ab7140 commit d048660
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 16 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,8 @@ additional documentation and more details are available in `man gplaces`. type `
## Statistic
| File | Blank | Comment | Code |
| --------- | ------ | ------ | ---- |
| gplaces.c | 273 | 63 | 1232 |
| gopher.c | 29 | 18 | 72 |
| gplaces.c | 272 | 63 | 1233 |
| gopher.c | 30 | 18 | 74 |
| spartan.c | 21 | 16 | 59 |
| gophers.c | 12 | 16 | 25 |
| tcp.c | 11 | 16 | 18 |
9 changes: 6 additions & 3 deletions gopher.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,18 @@ static int build_selector_url(Selector *sel, const char type, const char *host,
}


static void parse_gophermap_line(char *line, int start, int *pre, Selector **sel, SelectorList *list) {
static int parse_gophermap_line(char *line, int start, int *pre, Selector **sel, SelectorList *list) {
char *path, *host, *port;

(void)start;
(void)pre;

if (line[0] == '.' && line[1] == '\0') return 1;

*sel = NULL;

line[strcspn(line, "\r\n")] = '\0';
if (line[0] == '\0' || line[1] == '\0') return;
if (line[0] == '\0' || line[1] == '\0') return 0;

*sel = new_selector('`');

Expand All @@ -66,11 +68,12 @@ static void parse_gophermap_line(char *line, int start, int *pre, Selector **sel

insert:
SIMPLEQ_INSERT_TAIL(list, *sel, next);
return;
return 0;

fail:
free_selector(*sel);
*sel = NULL;
return 0;
}


Expand Down
21 changes: 10 additions & 11 deletions gplaces.c
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@
/*============================================================================*/
typedef struct Selector Selector;
typedef SIMPLEQ_HEAD(, Selector) SelectorList;
typedef void (*Parser)(char *, int start, int *pre, Selector **, SelectorList *);
typedef int (*Parser)(char *, int start, int *pre, Selector **, SelectorList *);

typedef struct Protocol {
const char *scheme, *port;
Expand Down Expand Up @@ -359,18 +359,19 @@ static int tcp_connect(Selector *sel) {
}


static void parse_plaintext_line(char *line, int start, int *pre, Selector **sel, SelectorList *list) {
static int parse_plaintext_line(char *line, int start, int *pre, Selector **sel, SelectorList *list) {
(void)start;
(void)pre;

line[strcspn(line, "\r\n")] = '\0';
*sel = new_selector('`');
(*sel)->repr = str_copy(line);
SIMPLEQ_INSERT_TAIL(list, *sel, next);
return 0;
}


static void parse_gemtext_line(char *line, int start, int *pre, Selector **sel, SelectorList *list) {
static int parse_gemtext_line(char *line, int start, int *pre, Selector **sel, SelectorList *list) {
char *url;
int level;

Expand All @@ -380,7 +381,7 @@ static void parse_gemtext_line(char *line, int start, int *pre, Selector **sel,

if (strncmp(line, "```", 3) == 0) {
*pre = !*pre;
return;
return 0;
}

line[strcspn(line, "\r\n")] = '\0';
Expand All @@ -400,7 +401,7 @@ static void parse_gemtext_line(char *line, int start, int *pre, Selector **sel,
*line = '\0';
line += 1 + strspn(line + 1, " \t");
}
if (!copy_url(*sel, url)) { free_selector(*sel); *sel = NULL; return; }
if (!copy_url(*sel, url)) { free_selector(*sel); *sel = NULL; return 0; }
if (*line) (*sel)->repr = str_copy(line);
else (*sel)->repr = str_copy(url);
} else if (line[0] == '#' && (level = 1 + strspn(&line[1], "#")) <= 3) {
Expand All @@ -416,6 +417,7 @@ static void parse_gemtext_line(char *line, int start, int *pre, Selector **sel,
}

SIMPLEQ_INSERT_TAIL(list, *sel, next);
return 0;
}


Expand All @@ -426,10 +428,7 @@ static SelectorList parse_file(FILE *fp, const Parser parser) {
Selector *sel;
int pre = 0, start = 1;

for (sel = NULL; (line = fgets(buffer, sizeof(buffer), fp)) != NULL; sel = NULL, start = 0) {
parser(line, start, &pre, &sel, &list);
}

for (sel = NULL; (line = fgets(buffer, sizeof(buffer), fp)) != NULL && !parser(line, start, &pre, &sel, &list); sel = NULL, start = 0);
return list;
}

Expand Down Expand Up @@ -1105,7 +1104,7 @@ static SelectorList download_text(Selector *sel, int ask, int handle, int print)
end = &buffer[sizeof(buffer) - 1]; /* if the buffer is full and we haven't found a \n, terminate the line */
}
*end = '\0';
parser(start, parsed == 0, &pre, &it, &list);
if ((ok = parser(start, parsed == 0, &pre, &it, &list))) goto out;
if (print && it) print_line(stdout, it, NULL, width, &links);
}
length -= parsed;
Expand All @@ -1118,7 +1117,7 @@ static SelectorList download_text(Selector *sel, int ask, int handle, int print)
if (prog > 0) fputc('\n', stderr);
if (!(ok = (received == 0 || (received < 0 && !sel->proto->error(sel, c, received))))) goto out;
if (length > 0) {
parser(buffer, parsed == 0, &pre, &it, &list);
ok = parser(buffer, parsed == 0, &pre, &it, &list);
if (print && it) print_line(stdout, it, NULL, width, &links);
}

Expand Down

0 comments on commit d048660

Please sign in to comment.