summaryrefslogtreecommitdiff
path: root/src/lsp/lsp_main.c
diff options
context:
space:
mode:
authorZuhaitz Méndez Fernández de Aránguiz <zuhaitz@debian>2026-01-11 15:11:00 +0000
committerZuhaitz Méndez Fernández de Aránguiz <zuhaitz@debian>2026-01-11 15:11:00 +0000
commit55247a3f12a9eee7ba3fd7ca6d8fcea7a82c20f3 (patch)
treea2a71e2eb8ca0b2c483518c1902d89d18709c9ab /src/lsp/lsp_main.c
parent2e7abed7cfe84a2c0df371cde35f8f68cfdca16c (diff)
Added src/ folder. Now I will add the rest.
Diffstat (limited to 'src/lsp/lsp_main.c')
-rw-r--r--src/lsp/lsp_main.c60
1 files changed, 60 insertions, 0 deletions
diff --git a/src/lsp/lsp_main.c b/src/lsp/lsp_main.c
new file mode 100644
index 0000000..9a3489b
--- /dev/null
+++ b/src/lsp/lsp_main.c
@@ -0,0 +1,60 @@
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+#include "json_rpc.h"
+
+// Simple Main Loop for LSP.
+int lsp_main(int argc, char **argv)
+{
+ (void)argc;
+ (void)argv;
+ fprintf(stderr, "zls: Zen Language Server starting...\n");
+
+ while (1)
+ {
+ // Read headers
+ int content_len = 0;
+ char line[512];
+ while (fgets(line, sizeof(line), stdin))
+ {
+ if (0 == strcmp(line, "\r\n"))
+ {
+ break; // End of headers
+ }
+ if (0 == strncmp(line, "Content-Length: ", 16))
+ {
+ content_len = atoi(line + 16);
+ }
+ }
+
+ if (content_len <= 0)
+ {
+ // Maybe EOF or error?
+ if (feof(stdin))
+ {
+ break;
+ }
+ continue; // Wait for more (yeah we gotta work on this).
+ }
+
+ // Read body.
+ char *body = malloc(content_len + 1);
+ if (fread(body, 1, content_len, stdin) != (size_t)content_len)
+ {
+ fprintf(stderr, "zls: Error reading body\n");
+ free(body);
+ break;
+ }
+ body[content_len] = 0;
+
+ // Process JSON-RPC.
+ fprintf(stderr, "zls: Received: %s\n", body);
+ handle_request(body);
+
+ free(body);
+ }
+
+ return 0;
+}