Hash :
31260c68
Author :
Thomas de Grivel
Date :
2023-01-15T23:16:20
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403
/* rtbuf
* Copyright 2018-2023 kmx.io <contact@kmx.io>
*
* Permission is hereby granted to use this software excepted
* on Apple computers granted the above copyright notice and
* this permission paragraph are included in all copies and
* substantial portions of this software.
*
* THIS SOFTWARE IS PROVIDED "AS-IS" WITHOUT ANY GUARANTEE OF
* PURPOSE AND PERFORMANCE. IN NO EVENT WHATSOEVER SHALL THE
* AUTHOR BE CONSIDERED LIABLE FOR THE USE AND PERFORMANCE OF
* THIS SOFTWARE.
*/
#include <assert.h>
#include <pthread.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include "cli.h"
#include "../librtbuf/symbol.h"
#include "../librtbuf/rtbuf.h"
#include "../librtbuf/lib.h"
#include "../librtbuf/var.h"
#include "rtbuf_cli.h"
s_cli g_cli;
pthread_t g_rtbuf_cli_run_thread = 0;
f_rtbuf_position g_rtbuf_position_cb = 0;
int rtbuf_cli_libs (int argc, const char *argv[])
{
unsigned int i = 0;
unsigned int n = g_rtbuf_lib_alloc.n - g_rtbuf_lib_alloc.free_n;
if (argc != 0)
return rtbuf_err("usage: libs");
(void) argv;
printf("Listing %i libraries :\n", n);
while (i < g_rtbuf_lib_alloc.n && n > 0) {
if (g_rtbuf_lib[i].path[0]) {
rtbuf_lib_print(g_rtbuf_lib + i);
n--;
}
i++;
}
return 0;
}
int rtbuf_cli_lib (int argc, const char *argv[])
{
int i;
if (argc != 1)
return rtbuf_err("usage: lib LIBRARY");
i = rtbuf_lib_find(argv[1]);
if (0 <= i && i < RTBUF_LIB_MAX) {
rtbuf_lib_print_long(i);
return 0;
}
return rtbuf_err("library not found");
}
int rtbuf_cli_load (int argc, const char *argv[])
{
s_rtbuf_lib *lib;
if (argc != 1)
return rtbuf_err("usage: load LIBRARY");
lib = rtbuf_lib_load(argv[1]);
if (!lib) {
printf("load failed\n");
return -1;
}
rtbuf_lib_print(lib);
return 0;
}
int rtbuf_cli_buffers (int argc, const char *argv[])
{
unsigned int i = 0;
unsigned int n = g_rtbuf_alloc.n;
if (argc != 0)
return rtbuf_err("usage: buffers");
(void) argv;
printf("Listing %i buffers :\n", n);
while (i < RTBUF_MAX && n > 0) {
if (g_rtbuf[i].data) {
rtbuf_print_long(g_rtbuf + i);
printf("\n");
n--;
}
i++;
}
return 0;
}
int rtbuf_cli_buffer (int argc, const char *argv[])
{
int i;
if (argc != 1)
return rtbuf_err("usage: buffer N");
if ((i = rtbuf_find(argv[1])) < 0)
return rtbuf_err("buffer not found");
rtbuf_print_long(g_rtbuf + i);
return 0;
}
int rtbuf_cli_new (int argc, const char *argv[])
{
int rl;
int rtb;
short x;
short y;
if (argc != 1 && argc != 3)
return rtbuf_err("usage: new LIBRARY [X Y]");
if ((rl = rtbuf_lib_find(argv[1])) < 0)
return rtbuf_err("library not found");
if ((rtb = rtbuf_new(g_rtbuf_lib[rl].proc)) < 0)
return rtbuf_err("buffer not created");
if (argc == 3 &&
g_rtbuf_position_cb &&
sscanf(argv[2], "%hd", &x) == 1 &&
sscanf(argv[3], "%hd", &y) == 1)
g_rtbuf_position_cb(&g_rtbuf[rtb], x, y);
return 0;
}
int rtbuf_cli_let (int argc, const char *argv[])
{
s_rtbuf_var *v;
short x;
short y;
if (argc < 4 || argv[2][0] != '=' || argv[2][1])
return rtbuf_err("usage: let VAR = TYPE ARG [...]");
if (strncmp("new", argv[3], 4) == 0) {
int rl;
int rtb;
if (argc != 4 && argc != 6)
return rtbuf_err("usage: let VAR = new LIBRARY [X Y]");
if ((rl = rtbuf_lib_find(argv[4])) < 0)
return rtbuf_err("library not found");
if ((rtb = rtbuf_new(g_rtbuf_lib[rl].proc)) < 0)
return rtbuf_err("buffer not created");
v = rtbuf_var_rtbuf_set(argv[1], rtb);
if (!v)
return rtbuf_err("rtbuf_var_rtbuf_set failed");
if (argc == 6 &&
g_rtbuf_position_cb &&
sscanf(argv[5], "%hd", &x) == 1 &&
sscanf(argv[6], "%hd", &y) == 1)
g_rtbuf_position_cb(&g_rtbuf[rtb], x, y);
return 0;
}
else if (strncmp("buffer", argv[3], 7) == 0) {
int rtb;
if (argc != 4)
return rtbuf_err("usage: let VAR = buffer N");
if ((rtb = rtbuf_find(argv[4])) < 0)
return rtbuf_err("buffer not found");
v = rtbuf_var_rtbuf_set(argv[1], rtb);
if (!v)
return rtbuf_err("rtbuf_var_rtbuf_set failed");
return 0;
}
return rtbuf_err("unknown type for let");
}
int rtbuf_cli_delete (int argc, const char *argv[])
{
int i;
if (argc != 1)
return rtbuf_err("usage: delete N");
i = rtbuf_find(argv[1]);
if (i < 0)
return rtbuf_err("buffer not found\n");
rtbuf_delete(&g_rtbuf[i]);
return 0;
}
int rtbuf_cli_bind (int argc, const char *argv[])
{
int src;
int out;
int dest;
int in;
if (argc != 4)
return rtbuf_err("usage: bind SOURCE OUT DEST IN");
if ((src = rtbuf_find(argv[1])) < 0)
return rtbuf_err("source buffer not found");
if ((out = rtbuf_out_find(&g_rtbuf[src], argv[2])) < 0)
return rtbuf_err("output not found");
if ((dest = rtbuf_find(argv[3])) < 0)
return rtbuf_err("destination buffer not found");
if ((in = rtbuf_in_find(&g_rtbuf[dest], argv[4])) < 0)
return rtbuf_err("input not found");
rtbuf_bind(src, out, &g_rtbuf[dest], in);
return 0;
}
int rtbuf_cli_unbind (int argc, const char *argv[])
{
int rtb;
if (argc < 1 || argc > 2)
return rtbuf_err("usage: unbind BUFFER [INPUT]");
if ((rtb = rtbuf_find(argv[1])) < 0)
return rtbuf_err("buffer not found");
if (argc == 2) {
int in = atoi(argv[2]);
if (in < 0 || (unsigned int) in >= g_rtbuf[rtb].proc->in_n)
return rtbuf_err("input not found");
rtbuf_in_unbind(&g_rtbuf[rtb], in);
}
else
rtbuf_unbind_all(&g_rtbuf[rtb]);
return 0;
}
void * rtbuf_cli_run_thread_proc (void *arg)
{
(void) arg;
printf("rtbuf thread: start\n");
if (!rtbuf_start()) {
g_rtbuf_run = 1;
g_rtbuf_running = 1;
}
while (g_rtbuf_run) {
if (rtbuf_run())
g_rtbuf_run = 0;
}
printf("rtbuf thread: stop\n");
rtbuf_stop();
g_rtbuf_running = 0;
return 0;
}
int rtbuf_cli_start ()
{
if (!g_rtbuf_run && g_rtbuf_cli_run_thread) {
if (pthread_join(g_rtbuf_cli_run_thread, 0))
return rtbuf_err("pthread_join failed");
g_rtbuf_cli_run_thread = 0;
}
if (!g_rtbuf_cli_run_thread) {
if (pthread_create(&g_rtbuf_cli_run_thread, 0, &rtbuf_cli_run_thread_proc,
0))
return rtbuf_err("rtbuf_cli_start: pthread_create failed");
}
return 0;
}
int rtbuf_cli_start_ (int argc, const char *argv[])
{
(void) argv;
if (argc != 0)
return rtbuf_err("usage: start");
return rtbuf_cli_start();
}
int rtbuf_cli_stop ()
{
if (g_rtbuf_run)
g_rtbuf_run = 0;
if (g_rtbuf_cli_run_thread) {
if (pthread_join(g_rtbuf_cli_run_thread, 0))
return rtbuf_err("rtbuf_cli_stop: pthread_join failed");
g_rtbuf_cli_run_thread = 0;
}
return 0;
}
int rtbuf_cli_stop_ (int argc, const char *argv[])
{
(void) argv;
if (argc != 0)
return rtbuf_err("usage: stop");
return rtbuf_cli_stop();
}
int rtbuf_cli_help (int argc, const char *argv[])
{
(void) argc;
(void) argv;
printf("Available commands :\n"
" libs List loaded libraries.\n"
" lib N Show library N.\n"
" load LIB Load library LIB.\n"
" buffers List buffers.\n"
" buffer N Show buffer N.\n"
" let VAR = buffer N Set variable to buffer.\n");
printf(" new LIB Instanciate library.\n"
" let VAR = new LIB Set variable to new instance.\n"
" delete BUFFER Unlink and delete BUFFER.\n"
" bind SOURCE OUT DEST IN Bind SOURCE OUT to DEST IN.\n"
" unbind BUFFER IN Unbind BUFFER IN.\n"
" start Start run thread.\n"
" stop Stop run thread.\n"
" help Show this help message.\n"
" exit Quit RTBUF.\n");
return 0;
}
int rtbuf_cli_exit (int argc, const char *argv[])
{
(void) argc;
(void) argv;
rtbuf_cli_stop();
librtbuf_shutdown();
close(0);
exit(0);
return 0;
}
s_cli_function rtbuf_cli_functions[] = {
{ "bind", 4, rtbuf_cli_bind },
{ "buffer", 1, rtbuf_cli_buffer },
{ "buffers", 0, rtbuf_cli_buffers },
{ "delete", 1, rtbuf_cli_delete },
{ "exit", 0, rtbuf_cli_exit },
{ "h", 0, rtbuf_cli_help },
{ "help", 0, rtbuf_cli_help },
{ "let", -1, rtbuf_cli_let },
{ "lib", 1, rtbuf_cli_lib },
{ "libs", 0, rtbuf_cli_libs },
{ "load", 1, rtbuf_cli_load },
{ "new", 1, rtbuf_cli_new },
{ "new", 3, rtbuf_cli_new },
{ "start", 0, rtbuf_cli_start_ },
{ "stop", 0, rtbuf_cli_stop_ },
{ "unbind", 1, rtbuf_cli_unbind },
{ "unbind", 2, rtbuf_cli_unbind },
{ 0, 0, 0 }
};
void debug_read (int argc, const char **argv, f_cli f)
{
union {
f_cli f;
void *p;
} fp;
if (argc < 1)
return;
printf("CLI READ %i %s(", argc, *argv);
argv++;
argc--;
fp.f = f;
while (1) {
if (argc < 1) {
printf("); %p\n", fp.p);
return;
}
printf("%s", *argv);
if (argc > 1)
printf(", ");
argv++;
argc--;
}
}
void repl_init ()
{
cli_init(&g_cli);
cli_prompt(&g_cli, "rtbuf> ");
cli_functions(&g_cli, rtbuf_cli_functions);
}
int load (const char *path)
{
FILE *fp = fopen(path, "r");
if (!fp)
return -1;
printf("; Loading %s\n", path);
while (cli_read_file(&g_cli, fp) >= 0)
cli_eval(&g_cli);
fclose(fp);
return 0;
}
int repl ()
{
while (cli_read(&g_cli) >= 0) {
/* debug_read(cli.argc, cli.argv, cli.f); */
cli_eval(&g_cli);
}
return 0;
}
int rtbuf_cli_do_event ()
{
if (cli_read_nonblocking(&g_cli) >= 0) {
/* debug_read(cli.argc, cli.argv, cli.f); */
cli_eval(&g_cli);
return 1;
}
return 0;
}
void rtbuf_cli_args (int argc, char *argv[])
{
while (--argc) {
char *arg = *++argv;
printf("loading script %s\n", arg);
load(arg);
}
}