1 | /**************************************** |
---|
2 | * Computer Algebra System SINGULAR * |
---|
3 | ****************************************/ |
---|
4 | /* $Id: creat_top.cc,v 1.23 2006-04-11 19:47:32 anne Exp $ */ |
---|
5 | /* |
---|
6 | * ABSTRACT: lib parsing |
---|
7 | */ |
---|
8 | |
---|
9 | #include <mod2.h> |
---|
10 | |
---|
11 | #include "modgen.h" |
---|
12 | #include <sys/stat.h> |
---|
13 | #include <sys/types.h> |
---|
14 | #include <fcntl.h> |
---|
15 | #include <unistd.h> |
---|
16 | |
---|
17 | |
---|
18 | extern int yylineno; |
---|
19 | extern int do_create_srcdir; |
---|
20 | |
---|
21 | void enter_id(FILE *fp, char *name, char *value); |
---|
22 | /*========================================================================*/ |
---|
23 | void mod_copy_tmp( |
---|
24 | FILE *fp_out, |
---|
25 | FILE *fp_in |
---|
26 | ) |
---|
27 | { |
---|
28 | int nread; |
---|
29 | char buf[1024]; |
---|
30 | |
---|
31 | fseek(fp_in, 0L, 0); |
---|
32 | do { |
---|
33 | memset(buf, 0, sizeof(buf)); |
---|
34 | fgets(buf, sizeof(buf), fp_in); |
---|
35 | modlineno++; |
---|
36 | //nread = fread (buf, sizeof (char), sizeof(buf)-1, fp_in); |
---|
37 | if(strncmp(buf, "#line @d ", 8)==0) { |
---|
38 | buf[6]='%'; |
---|
39 | fprintf(fp_out, buf, modlineno); |
---|
40 | } |
---|
41 | else |
---|
42 | fwrite(buf, sizeof(char), strlen(buf)/*nread*/, fp_out); |
---|
43 | } while(!feof(fp_in)); |
---|
44 | fputs("\n", fp_out); |
---|
45 | } |
---|
46 | |
---|
47 | /*========================================================================*/ |
---|
48 | void write_enter_id(FILE *fp) |
---|
49 | { |
---|
50 | fprintf(fp, "\nidhdl enter_id(char *name, char *value, idtyp t)\n"); |
---|
51 | fprintf(fp, "{\n"); |
---|
52 | fprintf(fp, " idhdl h;\n"); |
---|
53 | fprintf(fp, "\n"); |
---|
54 | fprintf(fp, " h=enterid(omStrDup(name),0, t, &(currPack->idroot), TRUE/*FALSE*/);\n"); |
---|
55 | fprintf(fp, " if(h!=NULL) {\n"); |
---|
56 | fprintf(fp, " switch(t) {\n"); |
---|
57 | fprintf(fp, " case STRING_CMD: \n"); |
---|
58 | fprintf(fp, " omFree(IDSTRING(h));\n"); |
---|
59 | fprintf(fp, " IDSTRING(h) = omStrDup(value);\n"); |
---|
60 | fprintf(fp, " break;\n"); |
---|
61 | fprintf(fp, " case PACKAGE_CMD: break;\n"); |
---|
62 | fprintf(fp, " case PROC_CMD: break;\n"); |
---|
63 | fprintf(fp, " }\n"); |
---|
64 | fprintf(fp, " } else \n"); |
---|
65 | fprintf(fp, " Warn(\"Cannot create '%%s'\\n\", name);\n"); |
---|
66 | fprintf(fp, " return(h);\n"); |
---|
67 | fprintf(fp, "}\n"); |
---|
68 | modlineno+=16; |
---|
69 | } |
---|
70 | |
---|
71 | /*========================================================================*/ |
---|
72 | void write_add_singular_proc(FILE *fp) |
---|
73 | { |
---|
74 | fprintf(fp, "\nidhdl add_singular_proc(FILE* binfp, char *procname,int line,\n"); |
---|
75 | fprintf(fp, " long pos, long end, BOOLEAN pstatic)\n"); |
---|
76 | fprintf(fp, "{\n"); |
---|
77 | fprintf(fp, " idhdl h;\n"); |
---|
78 | fprintf(fp, " procinfov pi;\n"); |
---|
79 | fprintf(fp, " char *tempstr;\n"); |
---|
80 | fprintf(fp, " h = enter_id(procname, NULL, PROC_CMD);\n"); |
---|
81 | fprintf(fp, " if(h == NULL) return NULL;\n"); |
---|
82 | fprintf(fp, "\n"); |
---|
83 | fprintf(fp, " pi = IDPROC(h);\n"); |
---|
84 | fprintf(fp, "\n"); |
---|
85 | fprintf(fp, " pi->libname = omStrDup(currPack->libname);\n"); |
---|
86 | fprintf(fp, " pi->procname = omStrDup(procname);\n"); |
---|
87 | fprintf(fp, " pi->language = LANG_SINGULAR;\n"); |
---|
88 | fprintf(fp, " pi->ref = 1;\n"); |
---|
89 | fprintf(fp, " pi->is_static = pstatic;\n"); |
---|
90 | fprintf(fp, " pi->data.s.proc_start = pos;\n"); |
---|
91 | fprintf(fp, " pi->data.s.def_end = pos;\n"); |
---|
92 | fprintf(fp, " pi->data.s.help_start = 0L;\n"); |
---|
93 | fprintf(fp, " pi->data.s.help_end = 0L;\n"); |
---|
94 | fprintf(fp, " pi->data.s.body_start = pos;\n"); |
---|
95 | fprintf(fp, " pi->data.s.body_end = end;\n"); |
---|
96 | fprintf(fp, " pi->data.s.proc_end = end;\n"); |
---|
97 | fprintf(fp, " pi->data.s.example_start = 0L;\n"); |
---|
98 | fprintf(fp, " pi->data.s.proc_lineno = line;\n"); |
---|
99 | fprintf(fp, " pi->data.s.body_lineno = line;\n"); |
---|
100 | fprintf(fp, " pi->data.s.example_lineno = 0;\n"); |
---|
101 | fprintf(fp, " pi->data.s.help_chksum = 0;\n"); |
---|
102 | fprintf(fp, " \n"); |
---|
103 | fprintf(fp, " rewind(binfp);\n"); |
---|
104 | fprintf(fp, " fseek(binfp,pos,SEEK_CUR);\n"); |
---|
105 | fprintf(fp, " tempstr=(char *)omAlloc(end-pos+2);\n"); |
---|
106 | fprintf(fp, " memset(tempstr,0,end-pos+2);\n"); |
---|
107 | fprintf(fp, " fread(tempstr,sizeof(char),end-pos+1,binfp);\n"); |
---|
108 | fprintf(fp, " pi->data.s.body = omStrDup(tempstr);\n"); |
---|
109 | fprintf(fp, " omFree(tempstr);\n\n"); |
---|
110 | fprintf(fp, " return(h);\n"); |
---|
111 | fprintf(fp, "}\n"); |
---|
112 | modlineno+=40; |
---|
113 | } |
---|
114 | |
---|
115 | /*========================================================================*/ |
---|
116 | void write_mod_init( |
---|
117 | moddefv module, |
---|
118 | FILE *fp |
---|
119 | ) |
---|
120 | { |
---|
121 | fprintf(fp, "\n\n"); |
---|
122 | fprintf(fp, "#line @d \"%s.cc\"\n", module->name); |
---|
123 | fprintf(fp, "extern \"C\" {\n"); |
---|
124 | fprintf(fp, "int mod_init(\n int (*iiAddCproc)("); |
---|
125 | #if 1 |
---|
126 | fprintf(fp, "char *libname, char *procname, BOOLEAN pstatic,\n"); |
---|
127 | fprintf(fp, " BOOLEAN(*func)(leftv res, leftv v)"); |
---|
128 | #endif |
---|
129 | fprintf(fp, ")\n )\n{\n"); |
---|
130 | fprintf(fp, " idhdl h;\n"); |
---|
131 | fprintf(fp, " char * tempstr;\n"); |
---|
132 | fprintf(fp, " char * tailstr;\n"); |
---|
133 | fprintf(fp, " FILE * binfp; \n"); |
---|
134 | fprintf(fp, " int ret;\n"); |
---|
135 | fprintf(fp, " struct stat sb; \n\n"); |
---|
136 | fprintf(fp, " tempstr = (char *)omAlloc(strlen(currPack->libname)+5);\n"); |
---|
137 | fprintf(fp, " memset(tempstr,0,strlen(currPack->libname)+5);\n"); |
---|
138 | fprintf(fp, " tailstr = strchr(currPack->libname,'.');\n"); |
---|
139 | fprintf(fp, " memcpy(tempstr,currPack->libname,strlen(currPack->libname)-strlen(tailstr));\n"); |
---|
140 | fprintf(fp, " memcpy(tempstr+strlen(tempstr),\".bin\",4);\n"); |
---|
141 | fprintf(fp, " ret=stat(tempstr,&sb);\n"); |
---|
142 | fprintf(fp, " if(ret==0) { \n"); |
---|
143 | fprintf(fp, " if ((sb.st_mode & S_IFMT) == S_IFREG) { \n"); |
---|
144 | fprintf(fp, " if (crccheck(tempstr)!=crcsum)\n"); |
---|
145 | fprintf(fp, " { Warn(\"file %%s does not agree with module version - ignoring file\",tempstr);\n"); |
---|
146 | fprintf(fp, " ret=-1;\n }\n"); |
---|
147 | fprintf(fp, " if ((binfp = fopen(tempstr,\"r\")) == NULL) return -1;\n"); |
---|
148 | fprintf(fp, " } \n else \n ret=-1; \n }\n\n"); |
---|
149 | fprintf(fp, " fill_help_package();\n"); |
---|
150 | fprintf(fp, " fill_example_package();\n\n"); |
---|
151 | } |
---|
152 | |
---|
153 | /*========================================================================*/ |
---|
154 | int write_intro( |
---|
155 | moddefv module |
---|
156 | ) |
---|
157 | { |
---|
158 | char filename[512]; |
---|
159 | |
---|
160 | if(do_create_srcdir) mkdir(module->name, 0755); |
---|
161 | strcpy(filename, build_filename(module, module->name, 1)); |
---|
162 | |
---|
163 | fflush(module->fmtfp); |
---|
164 | |
---|
165 | if( (module->modfp = fopen(filename, "w")) == NULL) { |
---|
166 | //free(filename); |
---|
167 | return -1; |
---|
168 | } |
---|
169 | if(trace)printf("Creating %s, ", filename);fflush(stdout); |
---|
170 | mod_write_header(module->modfp, module->name, 'c'); |
---|
171 | mod_copy_tmp(module->modfp, module->fmtfp); |
---|
172 | if(trace)printf("\n");fflush(stdout); |
---|
173 | fclose(module->fmtfp); module->fmtfp = NULL; |
---|
174 | if(create_tmpfile(module)) return -1; |
---|
175 | if(create_tmpfile(module, 1)) return -1; |
---|
176 | if(create_tmpfile(module, 2)) return -1; |
---|
177 | if(module->fmtfp2 == NULL) { printf("Cannot write HELP\n"); return -1; } |
---|
178 | if(module->fmtfp3 == NULL) { printf("Cannot write EXAMPLE\n"); return -1; } |
---|
179 | |
---|
180 | strcpy(filename, build_filename(module, module->name, 2)); |
---|
181 | //sprintf(filename, "%s/%s.h", module->name, module->name); |
---|
182 | if( (module->modfp_h = fopen(filename, "w")) == NULL) { |
---|
183 | //free(filename); |
---|
184 | return -1; |
---|
185 | } |
---|
186 | if(trace)printf("Creating %s, ", filename);fflush(stdout); |
---|
187 | mod_write_header(module->modfp_h, module->name, 'h'); |
---|
188 | if(trace)printf("\n");fflush(stdout); |
---|
189 | |
---|
190 | //free(filename); |
---|
191 | // write_enter_id(module->modfp); |
---|
192 | return 0; |
---|
193 | } |
---|