2012-03-09 02:15:46 +00:00
|
|
|
#include <Locator.h>
|
|
|
|
#include <Memory.h>
|
|
|
|
|
|
|
|
#include <tcpip.h>
|
|
|
|
|
|
|
|
|
2012-03-11 22:48:45 +00:00
|
|
|
#include "url.h"
|
|
|
|
#include "connection.h"
|
2012-03-09 02:15:46 +00:00
|
|
|
|
|
|
|
#include <stdio.h>
|
2012-03-11 22:48:45 +00:00
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
2012-03-09 02:15:46 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* connect gopher.floodgap.com:70
|
|
|
|
* send path
|
|
|
|
* read output.
|
|
|
|
* text -- ends with . <CR><LF>
|
|
|
|
* bin -- ends when connection is closed.
|
|
|
|
*/
|
|
|
|
|
|
|
|
// startup/shutdown flags.
|
|
|
|
enum {
|
|
|
|
kLoaded = 1,
|
|
|
|
kStarted = 2,
|
|
|
|
kConnected = 4
|
|
|
|
};
|
|
|
|
|
2012-03-11 22:48:45 +00:00
|
|
|
Word StartUp(displayPtr fx)
|
2012-03-09 02:15:46 +00:00
|
|
|
{
|
|
|
|
word status;
|
|
|
|
word flags = 0;
|
|
|
|
|
|
|
|
status = TCPIPStatus();
|
|
|
|
if (_toolErr)
|
|
|
|
{
|
2012-03-11 22:48:45 +00:00
|
|
|
LoadOneTool(54, 0x0201);
|
2012-03-09 02:15:46 +00:00
|
|
|
if (_toolErr) return -1;
|
|
|
|
|
|
|
|
status = 0;
|
|
|
|
flags |= kLoaded;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!status)
|
|
|
|
{
|
|
|
|
TCPIPStartUp();
|
|
|
|
if (_toolErr) return -1;
|
|
|
|
flags |= kStarted;
|
|
|
|
}
|
|
|
|
|
|
|
|
status = TCPIPGetConnectStatus();
|
|
|
|
if (!status)
|
|
|
|
{
|
2012-03-11 22:48:45 +00:00
|
|
|
TCPIPConnect(fx);
|
2012-03-09 02:15:46 +00:00
|
|
|
flags |= kConnected;
|
|
|
|
}
|
2012-03-11 22:48:45 +00:00
|
|
|
|
2012-03-09 02:15:46 +00:00
|
|
|
return flags;
|
|
|
|
}
|
|
|
|
|
2012-03-11 22:48:45 +00:00
|
|
|
void ShutDown(word flags, Boolean force, displayPtr fx)
|
2012-03-09 02:15:46 +00:00
|
|
|
{
|
|
|
|
if (flags & kConnected)
|
|
|
|
{
|
2012-03-11 22:48:45 +00:00
|
|
|
TCPIPDisconnect(force, fx);
|
2012-03-09 02:15:46 +00:00
|
|
|
if (_toolErr) return;
|
|
|
|
}
|
|
|
|
if (flags & kStarted)
|
|
|
|
{
|
|
|
|
TCPIPShutDown();
|
|
|
|
if (_toolErr) return;
|
|
|
|
}
|
|
|
|
if (flags & kLoaded)
|
|
|
|
{
|
|
|
|
UnloadOneTool(54);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-03-11 22:48:45 +00:00
|
|
|
|
|
|
|
int gopher_binary(Word ipid, FILE *file)
|
|
|
|
{
|
|
|
|
// gopher binary support.
|
|
|
|
// format: raw data until eof.
|
|
|
|
Word rv = 0;
|
|
|
|
|
|
|
|
for(;;)
|
|
|
|
{
|
|
|
|
static char buffer[512];
|
|
|
|
rrBuff rb;
|
|
|
|
Word count;
|
|
|
|
|
|
|
|
TCPIPPoll();
|
|
|
|
rv = TCPIPReadTCP(ipid, 0, (Ref)buffer, 512, &rb);
|
|
|
|
|
|
|
|
count = rb.rrBuffCount;
|
|
|
|
if (rv == 0 && count == 0) continue;
|
|
|
|
|
|
|
|
if (rv && !count) break;
|
|
|
|
|
|
|
|
if (!count) continue;
|
|
|
|
fwrite(buffer, count, 1, file);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
int gopher_text(Word ipid, FILE *file)
|
|
|
|
{
|
|
|
|
// text \r\n
|
|
|
|
// ...
|
|
|
|
// . \r\n
|
|
|
|
// any leading '.' must be doubled.
|
|
|
|
|
|
|
|
enum {
|
|
|
|
kStateText = 0,
|
|
|
|
kStateCR,
|
|
|
|
kStateEOL,
|
|
|
|
kStateDot,
|
|
|
|
kStateDotCR,
|
|
|
|
kStateEOF
|
|
|
|
};
|
|
|
|
|
|
|
|
static char buffer[256];
|
|
|
|
unsigned state = kStateEOL;
|
|
|
|
int rv = 0;
|
|
|
|
|
|
|
|
// I have bad luck with ReadLineTCP.
|
|
|
|
while (state != kStateEOF)
|
|
|
|
{
|
|
|
|
static char buffer[512];
|
|
|
|
rrBuff rb;
|
|
|
|
Word count;
|
|
|
|
Word i;
|
|
|
|
|
|
|
|
TCPIPPoll();
|
|
|
|
rv = TCPIPReadTCP(ipid, 0, (Ref)buffer, 512, &rb);
|
|
|
|
|
|
|
|
count = rb.rrBuffCount;
|
|
|
|
if (rv == 0 && count == 0) continue;
|
|
|
|
|
|
|
|
//fprintf(stderr, "rv = %x, count = %u\n", rv, rb.rrBuffCount);
|
|
|
|
|
|
|
|
//if (rv == tcperrConClosing) rv = 0;
|
|
|
|
if (rv && !count) break;
|
|
|
|
if (!count) continue;
|
|
|
|
|
|
|
|
// scan the buffer for a line.
|
|
|
|
for (i = 0; i < count; ++i)
|
|
|
|
{
|
|
|
|
char c = buffer[i];
|
|
|
|
|
|
|
|
if (c == '.')
|
|
|
|
{
|
|
|
|
if (state == kStateEOL) state = kStateDot;
|
|
|
|
else fputc(c, file);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (c == '\r')
|
|
|
|
{
|
|
|
|
if (state == kStateDot) state = kStateDotCR;
|
|
|
|
else state = kStateCR;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (c == '\n')
|
|
|
|
{
|
|
|
|
if (state == kStateCR)
|
|
|
|
{
|
|
|
|
state = kStateEOL;
|
|
|
|
fputc('\n', file);
|
|
|
|
}
|
|
|
|
else if (state == kStateDotCR)
|
|
|
|
{
|
|
|
|
state = kStateEOF;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
// otherwise, silently drop?
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
state = kStateText; // reset if kStateDot.
|
|
|
|
// . and \r will be silently dropped
|
|
|
|
fputc(c, file);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (state != kStateEOF)
|
|
|
|
fprintf(stderr, "warning: eof not detected\n");
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-03-09 02:15:46 +00:00
|
|
|
void do_url(const char *url)
|
|
|
|
{
|
|
|
|
URLComponents components;
|
2012-03-11 22:48:45 +00:00
|
|
|
Connection buffer;
|
2012-03-09 02:15:46 +00:00
|
|
|
char *host;
|
2012-03-11 22:48:45 +00:00
|
|
|
char type;
|
2012-03-09 02:15:46 +00:00
|
|
|
|
2012-03-11 22:48:45 +00:00
|
|
|
if (!ParseURL(url, strlen(url), &components))
|
2012-03-09 02:15:46 +00:00
|
|
|
{
|
|
|
|
fprintf(stderr, "Invalid URL: %s\n", url);
|
|
|
|
return;
|
|
|
|
}
|
2012-03-11 22:48:45 +00:00
|
|
|
|
|
|
|
if (!components.host.length)
|
2012-03-09 02:15:46 +00:00
|
|
|
{
|
|
|
|
fprintf(stderr, "No host\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-03-11 22:48:45 +00:00
|
|
|
if (!components.portNumber) components.portNumber = 70;
|
2012-03-09 02:15:46 +00:00
|
|
|
|
2012-03-11 22:48:45 +00:00
|
|
|
host = malloc(components.host.length + 1);
|
|
|
|
URLComponentGetC(url, &components, URLComponentHost, host);
|
2012-03-09 02:15:46 +00:00
|
|
|
|
2012-03-11 22:48:45 +00:00
|
|
|
ConnectionInit(&buffer, MMStartUp());
|
|
|
|
|
|
|
|
ConnectionOpenC(&buffer, host, components.portNumber);
|
2012-03-09 02:15:46 +00:00
|
|
|
|
|
|
|
while (!ConnectionPoll(&buffer)) ;
|
2012-03-11 22:48:45 +00:00
|
|
|
|
|
|
|
if (buffer.state == kConnectionStateError)
|
2012-03-09 02:15:46 +00:00
|
|
|
{
|
2012-03-11 22:48:45 +00:00
|
|
|
fprintf(stderr, "Unable to open host: %s:%u\n",
|
|
|
|
host,
|
|
|
|
components.portNumber);
|
2012-03-09 02:15:46 +00:00
|
|
|
free(host);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-03-11 22:48:45 +00:00
|
|
|
// connected....
|
2012-03-09 02:15:46 +00:00
|
|
|
|
2012-03-11 22:48:45 +00:00
|
|
|
// path is /[type][resource]
|
|
|
|
// where [type] is 1 char and the leading / is ignored.
|
|
|
|
|
|
|
|
if (components.path.length <= 1)
|
2012-03-09 02:15:46 +00:00
|
|
|
{
|
2012-03-11 22:48:45 +00:00
|
|
|
// / or blank
|
|
|
|
type = '1'; // directory
|
2012-03-09 02:15:46 +00:00
|
|
|
}
|
2012-03-11 22:48:45 +00:00
|
|
|
else if (components.path.length == 2)
|
2012-03-09 02:15:46 +00:00
|
|
|
{
|
2012-03-11 22:48:45 +00:00
|
|
|
// / type
|
|
|
|
// invalid -- treat as /
|
|
|
|
type = 1;
|
2012-03-09 02:15:46 +00:00
|
|
|
}
|
2012-03-11 22:48:45 +00:00
|
|
|
else
|
2012-03-09 02:15:46 +00:00
|
|
|
{
|
2012-03-11 22:48:45 +00:00
|
|
|
type = url[components.path.location+1];
|
|
|
|
TCPIPWriteTCP(
|
|
|
|
buffer.ipid,
|
|
|
|
url + components.path.location + 2,
|
|
|
|
components.path.length - 2,
|
|
|
|
0,
|
|
|
|
0);
|
2012-03-09 02:15:46 +00:00
|
|
|
}
|
2012-03-11 22:48:45 +00:00
|
|
|
//
|
|
|
|
TCPIPWriteTCP(buffer.ipid, "\r\n", 2, true, 0);
|
2012-03-09 02:15:46 +00:00
|
|
|
|
2012-03-11 22:48:45 +00:00
|
|
|
// 5 and 9 are binary, 1 is dir, all others text.
|
2012-03-09 02:15:46 +00:00
|
|
|
|
2012-03-11 22:48:45 +00:00
|
|
|
if (type == 1 || type == 9)
|
|
|
|
gopher_binary(buffer.ipid, stdout);
|
|
|
|
else
|
|
|
|
gopher_text(buffer.ipid, stdout);
|
2012-03-09 02:15:46 +00:00
|
|
|
|
2012-03-11 22:48:45 +00:00
|
|
|
fflush(stdout);
|
2012-03-09 02:15:46 +00:00
|
|
|
|
2012-03-11 22:48:45 +00:00
|
|
|
ConnectionClose(&buffer);
|
2012-03-09 02:15:46 +00:00
|
|
|
|
2012-03-11 22:48:45 +00:00
|
|
|
while (!ConnectionPoll(&buffer)) ; // wait for it to close.
|
2012-03-09 02:15:46 +00:00
|
|
|
|
2012-03-11 22:48:45 +00:00
|
|
|
free (host);
|
|
|
|
}
|
2012-03-09 02:15:46 +00:00
|
|
|
|
|
|
|
|
2012-03-11 22:48:45 +00:00
|
|
|
int main(int argc, char **argv)
|
|
|
|
{
|
2012-03-09 02:15:46 +00:00
|
|
|
|
2012-03-11 22:48:45 +00:00
|
|
|
int i;
|
|
|
|
Word flags;
|
|
|
|
|
|
|
|
flags = StartUp(NULL);
|
2012-03-09 02:15:46 +00:00
|
|
|
|
2012-03-11 22:48:45 +00:00
|
|
|
for (i = 1; i < argc; ++i)
|
2012-03-09 02:15:46 +00:00
|
|
|
{
|
2012-03-11 22:48:45 +00:00
|
|
|
do_url(argv[i]);
|
2012-03-09 02:15:46 +00:00
|
|
|
}
|
|
|
|
|
2012-03-11 22:48:45 +00:00
|
|
|
ShutDown(flags, false, NULL);
|
2012-03-09 02:15:46 +00:00
|
|
|
|
2012-03-11 22:48:45 +00:00
|
|
|
return 0;
|
2012-03-09 02:15:46 +00:00
|
|
|
}
|
|
|
|
|