1 | /* |
---|
2 | * |
---|
3 | */ |
---|
4 | |
---|
5 | #include <stdio.h> |
---|
6 | #include <stddef.h> |
---|
7 | #include <stdlib.h> |
---|
8 | #include <stdarg.h> |
---|
9 | #include <string.h> |
---|
10 | #include <unistd.h> |
---|
11 | |
---|
12 | #include "config.h" |
---|
13 | |
---|
14 | #ifdef HAVE_GETOPT_LONG |
---|
15 | #include <getopt.h> |
---|
16 | #else |
---|
17 | #include "my_getopt.h" |
---|
18 | #endif |
---|
19 | |
---|
20 | #include "modgen.h" |
---|
21 | #include "pathnames.h" |
---|
22 | #include "stype.h" |
---|
23 | |
---|
24 | /* |
---|
25 | * Syntax of a module file: |
---|
26 | * %{ |
---|
27 | * //here may come c/C++ code |
---|
28 | * %} |
---|
29 | * // start of section 1 |
---|
30 | * module="module-name"; |
---|
31 | * version=""; |
---|
32 | * info=""; |
---|
33 | * |
---|
34 | * cxxsource=misc.cc |
---|
35 | * |
---|
36 | * %% start of section 2 |
---|
37 | * |
---|
38 | * proc [<return-type>] <procname>[(argtypelist)] { |
---|
39 | * |
---|
40 | * } |
---|
41 | * |
---|
42 | * %% start of section 3 |
---|
43 | * // some other c/C++ code |
---|
44 | * |
---|
45 | */ |
---|
46 | |
---|
47 | /* |
---|
48 | * How does the module generator works: |
---|
49 | * 1. start parsing the module-definition file |
---|
50 | * 1.1 write c/C++ code into a temporary file |
---|
51 | * 1.2 get modulename in section 1 |
---|
52 | * 2 arrived at the beginnig of section |
---|
53 | * 2.1 write header of .cc and .h files |
---|
54 | * 2.2 append tmpfile to .cc file |
---|
55 | * 2.3 parse section 2 |
---|
56 | * 3. generate code for each proc definition |
---|
57 | * 4. append section 3 to .cc file |
---|
58 | * 5. generate Makefile |
---|
59 | * 6. finished |
---|
60 | */ |
---|
61 | |
---|
62 | EXTERN_VAR FILE* yyin; |
---|
63 | EXTERN_VAR int yylineno; |
---|
64 | EXTERN_VAR char *yytext; |
---|
65 | |
---|
66 | INST_VAR moddef module_def; |
---|
67 | |
---|
68 | extern int iiInitArithmetic(); |
---|
69 | int init_modgen( |
---|
70 | moddefv module_def, |
---|
71 | char *filename |
---|
72 | ); |
---|
73 | |
---|
74 | extern int yyparse (void); |
---|
75 | extern void init_type_conv(); |
---|
76 | VAR int debug = 0; |
---|
77 | VAR int trace = 0; |
---|
78 | VAR int do_create_makefile = 1; |
---|
79 | VAR int do_create_srcdir = 1; |
---|
80 | VAR char* inst_dir = "${SINGULARROOT}/${SINGUNAME}"; |
---|
81 | |
---|
82 | #ifdef IRIX_6 |
---|
83 | struct option |
---|
84 | { |
---|
85 | const char *name; |
---|
86 | /* has_arg can't be an enum because some compilers complain about |
---|
87 | type mismatches in all the code that assumes it is an int. */ |
---|
88 | int has_arg; |
---|
89 | int *flag; |
---|
90 | int val; |
---|
91 | }; |
---|
92 | #endif |
---|
93 | STATIC_VAR struct option long_options[] = |
---|
94 | { |
---|
95 | {"debug", 0, 0, 'd'}, |
---|
96 | {"help", 0, 0, '?'}, |
---|
97 | {"install-dir",1,0,'i'}, |
---|
98 | {"nocreate-makefile", 0, 0, 'm'}, |
---|
99 | {"nocreate-srcdir", 0, 0, 's'}, |
---|
100 | {"verbose", 0, 0, 'v'}, |
---|
101 | {0, 0, 0, 0} |
---|
102 | }; |
---|
103 | |
---|
104 | |
---|
105 | void usage(char *name) |
---|
106 | { |
---|
107 | int i; |
---|
108 | printf("usage: %s [args] filename\n", name); |
---|
109 | for(i=0; long_options[i].name != NULL; i++) |
---|
110 | { |
---|
111 | if(long_options[i].has_arg!=0) |
---|
112 | { |
---|
113 | switch(long_options[i].val) { |
---|
114 | case 'i': |
---|
115 | printf("\t-%c (--%s) %s\n", long_options[i].val, |
---|
116 | long_options[i].name, "<destination-dir>"); |
---|
117 | break; |
---|
118 | default: |
---|
119 | printf("\t-%c (--%s) %s\n", long_options[i].val, |
---|
120 | long_options[i].name, "<argument>"); |
---|
121 | } |
---|
122 | } else |
---|
123 | { |
---|
124 | printf("\t-%c (--%s) %s\n", long_options[i].val, |
---|
125 | long_options[i].name, ""); |
---|
126 | } |
---|
127 | } |
---|
128 | } |
---|
129 | |
---|
130 | main( int argc, char *argv[] ) |
---|
131 | { |
---|
132 | int i; |
---|
133 | int c; |
---|
134 | int option_index = 0; |
---|
135 | unsigned long cksm; |
---|
136 | |
---|
137 | #ifdef IRIX_6 |
---|
138 | while( (c=getopt (argc,argv, "dmvsi:")) != -1) |
---|
139 | #else |
---|
140 | while( (c=getopt_long (argc, argv, "dmvsi:", |
---|
141 | long_options, &option_index))>=0) |
---|
142 | #endif |
---|
143 | { |
---|
144 | switch (c) |
---|
145 | { |
---|
146 | case 'd' : debug++; break; |
---|
147 | case 'v' : trace=1; break; |
---|
148 | case 'm' : do_create_makefile = 0; break; |
---|
149 | case 'i' : inst_dir=optarg; break; |
---|
150 | case 's' : do_create_srcdir = 0; break; |
---|
151 | |
---|
152 | case '?' : usage(argv[0]); |
---|
153 | return 0; |
---|
154 | break; |
---|
155 | |
---|
156 | default: |
---|
157 | printf ("?? getopt returned character code 0%o ??\n", c); |
---|
158 | } |
---|
159 | } |
---|
160 | |
---|
161 | if (optind < argc) yyin = fopen( argv[optind], "rb" ); |
---|
162 | else { |
---|
163 | printf("no filename given\n"); |
---|
164 | usage(argv[0]); |
---|
165 | return 1; |
---|
166 | } |
---|
167 | |
---|
168 | iiInitArithmetic(); |
---|
169 | if(init_modgen(&module_def, argv[optind])) return 1; |
---|
170 | init_type_conv(); |
---|
171 | do { |
---|
172 | i=yyparse(); |
---|
173 | switch(i) { |
---|
174 | case 0: |
---|
175 | printf("FINISH? %s (%d)\n", yytext, yylineno); break; |
---|
176 | case 1: |
---|
177 | if(debug>2) |
---|
178 | printf("NEXT LOOP at line %d (%s) %d\n", yylineno, yytext, i); |
---|
179 | if(strlen(yytext)) |
---|
180 | printf("something went wrong at line %d\n", yylineno); |
---|
181 | break; |
---|
182 | default: |
---|
183 | break; |
---|
184 | } |
---|
185 | } |
---|
186 | while (!i); |
---|
187 | if(trace)printf("files for module '%s' created.\n", argv[optind]); |
---|
188 | |
---|
189 | //fflush(module_def.fmtfp); |
---|
190 | //PrintProclist(&module_def); |
---|
191 | //mod_create_makefile(&module_def); |
---|
192 | if(module_def.fmtfp != NULL) fclose(module_def.fmtfp); |
---|
193 | if(module_def.modfp != NULL) fclose(module_def.modfp); |
---|
194 | if(module_def.docfp != NULL) { |
---|
195 | fprintf(module_def.docfp,"1;"); |
---|
196 | fclose(module_def.docfp); |
---|
197 | } |
---|
198 | if(module_def.binfp != NULL) { |
---|
199 | //we have been writing to it previously |
---|
200 | fclose(module_def.binfp); |
---|
201 | cksm = crccheck(build_filename(&module_def,module_def.targetname,3)); |
---|
202 | } else { |
---|
203 | //we have not been writing to it or we could not open it |
---|
204 | if((module_def.binfp=fopen( |
---|
205 | build_filename(&module_def,module_def.targetname,3),"w"))!=NULL) { |
---|
206 | fprintf(module_def.binfp,"// no Singular procedure for this module\n"); |
---|
207 | fclose(module_def.binfp); |
---|
208 | cksm = crccheck(build_filename(&module_def,module_def.targetname,3)); |
---|
209 | } else { |
---|
210 | printf("Cannot open .bin file!!\n"); |
---|
211 | } |
---|
212 | } |
---|
213 | if(module_def.modfp_h != NULL) { |
---|
214 | fprintf(module_def.modfp_h,"unsigned long crcsum=%lu;\n",cksm); |
---|
215 | write_crctable(module_def.modfp_h); |
---|
216 | fclose(module_def.modfp_h); |
---|
217 | } |
---|
218 | return 0; |
---|
219 | } |
---|