/* compile: * cc ex6.c libtcc.a -ldl -o ex6 * * example test: * echo "int bar(){ return(3);}" | ./ex6 */ #include "libtcc.h" #include //malloc #include #include // FILE etc. int main() { int testint; int (*testfct)(void); void* ptr = malloc(100000); // too much. int symb; int outfd[2]; int infd[2]; TCCState* s; // -------- compiling string ---------- s = tcc_new( ); tcc_compile_string(s, "int bar(){ return(3-2);}\n" ); tcc_relocate(s, ptr); testfct = tcc_get_symbol( s, "bar" ); if ( ! testfct ) { fprintf( stderr, "no symbol from string\n" ); return (2); } testint = testfct(); fprintf( stderr, "integer from string: %i\n", testint ); // -------- compiling things from external process ---------- s = tcc_new( ); pipe(outfd); // Where the parent is going to write to pipe(infd); // From where parent is going to read fprintf( stderr, "ex6: fork\n"); if(!fork()) { close(STDOUT_FILENO); close(STDIN_FILENO); dup2(outfd[0], STDIN_FILENO); dup2(infd[1], STDOUT_FILENO); close(outfd[0]); close(outfd[1]); close(infd[0]); close(infd[1]); sleep(1); char *argv[]={"/bin/cat" , 0}; execv(argv[0],argv); } close(outfd[0]); close(infd[1]); write(outfd[1],"int bar(){ return(2);}\n",23); close(outfd[1]); tcc_compile_stream( s, (FILE*) infd[0] ); tcc_relocate(s, ptr); testfct = tcc_get_symbol( s, "bar" ); if ( ! testfct ) { fprintf( stderr, "no symbol from external program\n" ); return (2); } testint = testfct(); fprintf( stderr, "integer from external program %i\n", testint ); close(infd[0]); // -------- compiling things from STDIN ---------- s = tcc_new( ); fprintf( stderr, "ex6: compiling stream %i\n", (int) stdin); tcc_compile_stream( s, 0 ); tcc_relocate(s, ptr); fprintf( stderr, "fetching symbol\n" ); testfct = tcc_get_symbol( s, "bar" ); if ( ! testfct ) { fprintf( stderr, "no symbol from stream: %i\n", symb ); return (2); } testint = testfct(); fprintf( stderr, "integer from stdin: %i\n", testint ); return( 0 ); }