chiark / gitweb /
(dstr_vputf): Don't try calling @va_arg@ on things @char@-sized.
[mLib] / sym-test.c
CommitLineData
cd300147 1#include <stdio.h>
2#include <stdlib.h>
3#include <string.h>
4
5#include "sub.h"
6#include "sym.h"
7
8typedef struct word {
9 sym_base _b;
10 int i;
11} word;
12
13static int cmp(const void *a, const void *b)
14{
15 const word *const *v = b;
16 const word *const *w = a;
17 return (strcmp(SYM_NAME(*w), SYM_NAME(*v)));
18}
19
20int main(void)
21{
22 char buf[256];
23 char *p;
24 sym_table t;
25 size_t n = 0;
26
27 sub_init();
28 sym_create(&t);
29
30 while (fgets(buf, sizeof(buf), stdin)) {
31 buf[strlen(buf) - 1] = 0;
32/* printf("# %s\n", buf); */
33 p = strtok(buf, " ");
34
35 if (strcmp(p, "set") == 0) {
36 char *k = strtok(0, " ");
37 int i = atoi(strtok(0, " "));
38 unsigned f;
39 word *w = sym_find(&t, k, -1, sizeof(word), &f);
40 w->i = i;
41 if (!f)
42 n++;
43 } else if (strcmp(p, "get") == 0) {
44 char *k = strtok(0, " ");
45 word *w = sym_find(&t, k, -1, 0, 0);
46 if (w)
47 printf("%i\n", w->i);
48 else
49 puts("*MISSING*");
50 } else if (strcmp(p, "del") == 0) {
51 char *k = strtok(0, " ");
52 word *w = sym_find(&t, k, -1, 0, 0);
53 if (w) {
54 sym_remove(&t, w);
55 n--;
56 } else
57 puts("*MISSING*");
58 } else if (strcmp(p, "count") == 0) {
59 printf("%i\n", n);
60 } else if (strcmp(p, "show") == 0) {
61 sym_iter i;
62 word *w;
63 word **v, **vv;
64
65 if (!n)
66 puts("*EMPTY*");
67 else {
68 v = malloc(n * sizeof(*v));
69 if (!v) {
70 puts("*NOMEM*");
71 continue;
72 }
73 for (vv = v, sym_mkiter(&i, &t); (w = sym_next(&i)) != 0; vv++)
74 *vv = w;
75 qsort(v, n, sizeof(*v), cmp);
76 printf("%s:%i", SYM_NAME(*v), (*v)->i);
77 for (vv = v + 1; --n; vv++)
78 printf(" %s:%i", SYM_NAME(*vv), (*vv)->i);
79 free(v);
80 putchar('\n');
81 }
82 } else
83 puts("*BAD*");
84 }
85
86 sym_destroy(&t);
87 return (0);
88}