/* name: next.c purpose: generic storyboarding cgi script author: george j. carrette gjc@delphi.com $Id: next.c,v 1.6 1998/01/19 18:50:55 gjc Exp $ */ #include #include #include #include "chtml.h" static void failure(char *msg) {printf("Status: 500 Server Error (Application)\n"); printf("Content-type: text/plain\n\n"); printf("%s\n",msg);} static char *read_content(size_t *retlen) {char *content; *retlen = 0; if (getenv("REQUEST_METHOD") && (strcmp(getenv("REQUEST_METHOD"),"POST") == 0) && getenv("CONTENT_LENGTH") && ((*retlen = atol(getenv("CONTENT_LENGTH"))) > 0)) {content = (char *) malloc(*retlen + 1); content[*retlen] = 0; fread(content,1,*retlen,stdin); return(content);} else return(NULL);} static CHTML_STRITEM table = NULL; static void setup_from_envp(char *val) {char *key,*p; size_t n; if (!(p = strchr(val,'='))) return; n = p - val; key = (char *) malloc(n + 1); memcpy(key,val,n); key[n] = 0; chtml_stritem_kinsert(&table,key,p+1,NULL); free(key);} static void setup_from_qs(char *s) {chtml_stritem_kinsert_qs(&table,s);} int main(int argc,char **argv,char **envp) {char **p,*tname,*content; struct chtml *form; int len; size_t clen; if ((!(tname = getenv("PATH_INFO"))) || (strlen(tname) == 0)) {failure("chtml template not specified in url"); return(EXIT_FAILURE);} if (tname[0] == '/') ++tname; if (!(form = chtml_load(tname,NULL))) {failure("Could not load application template file"); return(EXIT_FAILURE);} for(p=envp;*p;++p) setup_from_envp(*p); if (getenv("QUERY_STRING")) setup_from_qs(getenv("QUERY_STRING")); if ((content = read_content(&clen))) {setup_from_qs(content); free(content);} if (atol(chtml_stritem_get(".DEBUG",table))) {printf("Content-type: text/plain\n\n"); chtml_stritem_debug_print(table);} len = chtml_size_write((char *(*)(char *,void *)) chtml_stritem_eval, table, form); if (len < 0) failure("Could not format result screen"); else {chtml_stritem_rewind(table); if (!(getenv("NEXT_NOTYPE") && atol(getenv("NEXT_NOTYPE")))) printf("Content-type: text/html\nContent-length: %d\n\n",len); chtml_do_write((char *(*)(char *,void *)) chtml_stritem_eval, table, form, (void (*)(char *,void *))fputs, stdout);} chtml_stritem_free(&table); chtml_free(form); return((len >= 0) ? EXIT_SUCCESS : EXIT_FAILURE);}