Mercurial
comparison 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 |
comparison
equal
deleted
inserted
replaced
| 107:1423044e3d58 | 108:f07abbcd2ec5 |
|---|---|
| 1 #include <stdio.h> | 1 #include <stdio.h> |
| 2 #include <stdlib.h> | 2 #include <stdlib.h> |
| 3 | 3 |
| 4 typedef struct { | 4 #define REPO_ROOT "/Users/mrjunejune/zenbu" |
| 5 float foo; | |
| 6 float bar; | |
| 7 } Values; | |
| 8 | 5 |
| 9 int capacity = 20; | 6 #include <stdint.h> |
| 7 #include <arpa/inet.h> | |
| 8 | |
| 9 void run_hg_command(FILE *hg_pipe) { | |
| 10 char channel; | |
| 11 uint32_t net_len; | |
| 12 | |
| 13 while (fread(&channel, 1, 1, hg_pipe) == 1) { | |
| 14 // 1. Read the 4-byte length | |
| 15 fread(&net_len, 4, 1, hg_pipe); | |
| 16 uint32_t length = ntohl(net_len); | |
| 17 | |
| 18 // 2. If it's an 'o' (output), it's the data you want | |
| 19 if (channel == 'o' || channel == 'e') { | |
| 20 char *buf = malloc(length + 1); | |
| 21 fread(buf, 1, length, hg_pipe); | |
| 22 buf[length] = '\0'; | |
| 23 printf("%s", buf); // This is your capability string | |
| 24 free(buf); | |
| 25 } | |
| 26 // 3. IF THE CHANNEL IS 'r', THE COMMAND IS DONE. STOP HERE. | |
| 27 else if (channel == 'r') { | |
| 28 int32_t result; | |
| 29 // The 'r' channel payload is the 4-byte return code | |
| 30 fread(&result, 4, 1, hg_pipe); | |
| 31 printf("\nCommand finished with result: %d\n", ntohl(result)); | |
| 32 break; // <--- THIS IS YOUR EXIT | |
| 33 } | |
| 34 } | |
| 35 } | |
| 10 | 36 |
| 11 int main() | 37 int main() |
| 12 { | 38 { |
| 13 int a; | 39 char command[512]; |
| 14 void *p = malloc(sizeof(Values) + (sizeof(int) * 20)); | 40 snprintf(command, sizeof(command), "hg -R %s serve --stdio", REPO_ROOT); |
| 15 int *b = (int *)(p + sizeof(Values)); | 41 printf("command: %s\n", command); |
| 16 Values *c = (Values *)p; | 42 |
| 17 c->foo = 10.0f; | 43 FILE *hg_pipe = popen(command, "r+"); |
| 18 c->bar = 12.0f; | 44 if (!hg_pipe) |
| 19 for (int i = 0; i < 20; i++) | 45 { |
| 20 b[i] = i; | 46 printf("Failed to open pipe\n"); |
| 47 return -1; | |
| 48 } | |
| 21 | 49 |
| 22 for (int i = 0; i < 20; i++) | 50 fprintf(hg_pipe, "capabilities\n"); |
| 23 printf("values is %i\n", b[i]); | 51 fflush(hg_pipe); |
| 24 | 52 |
| 25 printf("c->foo: %f\n", c->foo); | 53 run_hg_command(hg_pipe); |
| 26 printf("c->bar: %f\n", c->bar); | 54 // char *output = malloc(sizeof(char) * 2048); |
| 55 // char *curr = output; | |
| 56 // int c; | |
| 57 // int number_of_breakline = 0; | |
| 58 // while ((c = fgetc(hg_pipe)) != NULL) | |
| 59 // { | |
| 60 // *curr++ = c; | |
| 61 // printf("output: %s\n", output); | |
| 62 // if (c == '\n') | |
| 63 // number_of_breakline++; | |
| 64 // if (number_of_breakline == 2) | |
| 65 // break; | |
| 66 // printf("char: %c\n", c); | |
| 67 // } | |
| 68 pclose(hg_pipe); | |
| 27 | 69 |
| 28 Values *d = (Values *)b - 1; | |
| 29 printf("d->foo: %f\n", d->foo); | |
| 30 printf("d->bar: %f\n", d->bar); | |
| 31 | 70 |
| 32 return 0; | 71 return 0; |
| 33 } | 72 } |
| 73 | |
| 74 |