Commit 3bbac5982e2228b061ebb2d941598be69f40ba0d

Thomas de Grivel 2018-07-26T12:17:27

rtbuf_lib_fun -> rtbuf_fun

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
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
diff --git a/build/rtbuf/Makefile b/build/rtbuf/Makefile
index cb6a229..7c57a76 100644
--- a/build/rtbuf/Makefile
+++ b/build/rtbuf/Makefile
@@ -8,7 +8,7 @@ CFLAGS = -O0 -ggdb -W -Wall -Werror
 LDFLAGS = -L${SRCDIR}/../cli/build/so
 LIBS = -lcli -lpthread
 HEADERS = rtbuf.h rtbuf_lib.h symbol.h
-OBJECTS = rtbuf.o rtbuf_lib.o rtbuf_cli.o symbol.o
+OBJECTS = rtbuf.o rtbuf_lib.o rtbuf_cli.o rtbuf_fun.o symbol.o
 
 SRC = ${HEADERS} ${OBJECTS:%.o=%.c}
 
diff --git a/rtbuf.c b/rtbuf.c
index cec4ccc..f56e440 100644
--- a/rtbuf.c
+++ b/rtbuf.c
@@ -4,6 +4,8 @@
 #include <stdlib.h>
 #include <strings.h>
 #include "rtbuf.h"
+#include "rtbuf_lib.h"
+#include "symbol.h"
 
 s_rtbuf      g_rtbuf[RTBUF_MAX];
 unsigned int g_rtbuf_max = RTBUF_MAX;
@@ -14,6 +16,7 @@ int          g_rtbuf_sorted[RTBUF_MAX];
 unsigned int g_rtbuf_sorted_n = 0;
 
 void print_rtbuf (unsigned int i);
+void print_rtbuf_fun (s_rtbuf_fun *fun);
 
 int rtbuf_init ()
 {
@@ -44,10 +47,10 @@ int rtbuf_new (s_rtbuf_fun *rf)
   assert(rf);
   if ((rtb = rtbuf_next()) < 0)
     return -1;
-  g_rtbuf[rtb].data = calloc(rf->spec.nmemb, rf->spec.size);
+  g_rtbuf[rtb].data = calloc(rf->nmemb, rf->size);
   g_rtbuf[rtb].fun = rf;
-  g_rtbuf[rtb].var = calloc(rf->spec.nvar, sizeof(int));
-  while (j < rf->spec.nvar)
+  g_rtbuf[rtb].var = calloc(rf->var_n, sizeof(int));
+  while (j < rf->var_n)
     g_rtbuf[rtb].var[j++] = -1;
   g_rtbuf_sort = 1;
   return rtb;
@@ -73,7 +76,7 @@ void rtbuf_var_bind (s_rtbuf *rtb, unsigned int i, int target)
 
 void rtbuf_unbind (s_rtbuf *rtb)
 {
-  unsigned int i = rtb->fun->spec.nvar;
+  unsigned int i = rtb->fun->var_n;
   while (i--)
     rtbuf_var_unbind(rtb, i);
 }
@@ -100,7 +103,7 @@ typedef struct rtbuf_var_stack {
 
 void rtbuf_var_stack_init (s_rtbuf_var_stack *rvs)
 {
-  printf("rtbuf_var_stack_init\n");
+  //printf("rtbuf_var_stack_init\n");
   bzero(rvs, sizeof(s_rtbuf_var_stack));
 }
 
@@ -108,7 +111,7 @@ s_rtbuf_var_ptr * rtbuf_var_stack_push (s_rtbuf_var_stack *rvs,
                                         unsigned int rtb,
                                         unsigned int var)
 {
-  printf("rtbuf_var_stack_push %i %i\n", rtb, var);
+  //printf("rtbuf_var_stack_push %i %i\n", rtb, var);
   s_rtbuf_var_ptr *top;
   if (rvs->size >= RTBUF_MAX)
     return 0;
@@ -121,7 +124,7 @@ s_rtbuf_var_ptr * rtbuf_var_stack_push (s_rtbuf_var_stack *rvs,
 
 s_rtbuf_var_ptr * rtbuf_var_stack_pop (s_rtbuf_var_stack *rvs)
 {
-  printf("rtbuf_var_stack_pop\n");
+  //printf("rtbuf_var_stack_pop\n");
   if (rvs->size <= 0)
     return 0;
   rvs->size--;
@@ -131,13 +134,13 @@ s_rtbuf_var_ptr * rtbuf_var_stack_pop (s_rtbuf_var_stack *rvs)
 s_rtbuf_var_ptr * rtbuf_var_stack_top (s_rtbuf_var_stack *rvs)
 {
   s_rtbuf_var_ptr *top;
-  printf("rtbuf_var_stack_top\n");
+  //printf("rtbuf_var_stack_top\n");
   if (rvs->size <= 0) {
-    printf(" rtbuf_var_stack_top => 0\n");
+    //printf(" rtbuf_var_stack_top => 0\n");
     return 0;
   }
   top = &rvs->st[rvs->size - 1];
-  printf(" rtbuf_var_stack_top => { %u, %u }\n", top->rtb, top->var);
+  //printf(" rtbuf_var_stack_top => { %u, %u }\n", top->rtb, top->var);
   return top;
 }
 
@@ -147,9 +150,9 @@ void rtbuf_find_roots (s_rtbuf_var_stack *rvs)
   unsigned int i = 0;
   unsigned int n = g_rtbuf_n;
   unsigned int c = 0;
-  printf("rtbuf_find_roots\n");
+  //printf("rtbuf_find_roots\n");
   while (i < RTBUF_MAX && n > 0) {
-    printf(" rtbuf_find_roots %u %u\n", i, n);
+    //printf(" rtbuf_find_roots %u %u\n", i, n);
     if (rtb->data) {
       if (rtb->flags & RTBUF_DELETE)
         rtbuf_delete(rtb);
@@ -164,7 +167,7 @@ void rtbuf_find_roots (s_rtbuf_var_stack *rvs)
     rtb++;
     i++;
   }
-  printf(" rtbuf_find_roots => %u\n", c);
+  //printf(" rtbuf_find_roots => %u\n", c);
 }
 
 void rtbuf_sort_push_child (s_rtbuf_var_stack *rvs,
@@ -173,7 +176,7 @@ void rtbuf_sort_push_child (s_rtbuf_var_stack *rvs,
   int rtb;
   unsigned int i = 0;
   int found = 0;
-  printf("rtbuf_sort_push_child { %u, %u }\n", ptr->rtb, ptr->var);
+  //printf("rtbuf_sort_push_child { %u, %u }\n", ptr->rtb, ptr->var);
   rtb = g_rtbuf[ptr->rtb].var[ptr->var];
   ptr->var++;
   if (rtb >= 0) {
@@ -216,7 +219,7 @@ void rtbuf_sort ()
   rtbuf_find_roots(&rvs);
   g_rtbuf_sorted_n = 0;
   while ((ptr = rtbuf_var_stack_top(&rvs))) {
-    if (ptr->var == g_rtbuf[ptr->rtb].fun->spec.nvar) {
+    if (ptr->var == g_rtbuf[ptr->rtb].fun->var_n) {
       g_rtbuf_sorted[g_rtbuf_sorted_n++] = ptr->rtb;
       rtbuf_var_stack_pop(&rvs);
     } else
@@ -234,8 +237,14 @@ void rtbuf_start ()
     rtbuf_sort();
   while (i < g_rtbuf_sorted_n) {
     s_rtbuf *rtb = &g_rtbuf[g_rtbuf_sorted[i]];
-    if (rtb->fun->start)
+    if (rtb->fun->start) {
+      printf(" start ");
+      print_rtbuf(g_rtbuf_sorted[i]);
+      printf(" ");
+      print_rtbuf_fun(rtb->fun);
+      printf("\n");
       rtb->fun->start(rtb);
+    }
     i++;
   }
 }
diff --git a/rtbuf.h b/rtbuf.h
index 059c581..890fc9a 100644
--- a/rtbuf.h
+++ b/rtbuf.h
@@ -1,25 +1,34 @@
 #ifndef RTBUF_H
 #define RTBUF_H
 
-typedef struct rtbuf_spec
-{
-  unsigned int nmemb; /* number of members in buffer data */
-  unsigned int size;  /* size of members in bytes in buffer data */
-  unsigned int nvar;  /* number of rtbuf variables */
-} s_rtbuf_spec;
-
 typedef struct rtbuf s_rtbuf;
 
 typedef int f_rtbuf_fun (s_rtbuf *rtbuf);
 
 typedef struct rtbuf_fun
 {
+  const char *name;    /* symbol */
   f_rtbuf_fun *f;
   f_rtbuf_fun *start;
   f_rtbuf_fun *stop;
-  s_rtbuf_spec spec;
+  unsigned int nmemb;  /* number of members in buffer data */
+  unsigned int size;   /* size of members in bytes in buffer data */
+  const char **var;
+  unsigned int var_n;  /* number of rtbuf variables */
+  struct rtbuf_lib *lib;
+  unsigned int lib_fun;
 } s_rtbuf_fun;
 
+#define RTBUF_FUN_MAX 32768
+s_rtbuf_fun g_rtbuf_fun[RTBUF_FUN_MAX];
+unsigned int g_rtbuf_fun_n;
+
+typedef struct rtbuf_lib_fun s_rtbuf_lib_fun;
+
+s_rtbuf_fun * find_rtbuf_fun (const char *x);
+s_rtbuf_fun * new_rtbuf_fun (s_rtbuf_lib_fun *x);
+void delete_rtbuf_fun (s_rtbuf_fun *fun);
+
 #define RTBUF_SORT   0x0001
 #define RTBUF_DELETE 0x0002
 
diff --git a/rtbuf_cli.c b/rtbuf_cli.c
index db9e3dd..c8b45e3 100644
--- a/rtbuf_cli.c
+++ b/rtbuf_cli.c
@@ -5,13 +5,24 @@
 #include <stdio.h>
 #include <cli.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].path);
+  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)
@@ -20,14 +31,13 @@ void print_lib_long (unsigned int i)
   unsigned int j = 0;
   assert(i < RTBUF_LIB_MAX);
   lib = &g_rtbuf_lib[i];
-  printf("#<lib %i \"%s\"\n", i, lib->path);
+  printf("#<lib %i %s", i, lib->name);
   while (j < lib->fun_n) {
-    s_rtbuf_fun *fun = &lib->fun[j];
-    printf("  #<fun %i %i %i %i>\n", j, fun->spec.nmemb, fun->spec.size,
-           fun->spec.nvar);
+    printf("\n  ");
+    print_rtbuf_fun(lib->fun[j]);
     j++;
   }
-  printf("  >\n");
+  printf(">\n");
   fflush(stdout);
 }
 
@@ -50,9 +60,9 @@ void print_rtbuf_long (unsigned int i)
     printf(" %x", rtb->flags);
     printf(" %d", rtb->refc);
   }
-  while (j < rtb->fun->spec.nvar) {
+  while (j < rtb->fun->var_n) {
     if (rtb->var[j] >= 0) {
-      printf("\n  var[%i] = ", j);
+      printf("\n  %i %s = ", j, rtb->fun->var[j]);
       print_rtbuf(rtb->var[j]);
     }
     j++;
@@ -80,15 +90,15 @@ int rtbuf_cli_libs (int argc, const char *argv[])
 
 int rtbuf_cli_lib (int argc, const char *argv[])
 {
-  unsigned int i;
-  assert(argc == 1);
-  i = atoi(argv[1]);
-  if (i >= RTBUF_LIB_MAX) {
-    printf("library not found\n");
-    return -1;
+  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;
   }
-  print_lib_long(i);
-  return 0;
+  return rtbuf_err("library not found");
 }
 
 int rtbuf_cli_load (int argc, const char *argv[])
@@ -146,7 +156,7 @@ int rtbuf_cli_new (int argc, const char *argv[])
     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)
+  if ((rtb = rtbuf_new(g_rtbuf_lib[rl].fun[rf])) < 0)
     return rtbuf_err("buffer not created");
   print_rtbuf(rtb);
   printf("\n");
@@ -177,7 +187,7 @@ int rtbuf_cli_bind (int argc, const char *argv[])
   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->spec.nvar)
+  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");
@@ -195,7 +205,7 @@ int rtbuf_cli_unbind (int argc, const char *argv[])
     return rtbuf_err("buffer not found");
   if (argc == 2) {
     int var = atoi(argv[2]);
-    if (var < 0 || (unsigned int) var >= g_rtbuf[rtb].fun->spec.nvar)
+    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);
   }
@@ -327,11 +337,12 @@ int main (int argc, char *argv[])
 {
   (void) argc;
   (void) argv;
+  init_symbols();
   rtbuf_lib_init();
   return repl();
 }
 
-int rtbuf_err (const char *msg)
+extern int rtbuf_err (const char *msg)
 {
   fprintf(stderr, "%s\n", msg);
   return -1;
diff --git a/rtbuf_fun.c b/rtbuf_fun.c
new file mode 100644
index 0000000..fcf2bc0
--- /dev/null
+++ b/rtbuf_fun.c
@@ -0,0 +1,82 @@
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <strings.h>
+#include "rtbuf.h"
+#include "rtbuf_lib.h"
+#include "symbol.h"
+
+s_rtbuf_fun g_rtbuf_fun[RTBUF_FUN_MAX];
+unsigned int g_rtbuf_fun_n = 0;
+
+int rtbuf_fun_p (s_rtbuf_fun *fun)
+{
+  return fun && fun->name;
+}
+
+s_rtbuf_fun * find_rtbuf_fun (const char *x)
+{
+  const char *sym = find_symbol(x);
+  if (sym) {
+    unsigned int i = 0;
+    unsigned int n = g_rtbuf_fun_n;
+    while (i < RTBUF_FUN_MAX && n > 0) {
+      if (rtbuf_fun_p(&g_rtbuf_fun[i]) &&
+          sym == g_rtbuf_fun[i].name)
+        return &g_rtbuf_fun[i];
+      i++;
+    }
+  }
+  return 0;
+}
+
+void init_rtbuf_fun (s_rtbuf_fun *fun, s_rtbuf_lib_fun *x)
+{
+  const char **var = x->var;
+  unsigned int j = 0;
+  fun->name = intern(x->name);
+  fun->f = x->f;
+  fun->start = x->start;
+  fun->stop = x->stop;
+  fun->nmemb = x->nmemb;
+  fun->size = x->size;
+  fun->var_n = 0;
+  if (var)
+    while (*var) {
+      fun->var_n++;
+      var++;
+    }
+  fun->var = malloc(sizeof(const char *) * (fun->var_n + 1));
+  if ((var = x->var))
+    while (*var)
+      fun->var[j++] = intern(*var++);
+  fun->var[j] = 0;
+}
+
+s_rtbuf_fun * new_rtbuf_fun (s_rtbuf_lib_fun *x)
+{
+  unsigned int i = 0;
+  if (g_rtbuf_fun_n == RTBUF_FUN_MAX) {
+    fprintf(stderr, "RTBUF_FUN_MAX exceeded\n");
+    return 0;
+  }
+  while (i < RTBUF_FUN_MAX) {
+    if (!rtbuf_fun_p(&g_rtbuf_fun[i])) {
+      init_rtbuf_fun(&g_rtbuf_fun[i], x);
+      g_rtbuf_fun_n++;
+      return &g_rtbuf_fun[i];
+    }
+    i++;
+  }
+  return 0;
+}
+
+void delete_rtbuf_fun (s_rtbuf_fun *fun)
+{
+  if (fun && fun->var) {
+    free(fun->var);
+    bzero(fun, sizeof(s_rtbuf_fun));
+    g_rtbuf_fun_n--;
+  }
+  fun->name = 0;
+}
diff --git a/rtbuf_lib.c b/rtbuf_lib.c
index 64feabc..4450118 100644
--- a/rtbuf_lib.c
+++ b/rtbuf_lib.c
@@ -6,6 +6,7 @@
 #include <string.h>
 #include <strings.h>
 #include "rtbuf_lib.h"
+#include "symbol.h"
 
 s_rtbuf_lib g_rtbuf_lib[RTBUF_LIB_MAX];
 unsigned int g_rtbuf_lib_n = 0;
@@ -32,22 +33,50 @@ void rtbuf_lib_init ()
   *out = 0;
 }
 
+int rtbuf_lib_p (s_rtbuf_lib *lib)
+{
+  return lib->lib ? 1 : 0;
+}
+
 int rtbuf_lib_find (const char *str)
 {
-  int i = atoi(str);
-  if (0 <= i && i < RTBUF_LIB_MAX) {
-    s_rtbuf_lib *lib = &g_rtbuf_lib[i];
-    if (lib->path[0] && lib->lib && lib->fun)
+  const char *sym;
+  if ('0' <= str[0] && str[0] <= '9') {
+    int i = atoi(str);
+    if (0 <= i && i < RTBUF_LIB_MAX && rtbuf_lib_p(&g_rtbuf_lib[i]))
       return i;
   }
+  if ((sym = find_symbol(str))) {
+    unsigned int i = 0;
+    unsigned int n = g_rtbuf_lib_n;
+    while (i < RTBUF_LIB_MAX && n > 0) {
+      if (rtbuf_lib_p(&g_rtbuf_lib[i])) {
+        if (sym == g_rtbuf_lib[i].name)
+          return i;
+        n--;
+      }
+      i++;
+    }
+  }
   return -1;
 }
 
 int rtbuf_lib_find_fun (s_rtbuf_lib *rl, const char *str)
 {
-  int i = atoi(str);
-  if (0 <= i && (unsigned) i < rl->fun_n)
-    return i;
+  const char *sym;
+  if ('0' <= str[0] && str[0] <= '9') {
+    int i = atoi(str);
+    if (0 <= i && (unsigned) i < rl->fun_n)
+      return i;
+  }
+  if ((sym = find_symbol(str))) {
+    unsigned int i = 0;
+    while (i < rl->fun_n) {
+      if (sym == rl->fun[i]->name)
+        return i;
+      i++;
+    }
+  }
   return -1;
 }
 
@@ -56,7 +85,7 @@ s_rtbuf_lib * rtbuf_lib_new ()
   unsigned int i = 0;
   s_rtbuf_lib *lib = g_rtbuf_lib;
   while (i < RTBUF_LIB_MAX) {
-    if (lib->path[0] == 0) {
+    if (lib->path == 0) {
       g_rtbuf_lib_n++;
       return lib;
     }
@@ -79,31 +108,32 @@ void rtbuf_lib_load_path (s_rtbuf_lib *lib, const char *name)
   lib->lib = 0;
   while (*path && !lib->lib) {
     const char *in = *path++;
-    char *out = lib->path;
+    lib->path = &g_string[g_string_n];
     while (*in)
-      *out++ = *in++;
+      g_string[g_string_n++] = *in++;
     in = name;
     while (*in)
-      *out++ = *in++;
+      g_string[g_string_n++] = *in++;
     in = ".so";
     while (*in)
-      *out++ = *in++;
-    *out = 0;
+      g_string[g_string_n++] = *in++;
+    g_string[g_string_n++] = 0;
     printf("lib_load path \"%s\"\n", lib->path);
     lib->lib = dlopen(lib->path, 0);
-    printf("lib_load lib %p\n", lib->lib);
   }
 }
 
-int rtbuf_fun_p (s_rtbuf_fun *fun)
+int rtbuf_lib_fun_p (s_rtbuf_lib_fun *fun)
 {
-  return fun->f || fun->start || fun->stop;
+  return fun->name || fun->f || fun->start || fun->stop;
 }
 
 s_rtbuf_lib * rtbuf_lib_load (const char *name)
 {
   s_rtbuf_lib *lib = rtbuf_lib_new();
+  s_rtbuf_lib_fun *fun;
   unsigned long *ver;
+  unsigned int i = 0;
   if (!lib)
     return 0;
   rtbuf_lib_load_path(lib, name);
@@ -114,10 +144,26 @@ s_rtbuf_lib * rtbuf_lib_load (const char *name)
   ver = dlsym(lib->lib, "rtbuf_lib_ver");
   printf("lib_load ver %lu\n", *ver);
   assert(*ver == RTBUF_LIB_VER);
-  lib->fun = dlsym(lib->lib, "rtbuf_lib_fun");
+  lib->name = intern(name);
+  printf("lib_load name %s\n", lib->name);
+  fun = dlsym(lib->lib, "rtbuf_lib_fun");
   lib->fun_n = 0;
   while (lib->fun_n < RTBUF_LIB_MAX &&
-         rtbuf_fun_p(&lib->fun[lib->fun_n]))
+         rtbuf_lib_fun_p(&fun[lib->fun_n])) {
+    if (fun[lib->fun_n].name == 0) {
+      Dl_info info;
+      dladdr(fun[lib->fun_n].f, &info);
+      fun[lib->fun_n].name = intern(info.dli_sname);
+    }
     lib->fun_n++;
+  }
+  lib->fun = malloc(sizeof(s_rtbuf_fun*) * (lib->fun_n + 1));
+  while (i < lib->fun_n) {
+    lib->fun[i] = new_rtbuf_fun(&fun[i]);
+    lib->fun[i]->lib = lib;
+    lib->fun[i]->lib_fun = i;
+    i++;
+  }
+  lib->fun[i] = 0;
   return lib;
 }
diff --git a/rtbuf_lib.h b/rtbuf_lib.h
index 3a84869..8f89382 100644
--- a/rtbuf_lib.h
+++ b/rtbuf_lib.h
@@ -6,12 +6,22 @@
 #define RTBUF_LIB_MAX 1000
 #define RTBUF_LIB_VER 0x00010001
 
+typedef struct rtbuf_lib_fun {
+  const char *name;
+  f_rtbuf_fun *f;
+  f_rtbuf_fun *start;
+  f_rtbuf_fun *stop;
+  unsigned int nmemb; /* number of members in buffer data */
+  unsigned int size;  /* size of members in bytes in buffer data */
+  const char **var;  /* names of variables, end with NULL */
+} s_rtbuf_lib_fun;
+
 typedef struct rtbuf_lib {
-  s_rtbuf_fun *fun;
+  const char *name;
+  s_rtbuf_fun **fun;
   unsigned int fun_n;
   void *lib;
-  char path[1024];
-  char *name;
+  const char *path;
 } s_rtbuf_lib;
 
 extern s_rtbuf_lib  g_rtbuf_lib[];
diff --git a/rtbuf_signal.c b/rtbuf_signal.c
index 4ffd8aa..3a56bd7 100644
--- a/rtbuf_signal.c
+++ b/rtbuf_signal.c
@@ -16,16 +16,18 @@ typedef struct sinus_data {
   double phase;
 } s_sinus_data;
 
-static int sinus (s_rtbuf *rtb);
-static int sinus_start (s_rtbuf *rtb);
-
-unsigned long rtbuf_lib_ver = RTBUF_LIB_VER;
-s_rtbuf_fun rtbuf_lib_fun[] = {
-  { sinus, sinus_start, 0,
-    { sizeof(s_sinus_data) / sizeof(double),
-      sizeof(double),
-      SINUS_ARITY } },
-  { 0, 0, 0, { 0, 0, 0 } }
+extern int sinus (s_rtbuf *rtb);
+extern int sinus_start (s_rtbuf *rtb);
+
+const char *sinus_vars[] = { "frequency", "amplitude", 0 };
+
+const char     *rtbuf_lib_name = "signal";
+unsigned long   rtbuf_lib_ver = RTBUF_LIB_VER;
+s_rtbuf_lib_fun rtbuf_lib_fun[] = {
+  { "sinus", sinus, sinus_start, 0,
+    sizeof(s_sinus_data) / sizeof(double),
+    sizeof(double), sinus_vars },
+  { 0, 0, 0, 0, 0, 0, 0 }
 };
 
 int sinus (s_rtbuf *rtb)
@@ -37,7 +39,7 @@ int sinus (s_rtbuf *rtb)
   double *freq_samples = freq < 0 ? 0 : (double*) g_rtbuf[freq].data;
   phase = data->phase;
   while (i < RTBUF_SIGNAL_SAMPLES) {
-    double f = freq ? freq_samples[i] : 220;
+    double f = freq_samples ? freq_samples[i] : 220;
     //printf(" i=%u freq=%f", i, f);
     f /= (double) RTBUF_SIGNAL_SAMPLERATE;
     phase = phase + 2.0 * M_PI * f;
diff --git a/rtbuf_sndio.c b/rtbuf_sndio.c
index 011fbbb..2f04fbf 100644
--- a/rtbuf_sndio.c
+++ b/rtbuf_sndio.c
@@ -31,15 +31,20 @@ typedef struct sndio_output_data {
 static int sndio_output (s_rtbuf *rtb);
 static int sndio_output_start (s_rtbuf *rtb);
 static int sndio_output_stop (s_rtbuf *rtb);
-
-unsigned long rtbuf_lib_ver = RTBUF_LIB_VER;
-s_rtbuf_fun rtbuf_lib_fun[] = {
-  { sndio_input, sndio_input_start, sndio_input_stop,
-    { sizeof(s_sndio_input_data) / sizeof(double), sizeof(double),
-      0 } },
-  { sndio_output, sndio_output_start, sndio_output_stop,
-    { 1, sizeof(s_sndio_output_data), RTBUF_SNDIO_CHANNELS } },
-  { 0, 0, 0, { 0, 0, 0 } }
+static const char *sndio_output_vars[] = { "left", "right", 0 };
+
+extern const char     *rtbuf_lib_name;
+extern unsigned long   rtbuf_lib_ver;
+extern s_rtbuf_lib_fun rtbuf_lib_fun[];
+
+const char     *rtbuf_lib_name = "sndio";
+unsigned long   rtbuf_lib_ver = RTBUF_LIB_VER;
+s_rtbuf_lib_fun rtbuf_lib_fun[] = {
+  { "input", sndio_input, sndio_input_start, sndio_input_stop,
+    sizeof(s_sndio_input_data) / sizeof(double), sizeof(double), 0 },
+  { "output", sndio_output, sndio_output_start, sndio_output_stop,
+    1, sizeof(s_sndio_output_data), sndio_output_vars },
+  { 0, 0, 0, 0, 0, 0, 0 }
 };
 
 int sndio_input (s_rtbuf *rtb)
@@ -170,9 +175,10 @@ int sndio_output (s_rtbuf *rtb)
   //printf("\n");
   return 0;
 }
-
+/*
 int rtbuf_err (const char *msg)
 {
   fprintf(stderr, "%s\n", msg);
   return -1;
 }
+*/
diff --git a/symbol.c b/symbol.c
index 0f22667..d50b6fb 100644
--- a/symbol.c
+++ b/symbol.c
@@ -4,50 +4,63 @@
 #include <strings.h>
 #include "symbol.h"
 
-s_symbol g_symbols[SYMBOL_MAX];
+char         g_string[STRING_MAX];
+unsigned int g_string_n = 0;
+
+const char  *g_symbols[SYMBOL_MAX];
 unsigned int g_symbols_n = 0;
 
 void init_symbols () {
+  bzero(g_string, sizeof(g_string));
   bzero(g_symbols, sizeof(g_symbols));
 }
 
-int find_symbol (const char *name)
+symbol find_symbol (const char *name)
 {
   unsigned int i = 0;
   unsigned int n = g_symbols_n;
   while (i < SYMBOL_MAX && n > 0) {
-    if (g_symbols[i].name) {
-      if (strcmp(name, g_symbols[i].name) == 0)
-        return i;
+    if (g_symbols[i]) {
+      if (name == g_symbols[i] ||
+          strcmp(name, g_symbols[i]) == 0)
+        return g_symbols[i];
       n--;
     }
     i++;
   }
-  return -1;
+  return 0;
 }
 
-int make_symbol (const char *name)
+symbol make_symbol (const char *name)
 {
   unsigned int i = 0;
   if (g_symbols_n == SYMBOL_MAX) {
     fprintf(stderr, "maximum number of symbols exceeded\n");
-    return -1;
+    return 0;
   }
   while (i < SYMBOL_MAX) {
-    if (g_symbols[i].name == 0) {
-      g_symbols[i].name = name;
+    if (g_symbols[i] == 0) {
+      const char *in = name;
+      char *out = &g_string[g_string_n];
+      g_symbols[i] = out;
+      while (*in) {
+        *out++ = *in++;
+        g_string_n++;
+      }
+      *out = 0;
+      g_string_n++;
       g_symbols_n++;
-      return i;
+      return g_symbols[i];
     }
     i++;
   }
-  return -1;
+  return 0;
 }
 
-int intern (const char *name)
+const char * intern (const char *name)
 {
-  int i = find_symbol(name);
-  if (i < 0)
-    i = make_symbol(name);
-  return i;
+  const char *sym = find_symbol(name);
+  if (sym == 0)
+    sym = make_symbol(name);
+  return sym;
 }
diff --git a/symbol.h b/symbol.h
index 98e16d7..f2bf1ae 100644
--- a/symbol.h
+++ b/symbol.h
@@ -1,17 +1,19 @@
 #ifndef SYMBOL_H
 #define SYMBOL_H
 
-typedef struct symbol
-{
-  const char *name;
-} s_symbol;
+typedef const char *symbol;
+
+#define STRING_MAX 65536
+extern char         g_string[STRING_MAX];
+extern unsigned int g_string_n;
 
 #define SYMBOL_MAX 32768
-s_symbol g_symbols[SYMBOL_MAX];
-unsigned int g_symbols_n;
+extern symbol       g_symbols[SYMBOL_MAX];
+extern unsigned int g_symbols_n;
 
-int find_symbol (const char *name);
-int make_symbol (const char *name);
-int intern (const char *name);
+void init_symbols ();
+symbol find_symbol (symbol name);
+symbol make_symbol (symbol name);
+symbol intern (symbol name);
 
 #endif