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 | WerrorS("missing blackbox_destroy"); |
---|
27 | } |
---|
28 | char *blackbox_default_String(blackbox */*b*/,void */*d*/) |
---|
29 | { |
---|
30 | WerrorS("missing blackbox_String"); |
---|
31 | return omStrDup(""); |
---|
32 | } |
---|
33 | void *blackbox_default_Copy(blackbox */*b*/,void */*d*/) |
---|
34 | { |
---|
35 | WerrorS("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 | else if(op==STRING_CMD) |
---|
108 | { |
---|
109 | blackbox *b=getBlackboxStuff(args->Typ()); |
---|
110 | res->data=b->blackbox_String(b,args->Data()); |
---|
111 | res->rtyp=STRING_CMD; |
---|
112 | args=args->next; |
---|
113 | if(args!=NULL) |
---|
114 | { |
---|
115 | sleftv res2; |
---|
116 | int ret=iiExprArithM(&res2,args,op); |
---|
117 | if (ret) return TRUE; |
---|
118 | char *s2=(char*)omAlloc(strlen((char*)res->data)+strlen((char*)res2.data)+1); |
---|
119 | sprintf(s2,"%s%s",(char*)res->data,(char*)res2.data); |
---|
120 | omFree(res2.data); |
---|
121 | omFree(res->data); |
---|
122 | res->data=s2; |
---|
123 | } |
---|
124 | return FALSE; |
---|
125 | } |
---|
126 | return WrongOp("blackbox_OpM", op, args); |
---|
127 | } |
---|
128 | |
---|
129 | BOOLEAN blackbox_default_Check(blackbox *,leftv,leftv) |
---|
130 | { |
---|
131 | return FALSE; |
---|
132 | } |
---|
133 | int setBlackboxStuff(blackbox *bb, const char *n) |
---|
134 | { |
---|
135 | int where=-1; |
---|
136 | if (MAX_BB_TYPES<=blackboxTableCnt) |
---|
137 | { |
---|
138 | // second try, find empty slot from removed bb: |
---|
139 | for (int i=0;i<MAX_BB_TYPES;i++) |
---|
140 | { |
---|
141 | if (blackboxTable[i]==NULL) { where=i; break; } |
---|
142 | } |
---|
143 | } |
---|
144 | else |
---|
145 | { |
---|
146 | where=blackboxTableCnt; |
---|
147 | blackboxTableCnt++; |
---|
148 | } |
---|
149 | if (where==-1) |
---|
150 | { |
---|
151 | WerrorS("too many bb types defined"); |
---|
152 | return 0; |
---|
153 | } |
---|
154 | else |
---|
155 | { |
---|
156 | blackboxTable[where]=bb; |
---|
157 | blackboxName[where]=omStrDup(n); |
---|
158 | #ifdef BLACKBOX_DEVEL |
---|
159 | Print("setBlackboxStuff: define bb:name=%s:rt=%d (table:cnt=%d)\n",blackboxName[where],where+BLACKBOX_OFFSET,where); |
---|
160 | #endif |
---|
161 | if (bb->blackbox_destroy==NULL) bb->blackbox_destroy=blackbox_default_destroy; |
---|
162 | if (bb->blackbox_String==NULL) bb->blackbox_String=blackbox_default_String; |
---|
163 | if (bb->blackbox_Print==NULL) bb->blackbox_Print=blackbox_default_Print; |
---|
164 | if (bb->blackbox_Init==NULL) bb->blackbox_Init=blackbox_default_Init; |
---|
165 | if (bb->blackbox_Copy==NULL) bb->blackbox_Copy=blackbox_default_Copy; |
---|
166 | if (bb->blackbox_Op1==NULL) bb->blackbox_Op1=blackboxDefaultOp1; |
---|
167 | if (bb->blackbox_Op2==NULL) bb->blackbox_Op2=blackboxDefaultOp2; |
---|
168 | if (bb->blackbox_Op3==NULL) bb->blackbox_Op3=blackbox_default_Op3; |
---|
169 | if (bb->blackbox_OpM==NULL) bb->blackbox_OpM=blackbox_default_OpM; |
---|
170 | if (bb->blackbox_CheckAssign==NULL) bb->blackbox_CheckAssign=blackbox_default_Check; |
---|
171 | if (bb->blackbox_serialize==NULL) bb->blackbox_serialize=blackbox_default_serialize; |
---|
172 | if (bb->blackbox_deserialize==NULL) bb->blackbox_deserialize=blackbox_default_deserialize; |
---|
173 | return where+BLACKBOX_OFFSET; |
---|
174 | } |
---|
175 | } |
---|
176 | |
---|
177 | void removeBlackboxStuff(const int rt) |
---|
178 | { |
---|
179 | omfree(blackboxTable[rt-BLACKBOX_OFFSET]); |
---|
180 | omfree(blackboxName[rt-BLACKBOX_OFFSET]); |
---|
181 | blackboxTable[rt-BLACKBOX_OFFSET]=NULL; |
---|
182 | blackboxName[rt-BLACKBOX_OFFSET]=NULL; |
---|
183 | } |
---|
184 | const char *getBlackboxName(const int t) |
---|
185 | { |
---|
186 | char *b=blackboxName[t-BLACKBOX_OFFSET]; |
---|
187 | if (b!=NULL) return b; |
---|
188 | else return ""; |
---|
189 | } |
---|
190 | int blackboxIsCmd(const char *n, int & tok) |
---|
191 | { |
---|
192 | for(int i=blackboxTableCnt-1;i>=0;i--) |
---|
193 | { |
---|
194 | if(strcmp(n,blackboxName[i])==0) |
---|
195 | { |
---|
196 | #ifdef BLACKBOX_DEVEL |
---|
197 | Print("blackboxIsCmd: found bb:%s:%d (table:%d)\n",n,i+BLACKBOX_OFFSET,i); |
---|
198 | #endif |
---|
199 | tok=i+BLACKBOX_OFFSET; |
---|
200 | return ROOT_DECL; |
---|
201 | } |
---|
202 | } |
---|
203 | return 0; |
---|
204 | } |
---|
205 | |
---|
206 | void printBlackboxTypes() |
---|
207 | { |
---|
208 | for(int i=blackboxTableCnt-1;i>=0;i--) |
---|
209 | { |
---|
210 | if (blackboxName[i]!=NULL) |
---|
211 | Print("type %d: %s\n",i,blackboxName[i]); |
---|
212 | } |
---|
213 | } |
---|