mirror of
https://github.com/ksherlock/gopher.git
synced 2025-03-11 06:33:11 +00:00
use ReadLine2 for text.
This commit is contained in:
parent
d12411b46e
commit
6ce962f3ed
108
gopher.c
108
gopher.c
@ -3,14 +3,17 @@
|
|||||||
|
|
||||||
#include <tcpip.h>
|
#include <tcpip.h>
|
||||||
|
|
||||||
|
|
||||||
#include "url.h"
|
#include "url.h"
|
||||||
#include "connection.h"
|
#include "connection.h"
|
||||||
|
#include "readline2.h"
|
||||||
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
#ifndef ORCA_BUILD
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* connect gopher.floodgap.com:70
|
* connect gopher.floodgap.com:70
|
||||||
@ -108,47 +111,50 @@ int gopher_binary(Word ipid, FILE *file)
|
|||||||
|
|
||||||
int gopher_text(Word ipid, FILE *file)
|
int gopher_text(Word ipid, FILE *file)
|
||||||
{
|
{
|
||||||
|
Word lines[2] = {0, 0};
|
||||||
// text \r\n
|
// text \r\n
|
||||||
// ...
|
// ...
|
||||||
// . \r\n
|
// . \r\n
|
||||||
// any leading '.' must be doubled.
|
// any leading '.' must be doubled.
|
||||||
|
|
||||||
Word eof = 0;
|
Word eof = 0;
|
||||||
Word rv = 0;
|
int rv = 0;
|
||||||
|
|
||||||
|
TCPIPPoll();
|
||||||
|
|
||||||
for(;;)
|
for(;;)
|
||||||
{
|
{
|
||||||
Word count;
|
Word count;
|
||||||
Handle h;
|
Handle h;
|
||||||
|
|
||||||
rlrBuff rb;
|
rlBuffer rb;
|
||||||
|
|
||||||
TCPIPPoll();
|
rv = ReadLine2(ipid, &rb);
|
||||||
|
|
||||||
rv = TCPIPReadLineTCP(ipid,
|
h = rb.bufferHandle;
|
||||||
"\p\r\n",
|
count = rb.bufferSize;
|
||||||
2,
|
|
||||||
NULL,
|
|
||||||
0xffff,
|
|
||||||
&rb);
|
|
||||||
|
|
||||||
//count = rb.rlrBuffCount;
|
if (rv < 0) break; // eof
|
||||||
// bug -- count is sometimes wildly wrong....
|
if (rv == 0) // no data available (yet)
|
||||||
|
|
||||||
h = rb.rlrBuffHandle;
|
|
||||||
count = h ? (Word)GetHandleSize(h) : 0;
|
|
||||||
|
|
||||||
if (rv && !count)
|
|
||||||
{
|
{
|
||||||
DisposeHandle(h);
|
TCPIPPoll();
|
||||||
break;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (count == 0 && rb.rlrIsDataFlag)
|
if (!rb.moreFlag) TCPIPPoll();
|
||||||
|
|
||||||
|
|
||||||
|
if (count == 0)
|
||||||
|
{
|
||||||
|
DisposeHandle(h);
|
||||||
|
if (rb.terminator)
|
||||||
{
|
{
|
||||||
fputc('\r', file);
|
fputc('\r', file);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
if (count)
|
if (count)
|
||||||
{
|
{
|
||||||
char *cp;
|
char *cp;
|
||||||
@ -156,7 +162,6 @@ int gopher_text(Word ipid, FILE *file)
|
|||||||
HLock(h);
|
HLock(h);
|
||||||
cp = *((char **)h);
|
cp = *((char **)h);
|
||||||
|
|
||||||
//fprintf(stderr, "%d %d\n", count, (int)GetHandleSize(h));
|
|
||||||
// .. -> .
|
// .. -> .
|
||||||
// . \r\n -> eof
|
// . \r\n -> eof
|
||||||
|
|
||||||
@ -172,6 +177,7 @@ int gopher_text(Word ipid, FILE *file)
|
|||||||
cp++;
|
cp++;
|
||||||
count--;
|
count--;
|
||||||
}
|
}
|
||||||
|
|
||||||
fwrite(cp, 1, count, file);
|
fwrite(cp, 1, count, file);
|
||||||
fputc('\r', file);
|
fputc('\r', file);
|
||||||
}
|
}
|
||||||
@ -182,47 +188,41 @@ int gopher_text(Word ipid, FILE *file)
|
|||||||
if (!eof)
|
if (!eof)
|
||||||
fprintf(stderr, "Warning: eof not found.\n");
|
fprintf(stderr, "Warning: eof not found.\n");
|
||||||
|
|
||||||
return rv;
|
return eof ? 0 : -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
int gopher_dir(Word ipid, FILE *file)
|
int gopher_dir(Word ipid, FILE *file)
|
||||||
{
|
{
|
||||||
Word eof = 0;
|
Word eof = 0;
|
||||||
Word rv = 0;
|
int rv = 0;
|
||||||
|
|
||||||
|
|
||||||
for(;;)
|
|
||||||
{
|
|
||||||
Word count;
|
|
||||||
Handle h;
|
|
||||||
|
|
||||||
rlrBuff rb;
|
|
||||||
|
|
||||||
TCPIPPoll();
|
TCPIPPoll();
|
||||||
|
|
||||||
rv = TCPIPReadLineTCP(ipid,
|
for(;;)
|
||||||
"\p\r\n",
|
|
||||||
2,
|
|
||||||
NULL,
|
|
||||||
0xffff,
|
|
||||||
&rb);
|
|
||||||
|
|
||||||
//count = rb.rlrBuffCount;
|
|
||||||
h = rb.rlrBuffHandle;
|
|
||||||
count = h ? (Word)GetHandleSize(h) : 0;
|
|
||||||
|
|
||||||
#if 0
|
|
||||||
if (rv || count || _toolErr)
|
|
||||||
{
|
{
|
||||||
fprintf(stderr, "rv = %x, _toolErr = %u, count = %u\n",
|
rlBuffer rb;
|
||||||
rv, _toolErr, count);
|
Word count;
|
||||||
|
Handle h;
|
||||||
|
|
||||||
|
|
||||||
|
rv = ReadLine2(ipid, &rb);
|
||||||
|
|
||||||
|
h = rb.bufferHandle;
|
||||||
|
count = rb.bufferSize;
|
||||||
|
|
||||||
|
if (rv < 0) break;
|
||||||
|
if (rv == 0)
|
||||||
|
{
|
||||||
|
TCPIPPoll();
|
||||||
|
continue;
|
||||||
}
|
}
|
||||||
#endif
|
if (!rb.moreFlag) TCPIPPoll();
|
||||||
|
|
||||||
if (rv && !count)
|
|
||||||
|
if (!count)
|
||||||
{
|
{
|
||||||
DisposeHandle(h);
|
// blank line?
|
||||||
break;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (count)
|
if (count)
|
||||||
@ -246,6 +246,7 @@ int gopher_dir(Word ipid, FILE *file)
|
|||||||
DisposeHandle(h);
|
DisposeHandle(h);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
// format is [type][name] \t [path] \t [server] \t [port]
|
// format is [type][name] \t [path] \t [server] \t [port]
|
||||||
j = 1;
|
j = 1;
|
||||||
tabs[0] = 0;
|
tabs[0] = 0;
|
||||||
@ -283,6 +284,7 @@ int gopher_dir(Word ipid, FILE *file)
|
|||||||
char c = buffer[i];
|
char c = buffer[i];
|
||||||
if (c >= '0' && c <= '9') port = port * 10 + c - '0';
|
if (c >= '0' && c <= '9') port = port * 10 + c - '0';
|
||||||
}
|
}
|
||||||
|
|
||||||
if (port == 70)
|
if (port == 70)
|
||||||
{
|
{
|
||||||
fprintf(file, "[%s/%c%s] %s\r",
|
fprintf(file, "[%s/%c%s] %s\r",
|
||||||
@ -312,7 +314,7 @@ int gopher_dir(Word ipid, FILE *file)
|
|||||||
if (!eof)
|
if (!eof)
|
||||||
fprintf(stderr, "Warning: eof not found.\n");
|
fprintf(stderr, "Warning: eof not found.\n");
|
||||||
|
|
||||||
return rv;
|
return eof ? 0 : -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
void do_url(const char *url)
|
void do_url(const char *url)
|
||||||
@ -387,7 +389,11 @@ void do_url(const char *url)
|
|||||||
|
|
||||||
// 5 and 9 are binary, 1 is dir, all others text.
|
// 5 and 9 are binary, 1 is dir, all others text.
|
||||||
|
|
||||||
|
#ifdef ORCA_BUILD
|
||||||
|
file = stdout;
|
||||||
|
#else
|
||||||
file = fdopen(STDOUT_FILENO, "wb");
|
file = fdopen(STDOUT_FILENO, "wb");
|
||||||
|
#endif
|
||||||
|
|
||||||
switch(type)
|
switch(type)
|
||||||
{
|
{
|
||||||
|
Loading…
x
Reference in New Issue
Block a user