1 | /* |
---|
2 | * $Id$ |
---|
3 | */ |
---|
4 | |
---|
5 | #include <stdio.h> |
---|
6 | #include <string.h> |
---|
7 | #include <stdlib.h> |
---|
8 | #include <ctype.h> |
---|
9 | #include "modgen.h" |
---|
10 | |
---|
11 | #include "typmap.h" |
---|
12 | |
---|
13 | |
---|
14 | #if 1 |
---|
15 | # define logx printf |
---|
16 | #else |
---|
17 | # define logx |
---|
18 | #endif |
---|
19 | /*#define DEBUG 1*/ |
---|
20 | |
---|
21 | extern void PrintProc(procdefv pi); |
---|
22 | //void write_function(moddefv module, procdefv proc); |
---|
23 | static void write_function_declaration(procdefv pi, FILE *fp); |
---|
24 | static void write_procedure_header(moddefv module, procdefv pi, FILE *fmtfp); |
---|
25 | static void gen_func_param_check(FILE *fp, procdefv pi, int i); |
---|
26 | |
---|
27 | |
---|
28 | #define SELF_CMD MAX_TOK+1 |
---|
29 | |
---|
30 | /*========================================================================*/ |
---|
31 | void setup_proc( |
---|
32 | moddefv module, |
---|
33 | procdefv proc |
---|
34 | ) |
---|
35 | { |
---|
36 | /* if proc is NULL, just return */ |
---|
37 | if( proc == NULL ) return; |
---|
38 | |
---|
39 | if(trace) printf("\n\tcreating '%s'...", proc->procname); fflush(stdout); |
---|
40 | fprintf(module->modfp, "#line %d \"%s\"\n", proc->lineno, module->filename); |
---|
41 | modlineno+=1; |
---|
42 | |
---|
43 | switch(proc->language) |
---|
44 | { |
---|
45 | case LANG_C: |
---|
46 | // if(proc->funcname == NULL) proc->funcname = strdup(proc->procname); |
---|
47 | if(proc->return_val.typ == NONE) { |
---|
48 | if(proc->return_val.typname == NULL) { |
---|
49 | proc->return_val.typname = strdup("NONE"); |
---|
50 | } |
---|
51 | } |
---|
52 | |
---|
53 | /* write call to add procname to list */ |
---|
54 | fprintf(module->modfp, |
---|
55 | " psModulFunctions->iiAddCproc(currPack->libname,\"%s\",%s, mod_%s);\n", |
---|
56 | proc->procname, |
---|
57 | proc->is_static ? "TRUE" : "FALSE", |
---|
58 | proc->procname); |
---|
59 | modlineno+=1; |
---|
60 | |
---|
61 | fprintf(module->modfp, "\n"); |
---|
62 | modlineno+=1; |
---|
63 | |
---|
64 | write_procedure_header(module, proc, module->fmtfp); |
---|
65 | fprintf(module->modfp_h, |
---|
66 | "BOOLEAN mod_%s(leftv res, leftv h);\n", proc->procname); |
---|
67 | break; |
---|
68 | |
---|
69 | case LANG_SINGULAR: |
---|
70 | fprintf(module->modfp," if(ret!=-1) {\n"); |
---|
71 | fprintf(module->modfp, |
---|
72 | " h = add_singular_proc(binfp,\"%s\", %d, %ld, %ld, %s);\n", |
---|
73 | proc->procname, proc->lineno, |
---|
74 | proc->sing_start, proc->sing_end, |
---|
75 | proc->is_static ? "TRUE" : "FALSE"); |
---|
76 | fprintf(module->modfp," }\n"); |
---|
77 | modlineno+=1; |
---|
78 | break; |
---|
79 | } |
---|
80 | |
---|
81 | } |
---|
82 | |
---|
83 | /*========================================================================*/ |
---|
84 | void write_function_header( |
---|
85 | moddefv module, |
---|
86 | procdefv proc |
---|
87 | ) |
---|
88 | { |
---|
89 | if(!proc->flags.auto_header) return; |
---|
90 | if(!proc->flags.start_of_code) return; |
---|
91 | |
---|
92 | if(debug>4) |
---|
93 | { |
---|
94 | printf("write_function_header: auto=%d\n", proc->flags.auto_header); |
---|
95 | printf("write_function_header: declaration: do=%d, done=%d\n", |
---|
96 | proc->flags.do_declaration, |
---|
97 | proc->flags.declaration_done); |
---|
98 | printf("write_function_header: typecheck: do=%d, done=%d\n", |
---|
99 | proc->flags.do_typecheck, |
---|
100 | proc->flags.typecheck_done); |
---|
101 | } |
---|
102 | |
---|
103 | if(proc->flags.do_declaration && !proc->flags.declaration_done) |
---|
104 | write_function_declaration(module, proc, module->fmtfp); |
---|
105 | |
---|
106 | if(proc->flags.do_typecheck && !proc->flags.typecheck_done) |
---|
107 | write_function_typecheck(module, proc, module->fmtfp); |
---|
108 | } |
---|
109 | |
---|
110 | /*========================================================================*/ |
---|
111 | /* |
---|
112 | * write declaration of function to file pointed by 'fp', usualy the |
---|
113 | * header file. |
---|
114 | * |
---|
115 | * BOOLEAN mod_<procname>(leftv res, leftv h); |
---|
116 | */ |
---|
117 | static void write_function_declaration(procdefv pi, FILE *fp) |
---|
118 | { |
---|
119 | int i; |
---|
120 | |
---|
121 | #if 0 |
---|
122 | switch( pi->return_val.typ) |
---|
123 | { |
---|
124 | case SELF_CMD: |
---|
125 | fprintf(fp, "BOOLEAN %s(res, ", pi->funcname); |
---|
126 | break; |
---|
127 | |
---|
128 | default: |
---|
129 | fprintf(fp, "%s %s(", type_conv[pi->return_val.typ], |
---|
130 | pi->funcname); |
---|
131 | } |
---|
132 | for (i=0;i<pi->paramcnt; i++) |
---|
133 | { |
---|
134 | fprintf(fp, "%s %s%d", type_conv[pi->param[i].typ], i); |
---|
135 | if(i<pi->paramcnt-1) fprintf(fp, ", "); |
---|
136 | } |
---|
137 | fprintf(fp, ");\n\n"); |
---|
138 | #endif |
---|
139 | } |
---|
140 | |
---|
141 | /*========================================================================*/ |
---|
142 | void write_function_declaration( |
---|
143 | moddefv module, |
---|
144 | procdefv pi, |
---|
145 | void *arg |
---|
146 | ) |
---|
147 | { |
---|
148 | int i; |
---|
149 | if(debug>4) printf("write_function_declaration: do=%d, done=%d\n", |
---|
150 | pi->flags.do_declaration, pi->flags.declaration_done); |
---|
151 | if(pi->flags.declaration_done) return; |
---|
152 | if(!pi->flags.do_declaration) return; |
---|
153 | if(pi->paramcnt>0) |
---|
154 | { |
---|
155 | fprintf(module->fmtfp, "#line %d \"%s\"\n", yylineno, module->filename); |
---|
156 | fprintf(module->fmtfp, "#line @d \"%s.cc\"\n", module->name); |
---|
157 | fprintf(module->fmtfp, " leftv __v = __h, __v_save;\n"); |
---|
158 | fprintf(module->fmtfp, " int __tok = NONE, __index = 0;\n"); |
---|
159 | for (i=0;i<pi->paramcnt; i++) |
---|
160 | { |
---|
161 | fprintf(module->fmtfp, |
---|
162 | " sleftv __s%s; leftv __z%s = &__s%s;\n", pi->param[i].varname, |
---|
163 | pi->param[i].varname, pi->param[i].varname); |
---|
164 | fprintf(module->fmtfp, " %s %s;\n", type_conv[pi->param[i].typ], |
---|
165 | pi->param[i].varname); |
---|
166 | } |
---|
167 | |
---|
168 | fprintf(module->fmtfp, "#line %d \"%s\"\n", yylineno, module->filename); |
---|
169 | fprintf(module->fmtfp, "\n"); |
---|
170 | } |
---|
171 | pi->flags.declaration_done = 1; |
---|
172 | } |
---|
173 | |
---|
174 | /*========================================================================*/ |
---|
175 | void write_function_nodecl( |
---|
176 | moddefv module, |
---|
177 | procdefv pi, |
---|
178 | void *arg |
---|
179 | ) |
---|
180 | { |
---|
181 | pi->flags.do_declaration = 0; |
---|
182 | pi->flags.do_typecheck = 0; |
---|
183 | pi->flags.auto_header = 0; |
---|
184 | } |
---|
185 | |
---|
186 | /*========================================================================*/ |
---|
187 | void write_function_error( |
---|
188 | moddefv module, |
---|
189 | procdefv pi, |
---|
190 | void *arg |
---|
191 | ) |
---|
192 | { |
---|
193 | int i; |
---|
194 | |
---|
195 | fprintf(module->fmtfp, "#line %d \"%s\"\n", yylineno, module->filename); |
---|
196 | fprintf(module->fmtfp, "#line @d \"%s.cc\"\n", module->name); |
---|
197 | fprintf(module->fmtfp, "#line %d \"%s\"\n", yylineno, module->filename); |
---|
198 | fprintf(module->fmtfp, "\n"); |
---|
199 | } |
---|
200 | |
---|
201 | /*========================================================================*/ |
---|
202 | void write_function_typecheck( |
---|
203 | moddefv module, |
---|
204 | procdefv pi, |
---|
205 | void *arg |
---|
206 | ) |
---|
207 | { |
---|
208 | if(debug>4) printf("write_function_typecheck: do=%d, done=%d\n", |
---|
209 | pi->flags.do_typecheck, pi->flags.typecheck_done); |
---|
210 | if(pi->flags.typecheck_done) return; |
---|
211 | if(!pi->flags.do_typecheck) return; |
---|
212 | |
---|
213 | fprintf(module->fmtfp, "#line %d \"%s\"\n", yylineno, module->filename); |
---|
214 | fprintf(module->fmtfp, "#line @d \"%s.cc\"\n", module->name); |
---|
215 | write_procedure_typecheck(module, pi, module->fmtfp); |
---|
216 | fprintf(module->fmtfp, "#line %d \"%s\"\n", yylineno, module->filename); |
---|
217 | pi->flags.typecheck_done = 1; |
---|
218 | } |
---|
219 | |
---|
220 | /*========================================================================*/ |
---|
221 | void write_function_result( |
---|
222 | moddefv module, |
---|
223 | procdefv pi, |
---|
224 | void *arg |
---|
225 | ) |
---|
226 | { |
---|
227 | fprintf(module->fmtfp, " __res->data =%s\n", arg); |
---|
228 | pi->flags.result_done = 1; |
---|
229 | write_procedure_return(module, pi, module->fmtfp); |
---|
230 | } |
---|
231 | |
---|
232 | /*========================================================================*/ |
---|
233 | void write_function_return( |
---|
234 | moddefv module, |
---|
235 | procdefv pi, |
---|
236 | void *arg |
---|
237 | ) |
---|
238 | { |
---|
239 | int i; |
---|
240 | |
---|
241 | if(arg!=NULL) |
---|
242 | { |
---|
243 | if( pi->funcname != NULL ) free(pi->funcname); |
---|
244 | pi->funcname = strdup((char *)arg); |
---|
245 | if(debug>2)printf("CMD: return(%s)\n", arg); |
---|
246 | |
---|
247 | /* write function declaration to header file */ |
---|
248 | switch( pi->return_val.typ) |
---|
249 | { |
---|
250 | case SELF_CMD: |
---|
251 | fprintf(module->modfp_h, "BOOLEAN %s(__res, ", pi->funcname); |
---|
252 | break; |
---|
253 | |
---|
254 | default: |
---|
255 | fprintf(module->modfp_h, "//%s %s(", type_conv[pi->return_val.typ], |
---|
256 | pi->funcname); |
---|
257 | } |
---|
258 | for (i=0;i<pi->paramcnt; i++) |
---|
259 | { |
---|
260 | fprintf(module->modfp_h, "%s %s", type_conv[pi->param[i].typ], |
---|
261 | pi->param[i].varname); |
---|
262 | if(i<pi->paramcnt-1) fprintf(module->modfp_h, ", "); |
---|
263 | } |
---|
264 | fprintf(module->modfp_h, ");\n\n"); |
---|
265 | } |
---|
266 | else |
---|
267 | { |
---|
268 | if(debug>2)printf("CMD: return()\n"); |
---|
269 | } |
---|
270 | |
---|
271 | fprintf(module->fmtfp, "#line %d \"%s\"\n", yylineno, module->filename); |
---|
272 | write_procedure_return(module, pi, module->fmtfp); |
---|
273 | } |
---|
274 | |
---|
275 | /*========================================================================*/ |
---|
276 | void write_function_singularcmd( |
---|
277 | moddefv module, |
---|
278 | procdefv pi, |
---|
279 | void *arg |
---|
280 | ) |
---|
281 | { |
---|
282 | int i; |
---|
283 | |
---|
284 | if(trace)printf("\n\t\tsingular command block..."); |
---|
285 | fprintf(module->fmtfp, "#line @d \"%s.cc\"\n", module->name); |
---|
286 | fprintf(module->fmtfp, "/* code for running singular commands */\n"); |
---|
287 | fprintf(module->fmtfp, "/*\n"); |
---|
288 | fprintf(module->fmtfp, " * #line %d \"%s\"\n", yylineno, module->filename); |
---|
289 | fprintf(module->fmtfp, " *\n"); |
---|
290 | fprintf(module->fmtfp, " * get idhdl for '%s'\n", arg); |
---|
291 | fprintf(module->fmtfp, " * building leftv of arguments\n"); |
---|
292 | fprintf(module->fmtfp, " * sleftv cmdret = iiMake_proc(cmd, ???, sl)\n"); |
---|
293 | fprintf(module->fmtfp, " *\n"); |
---|
294 | fprintf(module->fmtfp, "### SINGULAR command: '%s'\n", arg); |
---|
295 | fprintf(module->fmtfp, "*/\n"); |
---|
296 | } |
---|
297 | |
---|
298 | /*========================================================================*/ |
---|
299 | /* |
---|
300 | * write declaration of function to file pointed by 'fp', usualy the |
---|
301 | * <module>.cc file |
---|
302 | * |
---|
303 | * BOOLEAN mod_<procname>(leftv res, leftv h) |
---|
304 | * { |
---|
305 | * block of automatic type-checking and conversation |
---|
306 | * |
---|
307 | * c++Code if any in the definition-file |
---|
308 | * |
---|
309 | */ |
---|
310 | static void write_procedure_header( |
---|
311 | moddefv module, |
---|
312 | procdefv pi, |
---|
313 | FILE *fmtfp |
---|
314 | ) |
---|
315 | { |
---|
316 | int cnt = 0, i; |
---|
317 | |
---|
318 | if(trace)printf("\n\t\theader..."); fflush(stdout); |
---|
319 | |
---|
320 | fprintf(fmtfp, "#line %d \"%s\"\n", pi->lineno, module->filename); |
---|
321 | #ifdef ix86_win |
---|
322 | fprintf(module->modfp, |
---|
323 | " __declspec(dllexport) BOOLEAN mod_%s", pi->procname); |
---|
324 | fprintf("(leftv __res, leftv __h);\n"); |
---|
325 | #endif /* ix86_win */ |
---|
326 | fprintf(fmtfp, "BOOLEAN mod_%s(leftv __res, leftv __h)\n{\n", pi->procname); |
---|
327 | } |
---|
328 | |
---|
329 | /*========================================================================*/ |
---|
330 | /* |
---|
331 | * write declaration of function to file pointed by 'fp', usualy the |
---|
332 | * <module>.cc file |
---|
333 | * |
---|
334 | * BOOLEAN mod_<procname>(leftv res, leftv h) |
---|
335 | * { |
---|
336 | * block of automatic type-checking and conversation |
---|
337 | * |
---|
338 | * c++Code if any in the definition-file |
---|
339 | * |
---|
340 | */ |
---|
341 | void write_procedure_typecheck( |
---|
342 | moddefv module, |
---|
343 | procdefv pi, |
---|
344 | FILE *fmtfp |
---|
345 | ) |
---|
346 | { |
---|
347 | int cnt = 0, i; |
---|
348 | |
---|
349 | if(!pi->flags.declaration_done) return; |
---|
350 | |
---|
351 | if(trace)printf("type check..."); fflush(stdout); |
---|
352 | |
---|
353 | if(pi->paramcnt>0) |
---|
354 | { |
---|
355 | /* loop over all parameters, for type-checking and conversation */ |
---|
356 | for (i=0;i<pi->paramcnt; i++) |
---|
357 | gen_func_param_check(fmtfp, pi, i); |
---|
358 | |
---|
359 | fprintf(fmtfp, |
---|
360 | " if(__v!=NULL) { __tok = __v->Typ(); goto mod_%s_error; }\n", |
---|
361 | pi->procname); |
---|
362 | |
---|
363 | fprintf(fmtfp, "\n"); |
---|
364 | |
---|
365 | if(trace)printf("\n\t\terror handling..."); fflush(stdout); |
---|
366 | |
---|
367 | fprintf(module->fmtfp, " goto mod_%s_ok;\n", pi->procname); |
---|
368 | |
---|
369 | |
---|
370 | fprintf(module->fmtfp, " mod_%s_error:\n", pi->procname); |
---|
371 | fprintf(module->fmtfp, " Werror(\"%s(`%%s`) is not supported\", Tok2Cmdname(__tok));\n", |
---|
372 | pi->procname); |
---|
373 | fprintf(module->fmtfp, " Werror(\"expected %s(", pi->procname); |
---|
374 | for (i=0;i<pi->paramcnt; i++) |
---|
375 | { |
---|
376 | fprintf(module->fmtfp, "'%s'", pi->param[i].name); |
---|
377 | if(i!=pi->paramcnt-1) fprintf(module->fmtfp, ","); |
---|
378 | } |
---|
379 | fprintf(module->fmtfp, ")\");\n"); |
---|
380 | fprintf(module->fmtfp, " return TRUE;\n"); |
---|
381 | |
---|
382 | fprintf(module->fmtfp, " mod_%s_ok:\n", pi->procname); |
---|
383 | } |
---|
384 | } |
---|
385 | |
---|
386 | /*========================================================================*/ |
---|
387 | void write_finish_functions( |
---|
388 | moddefv module, |
---|
389 | procdefv proc |
---|
390 | ) |
---|
391 | { |
---|
392 | fprintf(module->modfp, " if(ret!=-1) fclose(binfp);\n"); |
---|
393 | fprintf(module->modfp, " return 0;\n}\n}\n\n"); |
---|
394 | fflush(module->modfp); |
---|
395 | modlineno+=3; |
---|
396 | |
---|
397 | fprintf(module->modfp, "#line %d \"%s.cc\"\n", modlineno++, module->name); |
---|
398 | fprintf(module->modfp, "/* Help section */\n"); |
---|
399 | fprintf(module->modfp, "void fill_help_package() {\n"); |
---|
400 | modlineno+=3; |
---|
401 | mod_copy_tmp(module->modfp, module->fmtfp2); |
---|
402 | fprintf(module->modfp, "#line %d \"%s.cc\"\n", modlineno++, module->name); |
---|
403 | fprintf(module->modfp, "} /* End of Help section */\n\n"); |
---|
404 | modlineno+=3; |
---|
405 | |
---|
406 | fprintf(module->modfp, "/* Example section */\n"); |
---|
407 | fprintf(module->modfp, "void fill_example_package() {\n"); |
---|
408 | modlineno+=3; |
---|
409 | mod_copy_tmp(module->modfp, module->fmtfp3); |
---|
410 | fprintf(module->modfp, "#line %d \"%s.cc\"\n", modlineno++, module->name); |
---|
411 | fprintf(module->modfp, "} /* End of Example section */\n\n"); |
---|
412 | modlineno+=2; |
---|
413 | |
---|
414 | mod_copy_tmp(module->modfp, module->fmtfp); |
---|
415 | fprintf(module->modfp, "#line %d \"%s.cc\"\n", modlineno++, module->name); |
---|
416 | if(trace)printf(" done.\n");fflush(stdout); |
---|
417 | fclose(module->fmtfp); module->fmtfp = NULL; |
---|
418 | fclose(module->fmtfp2); module->fmtfp2 = NULL; |
---|
419 | fclose(module->fmtfp3); module->fmtfp3 = NULL; |
---|
420 | return; |
---|
421 | } |
---|
422 | |
---|
423 | /*========================================================================*/ |
---|
424 | /* automatic type-checking and conversation for one parameter */ |
---|
425 | static void gen_func_param_check( |
---|
426 | FILE *fp, |
---|
427 | procdefv pi, |
---|
428 | int i |
---|
429 | ) |
---|
430 | { |
---|
431 | fprintf(fp, " if(__v==NULL) goto mod_%s_error;\n", pi->procname); |
---|
432 | fprintf(fp, " __tok = __v->Typ();\n"); |
---|
433 | fprintf(fp, " if((__index=iiTestConvert(__tok, %s))==0)\n", |
---|
434 | pi->param[i].typname); |
---|
435 | fprintf(fp, " goto mod_%s_error;\n", pi->procname); |
---|
436 | fprintf(fp, " __v_save = __v->next;\n"); |
---|
437 | fprintf(fp, " __v->next = NULL;\n"); |
---|
438 | fprintf(fp, " if(iiConvert(__tok, %s, __index, __v, __z%s))\n", |
---|
439 | pi->param[i].typname, pi->param[i].varname); |
---|
440 | fprintf(fp, " goto mod_%s_error;\n", pi->procname); |
---|
441 | fprintf(fp, " __v = __v_save;\n"); |
---|
442 | fprintf(fp, " %s = (%s)(long)__z%s->Data();\n", pi->param[i].varname, |
---|
443 | type_conv[pi->param[i].typ], pi->param[i].varname); |
---|
444 | |
---|
445 | } |
---|
446 | |
---|
447 | /*========================================================================*/ |
---|
448 | /* |
---|
449 | * write end of function to file pointed by 'fp', usualy the |
---|
450 | * <module>.cc file |
---|
451 | * |
---|
452 | * ... |
---|
453 | * |
---|
454 | * |
---|
455 | */ |
---|
456 | void write_procedure_return( |
---|
457 | moddefv module, |
---|
458 | procdefv pi, |
---|
459 | FILE *fmtfp |
---|
460 | ) |
---|
461 | { |
---|
462 | int i; |
---|
463 | |
---|
464 | if(trace)printf("\n\t\treturn block..."); |
---|
465 | |
---|
466 | if(debug>2)printf("### RETURN: '%s' '%s' '%d'\n", pi->return_val.name, |
---|
467 | pi->return_val.typname, pi->return_val.typ); |
---|
468 | fprintf(module->fmtfp, "#line @d \"%s.cc\"\n", module->name); |
---|
469 | if(pi->funcname == NULL) |
---|
470 | { |
---|
471 | if(!pi->flags.result_done) fprintf(fmtfp, " __res->data = NULL;\n"); |
---|
472 | fprintf(fmtfp, " __res->rtyp = %s;\n", pi->return_val.typname); |
---|
473 | //fprintf(fmtfp, " res->rtyp = NONE;\n"); |
---|
474 | //fprintf(fmtfp, " res->data = NULL;\n"); |
---|
475 | fprintf(fmtfp, " return FALSE;\n"); |
---|
476 | } |
---|
477 | else |
---|
478 | { |
---|
479 | switch( pi->return_val.typ) |
---|
480 | { |
---|
481 | case SELF_CMD: |
---|
482 | fprintf(fmtfp, " return(%s(__res", pi->funcname); |
---|
483 | for (i=0;i<pi->paramcnt; i++) |
---|
484 | fprintf(fmtfp, ", (%s) __%s->Data()", |
---|
485 | type_conv[pi->param[i].typ], pi->param[i].varname); |
---|
486 | fprintf(fmtfp, "));\n\n"); |
---|
487 | break; |
---|
488 | |
---|
489 | case NONE: |
---|
490 | if(!pi->flags.result_done) fprintf(fmtfp, " __res->data = NULL;\n"); |
---|
491 | fprintf(fmtfp, " __res->rtyp = %s;\n", pi->return_val.typname); |
---|
492 | fprintf(fmtfp, " return(%s(", pi->funcname); |
---|
493 | for (i=0;i<pi->paramcnt; i++) { |
---|
494 | fprintf(fmtfp, "(%s) __%s->Data()", |
---|
495 | type_conv[pi->param[i].typ], pi->param[i].varname); |
---|
496 | if(i<pi->paramcnt-1) fprintf(fmtfp, ", "); |
---|
497 | } |
---|
498 | fprintf(fmtfp, "));\n\n"); |
---|
499 | break; |
---|
500 | |
---|
501 | default: |
---|
502 | fprintf(fmtfp, " __res->rtyp = %s;\n", pi->return_val.typname); |
---|
503 | fprintf(fmtfp, " __res->data = (void *)%s;\n", pi->funcname); |
---|
504 | fprintf(fmtfp, " if((__res->data != NULL)||(__res->rtyp==INT_CMD)) return FALSE;\n"); |
---|
505 | fprintf(fmtfp, " else return TRUE;\n\n"); |
---|
506 | } |
---|
507 | } |
---|
508 | } |
---|
509 | |
---|
510 | /*========================================================================*/ |
---|
511 | |
---|
512 | /*========================================================================*/ |
---|
513 | |
---|
514 | void write_function_errorhandling( |
---|
515 | moddefv module, |
---|
516 | procdefv pi |
---|
517 | ) |
---|
518 | { |
---|
519 | int cnt = 0, i; |
---|
520 | |
---|
521 | switch(pi->language) |
---|
522 | { |
---|
523 | case LANG_C: |
---|
524 | fprintf(module->fmtfp, "}\n\n"); |
---|
525 | break; |
---|
526 | case LANG_SINGULAR: |
---|
527 | fprintf(module->binfp, "}\n// end of procedure %s\n\n", pi->procname); |
---|
528 | break; |
---|
529 | } |
---|
530 | } |
---|
531 | |
---|
532 | /*========================================================================*/ |
---|
533 | void write_help( |
---|
534 | moddefv pModule, |
---|
535 | procdefv pi |
---|
536 | ) |
---|
537 | { |
---|
538 | unsigned int i; |
---|
539 | if(pi->help_string!=NULL) |
---|
540 | { |
---|
541 | fprintf(pModule->fmtfp2, "#line %d \"%s\"\n", pi->lineno_other, |
---|
542 | pModule->filename); |
---|
543 | fprintf(pModule->fmtfp2, " enter_id(\"%s_help\",", pi->procname); |
---|
544 | fprintf(pModule->fmtfp2, " \""); |
---|
545 | for(i=0; i<strlen(pi->help_string); i++) |
---|
546 | { |
---|
547 | if(pi->help_string[i]=='\n') fprintf(pModule->fmtfp2,"\\n"); |
---|
548 | else fprintf(pModule->fmtfp2,"%c", pi->help_string[i]); |
---|
549 | } |
---|
550 | fprintf(pModule->fmtfp2, "\", STRING_CMD);\n\n", pi->help_string); |
---|
551 | } |
---|
552 | } |
---|
553 | |
---|
554 | /*========================================================================*/ |
---|
555 | void write_example( |
---|
556 | moddefv module, |
---|
557 | procdefv pi |
---|
558 | ) |
---|
559 | { |
---|
560 | unsigned int i; |
---|
561 | /* if proc is NULL, just return */ |
---|
562 | if( pi == NULL ) return; |
---|
563 | |
---|
564 | if(pi->example_string!=NULL) |
---|
565 | { |
---|
566 | fprintf(module->fmtfp3, "#line %d \"%s\"\n", pi->lineno_other, |
---|
567 | module->filename); |
---|
568 | fprintf(module->fmtfp3, " enter_id(\"%s_example\",\n", pi->procname); |
---|
569 | fprintf(module->fmtfp3, " \""); |
---|
570 | for(i=0; i<strlen(pi->example_string); i++) |
---|
571 | { |
---|
572 | if(pi->example_string[i]=='\n') fprintf(module->fmtfp3,"\\n"); |
---|
573 | else fprintf(module->fmtfp3,"%c", pi->example_string[i]); |
---|
574 | } |
---|
575 | fprintf(module->fmtfp3, "\", STRING_CMD);\n\n", pi->example_string); |
---|
576 | } |
---|
577 | } |
---|
578 | |
---|
579 | /*========================================================================*/ |
---|
580 | int write_singular_procedures( |
---|
581 | moddefv module, |
---|
582 | procdefv proc |
---|
583 | ) |
---|
584 | { |
---|
585 | if(module->binfp==NULL) |
---|
586 | { |
---|
587 | char filename[512]; |
---|
588 | |
---|
589 | strcpy(filename, build_filename(module, module->targetname, 3)); |
---|
590 | |
---|
591 | if( (module->binfp = fopen(filename, "w")) == NULL) |
---|
592 | { |
---|
593 | return -1; |
---|
594 | } |
---|
595 | if(trace)printf("Creating %s, ", filename);fflush(stdout); |
---|
596 | } |
---|
597 | |
---|
598 | /* */ |
---|
599 | fprintf(module->binfp, "// line %d \"%s\"\n", proc->lineno, |
---|
600 | module->filename); |
---|
601 | fprintf(module->binfp, "// proc %s\n", proc->procname); |
---|
602 | fflush(module->binfp); |
---|
603 | proc->sing_start = ftell(module->binfp); |
---|
604 | |
---|
605 | return 0; |
---|
606 | } |
---|
607 | |
---|
608 | /*========================================================================*/ |
---|
609 | int write_helpfile_help( |
---|
610 | moddefv module, |
---|
611 | procdefv proc |
---|
612 | ) |
---|
613 | { |
---|
614 | if(module->docfp==NULL) |
---|
615 | { |
---|
616 | char filename[512]; |
---|
617 | |
---|
618 | strcpy(filename, build_filename(module, module->targetname, 4)); |
---|
619 | if( (module->docfp = fopen(filename, "w")) == NULL) |
---|
620 | { |
---|
621 | return -1; |
---|
622 | } |
---|
623 | if(trace) |
---|
624 | { |
---|
625 | printf("Creating %s, ", filename);fflush(stdout); |
---|
626 | } |
---|
627 | fprintf(module->docfp, "$library = \"%s.so\";\n", |
---|
628 | (module->targetname!=NULL) ? module->targetname : module->name); |
---|
629 | fprintf(module->docfp, "$version = \"%s\";\n", |
---|
630 | (module->revision!=NULL) ? module->revision : "none"); |
---|
631 | fprintf(module->docfp, "$category = <<EOT;\n"); |
---|
632 | fprintf(module->docfp, "%s\nEOT\n", |
---|
633 | (module->category!=NULL) ? module->category : "none"); |
---|
634 | fprintf(module->docfp, "$info = <<EOT;\n"); |
---|
635 | fprintf(module->docfp, "%s\nEOT\n", |
---|
636 | (module->info!=NULL) ? module->info : "none"); |
---|
637 | } |
---|
638 | |
---|
639 | /* */ |
---|
640 | fprintf(module->docfp, "push(@procs, \"%s\");\n",proc->procname); |
---|
641 | fprintf(module->docfp, "$help{\"%s\"} = <<EOT;\n",proc->procname); |
---|
642 | fprintf(module->docfp, "%s\nEOT\n", |
---|
643 | (proc->help_string!=NULL) ? proc->help_string : |
---|
644 | "No help available for this command"); |
---|
645 | if(proc->example_string!=NULL) |
---|
646 | { |
---|
647 | fprintf(module->docfp, "$example{\"%s\"} = <<EOT;\n",proc->procname); |
---|
648 | fprintf(module->docfp, "%s\nEOT\n",proc->example_string); |
---|
649 | } |
---|
650 | fprintf(module->docfp, "$chksum{\"%s\"} = 0;\n", proc->procname); |
---|
651 | |
---|
652 | return 0; |
---|
653 | } |
---|
654 | |
---|
655 | /*========================================================================*/ |
---|
656 | void write_singular_parameter( |
---|
657 | moddefv module, |
---|
658 | int lineno, |
---|
659 | char *typname, |
---|
660 | char *varname |
---|
661 | ) |
---|
662 | { |
---|
663 | fprintf(module->binfp, " parameter %s %s;\n", typname, varname); |
---|
664 | } |
---|
665 | |
---|
666 | /*========================================================================*/ |
---|
667 | void write_singular_end( |
---|
668 | moddefv module, |
---|
669 | procdefv proc, |
---|
670 | int lineno |
---|
671 | ) |
---|
672 | { |
---|
673 | fprintf(module->binfp, "%s.so\n",module->targetname); |
---|
674 | fflush(module->binfp); |
---|
675 | proc->sing_end = ftell(module->binfp); |
---|
676 | } |
---|
677 | |
---|
678 | /*========================================================================*/ |
---|
679 | void write_codeline( |
---|
680 | moddefv module, |
---|
681 | procdefv proc, |
---|
682 | char *line, |
---|
683 | int lineno |
---|
684 | ) |
---|
685 | { |
---|
686 | switch(proc->language) |
---|
687 | { |
---|
688 | case LANG_SINGULAR: |
---|
689 | if(lineno>=0) |
---|
690 | fprintf(module->binfp, "// #line %d \"%s\"\n", |
---|
691 | lineno, module->filename); |
---|
692 | fprintf(module->binfp,"%s",line); |
---|
693 | break; |
---|
694 | |
---|
695 | case LANG_C: |
---|
696 | write_function_header(module, proc); |
---|
697 | if(lineno>=0) |
---|
698 | { |
---|
699 | fprintf(module->fmtfp, "#line %d \"%s\"\n", |
---|
700 | lineno, module->filename); |
---|
701 | fprintf(module->fmtfp, "#line @d \"%s.cc\"\n", module->name); |
---|
702 | } |
---|
703 | fprintf(module->fmtfp, "%s", line); break; |
---|
704 | } |
---|
705 | } |
---|
706 | |
---|
707 | /*========================================================================*/ |
---|