/* * 2017 by kabel@blackhole.sk * gcc -O2 -o highlight highlight.c */ #include #include #include #include #include #include #define EXECVE_EMPTY_ARGS ((char * const []) {NULL}) #define EXECVE_ARGS(...) ((char * const []) {__VA_ARGS__, NULL}) int main (int argc, char ** argv) { char *req_path, *qmark, *local_path, *ext; pid_t child; req_path = getenv("REQUEST_URI"); if (req_path == NULL || strncmp(req_path, "/~kabel/", 8)) return 1; qmark = strchr(req_path, '?'); if (qmark != NULL) *qmark = '\0'; local_path = malloc(22 + strlen(req_path)); sprintf(local_path, "/home/kabel/public_html/%s", req_path + 8); /* get rid of "/~kabel" */ ext = strrchr(local_path, '.'); if (ext == NULL) return 1; ++ext; printf("Content-type: text/html; charset=UTF-8\n\n" "\n" "\n" "\n" "\n" "Highlighted source code of %s\n" "\n" "\n" "\n" "
\n" "\n" "
\n" "plain\n" "| download\n" "
\n" "
\n" "
\n", req_path, req_path, req_path);

	fflush(stdout);

	child = fork();
	if (child == 0) {
		execve("/home/kabel/gentoo/usr/bin/highlight",
			EXECVE_ARGS("highlight", "-t", "8", "-Iflai",
				    local_path, "--force", "-S", ext),
			EXECVE_EMPTY_ARGS);

		return 1;
	} else if (child > 0) {
		waitpid(child, NULL, 0);
	} else {
		printf("highlighting failed");
	}

	printf("
\n" "\n" "\n"); fflush(stdout); return 0; }