CGI - kent

cart-free CGI

#include "common.h"
#include "cheapcgi.h"

int main()
         "<h1>absolutely cart-free</h1>");

char *value = cgiOptionalString("param");
if(value == NULL)
     puts("param not supplied");
    printf("value: %s", value);

    printf("<br>Numeric value: %d", cgiOptionalInt("numeric", -1));
    puts("<br>numeric parameter doesn't exist");

puts("<br>we're done</body></html>");
return 0;

  • cgiUsualString() --- cartUsualString()
  • cgiVarExists()     --- cartVarExists()
  • cgiString()  use only if sure that parameter exists, if parameter doesn't exist, the program will terminate silently, 
  • cgiOptionalInt()   if input is invalid, program will terminate silently, it actually calls cgiInt()
  • cgiInt()      DON'T USE: if input cannot be converted to number, program will terminate silently
Note: strings returned by cgiString() have only one memory location -- need to make copy if will change it (with strtok or else)

A very simple program

#include "common.h"
#include "hash.h"
#include "cheapcgi.h"
#include "cart.h"
#include "hui.h" // for hUserCookie()

// cart the global variable
struct cart *cart;
struct hash *oldVars = NULL;

void doMiddle(struct cart *theCart)
    cart = theCart;
    cartWebStart(cart, "hg19", "This is page title");
    // form start
    printf("<FORM ACTION=\"../cgi-bin/program\">");

    // code that actually do stuff goes here, after this end form
    // get an integer value
    int int_val = cartInt(cart, val_name);
    // get a string
    char *str_val = cartString(cart, val_name2);



char *excludeVars[] = {"Submit", "submit", NULL};
int main()
    cartEmptyShell(doMiddle, hUserCookie(), excludeVars, oldVars);
    return 0;

Check box

Supposed way to use it...

cartMakeCheckBox(cart, cn_name, TRUE); // TRUE or 0 or 1
if(cartUsualInt(cart, cn_name, 0) == 0)
     // unchecked
     // checked

A secure way to use checkbox with cart.h library:

char *box_name = "vvv";
printf("<input type=checkbox name=%s %s> check me",
            cartVarExists(cart, box_name ? "checked" : "");
cartRemove(cart, box_name);

  • A plain checkbox made by "printf" doesn't have such "boolshad." thing, thus makes URL string much shorter.
  • If the box is unchecked, nothing should be inserted in the URL.
  • If the box is checked, a string like "vvv=on" will show up in URL. So whenever the variable exists in URL string means it was checked in last invocation.
  • After making checkbox, must call cartRemove() on the box variable, because we are using the box itself to record the state, not relying on the cart thing.

Text field

To decide whether user entered anything:
char *field_name;
if(cartUsualString(cart, field_name, "")[0] == '\0')
   puts("<p>User is dumb</p>");
   printf("<p>%s</p>", cartString(cart, field_name));

For text field the cartRemove() won't have any effect, even when the text input field is empty, the variable name will still appear in the URL.

Drop list

make one, showing previously selected item, or first item in array by default
// name of the control variable
char *vname = "sukn_genebody";

// array of values
char *modes[] = {"none", "thin", "full"};

// make it
cgiMakeDropList( vname, modes, ArraySize(string_arr), 
       cartUsualString( cart, vname, modes[0] )  );

Image button

char *but_name = "foobar";
printf("<INPUT TYPE=\"IMAGE\" SRC=\"foo.png\" name=\"%s\" value=\"whatsoeverythisdoesntmatter\">", but_name);
if(cartVarExists(cart, but_name))
   char *xtmp;  // make string: foobar.x
   char *ytmp;  // make string: foobar.y
   printf("clicked at %s %s", cartString(cart, xtmp), cartString(cart, ytmp));
   cartRemove(cart, but_name);

Note: the "value" attribute to image button is just to make sure the "foobar=itsvalue" appears in URL string and it will be caught by cartVarExists()

Update 09/14/2011: may I guess above contents have already been obsolete? ...