gopher/gopher.c

440 lines
6.9 KiB
C
Raw Normal View History

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-04-10 04:21:33 +00:00
#include "readline2.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-04-10 04:21:33 +00:00
#ifndef ORCA_BUILD
2012-03-12 02:44:29 +00:00
#include <unistd.h>
2012-04-10 04:21:33 +00:00
#endif
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;
2012-04-06 02:52:09 +00:00
fwrite(buffer, 1, count, file);
2012-03-11 22:48:45 +00:00
}
return rv;
}
int gopher_text(Word ipid, FILE *file)
{
2012-04-10 04:21:33 +00:00
Word lines[2] = {0, 0};
2012-03-11 22:48:45 +00:00
// text \r\n
// ...
// . \r\n
// any leading '.' must be doubled.
2012-04-06 02:52:09 +00:00
Word eof = 0;
2012-04-10 04:21:33 +00:00
int rv = 0;
TCPIPPoll();
2012-03-12 02:44:29 +00:00
2012-04-06 02:52:09 +00:00
for(;;)
{
2012-03-11 22:48:45 +00:00
Word count;
2012-04-06 02:52:09 +00:00
Handle h;
2012-04-10 04:21:33 +00:00
rlBuffer rb;
2012-03-11 22:48:45 +00:00
2012-04-10 04:21:33 +00:00
rv = ReadLine2(ipid, &rb);
2012-04-07 00:13:05 +00:00
2012-04-10 04:21:33 +00:00
h = rb.bufferHandle;
count = rb.bufferSize;
2012-03-11 22:48:45 +00:00
2012-04-10 04:21:33 +00:00
if (rv < 0) break; // eof
if (rv == 0) // no data available (yet)
2012-04-06 02:52:09 +00:00
{
2012-04-10 04:21:33 +00:00
TCPIPPoll();
continue;
2012-04-06 02:52:09 +00:00
}
2012-03-11 22:48:45 +00:00
2012-04-10 04:21:33 +00:00
if (!rb.moreFlag) TCPIPPoll();
if (count == 0)
2012-04-07 00:13:05 +00:00
{
2012-04-10 04:21:33 +00:00
DisposeHandle(h);
if (rb.terminator)
{
fputc('\r', file);
}
continue;
2012-04-07 00:13:05 +00:00
}
2012-03-11 22:48:45 +00:00
2012-04-06 02:52:09 +00:00
if (count)
2012-03-11 22:48:45 +00:00
{
2012-04-06 02:52:09 +00:00
char *cp;
2012-03-11 22:48:45 +00:00
2012-04-07 00:13:05 +00:00
HLock(h);
2012-04-06 02:52:09 +00:00
cp = *((char **)h);
// .. -> .
// . \r\n -> eof
if (*cp == '.')
2012-03-11 22:48:45 +00:00
{
2012-04-06 02:52:09 +00:00
if (count == 1)
{
DisposeHandle(h);
eof = 1;
break;
}
cp++;
count--;
}
2012-04-10 04:21:33 +00:00
2012-04-06 02:52:09 +00:00
fwrite(cp, 1, count, file);
fputc('\r', file);
2012-03-11 22:48:45 +00:00
}
2012-04-06 02:52:09 +00:00
DisposeHandle(h);
2012-03-11 22:48:45 +00:00
}
2012-04-06 02:52:09 +00:00
if (!eof)
2012-03-12 02:44:29 +00:00
fprintf(stderr, "Warning: eof not found.\n");
2012-03-11 22:48:45 +00:00
2012-04-10 04:21:33 +00:00
return eof ? 0 : -1;
2012-03-11 22:48:45 +00:00
}
2012-03-12 02:44:29 +00:00
int gopher_dir(Word ipid, FILE *file)
{
Word eof = 0;
2012-04-10 04:21:33 +00:00
int rv = 0;
2012-03-12 02:44:29 +00:00
2012-04-10 04:21:33 +00:00
TCPIPPoll();
2012-03-12 02:44:29 +00:00
for(;;)
{
2012-04-10 04:21:33 +00:00
rlBuffer rb;
2012-03-12 02:44:29 +00:00
Word count;
Handle h;
2012-04-10 04:21:33 +00:00
rv = ReadLine2(ipid, &rb);
2012-03-12 02:44:29 +00:00
2012-04-10 04:21:33 +00:00
h = rb.bufferHandle;
count = rb.bufferSize;
2012-03-12 02:44:29 +00:00
2012-04-10 04:21:33 +00:00
if (rv < 0) break;
if (rv == 0)
2012-03-12 02:44:29 +00:00
{
2012-04-10 04:21:33 +00:00
TCPIPPoll();
continue;
2012-03-12 02:44:29 +00:00
}
2012-04-10 04:21:33 +00:00
if (!rb.moreFlag) TCPIPPoll();
2012-03-12 02:44:29 +00:00
2012-04-10 04:21:33 +00:00
if (!count)
2012-04-06 02:52:09 +00:00
{
2012-04-10 04:21:33 +00:00
// blank line?
continue;
2012-04-06 02:52:09 +00:00
}
2012-03-12 02:44:29 +00:00
if (count)
{
Word tabs[4];
unsigned i,j;
char type;
2012-04-07 00:13:05 +00:00
char *buffer;
HLock(h);
buffer = *((char **)h);
2012-03-12 02:44:29 +00:00
type = *buffer;
++buffer;
--count;
if (type == '.' && count == 0)
{
eof = 1;
DisposeHandle(h);
break;
}
2012-04-10 04:21:33 +00:00
2012-03-12 02:44:29 +00:00
// format is [type][name] \t [path] \t [server] \t [port]
j = 1;
tabs[0] = 0;
tabs[1] = tabs[2] = tabs[3] = -1;
for (i = 0; i < count; ++i)
{
if (buffer[i] == '\t')
{
tabs[j++] = i;
buffer[i] = 0;
if (j == 4) break;
}
}
// 'i' is info record.
if (type == 'i')
{
if (tabs[1] == -1)
2012-04-06 02:52:09 +00:00
fwrite(buffer, 1, count, file);
else
fputs(buffer, file);
fputc('\r', file);
2012-03-12 02:44:29 +00:00
}
else if (j == 4) // all the tabs.
{
int port = 0;
// description
// file name
// server
// port
for (i = tabs[3] + 1; i < count; ++i)
{
char c = buffer[i];
if (c >= '0' && c <= '9') port = port * 10 + c - '0';
}
2012-04-10 04:21:33 +00:00
2012-03-12 02:44:29 +00:00
if (port == 70)
{
2012-04-07 00:13:05 +00:00
fprintf(file, "[%s/%c%s] %s\r",
2012-03-12 02:44:29 +00:00
buffer + 1 + tabs[2], // server
type, // type
2012-04-07 00:13:05 +00:00
buffer + 1 + tabs[1], // file name
buffer // description
2012-03-12 02:44:29 +00:00
);
}
else
{
2012-04-07 00:13:05 +00:00
fprintf(file, "[%s:%u/%c%s] %s\r",
2012-03-12 02:44:29 +00:00
buffer + 1 + tabs[2], // server
port, // port
type, // type
2012-04-07 00:13:05 +00:00
buffer + 1 + tabs[1], // file name
buffer // description
2012-03-12 02:44:29 +00:00
);
}
}
DisposeHandle(h);
}
}
if (!eof)
fprintf(stderr, "Warning: eof not found.\n");
2012-04-10 04:21:33 +00:00
return eof ? 0 : -1;
2012-03-12 02:44:29 +00:00
}
2012-03-11 22:48:45 +00:00
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-12 02:44:29 +00:00
FILE *file;
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 /
2012-03-12 02:44:29 +00:00
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-04-10 04:21:33 +00:00
#ifdef ORCA_BUILD
file = stdout;
#else
2012-03-12 02:44:29 +00:00
file = fdopen(STDOUT_FILENO, "wb");
2012-04-10 04:21:33 +00:00
#endif
2012-03-12 02:44:29 +00:00
switch(type)
{
case '1':
gopher_dir(buffer.ipid, file);
break;
case '5':
case '9':
gopher_binary(buffer.ipid, file);
break;
default:
gopher_text(buffer.ipid, file);
break;
}
2012-03-09 02:15:46 +00:00
2012-03-12 02:44:29 +00:00
fflush(file);
fclose(file);
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)
{
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
}