Common Gateway Interface
A Common Gateway Interface (CGI) program can be written in any language that allows it to be executed on a computer. The most commonly used languages for CGI at present are C and Perl. CGI programs written in C typically have to be compiled. They are difficult to modify and maintain. Therefore, many people prefer to write CGI programs in Perl, which is interpretive and resembles C language. However, writing and maintaining a large program in Perl is dreadful.

Ch is a superset of C interpreter with classes in C++. Existing C code can readily be used for CGI. The Ch language environment has been developed to be especially suitable for Web programming. The classes CResponse, CRequest, CServer, and CCookie are the basic building blocks for CGI in Ch. These CGI classes support wide character. They significantly simplify Web-based application development. For example, the code below can be used to display all environmental variables in common gateway interface in different platforms of Unix, Linux, and Windows.

#!/bin/ch #include <cgi.h> #include <stdlib.h> int main() { int i; CResponse Response; Response.begin(); Response.title("Environment values"); printf("<H1> Environment values</H1>\n"); for(i=0; environ[i] != NULL; i++) { printf("%s<br>\n", environ[i]); } Response.end(); }

CGI programming in Ch is simple, easy, and fun. This Web Calculator is just one of the typical application examples.

The following extensions of Ch over C make it ideal for programming in Common Gateway Interface.

  • String is a first-class object. For example,
          string_t s, a[3];
          s = "great string"
          strcpy(a[0], s);
          strcat(s, s);
          printf("s = %s\n", s);
  • High-level features for on-line plotting and numerical computing.
  • Easy debugging CGI code. The option -g in
     #!/bin/ch -g 
    in a Ch CGI program will turn a Web browser into a text console. All output, including error messages from execution of the CGI script, will be displayed inside your browser for convenient debugging.
  • High-level classes for CGI have been developed for CGI programming. These classes include CResponse, CRequest, CServer and CCookie. For example, the following Ch CGI program can be used to process fill-out forms on a Web server as demonstrated here
    int main() {
        int i, num;
        chstrarray name, value;
        class CResponse Response;
        class CRequest Request;
        printf("CGI FORM test script reports:\n\n");
        num = Request.getFormNameValue(name, value);
        if(num == 0) {
            printf("No input from FORM\n");
        else if(num == -2) {
            printf("No enough memory\n");
        printf("The following %d name/value pairs are submitted\n\n",num);
        for(i=0; i < num; i++)
  • Verbatim output from Ch programs. Often time, a block of HTML code needs to be sent as a standard output stream in a CGI program. For example, the CGI program below
        #include <cgi.h>
        char hello[] = "Hello, world";
        class CResponse Response;
        printf("<h4> <center> %s <center> </h4>\n", hello);
    will print out

    Hello, world

    in a web browser. Using verbatim output features, the above Ch CGI program can be simplified as

        #include <cgi.h>
        char hello[] = "Hello, world";
        class CResponse Response;
        fprintf stdout << ENDPRINT
            <Title> $hello </Title>
            <h4> $hello </h4>
    If the value of a variable is used inside a verbatim output block, a dollar sign $ can be used to retrieve the value of the variable as demonstrated by variable hello in the above program.
  • Ch and Ch-CGI classes support wide characters for internationalization. The CGI program below processes a fill-out form using wide characters for names and values. #!/bin/ch #include <wcgi.h> int main() { class CResponse Response; class CRequest Request; chchar *value, *name; Response.begin(); Response.title(L"Test of Request.getForm"); printf("<H1> Test of Request.getForm </H1> <hr>\n"); //get name name = Request.getForm(L"name"); printf("name is : "); fputws(name ? name : L"NULL", stdout); printf("<br>\n"); //get favorites value = Request.getForm(L"flavor"); prntf("flavor is :"); fputws(value ? value : L"NULL", stdout); printf("<br>\n"); Response.end(); }
  • Upload files to a Web server. An example is provided in Ch CGI User's Guide.

The following on-line examples will further demonstrate the capabilities of Ch for CGI. The source code for HTML and Ch CGI programs for these examples and sample code for using each member function of CGI classes are available in the distribution of the Ch language environment. They can be accessed through a Web page http://your_web_site/chhtml/lang/index.html after Ch is installed on your Web server.

  • Web calculator

  • Fill-out forms

  • Examples of using Ch CGI for web-based plotting, 3D graphics, numerical analysis, design and analysis of control systems are available.