1 | /**************************************** |
---|
2 | * Computer Algebra System SINGULAR * |
---|
3 | ****************************************/ |
---|
4 | /* |
---|
5 | * ABSTRACT: lib parsing |
---|
6 | */ |
---|
7 | |
---|
8 | #include <kernel/mod2.h> |
---|
9 | #include <febase.h> |
---|
10 | #include <grammar.h> |
---|
11 | #include <ipid.h> |
---|
12 | #include <ipshell.h> |
---|
13 | #include <mmemory.h> |
---|
14 | #include <structs.h> |
---|
15 | #include <subexpr.h> |
---|
16 | #include <tok.h> |
---|
17 | #include <regex.h> |
---|
18 | |
---|
19 | #include "modgen.h" |
---|
20 | #include "typmap.h" |
---|
21 | |
---|
22 | #define NEW_PARAM 1 |
---|
23 | |
---|
24 | #define SYSTYP_NONE 0 |
---|
25 | #define SYSTYP_LINUX 1 |
---|
26 | #define SYSTYP_HPUX9 2 |
---|
27 | #define SYSTYP_HPUX10 3 |
---|
28 | |
---|
29 | #if 1 |
---|
30 | # define logx printf |
---|
31 | #else |
---|
32 | # define logx |
---|
33 | #endif |
---|
34 | |
---|
35 | VAR char *DYNAinclude[] = { |
---|
36 | "", |
---|
37 | "#include <dlfcn.h>", |
---|
38 | "#include <dl.h>", |
---|
39 | "#include <dl.h>", |
---|
40 | "#include <>", |
---|
41 | "#include <>", |
---|
42 | "#include <>", |
---|
43 | NULL |
---|
44 | }; |
---|
45 | |
---|
46 | VAR int systyp = SYSTYP_NONE; |
---|
47 | |
---|
48 | VAR BOOLEAN expected_parms; |
---|
49 | VAR int cmdtok; |
---|
50 | VAR BOOLEAN siq=FALSE; |
---|
51 | VAR char *lastreserved=NULL; |
---|
52 | #define SELF_CMD MAX_TOK+1 |
---|
53 | |
---|
54 | void enter_id(FILE *fp, char *name, char *value); |
---|
55 | void mod_write_enter_id(FILE *fp); |
---|
56 | |
---|
57 | |
---|
58 | /*=============== types =====================*/ |
---|
59 | struct sValCmdTab |
---|
60 | { |
---|
61 | short cmd; |
---|
62 | short start; |
---|
63 | }; |
---|
64 | |
---|
65 | #include "iparith.inc" |
---|
66 | |
---|
67 | /*=================== general utilities ============================*/ |
---|
68 | int IsCmd(char *n, int & tok) |
---|
69 | { |
---|
70 | int an=1; |
---|
71 | int i,v; |
---|
72 | int en=LAST_IDENTIFIER; |
---|
73 | |
---|
74 | if( strcmp(n, "SELF") == 0) { |
---|
75 | tok = SELF_CMD; |
---|
76 | logx("IsCmd: [%d] %s\n", tok, n); |
---|
77 | return tok; |
---|
78 | } |
---|
79 | |
---|
80 | if( strcmp(n, "none") == 0) { |
---|
81 | tok = NONE; |
---|
82 | logx("IsCmd: [%d] %s\n", tok, n); |
---|
83 | return tok; |
---|
84 | } |
---|
85 | |
---|
86 | loop |
---|
87 | { |
---|
88 | if(an>=en-1) |
---|
89 | { |
---|
90 | if (strcmp(n, cmds[an].name) == 0) |
---|
91 | { |
---|
92 | i=an; |
---|
93 | break; |
---|
94 | } |
---|
95 | else if (strcmp(n, cmds[en].name) == 0) |
---|
96 | { |
---|
97 | i=en; |
---|
98 | break; |
---|
99 | } |
---|
100 | else |
---|
101 | { |
---|
102 | return 0; |
---|
103 | } |
---|
104 | } |
---|
105 | i=(an+en)/2; |
---|
106 | v=strcmp(n,cmds[i].name); |
---|
107 | if(v<0) |
---|
108 | { |
---|
109 | en=i; |
---|
110 | } |
---|
111 | else if(v>0) |
---|
112 | { |
---|
113 | an=i; |
---|
114 | } |
---|
115 | else /*v==0*/ |
---|
116 | { |
---|
117 | break; |
---|
118 | } |
---|
119 | } |
---|
120 | lastreserved=cmds[i].name; |
---|
121 | tok=cmds[i].tokval; |
---|
122 | if(cmds[i].alias==2) |
---|
123 | { |
---|
124 | printf("outdated identifier `%s` used - please change your code", |
---|
125 | cmds[i].name); |
---|
126 | cmds[i].alias=1; |
---|
127 | } |
---|
128 | #if 0 |
---|
129 | if (!expected_parms) |
---|
130 | { |
---|
131 | switch (tok) |
---|
132 | { |
---|
133 | case IDEAL_CMD: |
---|
134 | case INT_CMD: |
---|
135 | case INTVEC_CMD: |
---|
136 | case MAP_CMD: |
---|
137 | case MATRIX_CMD: |
---|
138 | case MODUL_CMD: |
---|
139 | case POLY_CMD: |
---|
140 | case PROC_CMD: |
---|
141 | case RING_CMD: |
---|
142 | case STRING_CMD: |
---|
143 | cmdtok = tok; |
---|
144 | break; |
---|
145 | } |
---|
146 | } |
---|
147 | #endif |
---|
148 | logx("IsCmd: [%d] %s\n", tok, n); |
---|
149 | |
---|
150 | if( (cmds[i].toktype==ROOT_DECL) || |
---|
151 | (cmds[i].toktype==ROOT_DECL_LIST) || |
---|
152 | (cmds[i].toktype==RING_DECL) || |
---|
153 | ((cmds[i].toktype>=DRING_CMD) && (cmds[i].toktype<=VECTOR_CMD))) |
---|
154 | return cmds[i].toktype; |
---|
155 | return 0; |
---|
156 | } |
---|
157 | |
---|
158 | char * decl2str(int n, char *name) |
---|
159 | { |
---|
160 | switch(n) |
---|
161 | { |
---|
162 | # include "decl.inc" |
---|
163 | |
---|
164 | /* first and last entry of tok.h cannot be grepped */ |
---|
165 | //case ATTRIB_CMD: strcpy(name,"ATTRIB_CMD"); break; |
---|
166 | case MAX_TOK: strcpy(name,"MAX_TOK"); break; |
---|
167 | default: strcpy(name,"(null)"); |
---|
168 | } |
---|
169 | #if 0 |
---|
170 | printf("[%d=%s]", n, name); |
---|
171 | fflush(stdout); |
---|
172 | #endif |
---|
173 | return(name); |
---|
174 | } |
---|
175 | |
---|
176 | #if 0 |
---|
177 | static int iiTabIndex(const jjValCmdTab dArithTab, const int len, const int op) |
---|
178 | { |
---|
179 | int a=0; |
---|
180 | int e=len; |
---|
181 | int p=len/2; |
---|
182 | while ( a!=e) |
---|
183 | { |
---|
184 | if (op==dArithTab[p].cmd) return dArithTab[p].start; |
---|
185 | else if (op<dArithTab[p].cmd) |
---|
186 | { e=p; p=a+(e-a)/2;} |
---|
187 | else |
---|
188 | { a=p; p=p+(e-p)/2; } |
---|
189 | } |
---|
190 | return -1; |
---|
191 | } |
---|
192 | #endif |
---|
193 | |
---|
194 | /*========================================================================*/ |
---|
195 | void PrintProc( |
---|
196 | procdef pi |
---|
197 | ) |
---|
198 | { |
---|
199 | int i; |
---|
200 | |
---|
201 | printf("proc: %s(", pi.procname); |
---|
202 | |
---|
203 | for(i=0; i<pi.paramcnt; i++) { |
---|
204 | printf("%s", pi.param[i].name); |
---|
205 | if(i < (pi.paramcnt-1)) printf(","); |
---|
206 | } |
---|
207 | printf(")\n"); |
---|
208 | } |
---|
209 | |
---|
210 | /*========================================================================*/ |
---|
211 | void make_version(char *p, moddefv module) |
---|
212 | { |
---|
213 | char ver[10]; |
---|
214 | char date[16]; |
---|
215 | char libnamebuf[128]; |
---|
216 | |
---|
217 | module->major = 0; |
---|
218 | module->minor = 0; |
---|
219 | module->level = 0; |
---|
220 | |
---|
221 | ver[0]='0'; ver[1]='.'; ver[2]='0'; ver[3]='.'; ver[4]='0'; ver[5]='\0'; |
---|
222 | date[0]='?'; date[1]='\0'; |
---|
223 | //if(what) sscanf(p,"%*[^=]= %*s %*s %10s %16s",ver,date); |
---|
224 | sscanf(p,"%*s %*s %10s %16s",ver,date); |
---|
225 | sscanf(ver, "%d.%d.%d", &module->major, &module->minor, &module->level); |
---|
226 | |
---|
227 | sprintf(libnamebuf,"(%s,%s)", ver, date); |
---|
228 | if(strcmp(libnamebuf, "(0.0.0,?)")==0) |
---|
229 | { |
---|
230 | sscanf(p,"%*[^\"]\"%[^\"]\"",libnamebuf); |
---|
231 | } |
---|
232 | module->revision = (char *)malloc(strlen(libnamebuf)+1); |
---|
233 | memset(module->revision, '\0', strlen(libnamebuf)+1); |
---|
234 | memcpy(module->revision, libnamebuf, strlen(libnamebuf)); |
---|
235 | } |
---|
236 | |
---|
237 | /*========================================================================*/ |
---|
238 | void Add2files( |
---|
239 | moddefv module, |
---|
240 | char *name |
---|
241 | ) |
---|
242 | { |
---|
243 | cfiles cfnew; |
---|
244 | memset((void *)&cfnew, '\0', sizeof(cfiles)); |
---|
245 | |
---|
246 | cfnew.filename = (char *)malloc(strlen(name)+1); |
---|
247 | memset(cfnew.filename, '\0', strlen(name)+1); |
---|
248 | memcpy(cfnew.filename, name, strlen(name)); |
---|
249 | |
---|
250 | if(module->filecnt==0) { |
---|
251 | module->files = (cfilesv)malloc(sizeof(cfiles)+1); |
---|
252 | } |
---|
253 | else { |
---|
254 | module->files = (cfilesv)realloc(module->files, |
---|
255 | (module->filecnt+1)*sizeof(cfiles)); |
---|
256 | } |
---|
257 | if(module->files == NULL) { printf("ERROR\n"); return; } |
---|
258 | |
---|
259 | memset((void *) &module->files[module->filecnt], '\0', sizeof(cfiles)); |
---|
260 | memcpy((void *)(&(module->files[module->filecnt])), |
---|
261 | (void *)&cfnew, sizeof(cfiles)); |
---|
262 | (module->filecnt)++; |
---|
263 | } |
---|
264 | |
---|
265 | /*========================================================================*/ |
---|
266 | void Add2proclist( |
---|
267 | moddefv module, |
---|
268 | char *name, |
---|
269 | char *ret_val, |
---|
270 | char *ret_typname, |
---|
271 | int ret_typ |
---|
272 | ) |
---|
273 | { |
---|
274 | procdef pnew; |
---|
275 | logx("Add2proclist(%s, %s)\n", name, ret_val); |
---|
276 | |
---|
277 | memset((void *)&pnew, '\0', sizeof(procdef)); |
---|
278 | |
---|
279 | pnew.procname = (char *)malloc(strlen(name)+1); |
---|
280 | if(pnew.procname==NULL) printf("Error 1\n"); |
---|
281 | memset(pnew.procname, '\0', strlen(name)+1); |
---|
282 | memcpy(pnew.procname, name, strlen(name)); |
---|
283 | |
---|
284 | pnew.funcname = (char *)malloc(strlen(name)+1); |
---|
285 | if(pnew.funcname==NULL) printf("Error 1\n"); |
---|
286 | memset(pnew.funcname, '\0', strlen(name)+1); |
---|
287 | memcpy(pnew.funcname, name, strlen(name)); |
---|
288 | |
---|
289 | pnew.param = NULL; |
---|
290 | (pnew).is_static = 0; |
---|
291 | (pnew).paramcnt = 0; |
---|
292 | pnew.c_code = NULL; |
---|
293 | |
---|
294 | pnew.return_val.name = (char *)malloc(strlen(ret_val)+1); |
---|
295 | memset(pnew.return_val.name, '\0', strlen(ret_val)+1); |
---|
296 | memcpy(pnew.return_val.name, ret_val, strlen(ret_val)); |
---|
297 | |
---|
298 | pnew.return_val.typname = (char *)malloc(strlen(ret_typname)+1); |
---|
299 | memset(pnew.return_val.typname, '\0', strlen(ret_typname)+1); |
---|
300 | memcpy(pnew.return_val.typname, ret_typname, strlen(ret_typname)); |
---|
301 | pnew.return_val.typ = ret_typ; |
---|
302 | |
---|
303 | if(module->proccnt==0) { |
---|
304 | module->procs = (procdefv)malloc(sizeof(procdef)+1); |
---|
305 | } |
---|
306 | else { |
---|
307 | module->procs = (procdefv)realloc(module->procs, |
---|
308 | (module->proccnt+1)*sizeof(procdef)); |
---|
309 | } |
---|
310 | if(module->procs == NULL) { printf("ERROR\n"); return; } |
---|
311 | |
---|
312 | memset((void *) &module->procs[module->proccnt], '\0', sizeof(procdef)); |
---|
313 | memcpy((void *)(&(module->procs[module->proccnt])), |
---|
314 | (void *)&pnew, sizeof(procdef)); |
---|
315 | (module->proccnt)++; |
---|
316 | } |
---|
317 | |
---|
318 | /*========================================================================*/ |
---|
319 | void AddParam( |
---|
320 | moddefv module, |
---|
321 | char *name, |
---|
322 | char *typname, |
---|
323 | int typ |
---|
324 | ) |
---|
325 | { |
---|
326 | paramdef pnew; |
---|
327 | int proccnt = module->proccnt-1; |
---|
328 | int paramcnt = 0; |
---|
329 | |
---|
330 | logx("AddParam(%d, %s, %s, %d)\n", module->procs[proccnt].paramcnt, |
---|
331 | module->procs[proccnt].procname, |
---|
332 | typname, typ); |
---|
333 | memset((void *)&pnew, '\0', sizeof(paramdef)); |
---|
334 | pnew.name = (char *)malloc(strlen(name)+1); |
---|
335 | memset(pnew.name, '\0', strlen(name)+1); |
---|
336 | pnew.next = NULL; |
---|
337 | memcpy(pnew.name, name, strlen(name)); |
---|
338 | pnew.typname = (char *)malloc(strlen(typname)+1); |
---|
339 | memset(pnew.typname, '\0', strlen(typname)+1); |
---|
340 | memcpy(pnew.typname, typname, strlen(typname)); |
---|
341 | pnew.typ = typ; |
---|
342 | |
---|
343 | paramcnt = module->procs[proccnt].paramcnt; |
---|
344 | if(module->procs[proccnt].paramcnt==0) { |
---|
345 | module->procs[proccnt].param = (paramdefv)malloc(sizeof(paramdef)); |
---|
346 | } |
---|
347 | else { |
---|
348 | module->procs[proccnt].param = |
---|
349 | (paramdefv)realloc(module->procs[proccnt].param, |
---|
350 | (paramcnt+1)*sizeof(paramdef)); |
---|
351 | } |
---|
352 | |
---|
353 | memcpy((void *)(&module->procs[proccnt].param[paramcnt]), |
---|
354 | (void *)&pnew, sizeof(paramdef)); |
---|
355 | (module->procs[proccnt].paramcnt)++; |
---|
356 | logx("AddParam() done\n"); |
---|
357 | } |
---|
358 | |
---|
359 | /*========================================================================*/ |
---|
360 | void PrintProclist( |
---|
361 | moddefv module |
---|
362 | ) |
---|
363 | { |
---|
364 | logx("PrintProclist()\n"); |
---|
365 | int j; |
---|
366 | for(j=0; j<module->proccnt; j++) { |
---|
367 | PrintProc(module->procs[j]); |
---|
368 | } |
---|
369 | } |
---|
370 | |
---|
371 | /*========================================================================*/ |
---|
372 | void generate_mod( |
---|
373 | moddefv module |
---|
374 | ) |
---|
375 | { |
---|
376 | procdefv v = NULL; |
---|
377 | cfilesv c_filelist = NULL; |
---|
378 | int proccnt; |
---|
379 | FILE *fp_c, *fp_h; |
---|
380 | char *filename; |
---|
381 | |
---|
382 | if(strcmp(S_UNAME, "ix86-Linux") == 0) { |
---|
383 | systyp = SYSTYP_LINUX; |
---|
384 | } else if (strcmp(S_UNAME, "HPUX-9")==0) { |
---|
385 | systyp = SYSTYP_HPUX9; |
---|
386 | } else if (strcmp(S_UNAME, "HPUX-10")==0) { |
---|
387 | systyp = SYSTYP_HPUX10; |
---|
388 | } |
---|
389 | init_type_conv(); |
---|
390 | printf("SYSTYP:%d\n", systyp); |
---|
391 | |
---|
392 | filename = (char *)malloc(strlen(module->name)+5); |
---|
393 | sprintf(filename, "%s.cc", module->name); |
---|
394 | fp_c = fopen(filename, "w"); |
---|
395 | printf("Creating %s, ", filename);fflush(stdout); |
---|
396 | mod_write_header(fp_c, module->name); |
---|
397 | mod_write_enter_id(fp_c); |
---|
398 | |
---|
399 | sprintf(filename, "%s.h", module->name); |
---|
400 | fp_h = fopen(filename, "w"); |
---|
401 | write_header(fp_h, module->name); |
---|
402 | printf("%s ...", filename);fflush(stdout); |
---|
403 | |
---|
404 | /* building mod_init() */ |
---|
405 | fprintf(fp_c, "extern \"C\"\n"); |
---|
406 | fprintf(fp_c, "int mod_init(int(*iiAddCproc)())\n{\n"); |
---|
407 | fprintf(fp_c, " idhdl h;\n\n"); |
---|
408 | |
---|
409 | if(module->version != NULL) enter_id(fp_c, "version", module->version); |
---|
410 | if(module->info != NULL) enter_id(fp_c, "info", module->info); |
---|
411 | if(module->helpfile != NULL) enter_id(fp_c, "helpfile", module->helpfile); |
---|
412 | |
---|
413 | for(proccnt=0; proccnt<module->proccnt; proccnt++) { |
---|
414 | printf("->%s, %s\n", module->procs[proccnt].procname, |
---|
415 | module->procs[proccnt].funcname); |
---|
416 | fprintf(fp_c, " iiAddCproc(\"%s\",\"%s\",%s, mod_%s);\n", |
---|
417 | module->name, module->procs[proccnt].procname, |
---|
418 | module->procs[proccnt].is_static ? "TRUE" : "FALSE", |
---|
419 | module->procs[proccnt].funcname); |
---|
420 | } |
---|
421 | fprintf(fp_c, " return 0;\n}\n\n"); |
---|
422 | |
---|
423 | /* building entry-functions */ |
---|
424 | for(proccnt=0; proccnt<module->proccnt; proccnt++) { |
---|
425 | generate_function(&module->procs[proccnt], fp_c); |
---|
426 | generate_header(&module->procs[proccnt], fp_h); |
---|
427 | } |
---|
428 | printf(" done.\n");fflush(stdout); |
---|
429 | fclose(fp_c); |
---|
430 | fclose(fp_h); |
---|
431 | } |
---|
432 | |
---|
433 | /*========================================================================*/ |
---|
434 | void generate_header(procdefv pi, FILE *fp) |
---|
435 | { |
---|
436 | int i; |
---|
437 | |
---|
438 | fprintf(fp, "BOOLEAN mod_%s(leftv res, leftv h);\n", pi->funcname); |
---|
439 | switch( pi->return_val.typ) { |
---|
440 | case SELF_CMD: |
---|
441 | fprintf(fp, "BOOLEAN %s(res, ", pi->funcname); |
---|
442 | break; |
---|
443 | |
---|
444 | default: |
---|
445 | fprintf(fp, "%s %s(", type_conv[pi->return_val.typ], |
---|
446 | pi->funcname); |
---|
447 | } |
---|
448 | for (i=0;i<pi->paramcnt; i++) { |
---|
449 | fprintf(fp, "%s res%d", type_conv[pi->param[i].typ], i); |
---|
450 | if(i<pi->paramcnt-1) fprintf(fp, ", "); |
---|
451 | } |
---|
452 | fprintf(fp, ");\n\n"); |
---|
453 | } |
---|
454 | |
---|
455 | void gen_func_param_check( |
---|
456 | FILE *fp, |
---|
457 | procdefv pi, |
---|
458 | int i |
---|
459 | ) |
---|
460 | { |
---|
461 | fprintf(fp, " if(v==NULL) goto mod_%s_error;\n", pi->funcname); |
---|
462 | fprintf(fp, " tok = v->Typ();\n"); |
---|
463 | fprintf(fp, " if((index=iiTestConvert(tok, %s))==0)\n", |
---|
464 | pi->param[i].typname); |
---|
465 | fprintf(fp, " goto mod_%s_error;\n", pi->funcname); |
---|
466 | fprintf(fp, " v_save = v->next;\n"); |
---|
467 | fprintf(fp, " v->next = NULL;\n"); |
---|
468 | fprintf(fp, " if(iiConvert(tok, %s, index, v, res%d))\n", |
---|
469 | pi->param[i].typname, i); |
---|
470 | fprintf(fp, " goto mod_%s_error;\n", pi->funcname); |
---|
471 | fprintf(fp, " v = v_save;\n"); |
---|
472 | } |
---|
473 | |
---|
474 | void generate_function(procdefv pi, FILE *fp) |
---|
475 | { |
---|
476 | int cnt = 0, i; |
---|
477 | printf("%s has %d paramters\n", pi->funcname, pi->paramcnt); |
---|
478 | |
---|
479 | fprintf(fp, "BOOLEAN mod_%s(leftv res, leftv h)\n{\n", pi->funcname); |
---|
480 | if(pi->paramcnt>0) { |
---|
481 | if(pi->param[0].typ==SELF_CMD) { |
---|
482 | if(pi->c_code != NULL) fprintf(fp, "%s\n", pi->c_code); |
---|
483 | |
---|
484 | fprintf(fp, " return(%s(res,h));\n", pi->funcname); |
---|
485 | fprintf(fp, "}\n\n"); |
---|
486 | } |
---|
487 | else { |
---|
488 | fprintf(fp, " leftv v = h, v_save;\n"); |
---|
489 | fprintf(fp, " int tok = NONE, index = 0;\n"); |
---|
490 | for (i=0;i<pi->paramcnt; i++) |
---|
491 | fprintf(fp, " leftv res%d = (leftv)Alloc0(sizeof(sleftv));\n", i); |
---|
492 | |
---|
493 | fprintf(fp, "\n"); |
---|
494 | |
---|
495 | if(pi->c_code != NULL) fprintf(fp, "%s\n", pi->c_code); |
---|
496 | |
---|
497 | for (i=0;i<pi->paramcnt; i++) gen_func_param_check(fp, pi, i); |
---|
498 | |
---|
499 | fprintf(fp, " if(v!=NULL) { tok = v->Typ(); goto mod_%s_error; }\n", |
---|
500 | pi->funcname); |
---|
501 | |
---|
502 | fprintf(fp, "\n"); |
---|
503 | switch( pi->return_val.typ) { |
---|
504 | case SELF_CMD: |
---|
505 | fprintf(fp, " return(%s(res", pi->funcname); |
---|
506 | for (i=0;i<pi->paramcnt; i++) |
---|
507 | fprintf(fp, ", (%s) res%d->Data()", |
---|
508 | type_conv[pi->param[i].typ], i); |
---|
509 | fprintf(fp, "));\n\n"); |
---|
510 | break; |
---|
511 | |
---|
512 | default: |
---|
513 | fprintf(fp, " res->rtyp = %s;\n", pi->return_val.typname); |
---|
514 | fprintf(fp, " res->data = (void *)%s(", pi->funcname); |
---|
515 | for (i=0;i<pi->paramcnt; i++) { |
---|
516 | fprintf(fp, "(%s) res%d->Data()", |
---|
517 | type_conv[pi->param[i].typ], i); |
---|
518 | if(i<pi->paramcnt-1) fprintf(fp, ", "); |
---|
519 | } |
---|
520 | fprintf(fp, ");\n return FALSE;\n\n"); |
---|
521 | } |
---|
522 | |
---|
523 | fprintf(fp, " mod_%s_error:\n", pi->funcname); |
---|
524 | fprintf(fp, " Werror(\"%s(`%%s`) is not supported\", Tok2Cmdname(tok));\n", |
---|
525 | pi->procname); |
---|
526 | fprintf(fp, " Werror(\"expected %s(", pi->procname); |
---|
527 | for (i=0;i<pi->paramcnt; i++) { |
---|
528 | fprintf(fp, "'%s'", pi->param[i].name); |
---|
529 | if(i!=pi->paramcnt-1) fprintf(fp, ","); |
---|
530 | } |
---|
531 | fprintf(fp, ")\");\n"); |
---|
532 | fprintf(fp, " return TRUE;\n}\n\n"); |
---|
533 | } |
---|
534 | } else { |
---|
535 | switch( pi->return_val.typ) { |
---|
536 | case SELF_CMD: |
---|
537 | fprintf(fp, " return(%s(res));\n}\n\n", pi->funcname); |
---|
538 | break; |
---|
539 | |
---|
540 | case NONE: |
---|
541 | fprintf(fp, " res->rtyp = %s;\n", pi->return_val.typname); |
---|
542 | fprintf(fp, " res->data = NULL;\n"); |
---|
543 | fprintf(fp, " %s();\n", pi->funcname); |
---|
544 | fprintf(fp, " return FALSE;\n}\n\n"); |
---|
545 | break; |
---|
546 | |
---|
547 | default: |
---|
548 | fprintf(fp, " res->rtyp = %s;\n", pi->return_val.typname); |
---|
549 | fprintf(fp, " res->data = (void *)%s();\n", pi->funcname); |
---|
550 | fprintf(fp, " return FALSE;\n}\n\n"); |
---|
551 | } |
---|
552 | |
---|
553 | } |
---|
554 | |
---|
555 | |
---|
556 | } |
---|
557 | |
---|
558 | /*========================================================================*/ |
---|
559 | void mod_write_header(FILE *fp, char *module) |
---|
560 | { |
---|
561 | #if 0 |
---|
562 | FILE *fp; |
---|
563 | char buf[BUFLEN]; |
---|
564 | |
---|
565 | regex_t preg; |
---|
566 | regmatch_t pmatch[1]; |
---|
567 | size_t nmatch = 0; |
---|
568 | char *regex = "@MODULE_NAME@"; |
---|
569 | |
---|
570 | rc = regcomp(&preg, regex, REG_NOSUB); |
---|
571 | if(rc) return -1; |
---|
572 | |
---|
573 | if(!regexec(&preg, d_entry->d_name, nmatch, pmatch, REG_NOTBOL)) |
---|
574 | cert_count++; |
---|
575 | regfree(&preg); |
---|
576 | |
---|
577 | #else |
---|
578 | write_header(fp, module); |
---|
579 | fprintf(fp, "#include <stdlib.h>\n"); |
---|
580 | fprintf(fp, "#include <stdio.h>\n"); |
---|
581 | fprintf(fp, "#include <string.h>\n"); |
---|
582 | fprintf(fp, "#include <ctype.h>\n"); |
---|
583 | fprintf(fp, "%s\n", DYNAinclude[systyp]); |
---|
584 | fprintf(fp, "\n"); |
---|
585 | fprintf(fp, "#include <locals.h>\n"); |
---|
586 | fprintf(fp, "#include \"%s.h\"\n", module); |
---|
587 | fprintf(fp, "\n"); |
---|
588 | #endif |
---|
589 | } |
---|
590 | |
---|
591 | /*========================================================================*/ |
---|
592 | void write_header(FILE *fp, char *module, char *comment) |
---|
593 | { |
---|
594 | fprintf(fp, "%s/*\n%s * This was automaticly generated by modgen\n", |
---|
595 | comment, comment); |
---|
596 | fprintf(fp, "%s * version %s\n", comment, MOD_GEN_VERSION); |
---|
597 | fprintf(fp, "%s * module %s\n", comment, module); |
---|
598 | fprintf(fp, "%s * Don't edit this file\n%s */\n", comment, comment); |
---|
599 | fprintf(fp, "%s\n", comment); |
---|
600 | fprintf(fp, "%s\n", comment); |
---|
601 | |
---|
602 | } |
---|
603 | |
---|
604 | /*========================================================================*/ |
---|
605 | void enter_id(FILE *fp, char *name, char *value) |
---|
606 | { |
---|
607 | fprintf(fp, " enter_id(\"%s\",\"%s\");\n",name, value); |
---|
608 | } |
---|
609 | |
---|
610 | void mod_write_enter_id(FILE *fp) |
---|
611 | { |
---|
612 | fprintf(fp, "void enter_id(char *name, char *value)\n"); |
---|
613 | fprintf(fp, "{\n idhdl h;\n\n"); |
---|
614 | fprintf(fp, " h = enterid(mstrdup(name),0, STRING_CMD, &IDROOT, FALSE);\n"); |
---|
615 | fprintf(fp, " if(h!=NULL) {\n"); |
---|
616 | fprintf(fp, " IDSTRING(h) = mstrdup(value);\n"); |
---|
617 | fprintf(fp, " }\n}\n\n"); |
---|
618 | } |
---|
619 | |
---|
620 | static char *object_name(char *p) |
---|
621 | { |
---|
622 | char *q = (char *)strrchr(p, '.'); |
---|
623 | if(q==NULL) return ""; |
---|
624 | *q = '\0'; |
---|
625 | char *r = (char *)malloc(strlen(p)+4); |
---|
626 | switch(systyp) { |
---|
627 | case SYSTYP_LINUX: |
---|
628 | sprintf(r, "%s.lo", p); |
---|
629 | break; |
---|
630 | |
---|
631 | case SYSTYP_HPUX9: |
---|
632 | sprintf(r, "%s.o", p); |
---|
633 | break; |
---|
634 | } |
---|
635 | |
---|
636 | *q = '.'; |
---|
637 | return(r); |
---|
638 | } |
---|
639 | |
---|
640 | /*========================================================================*/ |
---|
641 | void mod_create_makefile(moddefv module) |
---|
642 | { |
---|
643 | FILE *fp; |
---|
644 | fp = fopen("Makefile", "w"); |
---|
645 | cfilesv cf = module->files; |
---|
646 | int i; |
---|
647 | |
---|
648 | printf("Creating Makefile ...");fflush(stdout); |
---|
649 | write_header(fp, module->name, "#"); |
---|
650 | fprintf(fp, "CC\t= gcc\n"); |
---|
651 | fprintf(fp, "CXX\t= gcc\n"); |
---|
652 | fprintf(fp, "CFLAGS\t= -DSING_NDEBUG -DBUILD_MODULE -I. -I../include\n"); |
---|
653 | fprintf(fp, "#LD\t=\n"); |
---|
654 | fprintf(fp, "\n"); |
---|
655 | fprintf(fp, "SRCS\t= "); |
---|
656 | |
---|
657 | for(i=0; i<module->filecnt; i++) |
---|
658 | fprintf(fp, "%s ", cf[i].filename); |
---|
659 | |
---|
660 | fprintf(fp, "\nOBJS\t= "); |
---|
661 | for(i=0; i<module->filecnt; i++) |
---|
662 | fprintf(fp, "%s ", object_name(cf[i].filename)); |
---|
663 | |
---|
664 | fprintf(fp, "\n\n"); |
---|
665 | switch(systyp) { |
---|
666 | case SYSTYP_LINUX: |
---|
667 | fprintf(fp, "all:\t%s.so\n", module->name); |
---|
668 | fprintf(fp, "\n"); |
---|
669 | fprintf(fp, "%%.lo: %%.cc Makefile\n"); |
---|
670 | fprintf(fp, "\t${CC} ${CFLAGS} -c -fPIC -DPIC $< -o $*.lo\n"); |
---|
671 | fprintf(fp, "\n"); |
---|
672 | |
---|
673 | fprintf(fp, "%%.la: %%.cc Makefile\n"); |
---|
674 | fprintf(fp, "\t${CC} ${CFLAGS} -c $< -o $*.la\n"); |
---|
675 | fprintf(fp, "\n"); |
---|
676 | |
---|
677 | fprintf(fp, "%s.so: ${OBJS}\n", module->name); |
---|
678 | fprintf(fp, "\t${CC} ${CFLAGS} -shared -Wl,-soname -Wl,%s.so.%d \\\n", |
---|
679 | module->name, module->major); |
---|
680 | fprintf(fp, "\t\t-o %s.so.%d.%d.%d ${OBJS}\n", module->name, |
---|
681 | module->major, module->minor, module->level); |
---|
682 | fprintf(fp, "\trm -f %s.so\n", module->name); |
---|
683 | fprintf(fp, "\tln -s %s.so.%d.%d.%d %s.so\n", module->name, module->major, |
---|
684 | module->minor, module->level, module->name); |
---|
685 | fprintf(fp, "\n"); |
---|
686 | break; |
---|
687 | |
---|
688 | case SYSTYP_HPUX9: |
---|
689 | fprintf(fp, "all:\t%s.sl\n", module->name); |
---|
690 | fprintf(fp, "\n"); |
---|
691 | fprintf(fp, "%%.o: %%.cc Makefile\n"); |
---|
692 | fprintf(fp, "\t${CC} ${CFLAGS} -c -fPIC -DPIC $< -o $*.o\n"); |
---|
693 | fprintf(fp, "\n"); |
---|
694 | fprintf(fp, "%s.sl: ${OBJS}\n", module->name); |
---|
695 | fprintf(fp, "\t${LD} -b -o %s.sl \\\n", module->name); |
---|
696 | fprintf(fp, "\t\t${OBJS}\n"); |
---|
697 | break; |
---|
698 | |
---|
699 | } |
---|
700 | |
---|
701 | fprintf(fp, "clean:\n"); |
---|
702 | fprintf(fp, "\trm -f *.o *.lo *.so* *.sl *.la *~ core\n\n"); |
---|
703 | |
---|
704 | fprintf(fp, "distclean: clean\n"); |
---|
705 | fprintf(fp, "\trm -f %s.cc %s.h Makefile\n\n", module->name, module->name); |
---|
706 | |
---|
707 | fprintf(fp, "\n"); |
---|
708 | fprintf(fp, "\n"); |
---|
709 | |
---|
710 | fclose(fp); |
---|
711 | printf(" done.\n"); |
---|
712 | } |
---|
713 | |
---|
714 | /*========================================================================*/ |
---|