ascii-chat
Binary v0.16.114-28-gf5913657f
Library lib/v0.3.2-3302-gf5913657f
Video chat in your terminal
Loading...
Searching...
No Matches
json.c File Reference

Go to the source code of this file.

Functions

yyjson_mut_val * options_json_schema_for_option (yyjson_mut_doc *d, const char *name)
asciichat_error_t options_json_parse_value (const char *name, yyjson_val *v, char *output, size_t output_size, bool *omitted)

Function Documentation

◆ options_json_parse_value()

asciichat_error_t options_json_parse_value ( const char * name,
yyjson_val * value,
char * output,
size_t output_size,
bool * omitted )

Convert and parse a JSON value with the CLI handler, without publishing global options. omitted is set for an explicitly disabled optional flag such as test-pattern=false. Full cross-option validation remains in the normal CLI worker initialization.

Definition at line 89 of file options/json.c.

91 {
92 if (!name || !v || !output || !output_size || !omitted)
93 return SET_ERRNO(ERROR_INVALID_PARAM, "Missing JSON option argument");
94 *omitted = false;
96 if (!e || e->type < 0 || e->type >= NUM_OPTION_TYPES ||
98 return SET_ERRNO(ERROR_INVALID_PARAM, "Unsupported value option: %s", name);
99 asciichat_error_t err = validate_value(e, v);
100 if (err != ASCIICHAT_OK)
101 return err;
102 if (yyjson_is_str(v)) {
103 size_t len = yyjson_get_len(v);
104 if (len >= output_size ||
106 len >= e->default_value_size))
107 return SET_ERRNO(ERROR_INVALID_PARAM, "Option value too long: %s", name);
108 SAFE_STRNCPY(output, yyjson_get_str(v), output_size);
109 } else if (yyjson_is_bool(v)) {
110 SAFE_SNPRINTF(output, output_size, "%s",
111 yyjson_get_bool(v) ? "true" : "false");
112 } else {
113 SAFE_SNPRINTF(output, output_size, "%.17g", yyjson_get_num(v));
114 }
115 if (!strcmp(name, "test-pattern") && yyjson_is_bool(v)) {
116 if (!yyjson_get_bool(v)) {
117 *omitted = true;
118 return ASCIICHAT_OK;
119 }
120 SAFE_SNPRINTF(output, output_size, "0");
121 }
122 // Use the same registry descriptor and handlers as options_config_parse().
124 options_t *scratch = SAFE_CALLOC(1, sizeof(*scratch), options_t *);
125 err = g_builder_handlers[descriptor.type].apply_cli(
126 (char *)scratch + descriptor.offset, output, &descriptor);
127 SAFE_FREE(scratch);
128 return err;
129}
#define SET_ERRNO(code,...)
@ OPTION_TYPE_STRING
String value (–name foo).
Definition builder.h:183
@ OPTION_TYPE_ACTION
Definition builder.h:186
option_descriptor_t registry_entry_to_descriptor(const registry_entry_t *entry)
Definition core.c:168
const registry_entry_t * registry_find_entry_by_name(const char *long_name)
Get a registry entry by long name.
Definition core.c:136
#define SAFE_STRNCPY(dst, src, size)
#define SAFE_FREE(ptr)
#define SAFE_SNPRINTF(buffer, buffer_size,...)
#define SAFE_CALLOC(count, 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_INVALID_PARAM
struct options_state options_t
Immutable, RCU-published snapshot of the application's runtime configuration.
The complete registry entry for one command-line/configuration option.
Definition builder.h:271
size_t offset
offsetof(struct, field) - where to store value
Definition builder.h:278
option_type_t type
Value type.
Definition builder.h:277
Registry entry - stores option definition with mode bitmask and metadata.
option_type_t type
bool owns_memory
size_t default_value_size

References ASCIICHAT_OK, registry_entry_t::default_value_size, ERROR_INVALID_PARAM, option_descriptor_t::offset, OPTION_TYPE_ACTION, OPTION_TYPE_STRING, registry_entry_t::owns_memory, registry_entry_to_descriptor(), registry_find_entry_by_name(), SAFE_CALLOC, SAFE_FREE, SAFE_SNPRINTF, SAFE_STRNCPY, SET_ERRNO, option_descriptor_t::type, and registry_entry_t::type.

Referenced by mcp_request_parse().

◆ options_json_schema_for_option()

yyjson_mut_val * options_json_schema_for_option ( yyjson_mut_doc * doc,
const char * name )

Generate a JSON property from a registered option's metadata; document owns the result.

Definition at line 47 of file options/json.c.

48 {
49 if (!d || !name) {
50 SET_ERRNO(ERROR_INVALID_PARAM, "Missing option schema argument");
51 return NULL;
52 }
54 if (!e || e->type == OPTION_TYPE_ACTION) {
55 SET_ERRNO(ERROR_INVALID_PARAM, "Unknown value option: %s", name);
56 return NULL;
57 }
58 yyjson_mut_val *v = yyjson_mut_obj(d);
59 yyjson_mut_obj_add_str(d, v, "type", json_type(e));
60 yyjson_mut_obj_add_str(d, v, "description", e->help_text);
61 if (e->metadata.enum_values) {
62 yyjson_mut_val *values = yyjson_mut_arr(d);
63 for (size_t i = 0; e->metadata.enum_values[i]; i++)
64 yyjson_mut_arr_add_str(d, values, e->metadata.enum_values[i]);
65 yyjson_mut_obj_add_val(d, v, "enum", values);
66 }
67 if (!strcmp(name, "test-pattern")) {
68 yyjson_mut_obj_remove_str(v, "type");
69 yyjson_mut_obj_remove_str(v, "enum");
70 yyjson_mut_val *values = yyjson_mut_arr(d);
71 yyjson_mut_arr_add_bool(d, values, true);
72 yyjson_mut_arr_add_bool(d, values, false);
73 yyjson_mut_arr_add_int(d, values, 0);
74 yyjson_mut_arr_add_int(d, values, 1);
75 yyjson_mut_arr_add_str(d, values, "0");
76 yyjson_mut_arr_add_str(d, values, "1");
77 yyjson_mut_obj_add_val(d, v, "enum", values);
78 }
79 if (!strcmp(name, "seek")) {
80 yyjson_mut_obj_remove_str(v, "type");
81 yyjson_mut_val *types = yyjson_mut_arr(d);
82 yyjson_mut_arr_add_str(d, types, "string");
83 yyjson_mut_arr_add_str(d, types, "number");
84 yyjson_mut_obj_add_val(d, v, "type", types);
85 }
86 return v;
87}
const char ** enum_values
Definition builder.h:216
option_metadata_t metadata
Enum values, numeric ranges, examples.
const char * help_text

References option_metadata_t::enum_values, ERROR_INVALID_PARAM, registry_entry_t::help_text, registry_entry_t::metadata, OPTION_TYPE_ACTION, registry_find_entry_by_name(), SET_ERRNO, and registry_entry_t::type.

Referenced by mcp_tools_schema().