Skip to content

Commit 876942b

Browse files
authored
Merge pull request #678 from konakona418-forked/dev-fmt-run-script
format support for webui_run and webui_script
2 parents d9c86cc + bc42ffd commit 876942b

2 files changed

Lines changed: 146 additions & 0 deletions

File tree

include/webui_extensions.h

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
/*
2+
WebUI Library Extras
3+
https://webui.me
4+
https://github.com/webui-dev/webui
5+
Copyright (c) 2020-2025 Hassan Draga.
6+
Licensed under MIT License.
7+
All rights reserved.
8+
Canada.
9+
*/
10+
11+
#ifndef _WEBUI_EXTENSIONS_H
12+
#define _WEBUI_EXTENSIONS_H
13+
14+
#ifdef __cplusplus
15+
extern "C" {
16+
#endif
17+
18+
/**
19+
* This is to help the compiler identify whether to compile the extensions API funtions or not
20+
* - If you are including the webui.c file to your source file directly,
21+
* and wish to use the extensions API functions,
22+
* please include this header file before including webui.c,
23+
* or define this macro manually before including webui.c
24+
* - In other cases, as long as the webui.c file is not included directly,
25+
* you might need to define this macro in your compiler settings
26+
*/
27+
#ifndef WEBUI_EXTENSION_API
28+
#define WEBUI_EXTENSION_API
29+
#endif
30+
31+
#ifndef WEBUI_EXPORT
32+
#define WEBUI_EXPORT
33+
#warning "WEBUI_EXPORT not defined; Please include webui.h before webui_extensions.h"
34+
#endif
35+
36+
#include <stdbool.h>
37+
#include <stddef.h>
38+
39+
/**
40+
* @brief Construct a JavaScript string from a format string,
41+
* then execute it without waiting for the response. All clients.
42+
*
43+
* @param window The window number
44+
* @param fmt The JavaScript format string to be run
45+
*
46+
* @warning This function DOES NOT handle escape characters, proceed with caution.
47+
* For example, when passing string arguments that contain quotes or backslashes,
48+
* it may lead to unexpected behavior
49+
*
50+
* @note This function internally uses vsnprintf
51+
*
52+
* @example webui_run_fmt(myWindow, "alert('Hello %s');", "World");
53+
*/
54+
WEBUI_EXPORT void webui_run_fmt(size_t window, const char* fmt, ...);
55+
56+
/**
57+
* @brief Construct a JavaScript string from a format string,
58+
* then execute it and get the response back. Work only in single client mode.
59+
* Make sure your local buffer can hold the response.
60+
*
61+
* @param window The window number
62+
* @param timeout The execution timeout in seconds
63+
* @param buffer The local buffer to hold the response
64+
* @param buffer_length The local buffer size
65+
* @param fmt The JavaScript format string to be run
66+
*
67+
* @return Returns True if there is no execution error
68+
*
69+
* @warning This function DOES NOT handle escape characters, proceed with caution.
70+
* For example, when passing string arguments that contain quotes or backslashes,
71+
* it may lead to unexpected behavior
72+
*
73+
* @note This function internally uses vsnprintf
74+
*
75+
* @example bool err = webui_script_fmt(myWindow, 0, myBuffer, myBufferSize,
76+
* "return %d + %d;", 4, 6);
77+
*/
78+
WEBUI_EXPORT bool webui_script_fmt(size_t window, size_t timeout,
79+
char* buffer, size_t buffer_length,
80+
const char* fmt, ...);
81+
82+
#ifdef __cplusplus
83+
}
84+
#endif
85+
86+
#endif /*_WEBUI_EXTENSIONS_H */

src/webui.c

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -847,6 +847,33 @@ void webui_run(size_t window, const char* script) {
847847
_webui_send_all(win, 0, WEBUI_CMD_JS_QUICK, script, js_len);
848848
}
849849

850+
#ifdef WEBUI_EXTENSION_API
851+
void webui_run_fmt(size_t window, const char* fmt, ...) {
852+
int len;
853+
char* buf;
854+
855+
// Precalculate actual string length
856+
va_list args;
857+
va_start(args, fmt);
858+
len = vsnprintf(NULL, 0, fmt, args);
859+
va_end(args);
860+
861+
if (len < 1) return;
862+
863+
buf = (char*)_webui_malloc(len + 1);
864+
if (buf == NULL) return;
865+
866+
// Format and execute
867+
va_start(args, fmt);
868+
vsnprintf(buf, len + 1, fmt, args);
869+
va_end(args);
870+
871+
webui_run(window, buf);
872+
873+
_webui_free_mem((void*)buf);
874+
}
875+
#endif /* WEBUI_EXTENSION_API */
876+
850877
void webui_set_close_handler_wv(size_t window, bool(*close_handler)(size_t window)) {
851878

852879
// Initialization
@@ -1065,6 +1092,39 @@ bool webui_script(size_t window, const char* script, size_t timeout,
10651092
return webui_script_client(&e, script, timeout, buffer, buffer_length);
10661093
}
10671094

1095+
#ifdef WEBUI_EXTENSION_API
1096+
bool webui_script_fmt(
1097+
size_t window, size_t timeout,
1098+
char* buffer, size_t buffer_length,
1099+
const char* fmt, ...) {
1100+
int len;
1101+
char* buf;
1102+
bool status;
1103+
1104+
// Precalculate actual string length
1105+
va_list args;
1106+
va_start(args, fmt);
1107+
len = vsnprintf(NULL, 0, fmt, args);
1108+
va_end(args);
1109+
1110+
if (len < 1) return false;
1111+
1112+
buf = (char*)_webui_malloc(len + 1);
1113+
if (buf == NULL) return false;
1114+
1115+
// Format and execute
1116+
va_start(args, fmt);
1117+
vsnprintf(buf, len + 1, fmt, args);
1118+
va_end(args);
1119+
1120+
status = webui_script(window, buf, timeout, buffer, buffer_length);
1121+
1122+
_webui_free_mem((void*)buf);
1123+
1124+
return status;
1125+
}
1126+
#endif /* WEBUI_EXTENSION_API */
1127+
10681128
static uint32_t _webui_generate_random_uint32() {
10691129
uint32_t timestamp = (uint32_t) time(NULL);
10701130
// Get the higher 16 bits

0 commit comments

Comments
 (0)