LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-12-2004, 08:24 PM   #1
GridX
LQ Newbie
 
Registered: Sep 2003
Location: Canada
Distribution: Fedora & Gentoo-uClinux
Posts: 15

Rep: Reputation: 0
Form processing with cgi and uclinux


Hi, I read tutorials on cgi.... cant find that much help. I have a question.

I am working with a dragonball mc68k processor running uClinux. I will be installing the BOA webserver on this board. The board will control some lights. Either lights on or lights off. Now I want to put control in a web site so I can connect to the web site anywhere and turn on my house lights via web page. I have the C program written that will control the lights.
I want to do the following very simple form.

Code:
<FORM ACTION="URL">
 <INPUT TYPE="radio" NAME="state" VALUE="Y">LightsON<BR>
 <INPUT TYPE="radio" NAME="state" VALUE="N">LightsOFF<BR>
 <INPUT TYPE="submit" NAME="button" VALUE="Send">
 </FORM>

So when some one selects lights on, it will execute a cgi script that will call a c program that turns on a light.( the web server will be hosted on a NETdimm processor, the processor is connected to a light and the c program controls the light(on/off).

I want to implement this in a web site, so when a user goes to the web site they use the form on the website and select on or off in the form for lights.

So Do I write a shell script that calls the C program? or a shell script that calls a cgi script which inturn calls my C lights program
Code:
#!/bin/sh
eval "`cgi script here?  or my C lights program? $*`"

#what else do I need in this shell script.?

cat - << \END
echo Content-type: text/plain
echo
echo done.
I am confused? I dont know how to write cgi scripts, do I need one to process user input to my very simple form(on/off) or all I need is a shell script that calls my C lights program?

any guidance will be helpfull
 
Old 04-12-2004, 09:06 PM   #2
aluser
Member
 
Registered: Mar 2004
Location: Massachusetts
Distribution: Debian
Posts: 557

Rep: Reputation: 43
Here's an example perl script which handles both the job of printing the web page and running the lightsprogram. I make the assumption that your program is called lightsprogram and accepts -state, -on, and -off switches. I assume the -state switch prints "on" or "off".

This can of course be done through a shell script but perl has the nice CGI library, and I know it better.. hopefully this example is reasonably transparent anyway.

You would put this script in your cgi-bin directory and chmod +x it.

Code:
#!/usr/bin/perl
use strict;
use warnings;
use CGI ();

my $q = CGI->new();
my $current_state;
my $form_state = $q->param('state');
if (not defined $form_state or ($form_state ne 'Y' and $form_state ne 'N')) {
	my $output = `lightsprogram -state`;
	if (substr($output, 0, 2) eq 'on') {
		$current_state = 1;
	} else {
		$current_state = 0;
	}
} elsif ($form_state eq 'Y') {
	system('lightsprogram -on');
	$current_state = 1;
} else {
	system('lightsprogram -off');
	$current_state = 0;
}

my $onoff_word = $current_state ? "ON" : "OFF";
my $on_checked = $current_state ? "checked" : "";
my $off_checked = $current_state ? "" : "checked";
print $q->header, qq(
	<html>
	<head>
	<title>Lights</title>
	</head>
	<body>
	<h1>Lights are now $onoff_word</h1>
	<form action="lights.cgi" method=GET>
	  <input type=radio name=state value=y $on_checked />Lights ON<br />
	  <input type=radio name=state value=n $off_checked />Lights OFF<br />
	  <input type=submit value=Send />
	</form>
	</body>
	</html>
);
 
Old 04-12-2004, 10:38 PM   #3
GridX
LQ Newbie
 
Registered: Sep 2003
Location: Canada
Distribution: Fedora & Gentoo-uClinux
Posts: 15

Original Poster
Rep: Reputation: 0
Hey thanks for the help, nice job on the script but I cant use perl scripts(sorry), boa doesnt support it, I have to use either unix shell scripts or C programming scripts.


I dont need the results sent back to the web page, all I need is the user imput to activate or call my C program. C lights program

GridX

Last edited by GridX; 04-12-2004 at 10:48 PM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
shell script works form command line but not form crontab saifee General 1 10-14-2004 10:27 AM
Exec CGI within a form field newuser455 General 0 09-12-2004 01:50 PM
how do I copy a whoel folder form one directory to another form the command line? zwyrbla Linux - Newbie 8 08-24-2004 06:40 PM
form processing scripts help? tuxfood Programming 1 05-11-2004 06:28 AM
CGI guestbook cant see form amp2000 Programming 2 10-31-2001 01:37 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:34 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration