GSP - A simple exemple : using c language |
|
This second example is the first one little brother, it shows the same gsp page but uses C code.
The GSP source page : c.gsp |
1:<html>
2:<%@ language="c"
3: mime type = "text/html"
4: %>
5:<%!
6: /*
7: * a comment to test the ! tag
8: */
9:%>
10:<body>
11:<%{
12: char hello[256];
13: int a;
14: int b;
15: int c;
16: char msg[256];
17: strcpy(hello,"Hello world");
18: a=20;
19: b=22;
20: c=a+b;
21: sprintf(msg,"%d",c);
22:%>
23:<h1><%=hello%></h1>
24:<p>The answer is <%=msg%></p>
25:<%}%>
26:</body>
27:</html>
Note : take care! In ansi c, variables declarations must take place before any code. That's why a new block starts at line 11 and ends at line 25.
Line numbers appears only for this note's convenience.
Invoking GSP to produce the cgi C source code |
bash# gsp -o gsp-c.c -m c.gsp
This command will produce one file :
gsp-c.c
#include <stdlib.h>
#include <stdio.h>
/*
* a comment to test the ! tag
*/
void c_gsp() {
printf("%s","Content-Type: ");
printf("%s","text/html");
printf("%s","\n\n");
printf("%s","<html>\n");
printf("%s","\n");
printf("%s","\n<body>\n");
{
char hello[256];
int a;
int b;
int c;
char msg[256];
strcpy(hello,"Hello world");
a=20;
b=22;
c=a+b;
sprintf(msg,"%d",c);
printf("%s","\n<h1>");
printf("%s",hello);
printf("%s","</h1>\n<p>The answer is ");
printf("%s",msg);
printf("%s","</p>\n");
}printf("%s","\n</body>\n</html>\n");
}
int main(int argc,char ** argv) {
char defaultPage[]="c.gsp";
char * requestedPage=getenv("PATH_INFO");
if (requestedPage==NULL) requestedPage=defaultPage;
if (requestedPage[0]=='/') requestedPage=&(requestedPage[1]);
if (strncmp(requestedPage,"c.gsp",6)==0||requestedPage[0]=='\0')
c_gsp();
exit(0);
}
Compiling the program |
bash# gcc -o gsp-c.cgi gsp-c.c -lstdc
Copy it in your http server cgi-bin folder.
|
|
last update 01/18/2004 - 17:27 |