/* example.c * Parser Based C Program Example * Kacper Nowicki (kacper@fuw.edu.pl), 1993 */ #include <stdio.h> #include "parser.h" int get_year(void); int main(int argc, char **argv) { char *fname = NULL; /* Variable for first name */ int birthdate = 0; /* Variable for birthdate */ char *age; /* Variable for age string */ int check = 0; /* Variable for checbox */ char **desc = NULL; /* Variable for person description */ int nread; int year; /* This input table for parser * It has 3 fields : * pointer to variable, * name of the variable (name set in WWW form) * variable type (one of : string, char, int, long, float) * Read longer description in parser.h */ partab_t table[]= { {&fname, "fname", PARSER_STRING}, {&birthdate,"birthdate",PARSER_INT}, {&age, "age", PARSER_STRING}, {&check, "check", PARSER_BOOL}, {&desc, "desc", PARSER_MULTIS}, {NULL, NULL, 0} }; printf("Content-type: text/html\r\n\r\n"); /* Run parser and get values to variables */ nread = parser(table); if(nread < 0) { printf("parser internal error !"); return(1); } else if(nread < sizeof(table)/sizeof(partab_t) - 3) { printf("ERROR: Parser read %d variable(s)", nread); return(1); } /* Check if the form was filled properly */ if(! fname) { printf("You should enter your name, to receive proper output."); return(1); } if(birthdate <= 0) { printf("You enter wrong birthdate. Try one more once."); return(1); } year = get_year(); if(birthdate > year) { printf("You enter wrong birthdate %d, we have still %d", birthdate, year); return(1); } if(check && !desc) { printf("You checked box but didn't select anything !"); return(1); } if(birthdate < 100) birthdate += 1900; /* Prepare output */ printf("Dear <b>%s</b> you are %d years old.<p>", fname, year - birthdate); printf("You said, that you are <i>%s</i>, is this correct ?", age); if(check) { printf("<p><i>Whow !</i> You are %s", *desc); while(*++desc) printf(", %s", *desc); printf(" person !"); } printf("<hr><i> Kacper Nowicki (kacper@fuw.edu.pl)</i> - online"); return(0); } /* main */ /* Find year */ #include <sys/types.h> #include <sys/time.h> #include <time.h> int get_year(void) { time_t clk; clk = time(NULL); return(atoi(ctime(&clk) + 20)); } /* get_year */