1 | #include "config.h" |
---|
2 | #include <kernel/mod2.h> |
---|
3 | #include <misc/auxiliary.h> |
---|
4 | |
---|
5 | #include "tok.h" |
---|
6 | #include "subexpr.h" |
---|
7 | #include "ipshell.h" |
---|
8 | |
---|
9 | #include "blackbox.h" |
---|
10 | |
---|
11 | #define MAX_BB_TYPES 256 |
---|
12 | // #define BLACKBOX_DEVEL 1 |
---|
13 | |
---|
14 | static blackbox* blackboxTable[MAX_BB_TYPES]; |
---|
15 | static char * blackboxName[MAX_BB_TYPES]; |
---|
16 | static int blackboxTableCnt=0; |
---|
17 | #define BLACKBOX_OFFSET (MAX_TOK+1) |
---|
18 | blackbox* getBlackboxStuff(const int t) |
---|
19 | { |
---|
20 | return (blackboxTable[t-BLACKBOX_OFFSET]); |
---|
21 | } |
---|
22 | |
---|
23 | |
---|
24 | void blackbox_default_destroy(blackbox *b, void *d) |
---|
25 | { |
---|
26 | Werror("missing blackbox_destroy"); |
---|
27 | } |
---|
28 | char *blackbox_default_String(blackbox *b,void *d) |
---|
29 | { |
---|
30 | Werror("missing blackbox_String"); |
---|
31 | return omStrDup(""); |
---|
32 | } |
---|
33 | void *blackbox_default_Copy(blackbox *b,void *d) |
---|
34 | { |
---|
35 | Werror("missing blackbox_Copy"); |
---|
36 | return NULL; |
---|
37 | } |
---|
38 | void blackbox_default_Print(blackbox *b,void *d) |
---|
39 | { |
---|
40 | char *s=b->blackbox_String(b,d); |
---|
41 | PrintS(s); |
---|
42 | omFree(s); |
---|
43 | } |
---|
44 | void *blackbox_default_Init(blackbox *b) |
---|
45 | { |
---|
46 | return NULL; |
---|
47 | } |
---|
48 | |
---|
49 | BOOLEAN blackbox_default_serialize(blackbox *b, void *d, si_link f) |
---|
50 | { |
---|
51 | return TRUE; |
---|
52 | } |
---|
53 | |
---|
54 | BOOLEAN blackbox_default_deserialize(blackbox **b, void **d, si_link f) |
---|
55 | { |
---|
56 | return TRUE; |
---|
57 | } |
---|
58 | |
---|
59 | // Tok2Cmdname -> iiTwoOps |
---|
60 | BOOLEAN WrongOp(const char* cmd, int op, leftv bb) |
---|
61 | { |
---|
62 | assume( bb->Typ() > MAX_TOK ); // it IS a blackbox type, right?! |
---|
63 | |
---|
64 | if( op > 127 ) |
---|
65 | Werror("'%s' of type %s(%d) for op %s(%d) not implemented", |
---|
66 | cmd, |
---|
67 | getBlackboxName(bb->Typ()),bb->Typ(), |
---|
68 | iiTwoOps(op), op); |
---|
69 | else |
---|
70 | Werror("'%s' of type %s(%d) for op '%c' not implemented", |
---|
71 | cmd, |
---|
72 | getBlackboxName(bb->Typ()), bb->Typ(), |
---|
73 | op); |
---|
74 | |
---|
75 | return TRUE; |
---|
76 | } |
---|
77 | |
---|
78 | BOOLEAN blackboxDefaultOp1(int op,leftv l, leftv r) |
---|
79 | { |
---|
80 | if (op==TYPEOF_CMD) |
---|
81 | { |
---|
82 | l->data=omStrDup(getBlackboxName(r->Typ())); |
---|
83 | l->rtyp=STRING_CMD; |
---|
84 | return FALSE; |
---|
85 | } |
---|
86 | |
---|
87 | return WrongOp("blackbox_Op1", op, r); |
---|
88 | } |
---|
89 | |
---|
90 | BOOLEAN blackboxDefaultOp2(int op,leftv l, leftv r1, leftv r2) |
---|
91 | { |
---|
92 | return WrongOp("blackbox_Op2", op, r1); |
---|
93 | } |
---|
94 | |
---|
95 | BOOLEAN blackbox_default_Op3(int op,leftv l, leftv r1,leftv r2, leftv r3) |
---|
96 | { |
---|
97 | return WrongOp("blackbox_Op3", op, r1); |
---|
98 | } |
---|
99 | |
---|
100 | BOOLEAN blackbox_default_OpM(int op,leftv res, leftv args) |
---|
101 | { |
---|
102 | if (op==LIST_CMD) |
---|
103 | { |
---|
104 | res->rtyp=LIST_CMD; |
---|
105 | return jjLIST_PL(res,args); |
---|
106 | } |
---|
107 | return WrongOp("blackbox_OpM", op, args); |
---|
108 | } |
---|
109 | |
---|
110 | BOOLEAN blackbox_default_Check(blackbox *b, void *d) |
---|
111 | { |
---|
112 | return FALSE; |
---|
113 | } |
---|
114 | int setBlackboxStuff(blackbox *bb, const char *n) |
---|
115 | { |
---|
116 | int where=-1; |
---|
117 | if (MAX_BB_TYPES<=blackboxTableCnt) |
---|
118 | { |
---|
119 | // second try, find empty slot from removed bb: |
---|
120 | for (int i=0;i<MAX_BB_TYPES;i++) |
---|
121 | { |
---|
122 | if (blackboxTable[i]==NULL) { where=i; break; } |
---|
123 | } |
---|
124 | } |
---|
125 | else |
---|
126 | { |
---|
127 | where=blackboxTableCnt; |
---|
128 | blackboxTableCnt++; |
---|
129 | } |
---|
130 | if (where==-1) |
---|
131 | { |
---|
132 | WerrorS("too many bb types defined"); |
---|
133 | return 0; |
---|
134 | } |
---|
135 | else |
---|
136 | { |
---|
137 | blackboxTable[where]=bb; |
---|
138 | blackboxName[where]=omStrDup(n); |
---|
139 | #ifdef BLACKBOX_DEVEL |
---|
140 | Print("setBlackboxStuff: define bb:name=%s:rt=%d (table:cnt=%d)\n",blackboxName[where],where+BLACKBOX_OFFSET,where); |
---|
141 | #endif |
---|
142 | if (bb->blackbox_destroy==NULL) bb->blackbox_destroy=blackbox_default_destroy; |
---|
143 | if (bb->blackbox_String==NULL) bb->blackbox_String=blackbox_default_String; |
---|
144 | if (bb->blackbox_Print==NULL) bb->blackbox_Print=blackbox_default_Print; |
---|
145 | if (bb->blackbox_Init==NULL) bb->blackbox_Init=blackbox_default_Init; |
---|
146 | if (bb->blackbox_Copy==NULL) bb->blackbox_Copy=blackbox_default_Copy; |
---|
147 | if (bb->blackbox_Op1==NULL) bb->blackbox_Op1=blackboxDefaultOp1; |
---|
148 | if (bb->blackbox_Op2==NULL) bb->blackbox_Op2=blackboxDefaultOp2; |
---|
149 | if (bb->blackbox_Op3==NULL) bb->blackbox_Op3=blackbox_default_Op3; |
---|
150 | if (bb->blackbox_OpM==NULL) bb->blackbox_OpM=blackbox_default_OpM; |
---|
151 | if (bb->blackbox_Check==NULL) bb->blackbox_Check=blackbox_default_Check; |
---|
152 | if (bb->blackbox_serialize==NULL) bb->blackbox_serialize=blackbox_default_serialize; |
---|
153 | if (bb->blackbox_deserialize==NULL) bb->blackbox_deserialize=blackbox_default_deserialize; |
---|
154 | return where+BLACKBOX_OFFSET; |
---|
155 | } |
---|
156 | } |
---|
157 | |
---|
158 | void removeBlackboxStuff(const int rt) |
---|
159 | { |
---|
160 | omfree(blackboxTable[rt-BLACKBOX_OFFSET]); |
---|
161 | omfree(blackboxName[rt-BLACKBOX_OFFSET]); |
---|
162 | blackboxTable[rt-BLACKBOX_OFFSET]=NULL; |
---|
163 | blackboxName[rt-BLACKBOX_OFFSET]=NULL; |
---|
164 | } |
---|
165 | const char *getBlackboxName(const int t) |
---|
166 | { |
---|
167 | char *b=blackboxName[t-BLACKBOX_OFFSET]; |
---|
168 | if (b!=NULL) return b; |
---|
169 | else return ""; |
---|
170 | } |
---|
171 | int blackboxIsCmd(const char *n, int & tok) |
---|
172 | { |
---|
173 | for(int i=blackboxTableCnt-1;i>=0;i--) |
---|
174 | { |
---|
175 | if(strcmp(n,blackboxName[i])==0) |
---|
176 | { |
---|
177 | #ifdef BLACKBOX_DEVEL |
---|
178 | Print("blackboxIsCmd: found bb:%s:%d (table:%d)\n",n,i+BLACKBOX_OFFSET,i); |
---|
179 | #endif |
---|
180 | tok=i+BLACKBOX_OFFSET; |
---|
181 | return ROOT_DECL; |
---|
182 | } |
---|
183 | } |
---|
184 | return 0; |
---|
185 | } |
---|
186 | |
---|
187 | void printBlackboxTypes() |
---|
188 | { |
---|
189 | for(int i=blackboxTableCnt-1;i>=0;i--) |
---|
190 | { |
---|
191 | if (blackboxName[i]!=NULL) |
---|
192 | Print("type %d: %s\n",i,blackboxName[i]); |
---|
193 | } |
---|
194 | } |
---|