Parser for NCSA httpd 1.0a5

This page describes parser routine working with NCSA World-Wide Web server httpd 1.0a5.

You can find utility routines developed in NCSA together with example here. We want to present different package. Let us consider following (ruther silly) form example. It has just three variables defined :

Tell me your first name : <input name="fname" size=12>
Tell me your birthdate (year) : <input name="birthdate" 
    size=4 maxlength=4>
You encouter yourself <select name="age">
    <option> very young
    <option selected> young
    <option> mid age
    <option> old
</select>

It would be great if we could read form variables into C program variables. Let us define following variables :

char 
    *fname = NULL,      /* Variable for first name */
    *age;               /* Variable for age string */
int 
    birthdate = 0;      /* Variable for birthdate */
We initialized variables fname and birthdate into their error values. We know that age always has a value (it's value is generated from the menu).

Now we have to build a table of partab_t elements (for details see header file parser.h), to give parser form variables names and pointers to C program variables :

partab_t table[]=
{   {&fname,    "fname",    PARSER_STRING},
    {&birthdate,"birthdate",PARSER_INT},
    {&age,      "age",      PARSER_STRING},
    {NULL,      NULL,       0}
};
Isn't it easy ? You just have to give : You should remember to terminate your partab_t table with entry with first element equal NULL.

The last thing to do is to run parser function defined in parser.c source file. Our program ( example.c) uses following code :

nread = parser(table, argc, argv);
if(nread < 0)
{   printf("parser internal error !");
    return(1);
}
else if(nread < sizeof(table)/sizeof(partab_t) - 1)
{   printf("ERROR: Parser read %d variable(s)", nread);
    return(1);
}
Return value from parser function is less than zero if internal error occurs. In other case it is equal to number of variables read from the form. Our program returns error if this number is not equal to number of variables defined in table.

If parser is sucessful you can easily use variables read from the form. You can make calculations with int, long and float variables or deal with separately mallocked strings (char *).

Now look for the working version of form and C program or look at the sources :

parser
parser.h
parser.c
form exmaple
example.html
example.c
You can also get tar file containing sources if you press here.

If you have any suggestions or comments press this. You can compare mailing program based on parser ( mailwww) to program written with procedures from util.c ( mailn).


This document is placed at FUW (Physics Department of Warsaw University). If you want to go to our home page press this.
Kacper Nowicki (kacper@fuw.edu.pl) - 20.12.93