yacgi: Yet Another C/C++ library for CGI Programming

version 1.2
Andrew Girow / Andriy Zhyrov /

Copyright © 1997 Andrew Girow. All Rights Reserved.

Table of Contents


What's new in version 1.2?

Version 1.02 corrects bugs in previous versions: New examples are added

Why yacgi?

There are two main sources for yacgi:

There are two main reasons for yacgi:


Basic concepts

In general, a binary relation consists of two terms a key and a value which refer to entities, and a predicate an access function which connects the terms by saying something about them.

example:   "A person works in an enterprise."

Picture 1.

In general, an access function is a function which maps one object into the powerset of another (the set of all subset). While defining a relation one gives the key and value object types involved, and one defines the access function and gives information about its cardinality. When the cardinal of an access function is unique then it is a function. When the cardinal of an access function is multiple then it is a multiple-valued function.

For the CGI programming (about the CGI standard, see the CGI documentation at NCSA.) we can use a very simple model:

Picture 2. Binary Relation Model of CGI Programming


Credits and License Terms

yacgi can be used free of charge, provided that a credit notice is provided online. Alternatively, a nonexclusive Commercial License can be purchased, which grants the right to use cgic without a public credit notice.

Please see the file license for the details of the Basic License and Commercial License, including ordering information for the Commercial License.


Obtaining yacgi

Your web browser should inquire whether to save the file to disk when you select the link below.

Using yacgi

There are very few basic concepts that you need to know. We describe the concepts as abstract data types. There is a 1:1 correspondence between YACGI abstract data types and the Binary Relations Approach to CGI basic items.

Opening Relation
This code opens the relation.

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

main(int argc, char *argv[])
{
    CGI *cgi;

    printf("Content-type: text/html%c%c",10,10);
    /*-----------------------------------------------------------
     *              Opening Relation
     *-----------------------------------------------------------*/
    cgi = cgiOpen();
    if(!cgi)
    {
       printf("<H3>%s</H3>%c",cgiStateMsg(),10);
       exit(1);
    }

Closing Relation
You should close the relation before exit the program.

    /*------------------------------------------------------
     *                 Closing Relation.
     *------------------------------------------------------*/
    cgiClose(cgi);
}

Scanning Relation
You can use traversing or scanning the relation, accessing each stored pair of names-values in turn to perform some test or action.

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

main(int argc, char *argv[])
{
    CGI *cgi;
    int more;
    char *string;

    printf("Content-type: text/html%c%c",10,10);
    /*-----------------------------------------------------------
     *              Opening Relation
     *-----------------------------------------------------------*/
    cgi = cgiOpen();
    if(!cgi)
    {
       printf("<H3>%s</H3>%c",cgiStateMsg(),10);
       exit(1);
    }
    /*-----------------------------------------------------------
     *              Scanning Relation
     *-----------------------------------------------------------*/
    printf("<CENTER><H2>Scanning Name-Value Relation</H2></CENTER>");
    printf("You submitted the following name/value pairs:<p>%c",10);
    printf("<ul>%c",10);
    more= cgiFirst(cgi);
    while(more)
    {
        printf("<li> <code>%s = %s</code>%c",cgiName(cgi),
               cgiValue(cgi),10);
        more = cgiNext(cgi);
    }
    printf("</ul>%c",10);
    /*------------------------------------------------------
     *                 Closing Relation.
     *------------------------------------------------------*/
    cgiClose(cgi);
}

Evaluating Relation

We can "navigate" from objects to objects using binary relation. To do this, we use cgiValueFirst and cgiValueNext functions. For instance, we have a Name "sex" and want to know value of sex. The code is:

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

main(int argc, char *argv[])
{
    CGI *cgi;
    int more;
    char *string;

    printf("Content-type: text/html%c%c",10,10);
    /*-----------------------------------------------------------
     *              Opening Relation
     *-----------------------------------------------------------*/
    cgi = cgiOpen();
    if(!cgi)
    {
       printf("<H3>%s</H3>%c",cgiStateMsg(),10);
       exit(1);
    }
    /*-----------------------------------------------------------
     *              Evaluating Relation
     *-----------------------------------------------------------*/

    printf("<CENTER><H2>Evaluating Name-Value Relation</H2></CENTER>");
    printf("<p><b>%s</b><br>%c","First Name",10);
    string = cgiValueFirst(cgi, "First_Name");
    if(string) printf("<code>%s</code><br>%c",string,10);

    printf("<p><b>%s</b><br>%c","Last Name",10);
    string = cgiValueFirst(cgi, "Last_Name");
    if(string) printf("<code>%s</code><br>%c",string,10);

    printf("<p><b>%s</b><br>%c","Date of Birth",10);
    string = cgiValueFirst(cgi, "D_O_B");
    if(string) printf("<code>%s</code><br>%c",string,10);

    printf("<p><b>%s</b><br>%c","Sex",10);
    string = cgiValueFirst(cgi, "Sex");
    if(string) printf("<code>%s</code><br>%c",string,10);

    printf("<p><b>%s</b><br>%c","Degree",10);
    string = cgiValueFirst(cgi, "Degree");
    if(string) printf("<code>%s</code><br>%c",string,10);

    printf("<p><b>%s</b><br>%c","Current Occupation",10);
    string = cgiValueFirst(cgi, "Occupation");
    if(string) printf("<code>%s</code><br>%c",string,10);

    printf("<p><b>%s</b><br>%c","Duties",10);
    string = cgiValueFirst(cgi, "Duties");
    if(string) printf("<code>%s</code><br>%c",string,10);

    /*----------------------------------------------------------
     * Let us now look at multiple relations. Assume we want the
     * skills of the person.
     * We write:
     *---------------------------------------------------------*/

    printf("<p><b>%s</b><br>%c","Skills",10);
    string = cgiValueFirst(cgi, "Skills");
    while(string)
    {
        printf("<code>%s</code><br>%c",string,10);
        string = cgiValueNext(cgi, "Skills");
    }
    /*------------------------------------------------------
     *                 Closing Relation.
     *------------------------------------------------------*/
    cgiClose(cgi);
}

Advanced Features

Provides string, integer, floating-point, and safe string without shell metacharacters functions to retrieve form data.

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

main(int argc, char *argv[])
{
    CGI *cgi;
    int more;
    char *string;
    int flag;
    long i;
    double r;
    char c[31];


    printf("Content-type: text/html%c%c",10,10);
    /*-----------------------------------------------------------
     *              Opening Relation
     *-----------------------------------------------------------*/
    cgi = cgiOpen();
    if(!cgi)
    {
       printf("<H3>%s</H3>%c",cgiStateMsg(),10);
       exit(1);
    }
    /*-------------------------------------------------------------
     *              Advanced Features: Input String, Integer, Real,
     *              Safe String
     *-----------------------------------------------------------*/
    printf("<CENTER><H2>Advanced Features</H2></CENTER>");

    printf("<p><b>%s</b><br>%c","String",10);
    flag = cgiValueString(cgi, "String", c, 30);
    if(flag == CGI_OK || CGI_VAL_TRUNCATED )
        printf("<code>%s</code><br>%c",c,10);
    else
        printf("<code>%s</code><br>%c",cgiStateMsg(),10);

    printf("<p><b>%s</b><br>%c","Integer",10);
    flag = cgiValueInteger(cgi, "Integer", &i, 1);
    if(flag == CGI_OK )
        printf("<code>%ld</code><br>%c",i,10);
    else
        printf("<code>%s</code><br>%c",cgiStateMsg(),10);

    printf("<p><b>%s</b><br>%c","Real",10);
    flag = cgiValueReal(cgi, "Real", &r, 1);
    if(flag == CGI_OK )
        printf("<code>%lf</code><br>%c",r,10);
    else
        printf("<code>%s</code><br>%c",cgiStateMsg(),10);


    printf("<p><b>%s</b><br>%c","SafeString",10);
    string = cgiValueFirst(cgi, "SafeString");
    if(string)
        printf("<code>%s</code><br>%c",cgiSafeStr(string),10);
    /*------------------------------------------------------
     *                 Closing Relation.
     *------------------------------------------------------*/
    cgiClose(cgi);
    return(0);
}

The sample application 'yacsampl.c' is provided as part of the yacgi distribution. This CGI program accepts input submitted by the form yacsampl.html.

IMPORTANT: after compiling yacgi, you will need to place it in a location on your server system which is designated by your server administrator as an appropriate location for CGI scripts. Also, the URL of the action of the sample form in yacsampl.html must be changed to correctly indicate the location of yacsampl on your web server.


yacgi reference

There are very few basic concepts that you need to know. We describe the concepts as abstract data types.

HOME

Copyright © 1997 Andrew Girow. All Rights Reserved.


Last updated: July 21, 1997 1