LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
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 07-31-2007, 08:26 PM   #1
mcnscott
LQ Newbie
 
Registered: Jul 2007
Posts: 3

Rep: Reputation: 0
executing CGI scripts from command line


Hi everyone, I'm having a hard time trying to figure out how to implement this.

Develop a program written in C that will handle encoding and decoding of
CGI data.

* Your program should run from a command-line interface in Linux (preferably)
or DOS. It should prompt the user to enter these items:
1. Name
2. Address
3. Phone number

* Once you collect this data, CGI encode it and display the encoded string.

* Decode the CGI data, and display a label and associated value (these should
match exactly what the user entered.)

* Do not link in or use any code except what can be found in the standard
libraries.

Now from I can set up and make a program that will fulfill the data requirements with executable that a user runs, though I'm not sure how to convert it to CGI. I can also make the CGI script so that if the user goes to a webpage, puts in the information the requirements will be fulfilled. My problem is how do I encode and decode data as CGI from a command line or shell, and can data from an executable be made into CGI data. Thanks.
 
Old 07-31-2007, 10:51 PM   #2
raskin
Senior Member
 
Registered: Sep 2005
Location: France
Distribution: approximately NixOS (http://nixos.org)
Posts: 1,900

Rep: Reputation: 69
I am not really sure what it is about, but I will assume that CGI encoding is HTTP GET request encoding (which is URI encoding) which is used primarily by CGI scripts. It is surely not about writing a CGI script. Anyway I am sure that you do not need to run a CGI script with webserver.
http://www.ietf.org/rfc/rfc2396.txt describes URI syntax. I think you need to create a string like name=...&address=...&phone=... which is encoded as to be safe and allowed for URL query part. The interesting part for you in this case is what symbols are allowed and what must be %-escaped (and how it should be done).
 
Old 08-01-2007, 01:47 AM   #3
mcnscott
LQ Newbie
 
Registered: Jul 2007
Posts: 3

Original Poster
Rep: Reputation: 0
Thank you very much, I'm going to focus on just making an executable that asks the user for input and encodes it via URI encoding. The part that was throwing me was how to make a program that is suppose to be run on the back end of a server able to be executed from a command line, though I suspect that finding out and researching URI encoding is the purpose of the assignment (kind of sneaky), because I can make a C program that asks for input and then echoes it back to the user with no difficulty.
 
Old 08-01-2007, 01:52 AM   #4
raskin
Senior Member
 
Registered: Sep 2005
Location: France
Distribution: approximately NixOS (http://nixos.org)
Posts: 1,900

Rep: Reputation: 69
Really the part you asked about is not that tricky - if you ever need it, remember that Apache HTTPD just runs CGI scripts like they would be run from command line, but sets additional environment variables (like QUERY_STRING) and can feed POST data to stdin. apache.org has nice manuals for it.. But surely a user will be puzzled to see Content-Type: right before questions.
 
Old 08-01-2007, 02:16 AM   #5
mcnscott
LQ Newbie
 
Registered: Jul 2007
Posts: 3

Original Poster
Rep: Reputation: 0
Thank you for the help Raskin, I installed apache on my machine here at home, and made it a localhost so that I could just test CGI scripts after I made them with my C compiler, (I've written quite a fewn scripts so far in C and PERL with no problems getting them to execute when I type the URL for my localhost). Do you think you could point me to what direction I should be headed next? Personally, I'm thinking of using g++ and writing a C program the user will execute from linux command line it will:

1. ask the user for the input
2. convert it URI style as you suggested
3. then decode the text
4. display it back to the user with labels
5. exit

Looking at the requirements does this seem sensible or should I be looking at a different approach? Thanks a bunch for your help.
 
Old 08-01-2007, 03:31 AM   #6
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
it seems as if you are being asked to implement a rudimentary
CGI enabled server,
this is quite interesting:



http://www.w3.org/CGI/
 
Old 08-01-2007, 04:16 AM   #7
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
homework isn't strictly allowed but in your case you are
not one of those who want their work done for them!

well, I became a bit interested here, and started investigating a bit.
i don't know if you'll find these interesting but I made a simple form
and a shell cgi script to have a look at what goes on.
it illustrates what the script gets from the server..
HTML FORM
Code:
</PRE><FORM METHOD="POST" ACTION="test.sh.cgi"  ENCTYPE="multipart/form-data">
<P>Add some text</P>
<INPUT NAME="TEXT"> </INPUT>
<P>Choose a stone</P>
<SELECT NAME="STONE" SIZE=8>
<OPTION  VALUE="mick">mick
<OPTION  VALUE="keef">keef
<OPTION  VALUE="charlie">charlie
<OPTION  VALUE="bill">bill
<OPTION  VALUE="brian">brian
</SELECT>
<BR>
<INPUT TYPE="submit" NAME="OK" VALUE="OK"><INPUT TYPE="hidden" NAME=".cgifields" VALUE="STONE"></FORM>
CGI script
Code:
#!/bin/ksh

cat <<EOF
Content-Type: text/html

<pre>
<H4>parameters</H4>"$@"
<H4>standard input</H4>
EOF
while read line; do
    echo $line
done
echo "<H4>ENVIRONMENT VARIABLES</H4>"
env
try with POST and GET

Last edited by bigearsbilly; 08-01-2007 at 08:02 AM.
 
Old 08-01-2007, 08:08 PM   #8
jarro_2783
LQ Newbie
 
Registered: Jul 2006
Posts: 11

Rep: Reputation: 0
We did this in uni last year, it's quite easy when you know the little details.

Everything you want to appear on the web page you just printf.
You have to print the following line first though.

Content-Type:text/html\n\n

To do forms I think the best way is to use POST not GET. To set up the form just do what the above post says. Then the uri information is given on stdin. You can do something like this to read it all.

Code:
#define URI_MAX_LEN 1000
char uri[URI_MAX_LEN];
fgets(uri, URI_MAX_LEN, stdin);
The uri format is name=value?name2=value2?name3=value3 etc...

Just one thing I should point out.
Quote:
* Once you collect this data, CGI encode it and display the encoded string.
You don't "CGI encode it" as such. You printf everything, including the form you want data submitted (or the form can be a separate page which has its action as your CGI program), then you process the form.

However since this is a command prompt thing, not actually running on a web server, to CGI encode it you would just print it out as name=value?name2=value2 etc. sprintf is useful, just remember to check buffer lengths.

A tip: the "hidden" input element in html is very useful for saving some "state" in your program (HTML is completely stateless).

It seems like they're asking you to do something a little strange since CGI encoding is done by your internet browser, you can probably ignore half of what I said since it relates to actually making a CGI script to run on a web site.

Last edited by jarro_2783; 08-01-2007 at 08:10 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
Executing command line through Code socialjazz Programming 2 10-02-2006 03:09 PM
CGI Scripts not executing Mr_Oz Linux - Enterprise 7 04-18-2006 03:21 PM
CGI Scripts not executing, pls help john_d13 Linux - General 3 12-22-2004 02:05 PM
Problem executing from command line jleyba213 Linux - General 4 04-09-2004 12:43 PM
executing from command line nodger Linux - Newbie 3 01-20-2004 10:30 AM

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

All times are GMT -5. The time now is 11:48 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