星期四, 5月 17, 2007

CGI

CGIstr.c

/*************************************************************************/
/** **/
/** getcgivars.c-- routine to read CGI input variables into an **/
/** array of strings. **/
/** **/
/** Written in 1996 by James Marshall, james@jmarshall.com, except **/
/** that the x2c() and unescape_url() routines were lifted directly **/
/** from NCSA's sample program util.c, packaged with their HTTPD. **/
/** **/
/** For the latest, see http://www.jmarshall.com/easy/cgi/ . **/
/** **/
/*************************************************************************/

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

/** Convert a two-char hex string into the char it represents. **/
char x2c(char *what)
{
register char digit;

digit = (what[0] >= 'A' ? ((what[0] & 0xdf) - 'A')+10 : (what[0] - '0'));
digit *= 16;
digit += (what[1] >= 'A' ? ((what[1] & 0xdf) - 'A')+10 : (what[1] - '0'));
return(digit);
}

/** Reduce any %xx escape sequences to the characters they represent. **/
void unescape_url(char *url)
{
register int i,j;

for(i=0,j=0; url[j]; ++i,++j) {
if((url[i] = url[j]) == '%') {
url[i] = x2c(&url[j+1]) ;
j+= 2 ;
}
}
url[i] = '0' ;
}

/** Read the CGI input and place all name/val pairs into list. **/
/** Returns list containing name1, value1, name2, value2, ... , NULL **/
char **getcgivars()
{
register int i ;
char *request_method ;
int content_length;
char *cgiinput ;
char **cgivars ;
char **pairlist ;
int paircount ;
char *nvpair ;
char *eqpos ;

/** Depending on the request method, read all CGI input into cgiinput. **/
request_method= getenv("REQUEST_METHOD") ;

if(request_method == NULL)
{
printf("request_method is NULLn");
request_method= "GET";
}

if (!strcmp(request_method, "GET") || !strcmp(request_method, "HEAD") ) {
/* Some servers apparently don't provide QUERY_STRING if it's empty, */
/* so avoid strdup()'ing a NULL pointer here. */
char *qs ;
qs = getenv("QUERY_STRING") ;

printf("<p>qs: %s</p>",qs);
if(qs == NULL)
{
printf("qs is NULLn");
qs = "http://192.168.0.200/cgi-bin/led_flash.cgi?checkedbox=LED0&checkedbox=LED2&submitbutton=Do+it%21";
}

cgiinput= strdup(qs ? qs : "") ;
}
else if (!strcmp(request_method, "POST")) {
/* strcasecmp() is not supported in Windows-- use strcmpi() instead */
if ( strcasecmp(getenv("CONTENT_TYPE"), "application/x-www-form-urlencoded")) {
printf("Content-Type: text/plainnn") ;
printf("getcgivars(): Unsupported Content-Type.n") ;
exit(1) ;
}
if ( !(content_length = atoi(getenv("CONTENT_LENGTH"))) ) {
printf("Content-Type: text/plainnn") ;
printf("getcgivars(): No Content-Length was sent with the POST request.n") ;
exit(1) ;
}
if ( !(cgiinput= (char *) malloc(content_length+1)) ) {
printf("Content-Type: text/plainnn") ;
printf("getcgivars(): Couldn't malloc for cgiinput.n") ;
exit(1) ;
}
if (!fread(cgiinput, content_length, 1, stdin)) {
printf("Content-Type: text/plainnn") ;
printf("getcgivars(): Couldn't read CGI input from STDIN.n") ;
exit(1) ;
}
cgiinput[content_length]='0' ;
}
else {
printf("Content-Type: text/plainnn") ;
printf("getcgivars(): Unsupported REQUEST_METHOD.n") ;
exit(1) ;
}

/** Change all plusses back to spaces. **/
for (i=0; cgiinput[i]; i++) if (cgiinput[i] == '+') cgiinput[i] = ' ' ;

/** First, split on "&" and ";" to extract the name-value pairs into **/
/** pairlist. **/
pairlist= (char **) malloc(256*sizeof(char **)) ;
paircount= 0 ;
nvpair= strtok(cgiinput, "&;?") ;
while (nvpair) {
pairlist[paircount++]= strdup(nvpair) ;
if (!(paircount%256))
pairlist= (char **) realloc(pairlist,(paircount+256)*sizeof(char **)) ;
nvpair= strtok(NULL, "&;") ;
}
pairlist[paircount]= 0 ; /* terminate the list with NULL */

/** Then, from the list of pairs, extract the names and values. **/
cgivars= (char **) malloc((paircount*2+1)*sizeof(char **)) ;
for (i= 0; i<paircount; i++) {
if (eqpos=strchr(pairlist[i], '=')) {
*eqpos= '0' ;
unescape_url(cgivars[i*2+1]= strdup(eqpos+1)) ;
} else {
unescape_url(cgivars[i*2+1]= strdup("")) ;
}
unescape_url(cgivars[i*2]= strdup(pairlist[i])) ;
}
cgivars[paircount*2]= 0 ; /* terminate the list with NULL */

/** Free anything that needs to be freed. **/
free(cgiinput) ;
for (i=0; pairlist[i]; i++) free(pairlist[i]) ;
free(pairlist) ;

/** Return the list of name-value strings. **/
return cgivars ;

}

/***************** end of the getcgivars() module ********************/

led_flash.c

/****************************************/
/** Creator PIO(LED) Test Program **/
/****************************************/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>

#include <signal.h>
#include <strings.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <errno.h>
#include <netdb.h>

//#include "led-creator.h"
#include "creator_s3c2410_lcd.h"

char **getcgivars();

int testLED(unsigned short data)
{
int fd, ret;
// unsigned short data;

fd = open("/dev/lcd", O_RDWR);
if (fd < 0){
printf("Open /dev/lcd errorn");
return (-1);
}
/*

Update the code

*/
// 點亮全部8 LED Lamps, 1 : 亮, 0 : 不亮
//data = 0xff; //LED_ALL_ON;
ioctl(fd, LED_IOCTL_SET, &data);
//sleep(3);
#if 0
// 點亮全部8 LED Lamps, 1 : 亮, 0 : 不亮
data = 0xff; //LED_ALL_ON;
ioctl(fd, LED_IOCTL_SET, &data);
sleep(3);

// 熄滅單一個D9之LED Lamp
data = 0x1; //LED_D9_INDEX;
ret = ioctl(fd, LED_IOCTL_BIT_CLEAR, &data);
sleep(3);

// 點亮單一個D9之LED Lamp
data = 0x1; //LED_D9_INDEX;
ret = ioctl(fd, LED_IOCTL_BIT_SET, &data);
#endif

close(fd);
return (ret);
}


//char * msg = "LED toggle! q-- exit,2- led on, 1-- led off n";

int main()
{
int i, j;
char** cgivars;
char *ptr;
char Index;
unsigned char value;

printf("Content-type: text/htmlnn");
printf("<html><head><title>CGI DEMO</title></head>n");
printf("<body bgcolor="#FFFFFF">n");
printf("<h1>Hello CGI World!</h1>n");

printf("If you can read this and you can see the LED flash, then the CGI worked.n");
printf("</body></html>n");
fflush(stdout);

cgivars = getcgivars();

#if 1
i = 0; value = 0;
while(cgivars[i] != 0)
{
printf("<p>%s = %s</p>n", cgivars[i], cgivars[i+1]);
ptr = cgivars[i+1];
Index = ptr[3] - '0';
//printf("ptr=%s, Index=%dn", ptr, Index);

value += (1 << Index);
i= i + 2;
// Compare cgivars with LEDx and then call LEDtest
}

#else
value = 0x55;
#endif


printf("LED data = %xn", value);
testLED(value);

free(cgivars);

return 0;
}

0 意見: