File: c-embed.c

package info (click to toggle)
entity 0.7.2-6
  • links: PTS
  • area: main
  • in suites: woody
  • size: 5,352 kB
  • ctags: 5,272
  • sloc: ansic: 61,707; sh: 7,921; makefile: 732; perl: 399
file content (388 lines) | stat: -rw-r--r-- 10,072 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
#include <stdio.h>
#include <stdlib.h>
#include <glib.h>
#include <gmodule.h>
#include <dlfcn.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <errno.h>
#include "entity.h"

static char *codedir;
static int link_in_so (ENode * node, gchar * lafile);
static void import_in (GModule * module, gchar * function);

static char *stdheaders = "                                        \
#include <stdio.h>\n                                               \
#include <stdlib.h>\n                                              \
#include <glib.h>\n                                                \
#include <entity.h>\n						   \
";

static GHashTable *c_functions_ht = NULL;

static gint
check_checksum (char *obj, char *code)
{
    gchar *checksumfile = g_strconcat (codedir, "/.sum/", obj, NULL);
    unsigned int old_hash = 0;
    unsigned int new_hash = x31_hash (code);
    FILE *file_handle = NULL;

    /* if there is a hash, read it in and check agains the new hash */
    file_handle = fopen (checksumfile, "r");

    if (file_handle) {
	fscanf (file_handle, "%u", &old_hash);
	fclose (file_handle);
    }
	
    g_free (checksumfile);

    /* if they are the same ...do nothing but return true */
    if (old_hash == new_hash)
	return (TRUE);

    return (FALSE);
}

static void
write_checksum (char *obj, char *code)
{
    gchar *checksumfile = g_strconcat (codedir, "/.sum/", obj, NULL);
    unsigned int new_hash = x31_hash (code);
    FILE *file_handle = NULL;
    
    file_handle = fopen (checksumfile, "w+");
    if (file_handle) {
	fprintf (file_handle, "%u", new_hash);
	fclose (file_handle);
    }
    g_free (checksumfile);
}

gchar *
c_compile_str_get (ENode * node, char *tag)
{
    char *str = NULL;
    char *temp;
    GSList *list;
    ENode *child;

    /* check current node for the tag first */
    str = enode_attrib_str (node, tag, NULL);

    /* Tag was not found, now check for children */
    if (!str) {
	for (list = node->children; list; list = list->next) {
	    child = (ENode *) list->data;
	    if (strcmp (child->element->str, tag) != 0 || !child->data) {
		continue;
	    }
	    temp = g_strconcat (child->data->str, str, NULL);
	    if (str) {
		g_free (str);
	    }
	    str = temp;
	}
    }

    /* Make sure the string has "something" */
    if (!str) {
	str = "";
    }

    return (str);
}

/* Render all the c-code tags. * Code that has been modified will be
 * recompiled and relinked into * .so files.   These .so are then dlopen'ed
 * and functions that have * been exported to entity using the <c-function>
 * tag will then * added to the c_functions_ht so that they can be called
 * from entity. */
static void
c_node_render (ENode * node)
{
    gchar *obj;			/* Object name, append .so for the lib name. */
    gchar *includes;		/* Text inside of <c-includes>. */
    gchar *libs;		/* Text inside of <c-libs>. */

    gchar *obj_cmd;		/* Commands that are ran to compile the c. */
    gchar *so_cmd;

    gchar *lafile;		/* Path to the outputed .la library info file 
				 */
    gchar *data;

    FILE *fp = NULL;

    /* Check to make sure we have a valid node, with data */
    if (!node || !node->data) {
	return;
    }

    obj = enode_attrib_str (node, "object", NULL);

    if (!obj) {
	obj = "libdefault";	/* This will be really SLOW!!! because each
				 * new */
    } /* will have to be compiled. FIXME */
    else {
	obj = g_strconcat ("lib", obj, NULL);	/* libtool needs the object
						 * to start with a lib */
    }

    includes = c_compile_str_get (node, "c-includes");

    libs = c_compile_str_get (node, "c-libs");


    /* This is the data that is checksummed, we need all od this so that *
     * the relink and recompile happen if only the <c-[libs|includes] * are
     * changed in the xml. */
    data = g_strconcat (includes, libs, node->data->str, NULL);
    lafile = g_strconcat (codedir, "/", obj, ".la", NULL);

    if (check_checksum (obj, data) == FALSE) {
	char *tempfile = NULL;	/* File created to hold the c code. */
	int compile_ok = TRUE;
	
	tempfile = g_strconcat (codedir, "/entity.c", NULL);

	/* Open temp file. */
	if (tempfile) {
	    fp = fopen (tempfile, "w");
	}
	if (!fp) {
	    g_warning ("Unable to open temp file '%s' for writing: %s",
		       tempfile, g_strerror (errno));
	    return;
	}

	fprintf (fp, "%s", stdheaders);
	fprintf (fp, "%s", node->data->str);
	fclose (fp);

	/* Build the object compile string */
	obj_cmd = g_strconcat (DATADIR, "/libtool --mode=compile ", COMPILER, " ",
			       includes, " `entity-config --cflags` ", tempfile,
			       " -c -o ",
			       codedir, "/.objects/", obj, ".lo", NULL);

	/* Build the so linking string */
	so_cmd = g_strconcat (DATADIR, "/libtool --mode=link ", COMPILER, " ",
			      libs, " ", "-avoid-version -module ",
			      codedir, "/.objects/", obj, ".lo",
			      " -rpath /usr/lib  -o ", lafile, NULL);

	/* Rebuild if checksum is different */
	EDEBUG (("c-embed", "Executing libtool command: %s", obj_cmd));
	if (system (obj_cmd)) {
	    compile_ok = FALSE;
	    g_warning ("C-code was not recompiled! %s\n", obj_cmd);
	} else {

	    /* on to link stage */
	    EDEBUG (("c-embed", "Executing libtool command: %s", so_cmd));
	    if (system (so_cmd)) {
		compile_ok = FALSE;
		g_warning ("C-code was not relinked! %s\n", so_cmd);
	    }
	}

	/* Only write out checksum if the compile was ok */
	if (compile_ok == TRUE)
	    write_checksum (obj, data);

	g_free (obj_cmd);
	g_free (so_cmd);
	g_free (tempfile);
    }
    g_free (data);

    link_in_so (node, lafile);
    g_free (lafile);
}


static int
link_in_so (ENode * node, gchar * lafile)
{
    GModule *module;
    void (*init)(void);                    /* The init function. */
    gchar buf[2048];
    FILE *file_handle = NULL;
    int err = FALSE;
    gchar *modpath;
    gchar *dlname;
    gchar *command;
    gchar **segments = NULL;

    dlname = eutils_module_dlname (lafile);
    if (!dlname) {
	g_warning ("Unable to deduce shared object file to load, giving up!");
	return (1);
    }
    modpath = g_strconcat (codedir, "/.libs/", dlname, NULL);

    g_free (dlname);

    EDEBUG (("c-embed", "Loading object '%s' from path '%s'", dlname, modpath));

    module = g_module_open (modpath, G_MODULE_BIND_LAZY);

    if (!module) {
	g_warning
	    ("Error loading dynamic library '%s': %s\n",
	     modpath, g_module_error());
	return (1);
    }

    /* do a popen and hand it a nm command to generate function names */
    command = g_strconcat ("nm -p ", modpath, NULL);
    EDEBUG (("c-embed", "Executing command: '%s'", command));
    file_handle = popen (command, "r");
    g_free (command);
    g_free (modpath);

    /* check the file handle */
    if (file_handle) {
	while (FALSE == err) {
	    if (NULL != fgets (buf, 2048, file_handle)) {
		
		/* g_strchomp operates on string in-place */
		g_strchomp (buf);
		segments = g_strsplit (buf, " ", 0);

		if (segments && segments[1] && (0 == strcmp (segments[1], "T")) &&
		    segments[2] && segments[2] != '\0') {
		    import_in (module, segments[2]);	/* import the module
							 * from the .so */
		}
		
		if (segments)
		    g_strfreev (segments);

	    } else {
		err = TRUE;
	    }
	}

	(void) pclose (file_handle);
    }

    /* Call the entity_init function in the library if it's there. */
    if (g_module_symbol (module, "entity_c_init", (gpointer *) &init) ) {
        EDEBUG (("c-embed", "running 'entity_c_init' in C-code.\n"));
	enode_call_reference_push (node);
	init ();
	enode_call_reference_pop ();
    }

    return (0);
}

/* Imports in a module from a .so */
void
import_in (GModule * module, gchar * function)
{
    void (*func) (GSList *);

    if (function) {
	EDEBUG (("c-embed", "importing in function in '%s'", function));
	/* On some systems, all symbols start with _ */
	if (function[0] == '_')
	    function++;

	g_module_symbol (module, function, (void **) &func);

	if (func) {
	    g_hash_table_insert (c_functions_ht, g_strdup (function), func);
	} else {
	    g_warning ("nm returned function %s, but theres no such symbol",
		       function);
	}
    }
}

EBuf *
c_function_execute (ENode * calling_node, gchar * function, GSList * args)
{
    GSList *tmp;
    EBuf *retval = NULL;

    EBuf *(*funct) (ENode *, GSList *);

    funct = g_hash_table_lookup (c_functions_ht, function);

    if (funct) {
	enode_call_reference_push (calling_node);
	retval = funct (calling_node, args);
	enode_call_reference_pop ();
    } else {
	g_warning ("While trying to execute C function %s: %s not defined.\n", function, function);
    }

    /* Free argument list */
    tmp = args;
    while (tmp) {
	LangArg *arg = tmp->data;
	enode_call_free_arg (arg);
	tmp = tmp->next;
    }

    return (retval);
}

#ifdef STATIC_C
void
c_init (RendererFlags flags)
#else
void
renderer_init (RendererFlags flags)
#endif
{
    Element *element;
    char *sumdir;
    char *libdir;

    c_functions_ht = g_hash_table_new (g_str_hash, g_str_equal);

    if (flags & RENDERER_INIT) {
	codedir = g_strconcat (g_get_home_dir (), "/.entity/c-code", NULL);
	sumdir = g_strconcat (codedir, "/.sum", NULL);
	libdir = g_strconcat (codedir, "/.objects", NULL);

	if (mkdir (codedir, 0750) == -1 && errno != EEXIST) {
	    g_warning ("Cant create %s, no c-code tags can be rendered!\n",
		       codedir);
	}

	if (mkdir (sumdir, 0750) == -1 && errno != EEXIST) {
	    g_warning ("Cant create %s, no c-code tags can be rendered!\n",
		       sumdir);
	}

	if (mkdir (libdir, 0750) == -1 && errno != EEXIST) {
	    g_warning ("Cant create %s, no c-code tags can be rendered!\n",
		       libdir);
	}

	g_free (sumdir);
	g_free (libdir);
    }

    if (flags & RENDERER_REGISTER) {
	element = g_new0 (Element, 1);
	element->render_func = c_node_render;
	element->destroy_func = NULL;
	element->description =
	    "Embed C code directly into an Entity application.";
	element->tag = "c-code";
	element_register (element);

	language_register ("c-code", c_function_execute);
	language_register ("C", c_function_execute);
	language_register ("c", c_function_execute);
    }
}