/*
* Reads gopher output from a TCP port, outputs
* html on standard output.
* Usage: gopher2html gopher-string
* where gopher-string is the string sent to
* the gopher server to get the document.
*
* Gopher protocol is described in rfc1436
*/
#include \n", name);
print("Saving %s file %s in %s...\n", type, name, save);
while((n=read(ifd, buf, sizeof buf))>0) write(fd, buf, n);
close(fd);
print("done%s error
\n", cmd);
print("%s\n", buf);
exits("gopher error");
}
void wtext(char *buf, char *ebuf){
char *bp;
for(bp=buf;bp!=ebuf;bp++){
if(*bp=='<' || *bp=='>' || *bp=='&' || *bp=='"'){
if(bp!=buf) write(1, buf, bp-buf);
buf=bp+1;
switch(*bp){
case '<': print("<"); break;
case '>': print(">"); break;
case '&': print("&"); break;
case '"': print("""); break;
}
}
}
if(bp!=buf) write(1, buf, bp-buf);
}
void savefile(char *name, char *type){
int fd, n;
char save[30], buf[1024];
for(n=1;;n++){
if(n==100) errexit("can't save binary file %s: %r", name);
sprint(save, "gopher.save.%d", n);
fd=create(save, OWRITE, 0444);
if(fd!=-1) break;
}
print("%s
\n", title); while((n=read(ifd, buf, sizeof buf))>0) wtext(buf, buf+n); print("
\n"); } void copyfile(char *title){ char buf[1024]; int n; print("
\n", title); print("
\n");
}
/*
* A directory entry contains
* type name selector host port
* all tab separated, except type and name (type is one character)
*/
char ibuf[1024], *ibp, *eibuf;
#define EOF (-1)
int get(void){
int n;
Again:
if(ibp==eibuf){
n=read(ifd, ibuf, sizeof(ibuf));
if(n<=0) return EOF;
eibuf=ibuf+n;
ibp=ibuf;
}
if(*ibp=='\r'){
ibp++;
goto Again;
}
return *ibp++&255;
}
char *escape(char *in){
static char out[516];
char *op, *eop;
eop=out+512;
op=out;
for(;*in;in++){
if(op%s
\n", title);
for(;;){
type=get();
if(type==EOF || type=='.') break;
bp=name;
while((c=get())!=EOF && c!='\t') if(bp!=&name[512]) *bp++=c;
ename=bp;
bp=selector;
while((c=get())!=EOF && c!='\t') if(bp!=&selector[512]) *bp++=c;
*bp='\0';
bp=host;
while((c=get())!=EOF && c!='\t') if(bp!=&host[512]) *bp++=c;
*bp='\0';
bp=port;
while((c=get())!=EOF && c!='\t' && c!='\n') if(bp!=&port[512]) *bp++=c;
while(c!=EOF && c!='\n') c=get();
*bp='\0';
switch(type){
case '3':
print("