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