/* #define DEBUG */ #include #include #include #ifdef ULTRIX char *strdup(); #endif #include #if SOL || HP #include #endif #include #include #include #include #include #include #include #include #include int sock; struct sockaddr_in sockad; char acceptad[80]; int lenacceptad=80; int sockaccept; int sock2; struct sockaddr_in sockad2; char acceptad2[80]; int lenacceptad2=80; int sockaccept2; char request[4097]; int echonext=0; char savefilename[256]; int savefile; char remotehost[256]; char currenturl[256]; int HTDoConnect(); int HTDoRead(); HTCheckActiveIcon(i) int i; { return 0; } HTClearActiveIcon() { } HTProgress(msg) char *msg; { /* fprintf(stderr,"%s\n",msg); */ } int HTDoConnect(); int HTDoRead(); #define HTSIZE 4096 #define MAX_BUF 1025 char htbuff[HTSIZE]; static int handle,htbufflen,htbuffptr; int htopen(url) char *url; { char *cp; int i; cp=strchr(url+7,'/'); if(cp==NULL){printf("mc: Error in URL %s\n",url);return -999;}; strcpy(htbuff,"GET "); strcat(htbuff,cp); strcat(htbuff,"\r\n"); *cp=0; i=HTDoConnect(url,"HTTP",80,&handle); if(i) {printf("Connect to %s failed\n",url);return -999;}; write(handle,htbuff,strlen(htbuff)); htbufflen=0; htbuffptr=0; return 0; } int htgets(comm) char *comm; { int op; char c; if(htbufflen < 0) return 0; op=0; while(1){ if(htbuffptr >= htbufflen){ htbufflen=HTDoRead(handle,htbuff,HTSIZE); if(htbufflen == 0){ htbufflen= -1; return 0; }; htbuffptr = 0; }; c=htbuff[htbuffptr++];comm[op++]=c; if(c==10){comm[op]=0;return 1;}; }; } char x2c(what) char *what; { register char digit; digit = (what[0] >= 'A' ? ((what[0] & 0xdf) - 'A')+10 : (what[0] - '0')); digit *= 16; digit += (what[1] >= 'A' ? ((what[1] & 0xdf) - 'A')+10 : (what[1] - '0')); return(digit); } void fixstr(url) char *url; { register int x,y; for(x=0,y=0;url[y];++x,++y) { if((url[x]=url[y])=='+'){ url[x]=' ';} else if(url[x] == '&') { url[x] = 9;} else if(url[x] == '%') { url[x] = x2c(&url[y+1]); y+=2; } } url[x] = '\0'; } void sighandle() { } static char direrr[]="Error in fetching directory

\n"; static char indirect[]=" %s\n",url); #endif if(htopen(url)){ write(sockaccept,direrr,strlen(direrr)); write(sockaccept,url,strlen(url)); close(sockaccept); }; while(htgets(comm)) if(len=strlen(comm)){ if(strstr(comm," %s\n",url); #endif strcpy(currenturl,url); if(echonext){ write(sockaccept2,currenturl,strlen(currenturl)); write(sockaccept2,"\n",1); close(sockaccept2); echonext=0; write(sockaccept,"ok\n",3); close(sockaccept); return; }; if(htopen(url)){ write(sockaccept,objerr,strlen(objerr)); write(sockaccept,url,strlen(url)); close(sockaccept); }; savefile=open(savefilename,O_WRONLY | O_CREAT | O_TRUNC,S_IRUSR | S_IWUSR); if(savefile<0)exit(1); while(htgets(comm)) if(len=strlen(comm)){ write(sockaccept,comm,len); write(savefile,comm,len); }; close(sockaccept); close(savefile); } static char errmsg[]="Error in command to mc\n"; void frommosaic() { int i; char execcomm[80],url[256]; char *sp; sockaccept=accept(sock,acceptad,&lenacceptad); if(sockaccept<0){ signal(SIGQUIT,SIG_IGN); close(sock); exit(0); } i=read(sockaccept,request,4096); #ifdef DEBUG printf("%s\n",request); #endif sp=strtok(request," "); sp=strtok(NULL," "); strcpy(execcomm,sp+1); #ifdef DEBUG printf("**%s**\n",execcomm); #endif if(strstr(execcomm,"jg/dir")==execcomm){ strcpy(url,"http://"); strcat(url,remotehost); strcat(url,"/"); strcat(url,execcomm); dirfun(url); } else if(strstr(execcomm,"jg/serv")==execcomm){ strcpy(url,"http://"); strcat(url,remotehost); strcat(url,"/"); strcat(url,execcomm); objfun(url); } else if(strstr(execcomm,"xxx//")==execcomm){ strcpy(url,"http:"); strcat(url,execcomm+3); objfun(url); } else { write(sockaccept,errmsg,strlen(errmsg)); close(sockaccept); }; } void fromcontrol() { int i; char *sp,*sp2,tmp[256]; sockaccept2=accept(sock2,acceptad2,&lenacceptad2); if(sockaccept2<0){ signal(SIGQUIT,SIG_IGN); close(sock2); exit(0); } i=read(sockaccept2,request,4096); sp=strchr(request,'\n'); *sp = 0; #ifdef DEBUG printf("control::%s\n",request); #endif if(strstr(request,"geturl")==request){ #ifdef DEBUG printf("%s\n",currenturl); #endif write(sockaccept2,currenturl,strlen(currenturl)); write(sockaccept2,"\n",1); close(sockaccept2); } else if(strstr(request,"echonext")==request){ echonext=1; } else close(sockaccept2); } void main(argc, argv) int argc; char **argv; { int mosaicpid; char mosaicfile[80]; char response; fd_set readfds,exceptfds; strcpy(savefilename,"/tmp/"); strcat(savefilename,getenv("USER")); strcat(savefilename,".mosaicdata"); signal(SIGCHLD, sighandle); sock=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP); bzero(&sockad,sizeof(sockad)); sockad.sin_port=htons(3377); sockad.sin_family=AF_INET; if(bind(sock,&sockad,sizeof(sockad))<0) { perror(":"); exit(-1); } sock2=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP); bzero(&sockad2,sizeof(sockad2)); sockad2.sin_port=htons(3376); sockad2.sin_family=AF_INET; if(bind(sock2,&sockad2,sizeof(sockad2))<0) { perror(":"); exit(-1); } strcpy(remotehost,argv[2]); strcpy(mosaicfile,"http://localhost:3377/jg/dir?"); switch(mosaicpid=fork()) { case -1: perror("bad fork"); exit(1); case 0: execlp(argv[1], argv[1],mosaicfile,(char*)0); perror("xmosaic"); } (void)listen(sock,1); (void)listen(sock2,1); while(1){ FD_ZERO(&readfds); FD_SET(sock,&readfds); FD_SET(sock2,&readfds); FD_ZERO(&exceptfds); FD_SET(sock,&exceptfds); FD_SET(sock2,&exceptfds); if(select(10,&readfds,NULL,&exceptfds,NULL) <= 0) {perror("mc select error"),exit(1);}; if(FD_ISSET(sock,&readfds))frommosaic(); if(FD_ISSET(sock,&exceptfds)) {perror("mc error in browser socket"),exit(1);}; if(FD_ISSET(sock2,&readfds))fromcontrol(); if(FD_ISSET(sock2,&exceptfds)) {perror("mc error in control socket"),exit(1);}; } }