-   Programming (
-   -   executing CGI scripts from command line (

mcnscott 07-31-2007 08:26 PM

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

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.

raskin 07-31-2007 10:51 PM

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. 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).

mcnscott 08-01-2007 01:47 AM

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.

raskin 08-01-2007 01:52 AM

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. has nice manuals for it.. But surely a user will be puzzled to see Content-Type: right before questions.

mcnscott 08-01-2007 02:16 AM

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.

bigearsbilly 08-01-2007 03:31 AM

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

bigearsbilly 08-01-2007 04:16 AM

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..

</PRE><FORM METHOD="POST" ACTION=""  ENCTYPE="multipart/form-data">
<P>Add some text</P>
<P>Choose a stone</P>
<OPTION  VALUE="mick">mick
<OPTION  VALUE="keef">keef
<OPTION  VALUE="charlie">charlie
<OPTION  VALUE="bill">bill
<OPTION  VALUE="brian">brian
<INPUT TYPE="submit" NAME="OK" VALUE="OK"><INPUT TYPE="hidden" NAME=".cgifields" VALUE="STONE"></FORM>

CGI script


cat <<EOF
Content-Type: text/html

<H4>standard input</H4>
while read line; do
    echo $line

try with POST and GET

jarro_2783 08-01-2007 08:08 PM

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.


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.


#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.

* 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.

All times are GMT -5. The time now is 03:03 AM.