ascii-chat
Binary v0.16.114-28-gf5913657f
Library lib/v0.3.2-3302-gf5913657f
Video chat in your terminal
Loading...
Searching...
No Matches
options/json.c
Go to the documentation of this file.
1#include <ascii-chat/options/builder/internal.h>
4#include <math.h>
5#include <string.h>
6
7static const char *json_type(const registry_entry_t *entry) {
8 if (entry->type == OPTION_TYPE_BOOL)
9 return "boolean";
10 if (entry->type == OPTION_TYPE_INT || !strcmp(entry->long_name, "zalgo"))
11 return "integer";
12 if (entry->type == OPTION_TYPE_DOUBLE ||
13 (entry->type == OPTION_TYPE_CALLBACK && !entry->metadata.enum_values &&
14 entry->metadata.numeric_range.max && strcmp(entry->long_name, "seek")))
15 return "number";
16 return "string";
17}
18static asciichat_error_t validate_value(const registry_entry_t *e,
19 yyjson_val *v) {
20 if (!strcmp(e->long_name, "test-pattern")) {
21 if (yyjson_is_bool(v) ||
22 (yyjson_is_int(v) && yyjson_get_sint(v) >= 0 &&
23 yyjson_get_sint(v) <= 1) ||
24 (yyjson_is_str(v) &&
25 (!strcmp(yyjson_get_str(v), "0") || !strcmp(yyjson_get_str(v), "1"))))
26 return ASCIICHAT_OK;
28 "test-pattern must be boolean, 0 or 1");
29 }
30 const char *type = json_type(e);
31 bool ok = (!strcmp(type, "boolean") && yyjson_is_bool(v)) ||
32 (!strcmp(type, "integer") && yyjson_is_int(v)) ||
33 (!strcmp(type, "number") && yyjson_is_num(v)) ||
34 (!strcmp(type, "string") && yyjson_is_str(v)) ||
35 (!strcmp(e->long_name, "seek") && yyjson_is_num(v));
36 if (!ok)
37 return SET_ERRNO(ERROR_INVALID_PARAM, "Wrong type for %s", e->long_name);
38 if (yyjson_is_str(v) && (yyjson_get_len(v) > 4096 ||
39 strlen(yyjson_get_str(v)) != yyjson_get_len(v)))
40 return SET_ERRNO(ERROR_INVALID_PARAM, "Invalid or oversized option %s",
41 e->long_name);
42 if (yyjson_is_num(v) && !isfinite(yyjson_get_num(v)))
43 return SET_ERRNO(ERROR_INVALID_PARAM, "Non-finite option");
44 return ASCIICHAT_OK;
45}
46
47yyjson_mut_val *options_json_schema_for_option(yyjson_mut_doc *d,
48 const char *name) {
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}
88
89asciichat_error_t options_json_parse_value(const char *name, yyjson_val *v,
90 char *output, size_t output_size,
91 bool *omitted) {
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_INT
Integer value (–count 42).
Definition builder.h:182
@ OPTION_TYPE_DOUBLE
Floating point (–ratio 1.5).
Definition builder.h:184
@ OPTION_TYPE_STRING
String value (–name foo).
Definition builder.h:183
@ OPTION_TYPE_ACTION
Definition builder.h:186
@ OPTION_TYPE_CALLBACK
Custom parser function.
Definition builder.h:185
@ OPTION_TYPE_BOOL
Boolean flag (–flag, no value).
Definition builder.h:181
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.
Registry-backed JSON schemas and conversion through CLI option handlers.
Internal helper functions and data for registry implementation.
🔢 Mathematical Utility 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)
🔤 String Manipulation and Shell Escaping Utilities
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
int max
Maximum value (or 0 if no limit).
Definition builder.h:227
struct option_metadata_t::@061372117173071240020206164036331043262170062306 numeric_range
const char ** enum_values
Definition builder.h:216
Registry entry - stores option definition with mode bitmask and metadata.
option_type_t type
bool owns_memory
const char * long_name
size_t default_value_size
option_metadata_t metadata
Enum values, numeric ranges, examples.
const char * help_text