From 1ca5b346a556f2418c110b480ce1859ce7d5b36c Mon Sep 17 00:00:00 2001 From: grunfink Date: Thu, 12 Jun 2025 12:11:34 +0200 Subject: The usage "screen" shows info only about the entered command. If there is no command or no command starts with that string, the full list is shown as before. --- xs_version.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'xs_version.h') diff --git a/xs_version.h b/xs_version.h index c7789a7..c9418a7 100644 --- a/xs_version.h +++ b/xs_version.h @@ -1 +1 @@ -/* 871d420cef893b6efe32869407294baf084ce3ab 2025-05-04T11:01:01+02:00 */ +/* 963b6129fca4fb5c009533db0ed602ab8ed5424d 2025-06-12T11:33:02+02:00 */ -- cgit From 6fe632ec769f6a03825e55840093fada928a0a8a Mon Sep 17 00:00:00 2001 From: grunfink Date: Sat, 21 Jun 2025 19:20:18 +0200 Subject: Backport from xs. --- xs_json.h | 124 ++++++++++++++++++++++++++++++++++------------------------- xs_version.h | 2 +- 2 files changed, 73 insertions(+), 53 deletions(-) (limited to 'xs_version.h') diff --git a/xs_json.h b/xs_json.h index 8b449a9..436fa72 100644 --- a/xs_json.h +++ b/xs_json.h @@ -19,8 +19,8 @@ xs_val *xs_json_loads_full(const xs_str *json, int maxdepth); xstype xs_json_load_type(FILE *f); int xs_json_load_array_iter(FILE *f, xs_val **value, xstype *pt, int *c); int xs_json_load_object_iter(FILE *f, xs_str **key, xs_val **value, xstype *pt, int *c); -xs_list *xs_json_load_array(FILE *f, int maxdepth); -xs_dict *xs_json_load_object(FILE *f, int maxdepth); +int xs_json_load_array(FILE *f, int maxdepth, xs_list **l); +int xs_json_load_object(FILE *f, int maxdepth, xs_dict **d); #ifdef XS_IMPLEMENTATION @@ -377,43 +377,51 @@ int xs_json_load_array_iter(FILE *f, xs_val **value, xstype *pt, int *c) } -xs_list *xs_json_load_array(FILE *f, int maxdepth) +int xs_json_load_array(FILE *f, int maxdepth, xs_list **l) /* loads a full JSON array (after the initial OBRACK) */ +/* l can be NULL for the content to be dropped */ { xstype t; - xs_list *l = xs_list_new(); + int r = 0; int c = 0; for (;;) { xs *v = NULL; - int r = xs_json_load_array_iter(f, &v, &t, &c); - - if (r == -1) - l = xs_free(l); + r = xs_json_load_array_iter(f, &v, &t, &c); if (r == 1) { /* partial load? */ if (v == NULL && maxdepth != 0) { - if (t == XSTYPE_LIST) - v = xs_json_load_array(f, maxdepth - 1); + if (t == XSTYPE_LIST) { + if (l) + v = xs_list_new(); + + r = xs_json_load_array(f, maxdepth - 1, &v); + } else - if (t == XSTYPE_DICT) - v = xs_json_load_object(f, maxdepth - 1); + if (t == XSTYPE_DICT) { + if (l) + v = xs_dict_new(); + + r = xs_json_load_object(f, maxdepth - 1, &v); + } } - /* still null? fail */ - if (v == NULL) { - l = xs_free(l); + /* error? */ + if (r < 0) break; - } - l = xs_list_append(l, v); + if (l) + *l = xs_list_append(*l, v); } else break; } - return l; + if (r < 0 && l) + *l = xs_free(*l); + + return r; } @@ -465,59 +473,52 @@ int xs_json_load_object_iter(FILE *f, xs_str **key, xs_val **value, xstype *pt, } -xs_dict *xs_json_load_object(FILE *f, int maxdepth) +int xs_json_load_object(FILE *f, int maxdepth, xs_dict **d) /* loads a full JSON object (after the initial OCURLY) */ +/* d can be NULL for the content to be dropped */ { xstype t; - xs_dict *d = xs_dict_new(); + int r = 0; int c = 0; for (;;) { xs *k = NULL; xs *v = NULL; - int r = xs_json_load_object_iter(f, &k, &v, &t, &c); - - if (r == -1) - d = xs_free(d); + r = xs_json_load_object_iter(f, &k, &v, &t, &c); if (r == 1) { /* partial load? */ if (v == NULL && maxdepth != 0) { - if (t == XSTYPE_LIST) - v = xs_json_load_array(f, maxdepth - 1); + if (t == XSTYPE_LIST) { + if (d) + v = xs_list_new(); + + r = xs_json_load_array(f, maxdepth - 1, &v); + } else - if (t == XSTYPE_DICT) - v = xs_json_load_object(f, maxdepth - 1); + if (t == XSTYPE_DICT) { + if (d) + v = xs_dict_new(); + + r = xs_json_load_object(f, maxdepth - 1, &v); + } } - /* still null? fail */ - if (v == NULL) { - d = xs_free(d); + /* error? */ + if (r < 0) break; - } - d = xs_dict_append(d, k, v); + if (d) + *d = xs_dict_append(*d, k, v); } else break; } - return d; -} - - -xs_val *xs_json_loads_full(const xs_str *json, int maxdepth) -/* loads a string in JSON format and converts to a multiple data */ -{ - FILE *f; - xs_val *v = NULL; - - if ((f = fmemopen((char *)json, strlen(json), "r")) != NULL) { - v = xs_json_load_full(f, maxdepth); - fclose(f); - } + if (r < 0 && d) + *d = xs_free(*d); - return v; + return r; } @@ -545,11 +546,30 @@ xs_val *xs_json_load_full(FILE *f, int maxdepth) xs_val *v = NULL; xstype t = xs_json_load_type(f); - if (t == XSTYPE_LIST) - v = xs_json_load_array(f, maxdepth); + if (t == XSTYPE_LIST) { + v = xs_list_new(); + xs_json_load_array(f, maxdepth, &v); + } else - if (t == XSTYPE_DICT) - v = xs_json_load_object(f, maxdepth); + if (t == XSTYPE_DICT) { + v = xs_dict_new(); + xs_json_load_object(f, maxdepth, &v); + } + + return v; +} + + +xs_val *xs_json_loads_full(const xs_str *json, int maxdepth) +/* loads a string in JSON format and converts to a multiple data */ +{ + FILE *f; + xs_val *v = NULL; + + if ((f = fmemopen((char *)json, strlen(json), "r")) != NULL) { + v = xs_json_load_full(f, maxdepth); + fclose(f); + } return v; } diff --git a/xs_version.h b/xs_version.h index c9418a7..74f453f 100644 --- a/xs_version.h +++ b/xs_version.h @@ -1 +1 @@ -/* 963b6129fca4fb5c009533db0ed602ab8ed5424d 2025-06-12T11:33:02+02:00 */ +/* 851c21892c8df1d8f05ae95eaa4a5913e852653b 2025-06-21T17:58:11+02:00 */ -- cgit From 238c5c5f3918f4c64df2364b7c25eb82fd8f16d2 Mon Sep 17 00:00:00 2001 From: grunfink Date: Tue, 24 Jun 2025 12:59:02 +0200 Subject: Some JSON code tweaks. --- xs_json.h | 39 ++++++++++++++++++++++----------------- xs_version.h | 2 +- 2 files changed, 23 insertions(+), 18 deletions(-) (limited to 'xs_version.h') diff --git a/xs_json.h b/xs_json.h index 436fa72..07800fa 100644 --- a/xs_json.h +++ b/xs_json.h @@ -8,6 +8,7 @@ #define MAX_JSON_DEPTH 32 #endif +void xs_json_dump_value(const xs_val *data, int level, int indent, FILE *f); int xs_json_dump(const xs_val *data, int indent, FILE *f); xs_str *xs_json_dumps(const xs_val *data, int indent); @@ -77,7 +78,7 @@ static void _xs_json_indent(int level, int indent, FILE *f) } -static void _xs_json_dump(const xs_val *data, int level, int indent, FILE *f) +void xs_json_dump_value(const xs_val *data, int level, int indent, FILE *f) /* dumps partial data as JSON */ { int c = 0; @@ -108,7 +109,7 @@ static void _xs_json_dump(const xs_val *data, int level, int indent, FILE *f) fputc(',', f); _xs_json_indent(level + 1, indent, f); - _xs_json_dump(v, level + 1, indent, f); + xs_json_dump_value(v, level + 1, indent, f); c++; } @@ -135,7 +136,7 @@ static void _xs_json_dump(const xs_val *data, int level, int indent, FILE *f) if (indent) fputc(' ', f); - _xs_json_dump(v, level + 1, indent, f); + xs_json_dump_value(v, level + 1, indent, f); c++; } @@ -154,6 +155,20 @@ static void _xs_json_dump(const xs_val *data, int level, int indent, FILE *f) } +int xs_json_dump(const xs_val *data, int indent, FILE *f) +/* dumps data into a file as JSON */ +{ + xstype t = xs_type(data); + + if (t == XSTYPE_LIST || t == XSTYPE_DICT) { + xs_json_dump_value(data, 0, indent, f); + return 1; + } + + return 0; +} + + xs_str *xs_json_dumps(const xs_val *data, int indent) /* dumps data as a JSON string */ { @@ -173,20 +188,6 @@ xs_str *xs_json_dumps(const xs_val *data, int indent) } -int xs_json_dump(const xs_val *data, int indent, FILE *f) -/* dumps data into a file as JSON */ -{ - xstype t = xs_type(data); - - if (t == XSTYPE_LIST || t == XSTYPE_DICT) { - _xs_json_dump(data, 0, indent, f); - return 1; - } - - return 0; -} - - /** JSON loads **/ typedef enum { @@ -370,6 +371,8 @@ int xs_json_load_array_iter(FILE *f, xs_val **value, xstype *pt, int *c) else return -1; } + else + *pt = xs_type(*value); *c = *c + 1; @@ -466,6 +469,8 @@ int xs_json_load_object_iter(FILE *f, xs_str **key, xs_val **value, xstype *pt, else return -1; } + else + *pt = xs_type(*value); *c = *c + 1; diff --git a/xs_version.h b/xs_version.h index 74f453f..09b1bdc 100644 --- a/xs_version.h +++ b/xs_version.h @@ -1 +1 @@ -/* 851c21892c8df1d8f05ae95eaa4a5913e852653b 2025-06-21T17:58:11+02:00 */ +/* a32c0d513ae24ad28ffc5c6c2c1cde75bb758e09 2025-06-23T17:43:10+02:00 */ -- cgit From 08f03cde6b469428494d3870ac54f740fd4641c4 Mon Sep 17 00:00:00 2001 From: grunfink Date: Tue, 8 Jul 2025 17:30:45 +0200 Subject: Added some const here and there. --- snac.h | 2 +- webfinger.c | 2 +- xs_fcgi.h | 4 ++-- xs_httpd.h | 12 ++++++------ xs_version.h | 2 +- 5 files changed, 11 insertions(+), 11 deletions(-) (limited to 'xs_version.h') diff --git a/snac.h b/snac.h index cc66686..b50f345 100644 --- a/snac.h +++ b/snac.h @@ -325,7 +325,7 @@ void httpd(void); int webfinger_request_signed(snac *snac, const char *qs, xs_str **actor, xs_str **user); int webfinger_request(const char *qs, xs_str **actor, xs_str **user); int webfinger_request_fake(const char *qs, xs_str **actor, xs_str **user); -int webfinger_get_handler(xs_dict *req, const char *q_path, +int webfinger_get_handler(const xs_dict *req, const char *q_path, xs_val **body, int *b_size, char **ctype); const char *default_avatar_base64(void); diff --git a/webfinger.c b/webfinger.c index dc0855a..46b7edb 100644 --- a/webfinger.c +++ b/webfinger.c @@ -152,7 +152,7 @@ int webfinger_request_fake(const char *qs, xs_str **actor, xs_str **user) } -int webfinger_get_handler(xs_dict *req, const char *q_path, +int webfinger_get_handler(const xs_dict *req, const char *q_path, xs_val **body, int *b_size, char **ctype) /* serves webfinger queries */ { diff --git a/xs_fcgi.h b/xs_fcgi.h index 0b53dac..b3cb892 100644 --- a/xs_fcgi.h +++ b/xs_fcgi.h @@ -13,7 +13,7 @@ #define _XS_FCGI_H xs_dict *xs_fcgi_request(FILE *f, xs_str **payload, int *p_size, int *id); - void xs_fcgi_response(FILE *f, int status, xs_dict *headers, xs_str *body, int b_size, int id); + void xs_fcgi_response(FILE *f, int status, const xs_dict *headers, const xs_str *body, int b_size, int id); #ifdef XS_IMPLEMENTATION @@ -290,7 +290,7 @@ end: } -void xs_fcgi_response(FILE *f, int status, xs_dict *headers, xs_str *body, int b_size, int fcgi_id) +void xs_fcgi_response(FILE *f, int status, const xs_dict *headers, const xs_str *body, int b_size, int fcgi_id) /* writes an FCGI response */ { struct fcgi_record_header hdr = {0}; diff --git a/xs_httpd.h b/xs_httpd.h index 4cc8263..57759c4 100644 --- a/xs_httpd.h +++ b/xs_httpd.h @@ -5,7 +5,8 @@ #define _XS_HTTPD_H xs_dict *xs_httpd_request(FILE *f, xs_str **payload, int *p_size); -void xs_httpd_response(FILE *f, int status, const char *status_text, xs_dict *headers, xs_str *body, int b_size); +void xs_httpd_response(FILE *f, int status, const char *status_text, + const xs_dict *headers, const xs_val *body, int b_size); #ifdef XS_IMPLEMENTATION @@ -109,16 +110,15 @@ xs_dict *xs_httpd_request(FILE *f, xs_str **payload, int *p_size) } -void xs_httpd_response(FILE *f, int status, const char *status_text, xs_dict *headers, xs_str *body, int b_size) +void xs_httpd_response(FILE *f, int status, const char *status_text, + const xs_dict *headers, const xs_val *body, int b_size) /* sends an httpd response */ { - xs *proto; + fprintf(f, "HTTP/1.1 %d %s\r\n", status, status_text ? status_text : ""); + const xs_str *k; const xs_val *v; - proto = xs_fmt("HTTP/1.1 %d %s", status, status_text); - fprintf(f, "%s\r\n", proto); - xs_dict_foreach(headers, k, v) { fprintf(f, "%s: %s\r\n", k, v); } diff --git a/xs_version.h b/xs_version.h index 09b1bdc..466535b 100644 --- a/xs_version.h +++ b/xs_version.h @@ -1 +1 @@ -/* a32c0d513ae24ad28ffc5c6c2c1cde75bb758e09 2025-06-23T17:43:10+02:00 */ +/* 401d229ffbec89b4a5cf97793926b7afb84a4f26 2025-07-08T15:44:54+02:00 */ -- cgit