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