Writing WebTen C Language CGIs


Writing CGIs under WebTen is simple. This describes the construction of C Language CGIs for WebTen.

WebTen contains an extended Apache Web server that supports five different styles of CGIs -- shell CGIs, Perl CGIs, AppleScript CGIs, WSAPI CGIs, and C or C++ program CGIs. While almost any script or programming method can be used to generate text to be executed as a CGI, these methods are the ones that are most commonly used with WebTen.

C Language CGIs are computer programs

This document describes WebTen C Language CGIs. A C Language CGI is a computer program. To produce a C Language CGI you must first write the C Language source code using a Macintosh text editor program. Once the program is written, a C Language translator, called a C compiler, is used to translate the C Language into machine language. The machine language is stored in a file that can be executed by WebTen. WebTen C Language CGIs are constructed using either Tenon's CodeBuilder or MachTen. Both products have complete software development facilities and produce machine language executable files for use with WebTen.

You can use any text editor to create a C Language CGI. We recommend BBEdit as a great general purpose Macintosh text editor, but any editor will do (even SimpleText). A host of traditional UNIX text editors (e.g., Emacs and vi), are available under CodeBuilder and MachTen for creating and editing C Language source files. C Language CGI source files are given the suffix ".c" (e.g., mycgi.c).

Create a new CGI called mycgi.c. Once the C Language source file is constructed, invoke the C Language compiler under CodeBuilder using the following format:

cc -O -o mycgi mycgi.c

This command tells CodeBuilder to produce a machine language file named mycgi using the C Language source found in the file mycgi.c. The resulting machine language file or objectfile is directly executable under CodeBuilder and WebTen. You can use CodeBuilder debugging techniques to ensure that the C Language CGI operates correctly. Once the CGI is complete, store the CGI in the WebTen cgi-bin directory. Then reference the CGI with the following URL:

/cgi-bin/mycgi

The CGI will be invoked by WebTen and the output will be transported to your browser.

Basic Steps

C Language CGIs are high performance WebTen extensions

C Language CGIs are used for high-performance CGIs. Since each C Language CGIis a compiled program, executing a C Language CGI reaps the full benefit of thefastest performance the underlying Macintosh can deliver.

Printenv.c Example

The C Language CGI example included with WebTen is in a file named printenv.c, which is located in the WebTen cgi-bin directory.The printenv source code is in tenon/examples/printenv.c.text. Below is the content of the printenv.c CGI:

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

typedef struct {
	char name[128];
	char val[128];
} entry;

void getword(char *word, char *line, char stop);
char x2c(char *what);
void unescape_url(char *url);
void plustospace(char *str);

entry entries[10000];

main(int argc, char *argv[]) {
	register int x,m=0;
	char *cl;
  
	printf("Content-type: text/html%c%c",10,10);
  
	if(strcmp(getenv("REQUEST_METHOD"),"GET")) {
    
		printf("This script should be referenced with a METHOD of GET.\n");
    
		printf("If you don't understand this, see this ");
    
		printf("<A HREF=\"http://www.ncsa.uiuc.edu/SDG/Software/Mosaic/Docs/fill-out-forms/overviewhtml\">formsoverview</A>.%c",10);
		exit(1);
 	}
  
	cl = getenv("QUERY_STRING");
	if(cl == NULL) {
	printf("No query information to decode.\n");
	exit(1);
	}
	for(x=0;cl[0] != '\0';x++) {
		m=x;
		getword(entries[x].val,cl,'&');
		plustospace(entries[x].val);
		unescape_url(entries[x].val);
		getword(entries[x].name,entries[x].val,'=');
	}
  
	printf("<H1>Query Results</H1>");
	printf("You submitted the following name/value pairs:<p>%c",10);
	printf("<ul>%c",10);
  
	for(x=0; x <= m; x++)
    
	printf("<li> <code>%s = %s</code>%c", entries[x].name, entries[x].val,10);
	printf("</ul>%c",10);
}

This CGI prints the name/value parameter pairs that are available to any CGI when the CGI is invoked. The general flow of the printenv CGI is that it uses the printfstatement to output Content-type: text/html\n\n. This is needed in order for the CGI to inform WebTen and the remote browser of the type of content to follow.

The program then verifies whether or not a GET type of HTTP request was used to initiate the CGI. If a GET request was not used, an error message is returned with several printfstatements and the program exits. If a GET HTTP request is found, the environment variable QUERY_STRING is requested. If that string is unavailable, an error message is printed and the program exits. If QUERY_STRING is found, a for loop is entered. The for loop calls the getword subroutine to parse the string into name and value pairs. Once all of the parameters have been parsed, the printf subroutine is called several times to output a constant string "QUERY RESULTS", followed by the string "You submitted the following name/value pairs:", followed by a name and value pair on each line until all of the name/value parameters have been displayed. When the printenv CGI is referenced by the URL:

/cgi-bin/printenv?company=Tenon Intersystems&addr=1123 Chapala 
St.&city=Santa Barbara

It produces the following output:

Query Results

You submitted the following name/value pairs:


Page last updated 12/28/1999

http://www.tenon.com/support/webten/papers/webten-c-cgi.html