ascii-chat
Binary v0.16.114-28-gf5913657f
Library lib/v0.3.2-3302-gf5913657f
Video chat in your terminal
Loading...
Searching...
No Matches
stdio.c
Go to the documentation of this file.
1#include "mcp.h"
2#include <ctoon.h>
3#include <stdio.h>
4#include <stdlib.h>
5#include <string.h>
6
7static int protocol_fd = -1;
9 protocol_fd = platform_dup(STDOUT_FILENO);
10 if (protocol_fd < 0)
11 return SET_ERRNO(ERROR_FILE_OPERATION, "Cannot reserve MCP stdout");
12 return platform_dup2(STDERR_FILENO, STDOUT_FILENO);
13}
15 if (protocol_fd >= 0) {
16 platform_close(protocol_fd);
17 protocol_fd = -1;
18 }
19}
20char *mcp_response_encode_toon(yyjson_mut_doc *response, size_t *length) {
21 size_t json_length = 0;
22 char *json = yyjson_mut_write(response, 0, &json_length);
23 if (!json)
24 return NULL;
25 ctoon_doc *doc = ctoon_read_json_const(json, json_length, CTOON_READ_NOFLAG);
26 free(json);
27 if (!doc)
28 return NULL;
29 char *toon = ctoon_write(doc, length);
30 ctoon_doc_free(doc);
31 return toon;
32}
33
34void mcp_stdio_send(mcp_server_t *s, yyjson_mut_doc *response) {
35 size_t size = 0;
36 char *toon = mcp_response_encode_toon(response, &size);
37 if (!toon)
38 return;
39 mutex_lock(&s->output_mutex);
40 bool ok = platform_write_all(protocol_fd, toon, size) == size &&
41 platform_write_all(protocol_fd, "\n", 1) == 1;
42 mutex_unlock(&s->output_mutex);
43 free(toon);
44 if (!ok)
45 atomic_store_bool(&s->stopping, true);
46}
48 char *line = SAFE_MALLOC(MCP_MAX_INPUT + 1, char *);
49 size_t n = 0;
50 bool overflow = false;
51 while (!atomic_load_bool(&s->stopping)) {
52 int c = fgetc(stdin);
53 if (c == EOF)
54 break;
55 if (c != '\n') {
56 if (n < MCP_MAX_INPUT)
57 line[n++] = (char)c;
58 else
59 overflow = true;
60 continue;
61 }
62 if (n && line[n - 1] == '\r')
63 --n;
64 if (!n && !overflow)
65 continue;
66 yyjson_doc *doc =
67 overflow ? NULL : yyjson_read(line, n, YYJSON_READ_NOFLAG);
68 bool pending = false;
69 yyjson_mut_doc *response =
70 doc ? mcp_dispatch(s, doc, NULL, NULL, &pending)
71 : mcp_error(NULL, overflow ? -32600 : -32700,
72 overflow ? "Request exceeds 24 MiB" : "Parse error");
73 if (response) {
74 mcp_stdio_send(s, response);
75 yyjson_mut_doc_free(response);
76 }
77 if (doc)
78 yyjson_doc_free(doc);
79 n = 0;
80 overflow = false;
81 mcp_jobs_reap(s, false);
82 }
83 SAFE_FREE(line);
84 for (unsigned i = 0; i < s->policy.max_jobs; ++i)
85 if (s->jobs[i].renderer_job.started)
87 mcp_jobs_reap(s, true);
88 return ASCIICHAT_OK;
89}
#define SET_ERRNO(code,...)
void atomic_store_bool(atomic_t *a, bool value)
Atomically store a boolean value.
bool atomic_load_bool(atomic_t *a)
Atomic pointer wrapper.
int platform_close(int fd)
Definition filesystem.c:91
#define SAFE_FREE(ptr)
#define SAFE_MALLOC(size, cast)
asciichat_error_t
Error and exit codes - unified status values (0-255).
Definition error_codes.h:48
@ ASCIICHAT_OK
Definition error_codes.h:50
@ ERROR_FILE_OPERATION
void http_renderer_job_cancel(http_renderer_job_t *job)
#define MCP_MAX_INPUT
Definition mcp.h:30
yyjson_mut_doc * mcp_error(yyjson_val *id, int code, const char *message)
yyjson_mut_doc * mcp_dispatch(mcp_server_t *server, yyjson_doc *message, void *connection, const char *session_id, bool *pending)
struct mcp_server mcp_server_t
The useful part of mcp job is in server, message, response, request: together they describe what is p...
void mcp_jobs_reap(mcp_server_t *server, bool all)
asciichat_error_t mcp_stdio_reserve(void)
Definition stdio.c:8
char * mcp_response_encode_toon(yyjson_mut_doc *response, size_t *length)
Definition stdio.c:20
asciichat_error_t mcp_stdio_run(mcp_server_t *s)
Definition stdio.c:47
void mcp_stdio_send(mcp_server_t *s, yyjson_mut_doc *response)
Definition stdio.c:34
void mcp_stdio_release(void)
Definition stdio.c:14
🔤 String Manipulation and Shell Escaping Utilities
http_renderer_job_t renderer_job
Definition mcp.h:71
unsigned max_jobs
Definition mcp.h:42
mutex_t output_mutex
Definition mcp.h:89
atomic_t stopping
Definition mcp.h:91
mcp_job_t * jobs
Definition mcp.h:88
mcp_policy_t policy
Definition mcp.h:87