Update README.md

Add syntax highlighting to make the code blocks more readable.
This commit is contained in:
Jordan Biserkov 2014-02-22 11:03:56 +02:00
parent 52df8947dc
commit 87a09f15a8
1 changed files with 150 additions and 150 deletions

168
README.md
View File

@ -39,12 +39,13 @@ PH7 in 5 Minutes or Less
Here is what you do to start experimenting with the PH7 engine without having to do a lot of tedious reading and configuration: Here is what you do to start experimenting with the PH7 engine without having to do a lot of tedious reading and configuration:
Below is a simple C program that demonstrates how to use the C/C++ interface to PH7. This program compile and execute the following PHP script: Below is a simple C program that demonstrates how to use the C/C++ interface to PH7. This program compile and execute the following PHP script:
```php
<?php <?php
echo 'Welcome guest'.PHP_EOL; echo 'Welcome guest'.PHP_EOL;
echo 'Current system time is: '.date('Y-m-d H:i:s').PHP_EOL; echo 'Current system time is: '.date('Y-m-d H:i:s').PHP_EOL;
echo 'and you are running '.php_uname(); echo 'and you are running '.php_uname();
?> ?>
```
That is, this simple PHP script when running should display a greeting message, the current system time and the host operating system. A typical output of this program would look like this: That is, this simple PHP script when running should display a greeting message, the current system time and the host operating system. A typical output of this program would look like this:
@ -54,74 +55,72 @@ That is, this simple PHP script when running should display a greeting message,
Here is the C code. Note that you can get a working version of this program [here](http://www.symisc.net/downloads/ph7_intro.c): Here is the C code. Note that you can get a working version of this program [here](http://www.symisc.net/downloads/ph7_intro.c):
```c
/* Compile this file together with the ph7 engine source code to generate
* the executable. For example:
/* Compile this file together with the ph7 engine source code to generate * gcc -W -Wall -O6 -o ph7_test ph7_intro.c ph7.c
* the executable. For example: */
* gcc -W -Wall -O6 -o ph7_test ph7_intro.c ph7.c /*
*/ * This simple program is a quick introduction on how to embed and start
/* * experimenting with the PH7 engine without having to do a lot of tedious
* This simple program is a quick introduction on how to embed and start * reading and configuration.
* experimenting with the PH7 engine without having to do a lot of tedious *
* reading and configuration. * For an introduction to the PH7 C/C++ interface, please refer to this page
* * http://ph7.symisc.net/api_intro.html
* For an introduction to the PH7 C/C++ interface, please refer to this page * For the full C/C++ API reference guide, please refer to this page
* http://ph7.symisc.net/api_intro.html * http://ph7.symisc.net/c_api.html
* For the full C/C++ API reference guide, please refer to this page */
* http://ph7.symisc.net/c_api.html /*
*/ * The following is the PHP program to execute.
/* * <?php
* The following is the PHP program to execute. * echo 'Welcome guest'.PHP_EOL;
* <?php * echo 'Current system time is: '.date('Y-m-d H:i:s').PHP_EOL;
* echo 'Welcome guest'.PHP_EOL; * echo 'and you are running '.php_uname();
* echo 'Current system time is: '.date('Y-m-d H:i:s').PHP_EOL; * ?>
* echo 'and you are running '.php_uname(); * That is, this simple program when running should display a greeting
* ?> * message, the current system time and the host operating system.
* That is, this simple program when running should display a greeting * A typical output of this program would look like this:
* message, the current system time and the host operating system. *
* A typical output of this program would look like this: * Welcome guest
* * Current system time is: 2012-09-14 02:08:44
* Welcome guest * and you are running Microsoft Windows 7 localhost 6.1 build 7600 x86
* Current system time is: 2012-09-14 02:08:44 *
* and you are running Microsoft Windows 7 localhost 6.1 build 7600 x86 */
* #define PHP_PROG "<?php "\
*/ "echo 'Welcome guest'.PHP_EOL;"\
#define PHP_PROG "<?php "\ "echo 'Current system time is: '.date('Y-m-d H:i:s').PHP_EOL;"\
"echo 'Welcome guest'.PHP_EOL;"\ "echo 'and you are running '.php_uname();"\
"echo 'Current system time is: '.date('Y-m-d H:i:s').PHP_EOL;"\ "?>"
"echo 'and you are running '.php_uname();"\ /* Make sure you have the latest release of the PH7 engine
"?>" * from:
/* Make sure you have the latest release of the PH7 engine * http://ph7.symisc.net/downloads.html
* from: */
* http://ph7.symisc.net/downloads.html #include <stdio.h>
*/ #include <stdlib.h>
#include <stdio.h> /* Make sure this header file is available.*/
#include <stdlib.h> #include "ph7.h"
/* Make sure this header file is available.*/ /*
#include "ph7.h" * Display an error message and exit.
/* */
* Display an error message and exit. static void Fatal(const char *zMsg)
*/ {
static void Fatal(const char *zMsg)
{
puts(zMsg); puts(zMsg);
/* Shutdown the library */ /* Shutdown the library */
ph7_lib_shutdown(); ph7_lib_shutdown();
/* Exit immediately */ /* Exit immediately */
exit(0); exit(0);
} }
/* /*
* VM output consumer callback. * VM output consumer callback.
* Each time the virtual machine generates some outputs, the following * Each time the virtual machine generates some outputs, the following
* function gets called by the underlying virtual machine to consume * function gets called by the underlying virtual machine to consume
* the generated output. * the generated output.
* All this function does is redirecting the VM output to STDOUT. * All this function does is redirecting the VM output to STDOUT.
* This function is registered later via a call to ph7_vm_config() * This function is registered later via a call to ph7_vm_config()
* with a configuration verb set to: PH7_VM_CONFIG_OUTPUT. * with a configuration verb set to: PH7_VM_CONFIG_OUTPUT.
*/ */
static int Output_Consumer(const void *pOutput, unsigned int nOutputLen, void *pUserData /* Unused */) static int Output_Consumer(const void *pOutput, unsigned int nOutputLen, void *pUserData /* Unused */)
{ {
/* /*
* Note that it's preferable to use the write() system call to display the output * Note that it's preferable to use the write() system call to display the output
* rather than using the libc printf() which everybody now is extremely slow. * rather than using the libc printf() which everybody now is extremely slow.
@ -133,11 +132,11 @@ Here is the C code. Note that you can get a working version of this program [her
/* All done, VM output was redirected to STDOUT */ /* All done, VM output was redirected to STDOUT */
return PH7_OK; return PH7_OK;
} }
/* /*
* Main program: Compile and execute the PHP program defined above. * Main program: Compile and execute the PHP program defined above.
*/ */
int main(void) int main(void)
{ {
ph7 *pEngine; /* PH7 engine */ ph7 *pEngine; /* PH7 engine */
ph7_vm *pVm; /* Compiled PHP program */ ph7_vm *pVm; /* Compiled PHP program */
int rc; int rc;
@ -175,13 +174,13 @@ Here is the C code. Note that you can get a working version of this program [her
} }
/* Exit */ /* Exit */
Fatal("Compile error"); Fatal("Compile error");
} }
/* /*
* Now we have our script compiled, it's time to configure our VM. * Now we have our script compiled, it's time to configure our VM.
* We will install the output consumer callback defined above * We will install the output consumer callback defined above
* so that we can consume and redirect the VM output to STDOUT. * so that we can consume and redirect the VM output to STDOUT.
*/ */
rc = ph7_vm_config(pVm, rc = ph7_vm_config(pVm,
PH7_VM_CONFIG_OUTPUT, PH7_VM_CONFIG_OUTPUT,
Output_Consumer, /* Output Consumer callback */ Output_Consumer, /* Output Consumer callback */
0 /* Callback private data */ 0 /* Callback private data */
@ -189,17 +188,18 @@ Here is the C code. Note that you can get a working version of this program [her
if( rc != PH7_OK ){ if( rc != PH7_OK ){
Fatal("Error while installing the VM output consumer callback"); Fatal("Error while installing the VM output consumer callback");
} }
/* /*
* And finally, execute our program. Note that your output (STDOUT in our case) * And finally, execute our program. Note that your output (STDOUT in our case)
* should display the result. * should display the result.
*/ */
ph7_vm_exec(pVm,0); ph7_vm_exec(pVm,0);
/* All done, cleanup the mess left behind. /* All done, cleanup the mess left behind.
*/ */
ph7_vm_release(pVm); ph7_vm_release(pVm);
ph7_release(pEngine); ph7_release(pEngine);
return 0; return 0;
} }
```
We create a new [PH7 engine instance](http://ph7.symisc.net/c_api_func.html#ph7_init) using a call to [ph7_init()](http://ph7.symisc.net/c_api_func.html#ph7_init) on line 86. This is often the first PH7 API call that an application makes and is a prerequisite in order to compile PHP code using one of the compile interfaces. We create a new [PH7 engine instance](http://ph7.symisc.net/c_api_func.html#ph7_init) using a call to [ph7_init()](http://ph7.symisc.net/c_api_func.html#ph7_init) on line 86. This is often the first PH7 API call that an application makes and is a prerequisite in order to compile PHP code using one of the compile interfaces.