Debian Codegraph @

ipl/packs/loadfuncpp/xfload.cpp

raw
1 /*
2 * Sun Mar 23 09:43:59 2008
3 * This file was produced by
4 * rtt: Icon Version 9.5.a-C, Autumn, 2007
5 */
6 // and then modified by cs
7
8
9 #define COMPILER 0
10 extern "C" {
11 #include RTT
12 }
13
14 //#line 42 "fload.r"
15
16 //int glue(); //cs
17 //int makefunc(dptr d, char *name, int (*func)()); //cs
18 //int Zloadfunc (dptr r_args); //cs
19 //FncBlock(loadfunc, 2, 0) //cs
20
21 //cs new makefunc that allocates a proc_block
22 static int newmakefunc(dptr d, char *name, int (*func)(), int arity) {
23 value nom(NewString,name);
24 proc_block* pbp;
25 if( arity < 0 ) pbp = new proc_block(nom, (iconfvbl*)func);
26 else pbp = new proc_block(nom, (iconfunc*)func, arity);
27 if( pbp==0 ) return 0;
28 d->dword = D_Proc;
29 d->vword.bptr = (union block *)pbp;
30 return 1;
31 }
32 //cs end of new makefunc
33
34 //int Zloadfunc(r_args) //cs
35 //dptr r_args; //cs
36 inline int Z_loadfunc(dptr r_args) //cs
37 {
38 if (!cnv_c_str(&(r_args[1]), &(r_args[1]))) {
39 {
40 err_msg(
41
42 //#line 50 "fload.r"
43
44 103, &(r_args[1]));
45 return A_Resume;
46 }
47 }
48
49 //#line 51 "fload.r"
50
51 if (!cnv_c_str(&(r_args[2]), &(r_args[2]))) {
52 {
53 err_msg(
54
55 //#line 52 "fload.r"
56
57 103, &(r_args[2]));
58 return A_Resume;
59 }
60 }
61 //cs new third parameter: arity
62 C_integer r_i2;
63 if (!cnv_c_int(&(r_args[3]), &(r_i2))) {
64 err_msg(101, &(r_args[3]));
65 return A_Resume;
66 }
67 //cs end new third arity parameter
68
69 //#line 58 "fload.r"
70
71 {
72 int (*func)();
73 static char *curfile;
74 static void *handle;
75 char *funcname2;
76
77 //#line 67 "fload.r"
78
79 if (!handle || !curfile || strcmp(r_args[1].vword.sptr, curfile) != 0) {
80 if (curfile)
81 free((pointer)curfile);
82 curfile = salloc(r_args[1].vword.sptr);
83 handle = dlopen(r_args[1].vword.sptr, 1 | RTLD_GLOBAL);
84 }
85
86 //#line 76 "fload.r"
87
88 if (handle) {
89 func = (int (*)())dlsym(handle, r_args[2].vword.sptr);
90 if (!func) {
91
92 //#line 83 "fload.r"
93
94 //funcname2 = malloc(strlen(r_args[2].vword.sptr) + 2); //cs
95 funcname2 = (char*)malloc(strlen(r_args[2].vword.sptr) + 2); //cs
96 if (funcname2) {
97 *funcname2 = '_';
98 strcpy(funcname2 + 1, r_args[2].vword.sptr);
99 func = (int (*)())dlsym(handle, funcname2);
100 free(funcname2);
101 }
102 }
103 }
104 if (!handle || !func) {
105 //fprintf(stderr, "\nloadfunc(\"%s\",\"%s\"): %s\n", //cs
106 fprintf(stderr, "\nloadfuncpp(\"%s\",\"%s\"): %s\n", //cs
107 r_args[1].vword.sptr, r_args[2].vword.sptr, dlerror());
108 {
109 err_msg(
110
111 //#line 95 "fload.r"
112
113 216, NULL);
114 return A_Resume;
115 }
116 }
117
118 // if (!makefunc(&r_args[0], r_args[2].vword.sptr, func)) //cs
119 if (!newmakefunc(&r_args[0], r_args[2].vword.sptr, func, r_i2)) //cs
120 {
121 err_msg(
122
123 //#line 101 "fload.r"
124
125 305, NULL);
126 return A_Resume;
127 }
128 {
129 return A_Continue;
130 }
131 }
132 }
133
134
135 #if 0 //cs --- not used: we use a proc_block constructor, and no glue
136
137 //#line 111 "fload.r"
138
139 int makefunc(d, name, func)
140 dptr d;
141 char *name;
142 int (*func)();
143 {
144 struct b_proc *blk;
145
146 blk = (struct b_proc *)malloc(sizeof(struct b_proc));
147 if (!blk)
148 return 0;
149 blk->title = T_Proc;
150 blk->blksize = sizeof(struct b_proc);
151
152 //#line 127 "fload.r"
153
154 blk->entryp.ccode = glue;
155
156 //#line 130 "fload.r"
157
158 blk->nparam = -1;
159 blk->ndynam = -1;
160 blk->nstatic = 0;
161 blk->fstatic = 0;
162 blk->pname.dword = strlen(name);
163 blk->pname.vword.sptr = salloc(name);
164 blk->lnames[0].dword = 0;
165 blk->lnames[0].vword.sptr = (char *)func;
166
167 d->dword = D_Proc;
168 d->vword.bptr = (union block *)blk;
169 return 1;
170 }
171
172 //#line 190 "fload.r"
173
174 int glue(argc, dargv)
175 int argc;
176 dptr dargv;
177 {
178 int status, (*func)();
179 struct b_proc *blk;
180 struct descrip r;
181 struct {
182 struct tend_desc *previous;
183 int num;
184 struct descrip d[1];
185 } r_tend;
186
187 r_tend.d[0].dword = D_Null;
188 r_tend.num = 1;
189 r_tend.previous = tend;
190 tend = (struct tend_desc *)&r_tend;
191
192 //#line 199 "fload.r"
193
194 blk = (struct b_proc *)dargv[0].vword.bptr;
195 func = (int (*)())blk->lnames[0].vword.sptr;
196
197 r_tend.d[0] = dargv[0];
198 dargv[0] = nulldesc;
199 status = (*func)(argc, dargv);
200
201 if (status == 0) {
202 tend = r_tend.previous;
203
204 //#line 207 "fload.r"
205
206 return A_Continue;
207 }
208
209 //#line 208 "fload.r"
210
211 if (status < 0) {
212 tend = r_tend.previous;
213
214 //#line 209 "fload.r"
215
216 return A_Resume;
217 }
218 r = dargv[0];
219 dargv[0] = r_tend.d[0];
220 if (((r).dword == D_Null))
221 do {err_msg((int)status, NULL);{
222 tend = r_tend.previous;
223 return A_Resume;
224 }
225 }
226 while (0);
227
228 //#line 215 "fload.r"
229
230 do {err_msg((int)status, &r);{
231 tend = r_tend.previous;
232 return A_Resume;
233 }
234 }
235 while (0);
236 }
237
238 #endif //cs unused
239