main.c 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /* browser-host: run the NebOS browser as a Linux/Wayland program.
  2. *
  3. * Usage: browser-host [--size WxH] [--root DIR] [--headless]
  4. * [--wait MS] [--shot FILE] [URL|FILE]
  5. */
  6. #include <signal.h>
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <string.h>
  10. #include "host.h"
  11. extern void browser_start(const char *arg);
  12. static void usage(void)
  13. {
  14. fprintf(stderr,
  15. "usage: browser-host [--size WxH] [--root DIR] [--headless]\n"
  16. " [--wait MS] [--shot|--screenshot FILE]\n"
  17. " [URL|FILE]\n");
  18. }
  19. static int parse_size(const char *s, int *w, int *h)
  20. {
  21. char *end;
  22. *w = (int)strtol(s, &end, 10);
  23. if (end == s || (*end != 'x' && *end != 'X'))
  24. return -1;
  25. s = end + 1;
  26. *h = (int)strtol(s, &end, 10);
  27. if (end == s || *end)
  28. return -1;
  29. return (*w > 0 && *h > 0) ? 0 : -1;
  30. }
  31. int main(int argc, char **argv)
  32. {
  33. char argbuf[4096];
  34. unsigned int arglen = 0;
  35. const char *root = 0;
  36. int i;
  37. memset(&host_opts, 0, sizeof(host_opts));
  38. host_opts.wait_ms = 4000;
  39. argbuf[0] = '\0';
  40. for (i = 1; i < argc; i++) {
  41. const char *a = argv[i];
  42. if (!strcmp(a, "--size") && i + 1 < argc) {
  43. if (parse_size(argv[++i], &host_opts.width,
  44. &host_opts.height) != 0) {
  45. usage();
  46. return 1;
  47. }
  48. } else if (!strcmp(a, "--root") && i + 1 < argc) {
  49. root = argv[++i];
  50. } else if (!strcmp(a, "--headless")) {
  51. host_opts.headless = 1;
  52. } else if (!strcmp(a, "--wait") && i + 1 < argc) {
  53. host_opts.wait_ms = (unsigned int)strtoul(argv[++i],
  54. 0, 10);
  55. } else if (!strcmp(a, "--shot") && i + 1 < argc) {
  56. host_opts.shot = argv[++i];
  57. } else if (!strcmp(a, "--screenshot") && i + 1 < argc) {
  58. host_opts.shot = argv[++i];
  59. } else if (!strcmp(a, "--help") || !strcmp(a, "-h")) {
  60. usage();
  61. return 0;
  62. } else {
  63. size_t n = strlen(a);
  64. if (arglen && arglen + 1 < sizeof(argbuf))
  65. argbuf[arglen++] = ' ';
  66. if (arglen + n >= sizeof(argbuf))
  67. n = sizeof(argbuf) - arglen - 1;
  68. memcpy(argbuf + arglen, a, n);
  69. arglen += (unsigned int)n;
  70. argbuf[arglen] = '\0';
  71. }
  72. }
  73. if (host_opts.headless && !host_opts.shot)
  74. host_opts.shot = "/tmp/opencode/browser.ppm";
  75. if (host_runtime_init(argv[0], root) != 0)
  76. return 1;
  77. signal(SIGPIPE, SIG_IGN);
  78. browser_start(argbuf[0] ? argbuf : 0);
  79. return 0;
  80. }