1 | #include "kernel/mod2.h" |
---|
2 | |
---|
3 | #include "Singular/tok.h" |
---|
4 | #include "Singular/subexpr.h" |
---|
5 | #include "Singular/ipshell.h" |
---|
6 | #include "Singular/ipid.h" |
---|
7 | |
---|
8 | #include "Singular/blackbox.h" |
---|
9 | |
---|
10 | #define MAX_BB_TYPES 256 |
---|
11 | // #define BLACKBOX_DEVEL 1 |
---|
12 | |
---|
13 | GLOBAL_VAR static blackbox* blackboxTable[MAX_BB_TYPES]; |
---|
14 | GLOBAL_VAR static char * blackboxName[MAX_BB_TYPES]; |
---|
15 | GLOBAL_VAR static int blackboxTableCnt=0; |
---|
16 | #define BLACKBOX_OFFSET (MAX_TOK+1) |
---|
17 | blackbox* getBlackboxStuff(const int t) |
---|
18 | { |
---|
19 | if (t>MAX_TOK) /*MAX_TOK+1 is BLACKBOX_OFFSET*/ |
---|
20 | return (blackboxTable[t-BLACKBOX_OFFSET]); |
---|
21 | else |
---|
22 | return NULL; |
---|
23 | } |
---|
24 | |
---|
25 | |
---|
26 | void blackbox_default_destroy(blackbox */*b*/, void */*d*/) |
---|
27 | { |
---|
28 | WerrorS("missing blackbox_destroy"); |
---|
29 | } |
---|
30 | char *blackbox_default_String(blackbox */*b*/,void */*d*/) |
---|
31 | { |
---|
32 | WerrorS("missing blackbox_String"); |
---|
33 | return omStrDup(""); |
---|
34 | } |
---|
35 | void *blackbox_default_Copy(blackbox */*b*/,void */*d*/) |
---|
36 | { |
---|
37 | WerrorS("missing blackbox_Copy"); |
---|
38 | return NULL; |
---|
39 | } |
---|
40 | BOOLEAN blackbox_default_Assign(leftv l, leftv r) |
---|
41 | { |
---|
42 | int lt=l->Typ(); |
---|
43 | blackbox* b=getBlackboxStuff(lt); |
---|
44 | if ((lt==r->Typ()) |
---|
45 | && (l->Data()!=r->Data())) |
---|
46 | { |
---|
47 | b->blackbox_destroy(b,(void*)l->Data()); |
---|
48 | if (l->rtyp==IDHDL) |
---|
49 | IDDATA((idhdl)l->data)=(char*)b->blackbox_Copy(b,r->Data()); |
---|
50 | else |
---|
51 | l->data=b->blackbox_Copy(b,r->Data()); |
---|
52 | } |
---|
53 | return FALSE; |
---|
54 | } |
---|
55 | void blackbox_default_Print(blackbox *b,void *d) |
---|
56 | { |
---|
57 | char *s=b->blackbox_String(b,d); |
---|
58 | PrintS(s); |
---|
59 | omFree(s); |
---|
60 | } |
---|
61 | void *blackbox_default_Init(blackbox */*b*/) |
---|
62 | { |
---|
63 | return NULL; |
---|
64 | } |
---|
65 | |
---|
66 | BOOLEAN blackbox_default_serialize(blackbox */*b*/, void */*d*/, si_link /*f*/) |
---|
67 | { |
---|
68 | WerrorS("blackbox_serialize is not implemented"); |
---|
69 | return TRUE; |
---|
70 | } |
---|
71 | |
---|
72 | BOOLEAN blackbox_default_deserialize(blackbox **/*b*/, void **/*d*/, si_link /*f*/) |
---|
73 | { |
---|
74 | WerrorS("blackbox_deserialize is not implemented"); |
---|
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 | else if (op==NAMEOF_CMD) |
---|
87 | { |
---|
88 | if (r->name==NULL) l->data=omStrDup(""); |
---|
89 | else l->data=omStrDup(r->name); |
---|
90 | l->rtyp=STRING_CMD; |
---|
91 | return FALSE; |
---|
92 | } |
---|
93 | |
---|
94 | return TRUE; |
---|
95 | } |
---|
96 | |
---|
97 | BOOLEAN blackboxDefaultOp2(int /*op*/,leftv /*l*/, leftv /*r1*/, leftv /*r2*/) |
---|
98 | { |
---|
99 | return TRUE; |
---|
100 | } |
---|
101 | |
---|
102 | BOOLEAN blackboxDefaultOp3(int /*op*/,leftv /*l*/, leftv /*r1*/,leftv /*r2*/, leftv /*r3*/) |
---|
103 | { |
---|
104 | return TRUE; |
---|
105 | } |
---|
106 | |
---|
107 | BOOLEAN blackboxDefaultOpM(int op,leftv res, leftv args) |
---|
108 | { |
---|
109 | if (op==LIST_CMD) |
---|
110 | { |
---|
111 | res->rtyp=LIST_CMD; |
---|
112 | BOOLEAN bo=jjLIST_PL(res,args); |
---|
113 | args->CleanUp(); |
---|
114 | return bo; |
---|
115 | } |
---|
116 | else if(op==STRING_CMD) |
---|
117 | { |
---|
118 | blackbox *b=getBlackboxStuff(args->Typ()); |
---|
119 | res->data=b->blackbox_String(b,args->Data()); |
---|
120 | res->rtyp=STRING_CMD; |
---|
121 | args=args->next; |
---|
122 | if(args!=NULL) |
---|
123 | { |
---|
124 | sleftv res2; |
---|
125 | int ret=iiExprArithM(&res2,args,op); |
---|
126 | if (ret) return TRUE; |
---|
127 | size_t len=strlen((char*)res->data)+strlen((char*)res2.data)+1; |
---|
128 | char *s2=(char*)omAlloc(len); |
---|
129 | snprintf(s2,len,"%s%s",(char*)res->data,(char*)res2.data); |
---|
130 | omFree(res2.data); |
---|
131 | omFree(res->data); |
---|
132 | res->data=s2; |
---|
133 | } |
---|
134 | return FALSE; |
---|
135 | } |
---|
136 | return TRUE; |
---|
137 | } |
---|
138 | |
---|
139 | BOOLEAN blackbox_default_Check(blackbox *,leftv,leftv) |
---|
140 | { |
---|
141 | return FALSE; |
---|
142 | } |
---|
143 | int setBlackboxStuff(blackbox *bb, const char *n) |
---|
144 | { |
---|
145 | int where = -1; |
---|
146 | for (int i=0;i<MAX_BB_TYPES;i++) |
---|
147 | { |
---|
148 | if (blackboxTable[i]!=NULL && strcmp(blackboxName[i],n)==0) { |
---|
149 | where = i; |
---|
150 | break; |
---|
151 | } |
---|
152 | } |
---|
153 | if (where < 0) { |
---|
154 | if (MAX_BB_TYPES<=blackboxTableCnt) |
---|
155 | { |
---|
156 | // second try, find empty slot from removed bb: |
---|
157 | for (int i=0;i<MAX_BB_TYPES;i++) |
---|
158 | { |
---|
159 | if (blackboxTable[i]==NULL) { where=i; break; } |
---|
160 | } |
---|
161 | } |
---|
162 | else |
---|
163 | { |
---|
164 | where=blackboxTableCnt; |
---|
165 | blackboxTableCnt++; |
---|
166 | } |
---|
167 | } |
---|
168 | if (where==-1) |
---|
169 | { |
---|
170 | WerrorS("too many bb types defined"); |
---|
171 | return 0; |
---|
172 | } |
---|
173 | else |
---|
174 | { |
---|
175 | // check for alreday defined bb: |
---|
176 | for (int i=0;i<MAX_BB_TYPES;i++) |
---|
177 | { |
---|
178 | if ((blackboxName[i]!=NULL) && (strcmp(blackboxName[i],n)==0)) |
---|
179 | { |
---|
180 | Warn("not redefining blackbox type %s (%d)",n,i+BLACKBOX_OFFSET); |
---|
181 | return 0; |
---|
182 | } |
---|
183 | } |
---|
184 | blackboxTable[where]=bb; |
---|
185 | blackboxName[where]=omStrDup(n); |
---|
186 | #ifdef BLACKBOX_DEVEL |
---|
187 | Print("setBlackboxStuff: define bb:name=%s:rt=%d (table:cnt=%d)\n",blackboxName[where],where+BLACKBOX_OFFSET,where); |
---|
188 | #endif |
---|
189 | if (bb->blackbox_destroy==NULL) bb->blackbox_destroy=blackbox_default_destroy; |
---|
190 | if (bb->blackbox_String==NULL) bb->blackbox_String=blackbox_default_String; |
---|
191 | if (bb->blackbox_Print==NULL) bb->blackbox_Print=blackbox_default_Print; |
---|
192 | if (bb->blackbox_Init==NULL) bb->blackbox_Init=blackbox_default_Init; |
---|
193 | if (bb->blackbox_Copy==NULL) bb->blackbox_Copy=blackbox_default_Copy; |
---|
194 | if (bb->blackbox_Assign==NULL) bb->blackbox_Assign=blackbox_default_Assign; |
---|
195 | if (bb->blackbox_Op1==NULL) bb->blackbox_Op1=blackboxDefaultOp1; |
---|
196 | if (bb->blackbox_Op2==NULL) bb->blackbox_Op2=blackboxDefaultOp2; |
---|
197 | if (bb->blackbox_Op3==NULL) bb->blackbox_Op3=blackboxDefaultOp3; |
---|
198 | if (bb->blackbox_OpM==NULL) bb->blackbox_OpM=blackboxDefaultOpM; |
---|
199 | if (bb->blackbox_CheckAssign==NULL) bb->blackbox_CheckAssign=blackbox_default_Check; |
---|
200 | if (bb->blackbox_serialize==NULL) bb->blackbox_serialize=blackbox_default_serialize; |
---|
201 | if (bb->blackbox_deserialize==NULL) bb->blackbox_deserialize=blackbox_default_deserialize; |
---|
202 | return where+BLACKBOX_OFFSET; |
---|
203 | } |
---|
204 | } |
---|
205 | |
---|
206 | void removeBlackboxStuff(const int rt) |
---|
207 | { |
---|
208 | omfree(blackboxTable[rt-BLACKBOX_OFFSET]); |
---|
209 | omfree(blackboxName[rt-BLACKBOX_OFFSET]); |
---|
210 | blackboxTable[rt-BLACKBOX_OFFSET]=NULL; |
---|
211 | blackboxName[rt-BLACKBOX_OFFSET]=NULL; |
---|
212 | } |
---|
213 | const char *getBlackboxName(const int t) |
---|
214 | { |
---|
215 | char *b=blackboxName[t-BLACKBOX_OFFSET]; |
---|
216 | if (b!=NULL) return b; |
---|
217 | else return ""; |
---|
218 | } |
---|
219 | int blackboxIsCmd(const char *n, int & tok) |
---|
220 | { |
---|
221 | for(int i=blackboxTableCnt-1;i>=0;i--) |
---|
222 | { |
---|
223 | if(strcmp(n,blackboxName[i])==0) |
---|
224 | { |
---|
225 | #ifdef BLACKBOX_DEVEL |
---|
226 | Print("blackboxIsCmd: found bb:%s:%d (table:%d)\n",n,i+BLACKBOX_OFFSET,i); |
---|
227 | #endif |
---|
228 | tok=i+BLACKBOX_OFFSET; |
---|
229 | return ROOT_DECL; |
---|
230 | } |
---|
231 | } |
---|
232 | tok=0; |
---|
233 | return 0; |
---|
234 | } |
---|
235 | |
---|
236 | void printBlackboxTypes() |
---|
237 | { |
---|
238 | for(int i=blackboxTableCnt-1;i>=0;i--) |
---|
239 | { |
---|
240 | if (blackboxName[i]!=NULL) |
---|
241 | Print("type %d: %s\n",i,blackboxName[i]); |
---|
242 | } |
---|
243 | } |
---|
244 | |
---|
245 | struct blackbox_list *getBlackboxTypes() |
---|
246 | { |
---|
247 | int i = 0; |
---|
248 | void **l = (void **)omalloc0(blackboxTableCnt * sizeof(void *)); |
---|
249 | struct blackbox_list *list_struct = (struct blackbox_list *) omAlloc0(sizeof(struct blackbox_list)); |
---|
250 | list_struct->count = blackboxTableCnt; |
---|
251 | list_struct->list = l; |
---|
252 | |
---|
253 | for (i = blackboxTableCnt-1; i >= 0 ;i--) |
---|
254 | { |
---|
255 | if (blackboxName[i]!=NULL) { |
---|
256 | l[i] = (void *)omStrDup(blackboxName[i]); |
---|
257 | //Print("type %d: %s\n",i,blackboxName[i]); |
---|
258 | } else { |
---|
259 | l[i] = NULL; |
---|
260 | } |
---|
261 | } |
---|
262 | return list_struct; |
---|
263 | } |
---|