Back to index

wims  3.65+svn20090927
html.c
Go to the documentation of this file.
00001 /*    Copyright (C) 1998-2003 XIAO, Gang of Universite de Nice - Sophia Antipolis
00002  *
00003  *  This program is free software; you can redistribute it and/or modify
00004  *  it under the terms of the GNU General Public License as published by
00005  *  the Free Software Foundation; either version 2 of the License, or
00006  *  (at your option) any later version.
00007  *
00008  *  This program is distributed in the hope that it will be useful,
00009  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00010  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00011  *  GNU General Public License for more details.
00012  *
00013  *  You should have received a copy of the GNU General Public License
00014  *  along with this program; if not, write to the Free Software
00015  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00016  */
00017 
00018        /* html form simplification routines */
00019 
00020 #define MAX_MENU_ITEMS 256
00021 
00022 enum {
00023     FORM_SELECT, FORM_RADIO, FORM_BAR, FORM_CHECKBOX
00024 };
00025 
00026        /* Produces <select> and <option>s.
00027         * Parameter string: name from n1 to n2 [prompt plist]
00028         * or name, list slist [prompt plist] 
00029         * Using script to do this is too slow. */
00030 void _form_menus(char *p,int kind)
00031 {
00032     char *n, *li, *pp, *val, *p0, *p1, *p2, *pc;
00033     char *vlist[MAX_MENU_ITEMS], *plist[MAX_MENU_ITEMS];
00034     char nbuf[MAX_LINELEN+1],vbuf[MAX_LINELEN+1],pbuf[MAX_LINELEN+1];
00035     char buf[256];
00036     int i,i1=0,i2,itemcnt,type;
00037     if(!outputing) return;
00038     n=find_word_start(p); if(*n==0) return;
00039     pp=find_word_end(n);
00040     if(*pp!=0) *(pp++)=0;
00041     else {
00042        syntax: module_error("syntax_error"); return;
00043     }
00044     li=find_word_start(pp); pp=find_word_end(li);
00045     if(*pp!=0) *(pp++)=0; else goto syntax;
00046     itemcnt=0;
00047     if(strcmp(li,"from")==0) {
00048        p1=find_word_start(pp);
00049        p2=wordchr(p1,"to"); if(p2==NULL || p2<=p1) goto syntax;
00050        *(p2-1)=0; p2=find_word_start(p2+strlen("to"));
00051        pp=wordchr(p2,"prompt"); if(pp!=NULL) *(pp-1)=0;
00052        i1=evalue(p1); i2=evalue(p2);
00053        if(i1<-100000000 || i1>100000000 || i2<-100000000 || i2>100000000)
00054          goto syntax;
00055        if(i2<i1) return;
00056        if(i2>i1+MAX_MENU_ITEMS-1) i2=i1+MAX_MENU_ITEMS-1;
00057        itemcnt=i2-i1+1;
00058        type=0;
00059     }
00060     else {
00061        if(strcmp(li,"list")==0) {
00062            p1=find_word_start(pp);
00063            pp=wordchr(p1,"prompt"); if(pp!=NULL && pp>p1) *(pp-1)=0;
00064            strcpy(vbuf,p1);substit(vbuf);
00065            itemcnt=cutitems(vbuf,vlist,MAX_MENU_ITEMS);
00066            type=1;
00067        }
00068        else goto syntax;
00069     }
00070     for(i=0;i<itemcnt;i++) plist[i]=NULL;
00071     if(pp!=NULL) {
00072        p1=find_word_start(pp+strlen("prompt"));
00073        strcpy(pbuf,p1);substit(pbuf);
00074        cutitems(pbuf,plist,MAX_MENU_ITEMS);
00075     }
00076     strcpy(nbuf,n);substit(nbuf);
00077     if(kind==FORM_SELECT) {
00078        char *pp;
00079        pp=getvar("wims_formselect_switch"); if(pp==NULL) pp="";
00080        output("<select %s name=\"%s\">\n",pp,nbuf);
00081     }
00082     if(kind==FORM_BAR) _output_("<b>-</b>");
00083     val=getvar(nbuf);if(val==NULL) val="";
00084     for(i=0;i<itemcnt;i++) {
00085        if(type==0) {
00086            snprintf(buf,sizeof(buf),"%d",i+i1);
00087            p0=buf;
00088        }
00089        else p0=vlist[i];
00090        if(*find_word_start(p0)==0) continue;
00091        if(plist[i]==NULL) plist[i]=p0;
00092        if(*val!=0 && 
00093           (strcmp(p0,val)==0 || 
00094            ( (kind==FORM_SELECT || kind==FORM_CHECKBOX) 
00095             && itemchr(val,p0)!=NULL))) {
00096            if(kind==FORM_SELECT) pc=" selected=\"selected\"";
00097            else pc=" checked=\"checked\"";
00098        }
00099        else pc="";
00100        switch(kind) {
00101            case FORM_SELECT:
00102            output("<option value=\"%s\"%s>%s\n",p0,pc,plist[i]);
00103            break;
00104            
00105            case FORM_RADIO:
00106            output("<input type=\"radio\" name=\"%s\" value=\"%s\"%s>&nbsp;%s",
00107                  nbuf,p0,pc,plist[i]);
00108            if(i<itemcnt-1 && itemcnt>2) _output_(",");
00109            _output_("\n");
00110            break;
00111            
00112            case FORM_CHECKBOX:
00113            output("<input type=\"checkbox\" name=\"%s\" value=\"%s\"%s>&nbsp;%s",
00114                  nbuf,p0,pc,plist[i]);
00115            if(i<itemcnt-1 && itemcnt>2) _output_(",");
00116            _output_("\n");
00117            break;
00118            
00119            case FORM_BAR:
00120            output("<input type=\"radio\" name=\"%s\" value=\"%s\"%s>",
00121                  nbuf,p0,pc);
00122            break;
00123            
00124        }
00125     }
00126     if(kind==FORM_SELECT) _output_("</select>");
00127     if(kind==FORM_BAR) _output_("<b>+</b>");
00128 }
00129 
00130 void exec_formselect(char *p)
00131 {
00132     _form_menus(p,FORM_SELECT);
00133 }
00134 
00135 void exec_formradio(char *p)
00136 {
00137     _form_menus(p,FORM_RADIO);
00138 }
00139 
00140 void exec_formcheckbox(char *p)
00141 {
00142     _form_menus(p,FORM_CHECKBOX);
00143 }
00144 
00145 void exec_formbar(char *p)
00146 {
00147     _form_menus(p,FORM_BAR);
00148 }
00149