Difference between revisions of "C.php"

From Organic Design wiki
m
({{legacy}})
 
(80 intermediate revisions by 3 users not shown)
Line 1: Line 1:
<?
+
{{legacy}}
# SWF Compiler for XmlWiki Environment
+
<php><?
 +
# C compiler for XmlWiki Environment
 +
# Licensed under LGPL: www.gnu.org/copyleft/lesser.html
 +
if ($GLOBALS['xwIsAdmin']) {
 +
xwSetProperty($properties,'language','TEXT');
 +
$server = $_SERVER['HTTP_HOST'];
 +
$htdocs = $_SERVER['DOCUMENT_ROOT'];
 +
if (!ereg( "\\/$", $htdocs)) $htdocs .= '/';
 +
$args = $_REQUEST['args'];
 +
$src = $htdocs.'wiki/source.c';
 +
$gcc = '/usr/bin/gcc';
 +
$out = $htdocs.'wiki/a.out';
 +
$stdout = $htdocs.'wiki/a.stdout';
 +
$stderr = $htdocs.'wiki/a.stderr';
  
$server = $_SERVER['HTTP_HOST'];
+
# Write the source to tmp file source
$htdocs = $_SERVER['DOCUMENT_ROOT'];
+
if ($handle = fopen($src, 'w+')) {
if ( !ereg( "\\/$", $htdocs ) ) $htdocs .= '/';
+
fwrite($handle, "$article\n");
 
+
fclose($handle);
# if format is embed, just convert the content to an object-tag referring to raw-compiled-swf
 
if ( $format == 'embed' ) {
 
$article = "test";
 
}
 
 
 
else {
 
 
 
# Compile the script into a binary SWF
 
# - also builds hard wired common vector shapes (later converted to Ming from SVG)
 
 
 
# Check weather required extentions are available (GD2 and Ming)
 
$ext = get_loaded_extensions();
 
if ( !in_array( 'ming', $ext ) ) xwMessage( 'Ming extention not loaded! (at least version 0.3 required)', 'red' );
 
 
 
ming_useswfversion( $version );
 
global $lib, $swf;
 
$lib = array();
 
$swf = new SWFMovie();
 
$swf->setDimension( $width, $height );
 
eregi( '^#?([0-9a-f][0-9a-f])([0-9a-f][0-9a-f])([0-9a-f][0-9a-f])$' , $background, $m );
 
list(,$r,$g,$b) = $m;
 
$swf->setBackground( hexdec($r), hexdec($g), hexdec($b) );
 
$swf->setRate( $fps );
 
xwMessage( "SWF movie created from \"$title\": $width"." x $height @ $fps".'fps' );
 
 
 
# Don't redeclare (happens if more than one compile on same page)
 
if ( !isset( $GLOBALS[$tTitle] ) ) {
 
 
 
$GLOBALS[$tTitle] = true;
 
 
 
# Make symbol library available at runtime in library array
 
function libAdd( $item, $name ) {
 
global $lib, $swf;
 
$sym = new SWFSprite();
 
$sym->add( $item );
 
$sym->nextFrame();
 
$swf->addExport( $sym, $lib[] = $name );
 
 
}
 
}
  
 +
# Compile & excecute
 +
$article = "Compiling using $gcc\n";
 +
$now = microtime();
 +
shell_exec("$gcc $src 1>$stdout 2>$stderr");
 +
$article .= "Compilation took " . (microtime() - $now) . " usec\n";
 +
$article .= file_get_contents($stdout).file_get_contents($stderr);
 +
$article .= "\n\nExecuting compiled result:\n";
 +
$now = microtime();
 +
$article .= shell_exec("$out $args 2>&1");
 +
$article .= "Execution took " . (microtime() - $now) . " usec\n";
 
}
 
}
 
+
else xwMessage('Permission denied: Only admin can compile and execute C articles', 'red');
# Import images
+
?></php>
foreach ( xwGetListByTagname( $properties, 'import', $import ) as $image ) {
 
$ref = strtolower( trim( ereg_replace( '^.*/', '', $image ) ) );
 
$ref = ereg_replace( '\\..+$', '', $ref );
 
$w = $htdocs.'wiki';
 
shell_exec( "$w/png2dbl $htdocs$image $w/$ref.dbl" );
 
# investigate creating a unix level pipe and reading a la /dev
 
#$img = new SWFBitmap( popen( "$w/png2dbl $w$image $w/stream.dbl", 'rb' ) );
 
$img = new SWFBitmap( fopen( "$w/$ref.dbl", 'rb' ) );
 
libAdd( $img, $ref );
 
}
 
 
 
$grad = new SWFGradient();
 
for ( $n = 0; $n <= 10; $n++ ) {
 
$i = ( 2 - log( $n + 1 ) * 0.95 ) * 125;
 
if ( $i > 255 ) $i = 255; else if ( $i < 0 ) $i = 0;
 
$grad->addEntry( $n / 10, 255, 255, 255, $i );
 
}
 
$shape = new SWFShape();
 
$fill = $shape->addFill( $grad, SWFFILL_RADIAL_GRADIENT );
 
$fill->scaleTo( 0.17 );
 
$shape->setRightFill( $fill );
 
$shape->drawCircle( 100 );
 
libAdd( $shape, 'star' );
 
 
 
# Add compile.as common code
 
$article = xwArticleContent( 'compile.as' ).$article;
 
 
 
# Write exports and make all available at runtime in library array
 
$swf->writeExports();
 
$article = 'library=array("'.join( '","', $lib )."\");\n$article";
 
if ( count( $lib ) ) xwMessage( 'Pre-compiled symbols: "'.join( '", "', $lib ).'"' );
 
 
 
# Flashvars aren't working just compile assignments into script for now
 
foreach( split( '&', $flashvars ) as $var )
 
if ( ereg( '^(.+)=(.+)$', $var, $m ) )
 
$article = "$m[1]='$m[2]';\n$article";
 
 
 
# Replace #Include statements with content
 
$includes = array();
 
$more = true;
 
while ( $more ) {
 
$more = count( $includes );
 
$article = preg_replace(
 
'/^\\s*#include\\s+[\'"](.+?)[\'"].*?$/emi',
 
'xwArticleContent($includes["$1"]="$1",true);',
 
$article );
 
$more = count( $includes ) > $more;
 
}
 
if ( count( $includes ) ) xwMessage( 'Included articles: "'.join( '", "', array_keys( $includes ) ).'"' );
 
 
 
# Fix the ^= operator which doesn't work in Ming but does in Flash
 
$article = preg_replace( '/([a-z_]+[a-z_0-9.]+)\\s*\\^=/i', '$1=$1^', $article );
 
 
 
# Add actionscript in frames 1-3
 
$swf->add( new SWFAction( $article ) );
 
$swf->nextFrame();
 
$swf->add( new SWFAction( 'internal_reduce();' ) );
 
$swf->nextFrame();
 
$swf->add( new SWFAction( 'gotoAndPlay(2);' ) );
 
$swf->nextFrame();
 
 
 
if ( $format == 'raw' ) {
 
# Send the raw SWF back to the client and die
 
ob_end_clean();
 
header( 'Content-Type: application/x-shockwave-flash' );
 
$swf->output(9);
 
die();
 
}
 
else {
 
# Write the SWF to $url and render a link to it
 
 
$swf->save( "$htdocs$url" ,9 );
 
$article = "\n[[$title]] successfully compiled to [http://$server/$url $server/$url]\n";
 
}
 
 
 
}
 
?>
 

Latest revision as of 09:39, 14 March 2009

Legacy.svg Legacy: This article describes a concept that has been superseded in the course of ongoing development on the Organic Design wiki. Please do not develop this any further or base work on this concept, now this page is for historic record only.

<php><?

  1. C compiler for XmlWiki Environment
  2. Licensed under LGPL: www.gnu.org/copyleft/lesser.html

if ($GLOBALS['xwIsAdmin']) { xwSetProperty($properties,'language','TEXT'); $server = $_SERVER['HTTP_HOST']; $htdocs = $_SERVER['DOCUMENT_ROOT']; if (!ereg( "\\/$", $htdocs)) $htdocs .= '/'; $args = $_REQUEST['args']; $src = $htdocs.'wiki/source.c'; $gcc = '/usr/bin/gcc'; $out = $htdocs.'wiki/a.out'; $stdout = $htdocs.'wiki/a.stdout'; $stderr = $htdocs.'wiki/a.stderr';

# Write the source to tmp file source if ($handle = fopen($src, 'w+')) { fwrite($handle, "$article\n"); fclose($handle); }

# Compile & excecute $article = "Compiling using $gcc\n"; $now = microtime(); shell_exec("$gcc $src 1>$stdout 2>$stderr"); $article .= "Compilation took " . (microtime() - $now) . " usec\n"; $article .= file_get_contents($stdout).file_get_contents($stderr); $article .= "\n\nExecuting compiled result:\n"; $now = microtime(); $article .= shell_exec("$out $args 2>&1"); $article .= "Execution took " . (microtime() - $now) . " usec\n"; } else xwMessage('Permission denied: Only admin can compile and execute C articles', 'red'); ?></php>