~callum/gmsfn

638dceec3d80628c9cbf954fae7880e3eaae6a4b — Callum Brown 1 year, 3 months ago bbcd4b8
Allow relative paths for output files
1 files changed, 20 insertions(+), 5 deletions(-)

M src/gemlogger.c
M src/gemlogger.c => src/gemlogger.c +20 -5
@@ 109,9 109,9 @@ token_is_good_link(struct gemini_token tok)
}


/* Return url up to and including the last '/' */
/* Return url or file path up to and including the last '/' */
char *
get_url_without_filename(const char *url)
strip_filename(const char *url)
{
	size_t last_slash;
	for (size_t i = 0; i < strlen(url); ++i) {


@@ 216,21 216,36 @@ main(int argc, char *argv[])
	struct gemini_parser config_parser;
	gemini_parser_init(&config_parser, config_resp.bio);
	struct gemini_token config_tok;

	while (gemini_parser_next(&config_parser, &config_tok) == 0) {
		if (config_tok.token == GEMINI_HEADING) {
			if (config_tok.heading.level == 3) {
				/* Change output file */
				char *path = trim_ws(config_tok.heading.title);
				FILE *new_out_fp = fopen(path, "w+");
				char *out_path;
				/* If path is not absolute then prepend the path to the
					directory the config file is in */
				if (path[0] != '/') {
					char *config_dir = strip_filename(argv[1]);
					out_path = malloc(strlen(config_dir) + strlen(path) + 1);
					strcpy(out_path, config_dir);
					strcat(out_path, path);
				} else {
					out_path = path;
				}
				FILE *new_out_fp = fopen(out_path, "w+");
				if (!new_out_fp) {
					printf("Bad output file: %s\n", config_tok.heading.title);
					printf("Bad output file: %s\n", out_path);
					free(out_path);
					/* Skip links until a new output file is given */
					good_output_file = false;
					continue;
				}
				free(out_path);
				out_fp = new_out_fp;
				good_output_file = true;
			}

		} else if (good_output_file && token_is_good_link(config_tok)) {
			/* Make requests and output links */
			/* Get gemlog index page */


@@ 242,7 257,7 @@ main(int argc, char *argv[])
				continue;
			}

			char *base_url = get_url_without_filename(config_tok.link.url);
			char *base_url = strip_filename(config_tok.link.url);
			char *abs_url;

			/* Parse gemlog index page */