/**********************************************************************************/ /* Copyright (c) 2017, Christopher Deotte */ /* */ /* Permission is hereby granted, free of charge, to any person obtaining a copy */ /* of this software and associated documentation files (the "Software"), to deal */ /* in the Software without restriction, including without limitation the rights */ /* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell */ /* copies of the Software, and to permit persons to whom the Software is */ /* furnished to do so, subject to the following conditions: */ /* */ /* The above copyright notice and this permission notice shall be included in all */ /* copies or substantial portions of the Software. */ /* */ /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR */ /* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, */ /* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE */ /* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER */ /* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, */ /* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE */ /* SOFTWARE. */ /**********************************************************************************/ /***************************************************************************/ /* WEBGUI A web browser based graphical user interface */ /* Version: 1.0 - June 25 2017 */ /***************************************************************************/ /* Author: Christopher Deotte */ /* Advisor: Randolph E. Bank */ /* Funding: NSF DMS-1345013 */ /* Documentation: http://ccom.ucsd.edu/~cdeotte/webgui */ /***************************************************************************/ #include #include #include #include #include #include #include #include #include #include #include #include #include static int cmdct, nct; static const int maxnamelen=20, maxabbrlen=3, maxdeftlen=40; static const int maxwebpage=100000; static char **cmdn, **cmda, **cmdt; static char **nn, **na, **nd, *nt; static int **cmdp, *cmdpct, *cmdc; static int *no, *nu, *nw, *ni; static const int maxlines = 100, maxhistory=100; static int indexA = 0, indexB = 0, bufferwaitA = 0, bufferwaitB = 0; static char *bufferA, *bufferB; //maintained as Fortran strings (space padded) static char **history; //maintained as C strings (null terminated) static char *webpageA=NULL, *webpageB=""; static char *webpageC, *webpageD, title[81]=""; static int animate = 0; static int load_webpageC_from_file = 0; static int load_pngs_from_file = 0; static int initialized=0, init0=0, running=0, hindex=0, waiting=0; static char **usrlist; //maintained as Fortran strings static unsigned char **colors[3]={NULL,NULL,NULL}; static int ncolor[3]={0,0,0}; static unsigned char *bitmap[3]={NULL,NULL,NULL}; static int bitmaplen[3]={0,0,0}; static float **colorsGL[3]={NULL,NULL,NULL}; static int ncolorGL[3]={0,0,0}; static int starttri = 1000, maxtri=0; static int startlin = 1000, maxlin=0; static float **triangles[3]={NULL,NULL,NULL}; static float **lines[3]={NULL,NULL,NULL}; static int indexT[3]={0,0,0}, indexL[3]={0,0,0}; static int tricount[3][10]={{0}}; static int ccount[3][10]={{0}}; static int cframe=1, cpane=-1, usrlen=0; static int sg[3]={-1,-1,-1}, pic[3]={-1,-1,-1}; static char *databuffer=NULL; static int webGLendian=0, dbpreamble[3][20]={{0}}; static int query=-1, query2=-1; static unsigned char firewall[8]={0,0,0,0,0,0,0,0}; static int stopthread = 0; static struct timeval started; static int create_socket; static int lock[6]={0,0,0,0,0,0}, lock2[6]={0,0,0,0,0,0}; static struct sockaddr_in address; static pthread_t pth; /* external FORTRAN interface routines */ /* call these without the underscore */ /* fortran passes arguments by reference */ extern void webinit0_(char *str, int *len, int *iu, double *ru, char *su); extern void webinit_(char *str, int *len); extern int webstart_(int *x); extern void webstop_(); extern void webreadline_(char *str); extern void webwriteline_(char *str); extern void webupdate_(int *ip, double *rp, char *sp); extern void webupdate2_(int *ip, double *rp, char *sp, int *iu, double *ru, char *su); extern void webupdate3_(int *iu, double *ru, char *su); extern void websettitle_(char *str, int len); extern int webquery_(); extern void websetcolors_(int* nc, double* red, double* green, double* blue, int* ipane); extern void webimagedisplay_(int* nx, int* ny, int* image, int* ipane); extern void webframe_(int* iframe); extern void webfillflt_(float* x, float* y, float* z, int* n, int* icolor); extern void weblineflt_(float* x, float* y, float* z, int* n, int* icolor); extern void webfilldbl_(double* x, double* y, double* z, int* n, int* icolor); extern void weblinedbl_(double* x, double* y, double* z, int* n, int* icolor); extern void webgldisplay_(int* ipane); extern void webbutton_(int *highlight, char *cmd, int cmdlen); extern void webpause_(); extern void websetmode_(int* x); /* external C interface routines */ extern void webinit(char *str, int len); extern int webstart(int x); extern void webstop(); extern void webreadline(char *str); extern void webwriteline(char *str); extern void webupdate(int *ip, double *rp, char *sp); extern void websettitle(char *str); extern int webquery(); extern void websetcolors(int nc, double* red, double* green, double* blue, int ipane); extern void webimagedisplay(int nx, int ny, int* image, int ipane); extern void webframe(int iframe); extern void webfillflt(float* x, float* y, float* z, int n, int icolor); extern void weblineflt(float* x, float* y, float* z, int n, int icolor); extern void webfilldbl(double* x, double* y, double* z, int n, int icolor); extern void weblinedbl(double* x, double* y, double* z, int n, int icolor); extern void webgldisplay(int ipane); extern void webbutton(int highlight, char *cmd); extern void webpause(); extern void websetmode(int x); /* internal routines */ void *startlisten(void *arg); void webwritenline(char *str, int n); void shrinkBuffer(); void parse(char *str, char *key, char *value); void updatedeft(char *cmd); void updatewebpageA(int hide); void updatewebpageC(); void updatewebpageD(int xtra); void updatewebpageD2(int x, int xtra); void allupdate(); void updatedatabuffer(int x, int xtra); int updatedatabuffer3(int x, int endian); int countskips(int len); void listfiles(char *str); void pushcanvas(char *str); void setquery(char *str); void setendian(char *str); void setanimate(char *str); void setfirewall(char *str, unsigned char *ip); void releaseWebGL(char *str); int growtri(int x, int shrink); int growlin(int x, int shrink); void getdeftbyname(char *name, char *deft, char *abbr, int *nopt, int *index); void getdeftbyindex(char *name, char *deft, char *abbr, int *nopt, int index); int getindexbyname(char *name); int getindexbycmd(char *name); void remove0e(char *str); void c2fortran(char* str, int len); void fortran2c(char *str, int len, int quote); void removespaces(char *str, int size); void removeapost(char *str); void fixquotes(char *dst, char *src); void urldecode(char *dst, const char *src); int str2int(char* str); int power(int x, int y); int ispng(char* str); int isint(char *str); int isreal(char *str); int isvalid(int x, char c); unsigned long fsize(char* file); /* PNG image files */ static unsigned char folderpng[446] = {137, 80, 78, 71, 13, 10, 26, 10, 0, 0, 0, 13, 73, 72, 68, 82, 0, 0, 0, 19, 0, 0, 0, 17, 8, 2, 0, 0, 0, 176, 250, 0, 144, 0, 0, 0, 9, 112, 72, 89, 115, 0, 0, 14, 195, 0, 0, 14, 195, 1, 199, 111, 168, 100, 0, 0, 0, 9, 116, 69, 88, 116, 67, 111, 109, 109, 101, 110, 116, 0, 0, 137, 42, 141, 6, 0, 0, 1, 91, 73, 68, 65, 84, 120, 156, 99, 248, 79, 46, 96, 24, 80, 157, 103, 102, 154, 156, 158, 97, 124, 114, 154, 225, 241, 41, 250, 247, 239, 223, 255, 246, 237, 27, 81, 58, 129, 218, 144, 69, 15, 245, 107, 157, 56, 113, 2, 191, 102, 6, 36, 109, 51, 145, 208, 255, 189, 93, 170, 187, 218, 149, 182, 183, 200, 111, 109, 146, 221, 220, 40, 189, 161, 78, 2, 232, 144, 239, 223, 191, 163, 232, 4, 58, 242, 255, 247, 137, 255, 191, 244, 161, 32, 84, 176, 174, 70, 172, 167, 167, 231, 222, 189, 123, 40, 58, 129, 126, 251, 255, 186, 245, 255, 179, 198, 163, 147, 116, 209, 208, 129, 94, 141, 61, 157, 42, 59, 90, 21, 182, 52, 202, 0, 109, 6, 34, 160, 205, 8, 157, 192, 32, 249, 255, 160, 26, 168, 14, 95, 128, 60, 107, 4, 18, 139, 11, 185, 129, 54, 35, 116, 130, 244, 92, 201, 7, 251, 118, 38, 46, 244, 227, 104, 2, 144, 92, 144, 199, 145, 151, 151, 135, 208, 9, 12, 201, 255, 103, 210, 65, 190, 5, 42, 2, 122, 24, 27, 122, 191, 39, 28, 40, 59, 47, 135, 45, 55, 55, 23, 161, 19, 232, 25, 160, 78, 176, 107, 103, 130, 60, 140, 13, 61, 219, 236, 7, 148, 157, 147, 197, 130, 162, 19, 24, 1, 64, 157, 251, 123, 212, 65, 58, 31, 84, 99, 69, 119, 86, 186, 0, 101, 103, 101, 48, 161, 232, 4, 198, 27, 80, 39, 48, 0, 65, 58, 175, 228, 99, 69, 151, 23, 88, 3, 101, 103, 166, 51, 34, 252, 249, 230, 211, 119, 96, 64, 195, 163, 27, 24, 111, 171, 43, 133, 87, 148, 9, 44, 45, 230, 5, 134, 36, 48, 72, 128, 126, 3, 58, 18, 104, 27, 80, 155, 143, 143, 79, 115, 115, 51, 194, 78, 96, 42, 3, 166, 53, 160, 80, 46, 33, 0, 84, 3, 84, 137, 208, 9, 209, 12, 180, 249, 26, 33, 128, 156, 19, 0, 169, 153, 11, 97, 9, 86, 0, 45, 0, 0, 0, 0, 73, 69, 78, 68, 174, 66, 96, 130}; static unsigned char uppng[472] = {137, 80, 78, 71, 13, 10, 26, 10, 0, 0, 0, 13, 73, 72, 68, 82, 0, 0, 0, 19, 0, 0, 0, 17, 8, 2, 0, 0, 0, 176, 250, 0, 144, 0, 0, 0, 9, 112, 72, 89, 115, 0, 0, 14, 195, 0, 0, 14, 195, 1, 199, 111, 168, 100, 0, 0, 0, 9, 116, 69, 88, 116, 67, 111, 109, 109, 101, 110, 116, 0, 0, 137, 42, 141, 6, 0, 0, 1, 117, 73, 68, 65, 84, 120, 156, 99, 248, 79, 46, 96, 24, 80, 157, 103, 102, 154, 156, 158, 97, 124, 114, 154, 225, 241, 41, 250, 247, 239, 223, 255, 246, 237, 27, 81, 58, 129, 218, 144, 69, 15, 245, 107, 157, 56, 113, 2, 191, 102, 6, 36, 109, 51, 145, 208, 255, 189, 93, 170, 187, 218, 149, 182, 183, 200, 111, 109, 146, 221, 220, 40, 189, 161, 78, 2, 232, 144, 239, 223, 191, 163, 232, 4, 58, 242, 255, 247, 137, 255, 191, 244, 193, 17, 3, 3, 186, 255, 215, 213, 136, 245, 244, 244, 220, 187, 119, 15, 69, 39, 208, 111, 255, 95, 183, 254, 127, 214, 120, 116, 146, 46, 16, 49, 192, 0, 144, 125, 160, 87, 99, 79, 167, 202, 142, 86, 133, 45, 141, 50, 64, 155, 129, 8, 104, 51, 66, 39, 48, 72, 254, 63, 168, 6, 170, 3, 241, 81, 1, 194, 210, 103, 141, 64, 98, 113, 33, 55, 208, 102, 132, 78, 144, 158, 43, 249, 96, 223, 66, 253, 9, 214, 131, 236, 237, 153, 63, 142, 38, 0, 201, 5, 121, 28, 121, 121, 121, 8, 157, 192, 144, 252, 127, 38, 29, 228, 91, 160, 34, 160, 135, 191, 79, 4, 233, 4, 51, 224, 232, 253, 158, 112, 160, 236, 188, 28, 182, 220, 220, 92, 132, 78, 160, 103, 128, 58, 193, 174, 157, 9, 242, 240, 235, 86, 116, 55, 191, 110, 125, 182, 217, 15, 40, 59, 39, 139, 5, 69, 39, 48, 2, 128, 58, 247, 247, 168, 131, 116, 62, 168, 198, 138, 238, 172, 116, 1, 202, 206, 202, 96, 66, 209, 9, 140, 55, 160, 78, 96, 0, 130, 116, 94, 201, 199, 138, 46, 47, 176, 6, 202, 206, 76, 103, 68, 248, 243, 205, 167, 239, 192, 128, 134, 71, 55, 48, 222, 86, 87, 10, 175, 40, 19, 88, 90, 204, 11, 12, 73, 96, 144, 0, 253, 6, 116, 36, 208, 54, 160, 54, 31, 31, 159, 230, 230, 102, 132, 157, 192, 84, 6, 76, 107, 64, 161, 92, 66, 0, 168, 6, 168, 18, 37, 221, 2, 53, 3, 109, 190, 70, 8, 32, 231, 4, 0, 48, 59, 228, 11, 224, 190, 98, 9, 0, 0, 0, 0, 73, 69, 78, 68, 174, 66, 96, 130}; static unsigned char filepng[402] = {137, 80, 78, 71, 13, 10, 26, 10, 0, 0, 0, 13, 73, 72, 68, 82, 0, 0, 0, 19, 0, 0, 0, 17, 8, 2, 0, 0, 0, 176, 250, 0, 144, 0, 0, 0, 9, 112, 72, 89, 115, 0, 0, 14, 195, 0, 0, 14, 195, 1, 199, 111, 168, 100, 0, 0, 1, 68, 73, 68, 65, 84, 120, 156, 173, 207, 49, 75, 195, 64, 20, 7, 240, 126, 37, 191, 128, 131, 171, 131, 159, 192, 201, 73, 172, 109, 179, 148, 184, 180, 34, 71, 55, 161, 166, 46, 1, 117, 49, 149, 42, 100, 147, 22, 211, 54, 109, 176, 72, 12, 38, 214, 216, 138, 18, 18, 210, 44, 54, 216, 33, 49, 173, 13, 154, 162, 15, 110, 113, 72, 81, 15, 143, 199, 227, 224, 189, 31, 255, 187, 196, 39, 233, 73, 252, 167, 20, 15, 86, 122, 173, 229, 226, 198, 66, 185, 176, 8, 151, 253, 220, 26, 116, 148, 218, 60, 220, 89, 53, 77, 51, 12, 195, 120, 169, 170, 207, 52, 45, 65, 129, 108, 156, 237, 237, 110, 165, 47, 170, 247, 167, 197, 180, 120, 46, 128, 172, 84, 42, 195, 225, 48, 94, 210, 116, 187, 115, 229, 20, 214, 151, 220, 241, 43, 244, 209, 219, 24, 250, 244, 227, 29, 58, 76, 41, 138, 178, 44, 107, 174, 172, 171, 93, 96, 184, 64, 250, 225, 20, 100, 52, 155, 97, 9, 15, 158, 43, 165, 219, 7, 66, 217, 210, 250, 96, 114, 249, 60, 174, 122, 179, 249, 107, 169, 246, 9, 51, 197, 238, 221, 247, 76, 28, 139, 167, 63, 73, 181, 71, 250, 79, 229, 145, 48, 83, 210, 158, 8, 51, 27, 55, 61, 156, 9, 81, 127, 147, 85, 69, 195, 129, 47, 147, 96, 52, 25, 119, 148, 235, 166, 36, 29, 159, 148, 153, 82, 137, 101, 89, 199, 113, 226, 165, 239, 251, 237, 75, 237, 136, 227, 182, 17, 74, 101, 50, 201, 100, 50, 155, 205, 34, 132, 24, 134, 225, 56, 78, 150, 101, 207, 243, 226, 101, 20, 69, 182, 109, 215, 106, 53, 158, 231, 5, 65, 128, 85, 93, 215, 13, 195, 24, 12, 6, 174, 235, 6, 65, 0, 11, 120, 243, 11, 53, 195, 70, 162, 55, 207, 82, 20, 0, 0, 0, 0, 73, 69, 78, 68, 174, 66, 96, 130}; /* C wrappers of Fortran functions */ int webstart(int x){ return webstart_(&x); } void webreadline(char *str){ return webreadline_(str); } void webwriteline(char *str){ return webwriteline_(str); } void webinit(char *str, int len){ return webinit_(str,&len); } void webupdate(int *ip, double *rp, char *sp){ return webupdate_(ip,rp,sp); } void websettitle(char *str){ return websettitle_(str,(int)strlen(str)); } void webstop(){ return webstop_(); } void websetcolors(int nc, double* red, double* green, double* blue, int ipane){ return websetcolors_(&nc,red,green,blue,&ipane); } void webimagedisplay(int nx, int ny, int* image, int ipane){ return webimagedisplay_(&nx,&ny,image,&ipane); } void webframe(int iframe){ return webframe_(&iframe); } void webfillflt(float* x, float* y, float* z, int n, int icolor){ return webfillflt_(x,y,z,&n,&icolor); } void weblineflt(float* x, float* y, float* z, int n, int icolor){ return weblineflt_(x,y,z,&n,&icolor); } void webfilldbl(double* x, double* y, double* z, int n, int icolor){ return webfilldbl_(x,y,z,&n,&icolor); } void weblinedbl(double* x, double* y, double* z, int n, int icolor){ return weblinedbl_(x,y,z,&n,&icolor); } void webgldisplay(int ipane){ return webgldisplay_(&ipane); } int webquery(){ return webquery_(); } void webbutton(int highlight, char *cmd){ return webbutton_(&highlight,cmd,(int)strlen(cmd)); } void webpause(){ return webpause_(); } void websetmode(int x){ return websetmode_(&x); } /**********************************************************************/ /* WEBGUI A web browser based graphical user interface */ /* */ /* Author: Christopher Deotte */ /* */ /* Version: 1.0 - June 25, 2017 */ /**********************************************************************/ void webinit0_(char *str, int *len, int *iu, double *ru, char *su){ /* PLTMG calls this before webinit_ to init usrcmd parameter list */ /* str and len are same structure as described by webinit_ */ if (init0==1 || initialized==1) { printf("webgui: WARNING: cannot call webinit0 twice, ignoring call\n"); return; } init0 = 1; query = 2; int i, sct; char name[81], deft[81], abbr[81], value[81]; nct = 0; sct = 0; for (i=0; i<*len; i++){ if (str[i*80]=='n') nct++; if (str[i*80]=='s') sct++; c2fortran(str+i*80,80); removespaces(str+i*80,80); } usrlist = (char**)malloc((sct+2*nct)*sizeof(char*)); for (i=0; i=0) { no[n]=1; count++; } else printf("webgui: WARNING: parameter %s needs to be declared, ignoring associated options\n",name); } cmdct=0; for (i=0; i< *len; i++) //record command names and abbreviations. if (str[i*80]=='c'){ parse(str+i*80+1,"c",name); parse(str+i*80+1,"k",abbr); parse(str+i*80+1,"t",value); name[maxnamelen]='\0'; abbr[maxabbrlen]='\0'; value[maxnamelen]='\0'; removeapost(name); removeapost(abbr); removeapost(value); if (name[0]!='\0'){ strcpy(cmdn[cmdct],name); if (abbr[0]=='\0'){ strncpy(abbr,name,3); abbr[3]='\0'; for (k=0;k<3;k++) abbr[k] = tolower(abbr[k]); printf("webgui: WARNING: command %s needs an abbreviation, defaulting to %s\n",name,abbr); } strcpy(cmda[cmdct],abbr); strcpy(cmdt[cmdct],value); cmdct++; } } for (k=0; k=0){ nw[index]=1; if (strlen(deft)==0 && (nt[index]=='i' || nt[index]=='r')){ printf("webgui: WARNING: parameter %s should declare a default value, defaulting to 0\n",name); strcpy(nd[index],"0"); } cmdp[k][count]=index; count++; } else printf("webgui: WARNING: parameter %s needs to be declared, ignoring command association\n",name); } } cmdpct[k]=count; } /* below creates part 2 of 4 for index.html and sg for web browser */ strcpy(webpageB,""); int blen=0; for (k=0; k