diff playground/main.c @ 108:f07abbcd2ec5

[HgWeb] Will probably hold off on using it since it is not urgent.
author June Park <parkjune1995@gmail.com>
date Sat, 03 Jan 2026 17:29:12 -0800
parents 75de5903355c
children 1c446ab6f945
line wrap: on
line diff
--- a/playground/main.c	Sat Jan 03 10:48:11 2026 -0800
+++ b/playground/main.c	Sat Jan 03 17:29:12 2026 -0800
@@ -1,33 +1,74 @@
 #include <stdio.h>
 #include <stdlib.h>
 
-typedef struct {
-  float foo;
-  float bar;
-} Values;
+#define REPO_ROOT "/Users/mrjunejune/zenbu"
+
+#include <stdint.h>
+#include <arpa/inet.h>
+
+void run_hg_command(FILE *hg_pipe) {
+    char channel;
+    uint32_t net_len;
+    
+    while (fread(&channel, 1, 1, hg_pipe) == 1) {
+        // 1. Read the 4-byte length
+        fread(&net_len, 4, 1, hg_pipe);
+        uint32_t length = ntohl(net_len);
 
-int capacity = 20;
+        // 2. If it's an 'o' (output), it's the data you want
+        if (channel == 'o' || channel == 'e') {
+            char *buf = malloc(length + 1);
+            fread(buf, 1, length, hg_pipe);
+            buf[length] = '\0';
+            printf("%s", buf); // This is your capability string
+            free(buf);
+        } 
+        // 3. IF THE CHANNEL IS 'r', THE COMMAND IS DONE. STOP HERE.
+        else if (channel == 'r') {
+            int32_t result;
+            // The 'r' channel payload is the 4-byte return code
+            fread(&result, 4, 1, hg_pipe); 
+            printf("\nCommand finished with result: %d\n", ntohl(result));
+            break; // <--- THIS IS YOUR EXIT
+        }
+    }
+}
 
 int main()
 {
-  int a;
-  void *p = malloc(sizeof(Values) + (sizeof(int) * 20));
-  int *b = (int *)(p + sizeof(Values));
-  Values *c = (Values *)p;
-  c->foo = 10.0f;
-  c->bar = 12.0f;
-  for (int i = 0; i < 20; i++)
-    b[i] = i;
+  char command[512];
+  snprintf(command, sizeof(command), "hg -R %s serve --stdio", REPO_ROOT);
+  printf("command: %s\n", command);
+  
+  FILE *hg_pipe = popen(command, "r+");
+  if (!hg_pipe)
+  {
+    printf("Failed to open pipe\n");
+    return -1;
+  }
+
+  fprintf(hg_pipe, "capabilities\n");
+  fflush(hg_pipe);
 
-  for (int i = 0; i < 20; i++)
-    printf("values is %i\n", b[i]);
+  run_hg_command(hg_pipe);
+  // char *output = malloc(sizeof(char) * 2048);
+  // char *curr = output;
+  // int c;
+  // int number_of_breakline = 0;
+  // while ((c = fgetc(hg_pipe))  != NULL)
+  // {
+  //   *curr++ = c;
+  //   printf("output: %s\n", output);
+  //   if (c == '\n')
+  //     number_of_breakline++;
+  //   if (number_of_breakline == 2)
+  //     break;
+  //   printf("char: %c\n", c);
+  // }
+  pclose(hg_pipe);
 
-  printf("c->foo: %f\n", c->foo);
-  printf("c->bar: %f\n", c->bar);
-
-  Values *d = (Values *)b - 1;
-  printf("d->foo: %f\n", d->foo);
-  printf("d->bar: %f\n", d->bar);
 
   return 0;
 }
+
+