1 | /* |
---|
2 | * This was automatically generated by modgen |
---|
3 | * version 0.2 |
---|
4 | * module python |
---|
5 | * Don't edit this file |
---|
6 | */ |
---|
7 | |
---|
8 | |
---|
9 | #line 10 "python.cc" |
---|
10 | #include <stdlib.h> |
---|
11 | #include <stdio.h> |
---|
12 | #include <string.h> |
---|
13 | #include <ctype.h> |
---|
14 | |
---|
15 | #include <unistd.h> |
---|
16 | #include <sys/stat.h> |
---|
17 | #include <boost/python.hpp> |
---|
18 | #include <Python.h> |
---|
19 | #include <kernel/mod2.h> |
---|
20 | #include <tok.h> |
---|
21 | #include <structs.h> |
---|
22 | #include <ipid.h> |
---|
23 | |
---|
24 | #include <locals.h> |
---|
25 | #include <omalloc.h> |
---|
26 | #include "python.h" |
---|
27 | #line 19 "python.cc" |
---|
28 | |
---|
29 | idhdl enter_id(char *name, char *value, idtyp t) |
---|
30 | { |
---|
31 | idhdl h; |
---|
32 | |
---|
33 | h=enterid(omStrDup(name),0, t, &(currPack->idroot), TRUE/*FALSE*/); |
---|
34 | if(h!=NULL) { |
---|
35 | switch(t) { |
---|
36 | case STRING_CMD: |
---|
37 | omFree(IDSTRING(h)); |
---|
38 | IDSTRING(h) = omStrDup(value); |
---|
39 | break; |
---|
40 | case PACKAGE_CMD: break; |
---|
41 | case PROC_CMD: break; |
---|
42 | } |
---|
43 | } else |
---|
44 | Warn("Cannot create '%s'\n", name); |
---|
45 | return(h); |
---|
46 | } |
---|
47 | |
---|
48 | #line 37 "python.cc" |
---|
49 | |
---|
50 | idhdl add_singular_proc(FILE* binfp, char *procname,int line, |
---|
51 | long pos, long end, BOOLEAN pstatic) |
---|
52 | { |
---|
53 | idhdl h; |
---|
54 | procinfov pi; |
---|
55 | char *tempstr; |
---|
56 | h = enter_id(procname, NULL, PROC_CMD); |
---|
57 | if(h == NULL) return NULL; |
---|
58 | |
---|
59 | pi = IDPROC(h); |
---|
60 | |
---|
61 | pi->libname = omStrDup(currPack->libname); |
---|
62 | pi->procname = omStrDup(procname); |
---|
63 | pi->language = LANG_SINGULAR; |
---|
64 | pi->ref = 1; |
---|
65 | pi->is_static = pstatic; |
---|
66 | pi->data.s.proc_start = pos; |
---|
67 | pi->data.s.def_end = pos; |
---|
68 | pi->data.s.help_start = 0L; |
---|
69 | pi->data.s.help_end = 0L; |
---|
70 | pi->data.s.body_start = pos; |
---|
71 | pi->data.s.body_end = end; |
---|
72 | pi->data.s.proc_end = end; |
---|
73 | pi->data.s.example_start = 0L; |
---|
74 | pi->data.s.proc_lineno = line; |
---|
75 | pi->data.s.body_lineno = line; |
---|
76 | pi->data.s.example_lineno = 0; |
---|
77 | pi->data.s.help_chksum = 0; |
---|
78 | |
---|
79 | rewind(binfp); |
---|
80 | fseek(binfp,pos,SEEK_CUR); |
---|
81 | tempstr=(char *)omAlloc(end-pos+2); |
---|
82 | memset(tempstr,0,end-pos+2); |
---|
83 | fread(tempstr,sizeof(char),end-pos+1,binfp); |
---|
84 | pi->data.s.body = omStrDup(tempstr); |
---|
85 | omFree(tempstr); |
---|
86 | |
---|
87 | return(h); |
---|
88 | } |
---|
89 | |
---|
90 | unsigned long crccheck( |
---|
91 | char *file |
---|
92 | ) |
---|
93 | { |
---|
94 | unsigned char buf[BUFLEN1]; |
---|
95 | unsigned long crc = 0; |
---|
96 | long length = 0; |
---|
97 | long bytes_read; |
---|
98 | register FILE *fp; |
---|
99 | |
---|
100 | // open the file |
---|
101 | fp = fopen (file, "rb"); |
---|
102 | if (fp == NULL) return 0; |
---|
103 | |
---|
104 | // read the file chunk by chunk, determine the lenght and |
---|
105 | // start computing the checksum |
---|
106 | while ((bytes_read = fread (buf, 1, BUFLEN1, fp)) > 0) |
---|
107 | { |
---|
108 | unsigned char *cp = buf; |
---|
109 | length += bytes_read; |
---|
110 | while (bytes_read--) |
---|
111 | crc = (crc << 8) ^ crctab[((crc >> 24) ^ *(cp++)) & 0xFF]; |
---|
112 | } |
---|
113 | |
---|
114 | // check if something went wrong and close the file |
---|
115 | if (ferror (fp)) return 0; |
---|
116 | if (fclose(fp)==EOF) return 0; |
---|
117 | |
---|
118 | // second part of checksum computation |
---|
119 | bytes_read = length; |
---|
120 | while (bytes_read > 0) { |
---|
121 | crc = (crc << 8) ^ crctab[((crc >> 24) ^ bytes_read) & 0xFF]; |
---|
122 | bytes_read >>= 8; |
---|
123 | } |
---|
124 | |
---|
125 | crc = ~crc & 0xFFFFFFFF; |
---|
126 | |
---|
127 | return(crc); |
---|
128 | } |
---|
129 | |
---|
130 | void fill_help_package(); |
---|
131 | void fill_example_package(); |
---|
132 | |
---|
133 | #line 2 "python.mod" |
---|
134 | /* |
---|
135 | * |
---|
136 | * Test mod fuer modgen |
---|
137 | */ |
---|
138 | |
---|
139 | #include <stdio.h> |
---|
140 | #include <stdlib.h> |
---|
141 | #include <string.h> |
---|
142 | #include <time.h> |
---|
143 | #include <Python.h> |
---|
144 | #include "wrapper.h" |
---|
145 | |
---|
146 | void mbpython(char* in); |
---|
147 | #line 17 "python.mod" |
---|
148 | |
---|
149 | |
---|
150 | #line 137 "python.cc" |
---|
151 | extern "C" { |
---|
152 | //int mod_init( |
---|
153 | // int (*iiAddCproc)(char *libname, char *procname, BOOLEAN pstatic, |
---|
154 | // BOOLEAN(*func)(leftv res, leftv v)) |
---|
155 | // ) |
---|
156 | int mod_init( |
---|
157 | SModulFunctions* psModulFunctions |
---|
158 | ) |
---|
159 | { |
---|
160 | idhdl h; |
---|
161 | char * tempstr; |
---|
162 | FILE * binfp; |
---|
163 | int ret; |
---|
164 | struct stat sb; |
---|
165 | |
---|
166 | tempstr = (char *)omAlloc(strlen(currPack->libname)+5); |
---|
167 | memset(tempstr,0,strlen(currPack->libname)+1); |
---|
168 | memcpy(tempstr,currPack->libname,strlen(currPack->libname)); |
---|
169 | memcpy(tempstr+strlen(currPack->libname)-3,".bin",4); |
---|
170 | ret=stat(tempstr,&sb); |
---|
171 | if(ret==0) { |
---|
172 | if ((sb.st_mode & S_IFMT) == S_IFREG) { |
---|
173 | if (crccheck(tempstr)!=crcsum) |
---|
174 | { Warn("file %s does not agree with module version - ignoring file",tempstr); |
---|
175 | ret=-1; |
---|
176 | } |
---|
177 | if ((binfp = fopen(tempstr,"r")) == NULL) return -1; |
---|
178 | } |
---|
179 | else |
---|
180 | ret=-1; |
---|
181 | } |
---|
182 | |
---|
183 | fill_help_package(); |
---|
184 | fill_example_package(); |
---|
185 | |
---|
186 | #line 20 "python.mod" |
---|
187 | enter_id("category","tests", STRING_CMD); |
---|
188 | #line 22 "python.mod" |
---|
189 | enter_id("version","$Id$", STRING_CMD); |
---|
190 | #line 23 "python.mod" |
---|
191 | enter_id("info","LIBRARY: kernel.lib PROCEDURES OF GENERAL TYPE WRITEN IN C python(input); eval a string in python", STRING_CMD); |
---|
192 | #line 26 "python.mod" |
---|
193 | #line 27 "python.mod" |
---|
194 | Py_Initialize(); |
---|
195 | PyRun_SimpleString("from sys import path\n\ |
---|
196 | path.insert(0,'.')\n"); |
---|
197 | initSingular(); |
---|
198 | init_Singular(); |
---|
199 | |
---|
200 | #line 36 "python.mod" |
---|
201 | psModulFunctions->iiAddCproc(currPack->libname,"python",FALSE, mod_python); |
---|
202 | //psModulFunctions->iiAddCproc("packagename","procname",FALSE, procedure); |
---|
203 | if(ret!=-1) fclose(binfp); |
---|
204 | return 0; |
---|
205 | } |
---|
206 | } |
---|
207 | |
---|
208 | #line 187 "python.cc" |
---|
209 | /* Help section */ |
---|
210 | void fill_help_package() { |
---|
211 | |
---|
212 | #line 192 "python.cc" |
---|
213 | } /* End of Help section */ |
---|
214 | |
---|
215 | /* Example section */ |
---|
216 | void fill_example_package() { |
---|
217 | #line 43 "python.mod" |
---|
218 | enter_id("python_example", |
---|
219 | "\ |
---|
220 | python(\"print 1+1\");\ |
---|
221 | ", STRING_CMD); |
---|
222 | |
---|
223 | |
---|
224 | #line 206 "python.cc" |
---|
225 | } /* End of Example section */ |
---|
226 | |
---|
227 | #line 36 "python.mod" |
---|
228 | BOOLEAN mod_python(leftv __res, leftv __h) |
---|
229 | { |
---|
230 | #line 37 "python.mod" |
---|
231 | #line 214 "python.cc" |
---|
232 | |
---|
233 | #line 38 "python.mod" |
---|
234 | #line 217 "python.cc" |
---|
235 | leftv __v = __h, __v_save; |
---|
236 | int __tok = NONE, __index = 0; |
---|
237 | sleftv __sa; leftv __za = &__sa; |
---|
238 | char * a; |
---|
239 | #line 38 "python.mod" |
---|
240 | |
---|
241 | |
---|
242 | #line 39 "python.mod" |
---|
243 | #line 226 "python.cc" |
---|
244 | if(__v==NULL) goto mod_python_error; |
---|
245 | __tok = __v->Typ(); |
---|
246 | if((__index=iiTestConvert(__tok, STRING_CMD))==0) |
---|
247 | goto mod_python_error; |
---|
248 | __v_save = __v->next; |
---|
249 | __v->next = NULL; |
---|
250 | if(iiConvert(__tok, STRING_CMD, __index, __v, __za)) |
---|
251 | goto mod_python_error; |
---|
252 | __v = __v_save; |
---|
253 | a = (char *)__za->Data(); |
---|
254 | if(__v!=NULL) { __tok = __v->Typ(); goto mod_python_error; } |
---|
255 | |
---|
256 | #line 39 "python.mod" |
---|
257 | |
---|
258 | mbpython(a); |
---|
259 | #line 41 "python.mod" |
---|
260 | #line 243 "python.cc" |
---|
261 | __res->data = NULL; |
---|
262 | __res->rtyp = END_RING; |
---|
263 | return FALSE; |
---|
264 | |
---|
265 | mod_python_error: |
---|
266 | Werror("python(`%s`) is not supported", Tok2Cmdname(__tok)); |
---|
267 | Werror("expected python('string')"); |
---|
268 | return TRUE; |
---|
269 | } |
---|
270 | |
---|
271 | |
---|
272 | #line 254 "python.cc" |
---|
273 | #line 53 "python.mod" |
---|
274 | void mbpython(char* inp){ |
---|
275 | PyRun_SimpleString(inp); |
---|
276 | } |
---|