logo

badwolf

minimalist and privacy-oriented web browser based on WebKitGTK git clone https://hacktivis.me/git/badwolf.git

man_uri_scheme.c (1219B)


  1. // SPDX-FileCopyrightText: 2019 Badwolf Authors <https://hacktivis.me/projects/badwolf>
  2. // SPDX-License-Identifier: BSD-3-Clause
  3. #include "badwolf.h"
  4. #include "config.h"
  5. void
  6. man_uri_scheme_request_cb(WebKitURISchemeRequest *request, gpointer user_data)
  7. {
  8. const char *req_path = webkit_uri_scheme_request_get_path(request);
  9. if(req_path == NULL || req_path[0] == 0) req_path = "man";
  10. char *path = strdup(req_path);
  11. char *man_ref[2] = {path, NULL};
  12. char *sep = strrchr(path, '.');
  13. if(sep)
  14. {
  15. sep[0] = 0;
  16. man_ref[0] = sep + 1;
  17. man_ref[1] = path;
  18. }
  19. GSubprocessFlags flags_subproc = G_SUBPROCESS_FLAGS_STDOUT_PIPE | G_SUBPROCESS_FLAGS_STDERR_MERGE;
  20. GError *error_proc = NULL;
  21. GSubprocess *man_proc = g_subprocess_new(
  22. flags_subproc, &error_proc, BADWOLF_MAN_HTML_CMD, man_ref[0], man_ref[1], NULL);
  23. if(error_proc != NULL)
  24. {
  25. webkit_uri_scheme_request_finish_error(request, error_proc);
  26. free(path);
  27. return;
  28. }
  29. GInputStream *stream = g_subprocess_get_stdout_pipe(man_proc);
  30. // Pipe the contents out, can't wait beforehand as the buffer needs to drain on large manpages
  31. webkit_uri_scheme_request_finish(request, stream, -1, "text/html");
  32. free(path);
  33. g_object_unref(stream);
  34. }