comparison playground/main.c @ 109:1c446ab6f945

[MrJuneJune] New Blog about writing Seobeo library.
author June Park <parkjune1995@gmail.com>
date Sat, 03 Jan 2026 19:37:51 -0800
parents f07abbcd2ec5
children e7899c93da77
comparison
equal deleted inserted replaced
108:f07abbcd2ec5 109:1c446ab6f945
1 #include <stdio.h> 1 #include <stdio.h>
2 #include <stdlib.h> 2 #include <stdlib.h>
3 3
4 #define REPO_ROOT "/Users/mrjunejune/zenbu" 4 #define REPO_ROOT "/Users/mrjunejune/zenbu"
5
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 }
36 5
37 int main() 6 int main()
38 { 7 {
39 char command[512]; 8 char command[512];
40 snprintf(command, sizeof(command), "hg -R %s serve --stdio", REPO_ROOT); 9 snprintf(command, sizeof(command), "hg -R %s serve --stdio", REPO_ROOT);
48 } 17 }
49 18
50 fprintf(hg_pipe, "capabilities\n"); 19 fprintf(hg_pipe, "capabilities\n");
51 fflush(hg_pipe); 20 fflush(hg_pipe);
52 21
53 run_hg_command(hg_pipe); 22 char *output = malloc(sizeof(char) * 2048);
54 // char *output = malloc(sizeof(char) * 2048); 23 char *curr = output;
55 // char *curr = output; 24 int c;
56 // int c; 25 int number_of_breakline = 0;
57 // int number_of_breakline = 0; 26 while ((c = fgetc(hg_pipe)) != NULL)
58 // while ((c = fgetc(hg_pipe)) != NULL) 27 {
59 // { 28 *curr++ = c;
60 // *curr++ = c; 29 printf("output: %s\n", output);
61 // printf("output: %s\n", output); 30 if (c == '\n')
62 // if (c == '\n') 31 number_of_breakline++;
63 // number_of_breakline++; 32 if (number_of_breakline == 2)
64 // if (number_of_breakline == 2) 33 break;
65 // break; 34 printf("char: %c\n", c);
66 // printf("char: %c\n", c); 35 }
67 // }
68 pclose(hg_pipe); 36 pclose(hg_pipe);
69 37
70 38
71 return 0; 39 return 0;
72 } 40 }