Ok,
I was going to post this in the web server thread, but i will post it here instead.
CGI program can be written in any language because CGI is a method of passing data not a specific implementation. The easiest way is to create a C program using the Microtronix linux application wizard. For example this is a program that controls the LEDs on the cyclone dev board:
# include <stdio.h># include <string.h># include <stdlib.h>
// replace the path with proper path to nios2_system.h# include <
C:\altera\kits\nios2\bin\eclipse\workspace\linux1\build\include\nios2_system.h>
void main(void)
{
np_pio *pio = na_led_pio;
pio->np_piodirection = 3;
pio->np_piodata = 0;
int content_length=0;
char *cgiinput ;
unsigned char mydata=0;
pio->np_piodata = 0x00;
printf("Content-type: text/html\n");
printf("\n");
if ( !(content_length = atoi(getenv("CONTENT_LENGTH"))) ) {
printf("No Content-Length was sent with the POST request.\n") ;
exit(1) ;
}
if ( !(cgiinput=(char *)malloc(content_length+1)) ) {
printf("Could not allocate memory for cgiinput.\n") ;
exit(1) ;
}
if (!fread(cgiinput, content_length, 1, stdin)) {
printf("Could not read CGI input from STDIN.\n") ;
exit(1) ;
}
cgiinput[content_length]='\0' ;
if (strstr(cgiinput, "LED1")) mydata |= 1;
if (strstr(cgiinput, "LED2")) mydata |= 2;
if (strstr(cgiinput, "LED3")) mydata |= 4;
if (strstr(cgiinput, "LED4")) mydata |= 8;
if (strstr(cgiinput, "LED5")) mydata |= 16;
if (strstr(cgiinput, "LED6")) mydata |= 32;
if (strstr(cgiinput, "LED7")) mydata |= 64;
if (strstr(cgiinput, "LED8")) mydata |= 128;
pio->np_piodata = mydata;
// printf("cgi input was: \n%s\n", cgiinput);
printf("<html><head><title>CGI\n");
printf("Test</title><body>\n");
printf("<h1>LEDs set</h1>");
printf("
\n");
printf("</body></html>\n");
exit(0);
}
-----------
Please note that this program does not do proper parsing of CGI input parameters, it only checks if certain parameters were included. That works fine in this simple case.
To place this program in the cgi-bin directory, compile it first, then copy/paste to /home/httpd/cgi-bin and rename to "<filename>.cgi.exe" so that when the filesystem is built, it ends up with the proper ".cgi" extension.
Then you need a html file in order to make this CGI program useful. For example (Note: replace the IP with the dev boards IP, and cgitest2.cgi with the filename of your CGI program):
<FORM METHOD="POST"
ACTION="
http://192.168.1.122/cgi-bin/cgitest2.cgi">
1<INPUT TYPE="checkbox" NAME="LED1" VALUE="led1" CHECKED>
2<INPUT TYPE="checkbox" NAME="LED2" VALUE="led2">
3<INPUT TYPE="checkbox" NAME="LED3" VALUE="led3">
4<INPUT TYPE="checkbox" NAME="LED4" VALUE="led4">
5<INPUT TYPE="checkbox" NAME="LED5" VALUE="led5">
6<INPUT TYPE="checkbox" NAME="LED6" VALUE="led6">
7<INPUT TYPE="checkbox" NAME="LED7" VALUE="led7">
8<INPUT TYPE="checkbox" NAME="LED8" VALUE="led8">
<INPUT TYPE="submit" value="OK">
</FORM>
-------------------
You can execute this html file from your PC's desktop or host it on the dev board under /home/httpd.
I hope this helps, let me know if more explanation is needed.
-Thomas