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