int decodesfname(struct XMLDBtable *ptr,struct XMLDBtable *tree, void *data) { int i,j; struct config *config; struct XMLDBelement *e; char zero[1]={0}; char *symbol[]={"\"","\\","<",">","&",0}; char *entity[]={""","'","<",">","&",0}; struct Entitydata *entptr; entptr=EntityMake(); if (entptr==NULL) return -1; EntityAddEntityStringArray(entptr,entity); EntityAddSymbolStringArray(entptr,symbol); config=(struct config *)data; for (i=0;i<ptr->num;i++) { e=ptr->element[i]; if (strcmp(e->name,"remail")==0) { if (config->sfname.remail.buf !=NULL) XMLDBFreeBuffer(config->sfname.remail.buf); config->sfname.remail.type=0; for (j=0;j<e->atnum;j++) { if ((e->atval[j] !=NULL) && (strcmp(e->atval[j],"file")==0)) config->sfname.remail.type=1; } if (config->sfname.remail.type==1) { config->sfname.remail.buf=XMLDBCopyBuffer(e->data); XMLDBAddBuffer(config->sfname.remail.buf,zero,1); } else config->sfname.remail.buf=EntityDecodeBuffer(entptr,e->data); if (config->sfname.remail.buf==NULL) break; } if (strcmp(e->name,"remail.mail")==0) { if (config->sfname.remailmail.buf !=NULL) XMLDBFreeBuffer(config->sfname.remailmail.buf); config->sfname.remailmail.type=0; for (j=0;j<e->atnum;j++) { if ((e->atval[j] !=NULL) && (strcmp(e->atval[j],"file")==0)) config->sfname.remailmail.type=1; } if (config->sfname.remailmail.type==1) { config->sfname.remailmail.buf=XMLDBCopyBuffer(e->data); XMLDBAddBuffer(config->sfname.remailmail.buf,zero,1); } else config->sfname.remailmail.buf=EntityDecodeBuffer(entptr,e->data); if (config->sfname.remailmail.buf==NULL) break; } } free(entptr); if (i<ptr->num) return -1; return 0; }
struct XMLDBbuffer *ApplyModifier(struct XMLDBbuffer *mod,char *name, struct XMLDBbuffer *txt) { struct XMLDBbuffer *dst; struct XMLDBbuffer *nxml; char *vraw,*vurl,*vhtml; char *nraw,*nurl,*nhtml; struct Entitydata *ent=NULL; ent=EntityMake(); vraw=XMLToRaw(txt); vurl=XMLToURL(txt); vhtml=XMLToHTML(txt); nraw=name; nurl=CGIMakeURLString(name,strlen(name)); nhtml=RawToHTML(name,strlen(name)); nxml=RawToXML(name,strlen(name)); EntityAddEntityString(ent,"&vraw;"); EntityAddSymbolString(ent,vraw); EntityAddEntityString(ent,"&nraw;"); EntityAddSymbolString(ent,nraw); EntityAddEntityString(ent,"&vurl;"); EntityAddSymbolString(ent,vurl); EntityAddEntityString(ent,"&nurl;"); EntityAddSymbolString(ent,nurl); EntityAddEntityString(ent,"&vhtml;"); EntityAddSymbolString(ent,vhtml); EntityAddEntityString(ent,"&nhtml;"); EntityAddSymbolString(ent,nhtml); EntityAddEntityString(ent,"&vxml;"); EntityAddSymbol(ent,txt); EntityAddEntityString(ent,"&nxml;"); EntityAddSymbol(ent,nxml); dst=EntityDecodeBuffer(ent,mod); free(vraw); free(vurl); free(vhtml); free(nurl); free(nhtml); XMLDBFreeBuffer(nxml); free(ent); return dst; }
struct XMLdocdata *XMLdocMake() { struct XMLdocdata *ptr=NULL; ptr=malloc(sizeof(struct XMLdocdata)); memset(ptr,0,sizeof(struct XMLdocdata)); /* set up root element handler */ ptr->ent=EntityMake(); ptr->tag=TagDBMake(NULL); TagDBSetText(ptr->tag,EntityDecode,ptr->ent); return ptr; }
char *XMLToHTML(struct XMLDBbuffer *src) { char *symbol[]={"\"","\\","<",">","&","<br>",0}; char *entity[]={""","'","<",">","&","\n",0}; struct Entitydata *ent=NULL; char *txt=NULL; struct XMLDBbuffer *dst=NULL; ent=EntityMake(); EntityAddEntityStringArray(ent,entity); EntityAddSymbolStringArray(ent,symbol); dst=EntityDecodeBuffer(ent,src); txt=getDBstring(dst); XMLDBFreeBuffer(dst); free(ent); return txt; }
char *XMLToURL(struct XMLDBbuffer *src) { char *symbol[]={"\"","\\","<",">","&",0}; char *entity[]={""","'","<",">","&",0}; struct Entitydata *ent=NULL; char *txt=NULL; struct XMLDBbuffer *tmp=NULL; struct XMLDBbuffer *dst=NULL; ent=EntityMake(); EntityAddEntityStringArray(ent,entity); EntityAddSymbolStringArray(ent,symbol); tmp=EntityDecodeBuffer(ent,src); dst=EntityDecodeBuffer(ent,tmp); txt=CGIMakeURLString(dst->buf,dst->sze); XMLDBFreeBuffer(tmp); XMLDBFreeBuffer(dst); free(ent); return txt; }
int XMLdocRenderFile(struct XMLdocfile *ptr) { FILE *fp; char lbuf[256]; struct Entitydata *ent; char *filename=NULL; int s=0; if (ptr==NULL) return 0; if (ptr->fname==NULL) return 0; if (ptr->text==NULL) return 0; if (ptr->path !=NULL) { filename=malloc(strlen(ptr->path)+strlen(ptr->fname)+2); sprintf(filename,"%s/%s",ptr->path,ptr->fname); } else { filename=malloc(strlen(ptr->fname)+1); strcpy(filename,ptr->fname); } fp=fopen(filename,"r"); free(filename); if (fp==NULL) return 0; ent=EntityMake(); if (ent==NULL) { fclose(fp); return -1; } s=EntitySetText(ent,ptr->text,ptr->data); if ((s==0) && (ptr->type !=0)) { /* raw file so must encode entities */ char *symbol[]={"\"","\\","<",">","&",0}; char *entity[]={""","'","<",">","&",0}; s=EntityAddEntityStringArray(ent,entity); if (s==0) s=EntityAddSymbolStringArray(ent,symbol); } while ((s==0) && (fgets(lbuf,255,fp) !=NULL)) { s=EntityEncode(lbuf,strlen(lbuf),ent); if (s !=0) break; } fclose(fp); return s; }
char *RawToHTML(char *buf,int sze) { char *symbol[]={"\"","\\","<",">","&","\n",0}; char *entity[]={""","'","<",">","&","<br>",0}; char *dtxt=NULL; struct Entitydata *ent=NULL; struct XMLDBbuffer *src=NULL; struct XMLDBbuffer *dst=NULL; ent=EntityMake(); EntityAddEntityStringArray(ent,entity); EntityAddSymbolStringArray(ent,symbol); src=XMLDBMakeBuffer(sze); XMLDBAddBuffer(src,buf,sze); dst=EntityEncodeBuffer(ent,src); dtxt=getDBstring(dst); XMLDBFreeBuffer(src); XMLDBFreeBuffer(dst); free(ent); return dtxt; }
int XMLdocRenderElement(struct TagDBdata *tag,struct TagDBtable *tagdb, struct XMLdocelement *x,struct XMLDBelement *e) { int i,j,s=0; struct XMLdocmod *m=NULL; struct XMLDBbuffer *action=NULL; struct XMLDBbuffer *data=NULL; char *path=NULL; int xtp=0; int atp=0; path=x->path; action=x->action; xtp=x->type.xml; atp=x->type.action; TagDBSetTable(tag,tagdb); if (x->tagdb !=NULL) TagDBSetTable(tag,x->tagdb); if (e !=NULL) data=e->data; else { data=x->defval; xtp=x->type.defval; } if (e !=NULL) { /* decide if we need a modifier */ for (i=0;i<e->atnum;i++) { for (j=0;j<x->mod.num;j++) { m=x->mod.ptr[j]; if (strcmp(e->atval[i],m->name)==0) break; } if (j<x->mod.num) { /* we have a matching attribute */ if (m->action !=NULL) action=m->action; if (m->tagdb !=NULL) TagDBSetTable(tag,m->tagdb); if (m->path !=NULL) path=m->path; atp=m->type.action; xtp=m->type.xml; } } } /* we are now in a position to render this element */ if (action==NULL) { /* no action - render as text */ if (xtp==0) { /* data is text */ s=TagDBDecode(data->buf,data->sze,tag); s=TagDBDecode(NULL,0,tag); } else { struct XMLdocfile file; file.fname=NULL; if (data !=NULL) file.fname=getDBstring(data); file.path=path; file.text=TagDBDecode; file.data=tag; file.type=xtp-1; s= XMLdocRenderFile(&file); if (file.fname !=NULL) free(file.fname); } } else { /* use an entity decoder to handle the action */ struct Entitydata *ent; ent=EntityMake(); if (ent==NULL) return -1; s=EntityAddEntityString(ent,"<"); s=EntityAddEntityString(ent,">"); s=EntityAddSymbolString(ent,"<"); s=EntityAddSymbolString(ent,">"); if (xtp==0) { s=EntityAddEntityString(ent,"&xml;"); if (s==0) s=EntityAddSymbol(ent,data); } else { struct XMLdocfile file; file.fname=NULL; if (data !=NULL) file.fname=getDBstring(data); file.path=path; file.text=TagDBDecode; file.data=tag; file.type=xtp-1; s=EntityAddEntityString(ent,"&filename;"); if (s==0) s=EntityAddEntityString(ent,"&file;"); if (s==0) s=EntityAddSymbol(ent,e->data); if (s==0) s=EntitySetDecodeTrap(ent,XMLdocFileTrap,&file); } EntitySetText(ent,TagDBDecode,tag); if (atp==0) { s=EntityDecode(action->buf,action->sze,ent); if (s==0) s=EntityDecode(NULL,0,ent); } else { FILE *fp; char *afname=NULL; char lbuf[256]; afname=getDBstring(action); if (afname !=NULL) { fp=fopen(afname,"r"); if (fp !=NULL) { while ((s==0) && (fgets(lbuf,255,fp) !=NULL)) { s=EntityDecode(lbuf,strlen(lbuf),ent); if (s !=0) break; } if (s==0) s=EntityDecode(NULL,0,ent); fclose(fp); } free(afname); } } EntityFree(ent); } return 0; }
int xmldocbuild(struct XMLDBtable *ptr,struct XMLDBtable *tree,void *data) { int i,j; struct xmldoc *xmldoc; struct XMLDBelement *e; char zero[1]={0}; char *symbol[]={"\"","\\","<",">","&",0}; char *entity[]={""","'","<",">","&",0}; struct Entitydata *entptr; entptr=EntityMake(); if (entptr==NULL) return -1; EntityAddEntityStringArray(entptr,entity); EntityAddSymbolStringArray(entptr,symbol); xmldoc=(struct xmldoc *)data; for (i=0;i<ptr->num;i++) { e=ptr->element[i]; if (strcmp(e->name,"tree")==0) { XMLdocGetConfigSection(e->data,"tree","tree",decodetree,xmldoc); } else if (strcmp(e->name,"match")==0) { XMLdocGetConfigSection(e->data,"match","match",decodematch,xmldoc); } else if (strcmp(e->name,"script")==0) { if (xmldoc->sc.buf !=NULL) XMLDBFreeBuffer(xmldoc->sc.buf); xmldoc->sc.type=0; for (j=0;j<e->atnum;j++) { if ((e->atval[j] !=NULL) && (strcmp(e->atval[j],"file")==0)) xmldoc->sc.type=1; } if (xmldoc->sc.type==1) { xmldoc->sc.buf=XMLDBCopyBuffer(e->data); XMLDBAddBuffer(xmldoc->sc.buf,zero,1); } else xmldoc->sc.buf=EntityDecodeBuffer(entptr,e->data); if (xmldoc->sc.buf==NULL) break; } else if (strcmp(e->name,"map")==0) { XMLdocGetConfigSection(e->data,"map","map/entry",decodemap,xmldoc); } else if (strcmp(e->name,"external")==0) { int xtp=0; for (j=0;j<e->atnum;j++) { if ((e->atval[j] !=NULL) && (strcmp(e->atval[j],"file")==0)) xtp=1; } if (xtp==0) XMLdocGetConfigSection(e->data,"external","external/entry", decodeexternal,xmldoc); else { char *fname=NULL; FILE *fp; fname=getDBstring(e->data); if (fname==NULL) break; fp=fopen(fname,"r"); if (fp !=NULL) { loadexternal(fp,xmldoc); fclose(fp); } } } else if (strcmp(e->name,"xmldoc")==0) { int xtp=0; if (xmldoc->doc !=NULL) XMLdocFree(xmldoc->doc); xmldoc->doc=XMLdocMake(); for (j=0;j<e->atnum;j++) { if ((e->atval[j] !=NULL) && (strcmp(e->atval[j],"file")==0)) xtp=1; } if (xtp==0) XMLdocGetConfigSection(e->data,"xmldoc","xmldoc",XMLdocBuild, xmldoc->doc); else { char *fname=NULL; FILE *fp; fname=getDBstring(e->data); if (fname==NULL) break; fp=fopen(fname,"r"); if (fp !=NULL) { loadXMLdoc(fp,xmldoc->doc); fclose(fp); } } } } free(entptr); return 0; }
int decodeexternal(struct XMLDBtable *ptr,struct XMLDBtable *tree,void *data) { int i,s=0,n; struct xmldoc *xmldoc; struct XMLDBelement *e; struct XMLDBbuffer *dbuf=NULL; char *search=NULL; char *replace=NULL; char *symbol[]={"\"","\\","<",">","&",0}; char *entity[]={""","'","<",">","&",0}; struct Entitydata *entptr; entptr=EntityMake(); if (entptr==NULL) return -1; EntityAddEntityStringArray(entptr,entity); EntityAddSymbolStringArray(entptr,symbol); xmldoc=(struct xmldoc *)data; for (i=0;i<ptr->num;i++) { e=ptr->element[i]; if (strcmp(e->name,"search")==0) { if (search !=NULL) free(search); search=getDBstring(e->data); if (search==NULL) break; } else if (strcmp(e->name,"replace")==0) { if (replace !=NULL) free(replace); dbuf=EntityDecodeBuffer(entptr,e->data); if (dbuf !=NULL) { replace=getDBstring(dbuf); XMLDBFreeBuffer(dbuf); } if (replace==NULL) break; } } if (i<ptr->num) s=-1; if ((s==0) && (search !=NULL)) { n=xmldoc->external.search.num; if (s==0) { if (xmldoc->external.search.txt==NULL) xmldoc->external.search.txt=malloc(sizeof(char *)); else xmldoc->external.search.txt=realloc(xmldoc->external.search.txt, sizeof(char *)*(n+1)); if (xmldoc->external.search.txt==NULL) s=-1; if (s==0) xmldoc->external.search.txt[n]=NULL; } if (s==0) { if (xmldoc->external.replace.txt==NULL) xmldoc->external.replace.txt=malloc(sizeof(char *)); else xmldoc->external.replace.txt=realloc(xmldoc->external.replace.txt, sizeof(char *)*(n+1)); if (xmldoc->external.replace.txt==NULL) s=-1; if (s==0) xmldoc->external.replace.txt[n]=NULL; } if (s==0) { xmldoc->external.search.txt[n]=search; xmldoc->external.replace.txt[n]=replace; xmldoc->external.search.num++; xmldoc->external.replace.num++; } } free(entptr); if (s !=0) { if (search !=NULL) free(search); if (replace !=NULL) free(replace); return -1; } return 0; }
int main(int argc,char *argv[]) { int s=0,m,n; FILE *fp; FILE *xmlfp; unsigned char help=0; unsigned char option=0; unsigned char ilf=0; char *pathstr=NULL; char *scstr=NULL; int sctype=0; struct XMLDBbuffer *scbuf=NULL; struct XMLdata *xmldata=NULL; struct XMLDBdata *xmldbdata=NULL; struct XMLDBtree *tree=NULL; char *symbol[]={"\"","\\","<",">","&",0}; char *entity[]={""","'","<",">","&",0}; struct Entitydata *entptr; struct OptionText *ignore=NULL; struct OptionText *remove=NULL; char lbuf[255]; char zero[1]={0}; entptr=EntityMake(); EntityAddEntityStringArray(entptr,entity); EntityAddSymbolStringArray(entptr,symbol); OptionAdd(&opt,"-help",'x',&help); OptionAdd(&opt,"-option",'x',&option); OptionAdd(&opt,"ilf",'x',&ilf); OptionAdd(&opt,"i",'a',&ignore); OptionAdd(&opt,"r",'a',&remove); OptionAdd(&opt,"path",'t',&pathstr); OptionAdd(&opt,"script",'t',&scstr); OptionAdd(&opt,"sctype",'i',&sctype); arg=OptionProcess(1,argc,argv,&opt,NULL); if (help==1) { OptionPrintInfo(stdout,hlpstr); exit(0); } if (option==1) { OptionDump(stdout,&opt); exit(0); } if ((argc-arg)<3) { fprintf(stderr,"xmldoc pwd cfgfile xmldata\n"); exit(-1); } xmlfp=fopen(argv[arg+2],"r"); if (xmlfp==NULL) { fprintf(stderr,"file not found.\n"); exit(-1); } chdir(argv[arg]); fp=fopen(argv[arg+1],"r"); loadconfig(fp,&xmldoc); fclose(fp); if (argc>3) xmldoc.compare.data=argv[arg+3]; else xmldoc.compare.data=NULL; xmldoc.compare.check=cmp; xmldoc.map.iflg=-1; if (pathstr !=NULL) { free(xmldoc.tree.path); xmldoc.tree.path=pathstr; } if (scstr !=NULL) { xmldoc.sc.type=sctype; scbuf=XMLDBMakeBuffer(64); XMLDBAddBuffer(scbuf,scstr,strlen(scstr)); XMLDBFreeBuffer(xmldoc.sc.buf); if (sctype==1) { xmldoc.sc.buf=XMLDBCopyBuffer(scbuf); XMLDBAddBuffer(xmldoc.sc.buf,zero,1); } else xmldoc.sc.buf=EntityDecodeBuffer(entptr,scbuf); XMLDBFreeBuffer(scbuf); } xmldata=XMLMake(); xmldbdata=XMLDBMake(xmldata); tree=XMLDBMakeTree(); XMLDBBuildTree(xmldoc.tree.path,xmldoc.tree.delim,tree); XMLDBSetTree(xmldbdata,tree); /* Set the renderer to XMLrender */ XMLDBSetText(xmldbdata,render,&xmldoc); XMLSetStart(xmldata,XMLDBStart,xmldbdata); XMLSetEnd(xmldata,XMLDBEnd,xmldbdata); XMLSetComment(xmldata,redirect,xmldata); xmldoc.script=ScriptMake(); ScriptSetText(xmldoc.script,mapxml,&xmldoc); XMLdocSetText(xmldoc.doc,stream_output,stdout); if (remove !=NULL) { xmldoc.map.remove.num=remove->num; xmldoc.map.remove.txt=remove->txt; } if (ignore !=NULL) { xmldoc.map.ignore.num=ignore->num; xmldoc.map.ignore.txt=ignore->txt; } while(fgets(lbuf,255,xmlfp) !=NULL) { if (ilf) { m=0; for (n=0;(lbuf[n] !=0) && (n<256);n++) { if (lbuf[n]=='\n') continue; lbuf[m]=lbuf[n]; m++; } lbuf[m]=0; } s=XMLDecode(xmldata,lbuf,strlen(lbuf)); if (s !=0) break; } XMLFree(xmldata); XMLDBFree(xmldbdata); XMLDBFreeTree(tree); ScriptFree(xmldoc.script); fclose(xmlfp); fflush(stdout); return 0; }
int TagDBAddTable(struct TagDBtable *ptr, struct XMLDBbuffer *name,struct XMLDBbuffer *start, struct XMLDBbuffer *end) { char *symbol[]={"\"","\\","<",">","&",0}; char *entity[]={""","'","<",">","&",0}; int n,s=0; struct TagDBtag **tmp=NULL; struct TagDBtag *t=NULL; struct Entitydata *entptr=NULL; if (ptr==NULL) return -1; if (ptr->tag==NULL) ptr->tag=malloc(sizeof(struct TagDBtag *)); else { tmp=realloc(ptr->tag,sizeof(struct TagDBtag *)*(ptr->num+1)); if (tmp==NULL) return -1; ptr->tag=tmp; } if (ptr->tag==NULL) return -1; n=ptr->num; ptr->tag[n]=malloc(sizeof(struct TagDBtag)); if (ptr->tag[n]==NULL) return -1; t=ptr->tag[n]; t->name=malloc(name->sze+1); if (t->name==NULL) return -1; memcpy(t->name,name->buf,name->sze); t->name[name->sze]=0; t->start.encode=NULL; t->end.encode=NULL; t->start.decode=NULL; t->end.decode=NULL; entptr=EntityMake(); if (entptr==NULL) return -1; s=EntityAddEntityStringArray(entptr,entity); s=EntityAddSymbolStringArray(entptr,symbol); if (s !=0) { EntityFree(entptr); return -1; } if (start !=NULL) { t->start.encode=XMLDBCopyBuffer(start); if (t->start.encode==NULL) { EntityFree(entptr); return -1; } t->start.decode=EntityDecodeBuffer(entptr,start); if (t->start.decode==NULL) { EntityFree(entptr); return -1; } } if (end !=NULL) { t->end.encode=XMLDBCopyBuffer(end); if (t->end.encode==NULL) { EntityFree(entptr); return -1; } t->end.decode=EntityDecodeBuffer(entptr,end); if (t->end.decode==NULL) { EntityFree(entptr); return -1; } } EntityFree(entptr); ptr->num++; return 0; }
int TagDBStart(char *name,char end,int atnum,char **atname,char **atval, char *buf,int sze,void *data) { int i; struct TagDBdata *ptr=NULL; struct TagDBtable *tagdb=NULL; struct TagDBtag *t=NULL; ptr=(struct TagDBdata *) data; if (ptr->text.func==NULL) return 0; tagdb=ptr->tagdb; if (tagdb !=NULL) { for (i=0;i<tagdb->num;i++) { t=tagdb->tag[i]; if (strcmp(name,t->name)==0) break; } if (i<tagdb->num) { /* buffer attributes */ struct XMLDBbuffer *abuf=NULL; struct XMLDBbuffer *tbuf=NULL; struct Entitydata *entptr; entptr=EntityMake(); if (entptr==NULL) return -1; EntityAddEntityString(entptr,"&attr;"); abuf=XMLDBMakeBuffer(100); for (i=0;i<atnum;i++) { if (i !=0) XMLDBAddBuffer(abuf," ",1); XMLDBAddBuffer(abuf,atname[i],strlen(atname[i])); if (atval[i] !=0) { XMLDBAddBuffer(abuf,"=\"",2); XMLDBAddBuffer(abuf,atval[i],strlen(atval[i])); XMLDBAddBuffer(abuf,"\"",1); } } if (end !=0) XMLDBAddBuffer(abuf,&end,1); EntityAddSymbol(entptr,abuf); tbuf=EntityDecodeBuffer(entptr,t->start.decode); if (tbuf==NULL) return -1; (ptr->text.func)(tbuf->buf, tbuf->sze,ptr->text.data); XMLDBFreeBuffer(tbuf); XMLDBFreeBuffer(abuf); EntityFree(entptr); return 0; } } (ptr->text.func)("<",1,ptr->text.data); (ptr->text.func)(name,strlen(name),ptr->text.data); for (i=0;i<atnum;i++) { (ptr->text.func)(" ",1,ptr->text.data); (ptr->text.func)(atname[i],strlen(atname[i]),ptr->text.data); if (atval[i] !=0) { (ptr->text.func)("=\"",2,ptr->text.data); (ptr->text.func)(atval[i],strlen(atval[i]),ptr->text.data); (ptr->text.func)("\"",1,ptr->text.data); } } if (end !=0) (ptr->text.func)(&end,1,ptr->text.data); (ptr->text.func)(">",1,ptr->text.data); return 0; }