Hash :
51e01a59
Author :
Thomas de Grivel
Date :
2018-07-26T17:49:44
quit -> exit
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
#include <assert.h>
#include <cli.h>
#include <pthread.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include "rtbuf_lib.h"
#include "symbol.h"
pthread_t g_rtbuf_cli_thread = 0;
void print_lib (unsigned int i)
{
assert(i < RTBUF_LIB_MAX);
printf("#<lib %i %s>\n", i, g_rtbuf_lib[i].name);
}
void print_rtbuf_fun (s_rtbuf_fun *fun)
{
printf("#<fun %i %s nmemb=%u size=%u var_n=%u>",
fun->lib_fun,
fun->name,
fun->nmemb,
fun->size,
fun->var_n);
}
void print_lib_long (unsigned int i)
{
s_rtbuf_lib *lib;
unsigned int j = 0;
assert(i < RTBUF_LIB_MAX);
lib = &g_rtbuf_lib[i];
printf("#<lib %i %s", i, lib->name);
while (j < lib->fun_n) {
printf("\n ");
print_rtbuf_fun(lib->fun[j]);
j++;
}
printf(">\n");
fflush(stdout);
}
void print_rtbuf (unsigned int i)
{
assert(i < RTBUF_MAX);
printf("#<rtbuf %u>", i);
fflush(stdout);
}
void print_rtbuf_long (unsigned int i)
{
s_rtbuf *rtb;
unsigned int j = 0;
assert(i < RTBUF_MAX);
rtb = &g_rtbuf[i];
printf("#<rtbuf %i", i);
printf(" %p", rtb->data);
if (rtb->data) {
printf(" %x", rtb->flags);
printf(" %d", rtb->refc);
}
while (j < rtb->fun->var_n) {
if (rtb->var[j] >= 0) {
printf("\n %i %s = ", j, rtb->fun->var[j]);
print_rtbuf(rtb->var[j]);
}
j++;
}
printf(">\n");
fflush(stdout);
}
int rtbuf_cli_libs (int argc, const char *argv[])
{
unsigned int i = 0;
unsigned int n = g_rtbuf_lib_n;
assert(argc == 0);
(void) argv;
printf("Listing %i libraries :\n", n);
while (i < RTBUF_LIB_MAX && n > 0) {
if (g_rtbuf_lib[i].path[0]) {
print_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) {
print_lib_long(i);
return 0;
}
return rtbuf_err("library not found");
}
int rtbuf_cli_load (int argc, const char *argv[])
{
s_rtbuf_lib *lib;
unsigned int i;
assert(argc == 1);
lib = rtbuf_lib_load(argv[1]);
if (!lib) {
printf("load failed\n");
return -1;
}
i = ((void*) lib - (void*) g_rtbuf_lib) / sizeof(s_rtbuf_lib);
print_lib(i);
return 0;
}
int rtbuf_cli_buffers (int argc, const char *argv[])
{
unsigned int i = 0;
unsigned int n = g_rtbuf_n;
assert(argc == 0);
(void) argv;
printf("Listing %i buffers :\n", n);
while (i < RTBUF_MAX && n > 0) {
if (g_rtbuf[i].data) {
print_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");
print_rtbuf_long(i);
return 0;
}
int rtbuf_cli_new (int argc, const char *argv[])
{
int rl;
int rf;
int rtb;
if (argc != 2)
return rtbuf_err("usage: new LIBRARY FUNCTION");
if ((rl = rtbuf_lib_find(argv[1])) < 0)
return rtbuf_err("library not found");
if ((rf = rtbuf_lib_find_fun(&g_rtbuf_lib[rl], argv[2])) < 0)
return rtbuf_err("function not found");
if ((rtb = rtbuf_new(g_rtbuf_lib[rl].fun[rf])) < 0)
return rtbuf_err("buffer not created");
print_rtbuf(rtb);
printf("\n");
return 0;
}
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]);
print_rtbuf(i);
printf("\n");
return 0;
}
int rtbuf_cli_bind (int argc, const char *argv[])
{
int rtb;
int var;
int target;
if (argc != 3)
return rtbuf_err("usage: bind BUFFER VARIABLE TARGET");
if ((rtb = rtbuf_find(argv[1])) < 0)
return rtbuf_err("buffer not found");
var = atoi(argv[2]);
if (var < 0 || (unsigned int) var >= g_rtbuf[rtb].fun->var_n)
return rtbuf_err("variable not found");
if ((target = rtbuf_find(argv[3])) < 0)
return rtbuf_err("target not found");
rtbuf_var_bind(&g_rtbuf[rtb], var, target);
print_rtbuf_long(rtb);
return 0;
}
int rtbuf_cli_unbind (int argc, const char *argv[])
{
int rtb;
if (argc < 1 || argc > 2)
return rtbuf_err("usage: unbind BUFFER [VARIABLE]");
if ((rtb = rtbuf_find(argv[1])) < 0)
return rtbuf_err("buffer not found");
if (argc == 2) {
int var = atoi(argv[2]);
if (var < 0 || (unsigned int) var >= g_rtbuf[rtb].fun->var_n)
return rtbuf_err("variable not found");
rtbuf_var_unbind(&g_rtbuf[rtb], var);
}
else
rtbuf_unbind(&g_rtbuf[rtb]);
print_rtbuf_long(rtb);
return 0;
}
void * rtbuf_cli_thread_fun (void *arg)
{
(void) arg;
rtbuf_start();
g_rtbuf_run = 1;
while (g_rtbuf_run) {
rtbuf_run();
}
rtbuf_stop();
return 0;
}
int rtbuf_cli_start (int argc, const char *argv[])
{
(void) argv;
if (argc != 0)
return rtbuf_err("usage: start");
if (!g_rtbuf_cli_thread) {
if (pthread_create(&g_rtbuf_cli_thread, 0, &rtbuf_cli_thread_fun,
0))
return rtbuf_err("pthread_create failed");
}
return 0;
}
int rtbuf_cli_stop (int argc, const char *argv[])
{
(void) argv;
if (argc != 0)
return rtbuf_err("usage: stop");
g_rtbuf_run = 0;
if (pthread_join(g_rtbuf_cli_thread, 0))
return rtbuf_err("pthread_join failed");
g_rtbuf_cli_thread = 0;
return 0;
}
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 PATH Load library at PATH.\n"
" buffers List buffers.\n"
" buffer N Show buffer N.\n"
" new LIB FUN Instanciate library function.\n"
" delete RTBUF Unlink and delete RTBUF.\n"
" bind RTBUF VAR TARGET Bind RTBUF VAR to TARGET.\n"
" unbind RTBUF VAR Unbind RTBUF VAR.\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(0, 0);
close(0);
exit(0);
return 0;
}
s_cli_function rtbuf_cli_functions[] = {
{ "libs", 0, rtbuf_cli_libs },
{ "lib", 1, rtbuf_cli_lib },
{ "load", 1, rtbuf_cli_load },
{ "buffers", 0, rtbuf_cli_buffers },
{ "buffer", 1, rtbuf_cli_buffer },
{ "new", 2, rtbuf_cli_new },
{ "delete", 1, rtbuf_cli_delete },
{ "bind", 3, rtbuf_cli_bind },
{ "unbind", 1, rtbuf_cli_unbind },
{ "unbind", 2, rtbuf_cli_unbind },
{ "start", 0, rtbuf_cli_start },
{ "stop", 0, rtbuf_cli_stop },
{ "h", 0, rtbuf_cli_help },
{ "help", 0, rtbuf_cli_help },
{ "exit", 0, rtbuf_cli_exit },
{ 0, 0, 0 }
};
void debug_read (int argc, const char **argv, f_cli f)
{
if (argc < 1)
return;
printf("CLI READ %i %s(", argc, *argv);
argv++;
argc--;
while (1) {
if (argc < 1) {
printf("); %p\n", f);
return;
}
printf("%s", *argv);
if (argc > 1)
printf(", ");
argv++;
argc--;
}
}
int repl ()
{
s_cli cli;
cli_init(&cli);
cli_prompt(&cli, "rtbuf> ");
cli_functions(&cli, rtbuf_cli_functions);
while (1) {
if (cli_read(&cli) < 0)
return 0;
/* debug_read(cli.argc, cli.argv, cli.f); */
cli_eval(&cli);
}
return 0;
}
int main (int argc, char *argv[])
{
(void) argc;
(void) argv;
init_symbols();
rtbuf_lib_init();
return repl();
}
extern int rtbuf_err (const char *msg)
{
fprintf(stderr, "%s\n", msg);
return -1;
}