mirror of
https://github.com/ksherlock/gopher.git
synced 2025-02-13 03:30:26 +00:00
flags, move file code to do_gopher()
This commit is contained in:
parent
f4c23d7d87
commit
933308432d
17
flags.h
Normal file
17
flags.h
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
#ifndef __flags_h__
|
||||||
|
#define __flags_h__
|
||||||
|
|
||||||
|
typedef struct Flags {
|
||||||
|
|
||||||
|
int _0:1; // -1 (use http 1.0)
|
||||||
|
int _9:1; // -9 (use http 0.9)
|
||||||
|
int _O:1; // -O (file name from url)
|
||||||
|
int _v:1; // -v (verbose)
|
||||||
|
|
||||||
|
char *_o;
|
||||||
|
} Flags;
|
||||||
|
|
||||||
|
|
||||||
|
extern struct Flags flags;
|
||||||
|
|
||||||
|
#endif
|
107
gopher.c
107
gopher.c
@ -17,6 +17,7 @@
|
|||||||
#include "connection.h"
|
#include "connection.h"
|
||||||
#include "readline2.h"
|
#include "readline2.h"
|
||||||
#include "prototypes.h"
|
#include "prototypes.h"
|
||||||
|
#include "flags.h"
|
||||||
|
|
||||||
#include "s16debug.h"
|
#include "s16debug.h"
|
||||||
|
|
||||||
@ -249,28 +250,97 @@ static int gopher_dir(Word ipid, FILE *file)
|
|||||||
return eof ? 0 : -1;
|
return eof ? 0 : -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
int do_gopher(const char *url, URLComponents *components, FILE *file)
|
int do_gopher(const char *url, URLComponents *components)
|
||||||
{
|
{
|
||||||
Connection connection;
|
Connection connection;
|
||||||
char *host;
|
char *host;
|
||||||
|
char *path;
|
||||||
char type;
|
char type;
|
||||||
int ok;
|
int ok;
|
||||||
|
|
||||||
|
FILE *file;
|
||||||
|
char *filename;
|
||||||
|
|
||||||
|
file = stdout;
|
||||||
|
|
||||||
|
|
||||||
if (!components->portNumber) components->portNumber = 70;
|
if (!components->portNumber) components->portNumber = 70;
|
||||||
|
|
||||||
host = URLComponentGetCMalloc(url, components, URLComponentHost);
|
host = URLComponentGetCMalloc(url, components, URLComponentHost);
|
||||||
|
path = URLComponentGetCMalloc(url, components, URLComponentPath);
|
||||||
if (!host)
|
if (!host)
|
||||||
{
|
{
|
||||||
fprintf(stderr, "URL `%s': no host.", url);
|
fprintf(stderr, "URL `%s': no host.", url);
|
||||||
|
free(path);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (path && components->path.length <= 2)
|
||||||
|
{
|
||||||
|
free(path);
|
||||||
|
path = NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
// open the file.
|
||||||
|
filename = NULL;
|
||||||
|
if (flags._o)
|
||||||
|
{
|
||||||
|
filename = flags._o;
|
||||||
|
if (filename && !filename[0])
|
||||||
|
filename = NULL;
|
||||||
|
if (filename && filename[0] == '-' && !filename[1])
|
||||||
|
filename = NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (flags._O)
|
||||||
|
{
|
||||||
|
if (!path)
|
||||||
|
{
|
||||||
|
fprintf(stderr, "-O flag cannot be used with this URL.\n");
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
filename = strrchr(path + 2, '/');
|
||||||
|
if (filename) // *filename == '/'
|
||||||
|
{
|
||||||
|
filename++;
|
||||||
|
if (!filename[0])
|
||||||
|
{
|
||||||
|
// path/ ?
|
||||||
|
fprintf(stderr, "-O flag cannot be used with this URL.\n");
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
filename = path + 2;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (filename)
|
||||||
|
{
|
||||||
|
file = fopen(filename, "w");
|
||||||
|
if (!file)
|
||||||
|
{
|
||||||
|
fprintf(stderr, "Unable to to open file ``%s'': %s\n",
|
||||||
|
filename, strerror(errno));
|
||||||
|
|
||||||
|
free(host);
|
||||||
|
free(path);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
setfiletype(filename);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
ok = ConnectLoop(host, components->portNumber, &connection);
|
ok = ConnectLoop(host, components->portNumber, &connection);
|
||||||
|
|
||||||
if (!ok)
|
if (!ok)
|
||||||
{
|
{
|
||||||
free(host);
|
free(host);
|
||||||
|
free(path);
|
||||||
|
if (file != stdout) fclose(file);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -280,29 +350,26 @@ int do_gopher(const char *url, URLComponents *components, FILE *file)
|
|||||||
// path is /[type][resource]
|
// path is /[type][resource]
|
||||||
// where [type] is 1 char and the leading / is ignored.
|
// where [type] is 1 char and the leading / is ignored.
|
||||||
|
|
||||||
if (components->path.length <= 1)
|
if (path)
|
||||||
{
|
{
|
||||||
// / or blank
|
// path[0] = '/'
|
||||||
type = '1'; // directory
|
// path[1] = type
|
||||||
}
|
type = path[1];
|
||||||
else if (components->path.length == 2)
|
TCPIPWriteTCP(
|
||||||
{
|
connection.ipid,
|
||||||
// / type
|
path + 2,
|
||||||
// invalid -- treat as /
|
components->path.length - 2,
|
||||||
type = '1';
|
false,
|
||||||
|
false);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
type = url[components->path.location+1];
|
type = 1;
|
||||||
TCPIPWriteTCP(
|
|
||||||
connection.ipid,
|
|
||||||
url + components->path.location + 2,
|
|
||||||
components->path.length - 2,
|
|
||||||
0,
|
|
||||||
0);
|
|
||||||
}
|
}
|
||||||
//
|
//
|
||||||
TCPIPWriteTCP(connection.ipid, "\r\n", 2, true, 0);
|
TCPIPWriteTCP(connection.ipid, "\r\n", 2, true, false);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// 5 and 9 are binary, 1 is dir, all others text.
|
// 5 and 9 are binary, 1 is dir, all others text.
|
||||||
|
|
||||||
@ -322,10 +389,12 @@ int do_gopher(const char *url, URLComponents *components, FILE *file)
|
|||||||
}
|
}
|
||||||
|
|
||||||
fflush(file);
|
fflush(file);
|
||||||
|
if (file != stdout) fclose(file);
|
||||||
|
|
||||||
CloseLoop(&connection);
|
CloseLoop(&connection);
|
||||||
free(host);
|
free(host);
|
||||||
|
free(path);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
96
main.c
96
main.c
@ -9,11 +9,10 @@
|
|||||||
|
|
||||||
#include "url.h"
|
#include "url.h"
|
||||||
#include "connection.h"
|
#include "connection.h"
|
||||||
|
#include "prototypes.h"
|
||||||
|
#include "flags.h"
|
||||||
|
|
||||||
|
struct Flags flags;
|
||||||
extern int setfiletype(const char *filename);
|
|
||||||
extern void do_gopher(const char *url, URLComponents *components, FILE *file);
|
|
||||||
|
|
||||||
|
|
||||||
// startup/shutdown flags.
|
// startup/shutdown flags.
|
||||||
enum {
|
enum {
|
||||||
@ -91,9 +90,11 @@ void help(void)
|
|||||||
|
|
||||||
fputs("gopher [options] url\n", stdout);
|
fputs("gopher [options] url\n", stdout);
|
||||||
fputs("-h display help information.\n", stdout);
|
fputs("-h display help information.\n", stdout);
|
||||||
fputs("-v display version information.\n", stdout);
|
fputs("-V display version information.\n", stdout);
|
||||||
fputs("-O write output to file.\n", stdout);
|
fputs("-O write output to file.\n", stdout);
|
||||||
fputs("-o <file> write output to <file> instead of stdout.\n", stdout);
|
fputs("-o <file> write output to <file> instead of stdout.\n", stdout);
|
||||||
|
fputs("-0 use HTTP 1.0\n", stdout);
|
||||||
|
fputs("-9 use HTTP 0.9\n", stdout);
|
||||||
fputs("\n", stdout);
|
fputs("\n", stdout);
|
||||||
|
|
||||||
exit(0);
|
exit(0);
|
||||||
@ -144,34 +145,50 @@ char *get_url_filename(const char *cp, URLComponents *components)
|
|||||||
int main(int argc, char **argv)
|
int main(int argc, char **argv)
|
||||||
{
|
{
|
||||||
int i;
|
int i;
|
||||||
Word flags;
|
Word mf;
|
||||||
int ch;
|
int ch;
|
||||||
char *filename = NULL;
|
|
||||||
int flagO = 0;
|
|
||||||
|
|
||||||
flags = StartUp(NULL);
|
mf = StartUp(NULL);
|
||||||
|
|
||||||
if (flags == -1)
|
if (mf == -1)
|
||||||
{
|
{
|
||||||
fprintf(stderr, "Marinetti 3.0b3 or higher is required.\n");
|
fprintf(stderr, "Marinetti 3.0b3 or higher is required.\n");
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
while ((ch = getopt(argc, argv, "o:Oh")) != -1)
|
memset(&flags, 0, sizeof(flags));
|
||||||
|
|
||||||
|
while ((ch = getopt(argc, argv, "o:OhVv09")) != -1)
|
||||||
{
|
{
|
||||||
switch (ch)
|
switch (ch)
|
||||||
{
|
{
|
||||||
case 'v':
|
case 'V':
|
||||||
fputs("gopher v 0.1\n", stdout);
|
fputs("gopher v 0.2\n", stdout);
|
||||||
exit(0);
|
exit(0);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
case 'v':
|
||||||
|
flags._v = 1;
|
||||||
|
break;
|
||||||
|
|
||||||
case 'o':
|
case 'o':
|
||||||
filename = optarg;
|
flags._O = 0;
|
||||||
|
flags._o = optarg;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 'O':
|
case 'O':
|
||||||
flagO = 1;
|
flags._O = 1;
|
||||||
|
flags._o = NULL;
|
||||||
|
break;
|
||||||
|
|
||||||
|
case '9':
|
||||||
|
flags._0 = 1;
|
||||||
|
flags._9 = 0;
|
||||||
|
break;
|
||||||
|
|
||||||
|
case '0':
|
||||||
|
flags._0 = 1;
|
||||||
|
flags._9 = 0;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 'h':
|
case 'h':
|
||||||
@ -213,48 +230,11 @@ int main(int argc, char **argv)
|
|||||||
|
|
||||||
if (components.schemeType == SCHEME_GOPHER)
|
if (components.schemeType == SCHEME_GOPHER)
|
||||||
{
|
{
|
||||||
FILE *file = NULL;
|
do_gopher(url, &components);
|
||||||
|
}
|
||||||
if (filename)
|
else if (components.schemeType == SCHEME_HTTP)
|
||||||
{
|
{
|
||||||
file = fopen(filename, "w");
|
do_http(url, &components);
|
||||||
if (!file)
|
|
||||||
{
|
|
||||||
fprintf(stderr, "Unable to open file ``%s'': %s",
|
|
||||||
filename, strerror(errno));
|
|
||||||
exit(1);
|
|
||||||
}
|
|
||||||
setfiletype(filename);
|
|
||||||
}
|
|
||||||
else if (flagO)
|
|
||||||
{
|
|
||||||
// get the file name from the URL.
|
|
||||||
|
|
||||||
filename = get_url_filename(url, &components);
|
|
||||||
if (!filename)
|
|
||||||
{
|
|
||||||
fprintf(stderr, "-O flag cannot be used with this URL.\n");
|
|
||||||
exit(1);
|
|
||||||
}
|
|
||||||
|
|
||||||
file = fopen(filename, "w");
|
|
||||||
if (!file)
|
|
||||||
{
|
|
||||||
fprintf(stderr, "Unable to open file ``%s'': %s",
|
|
||||||
filename, strerror(errno));
|
|
||||||
exit(1);
|
|
||||||
}
|
|
||||||
|
|
||||||
setfiletype(filename);
|
|
||||||
|
|
||||||
free(filename);
|
|
||||||
filename = NULL;
|
|
||||||
}
|
|
||||||
else file = stdout;
|
|
||||||
|
|
||||||
do_gopher(url, &components, file);
|
|
||||||
|
|
||||||
if (file != stdout) fclose(file);
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
@ -263,7 +243,7 @@ int main(int argc, char **argv)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
ShutDown(flags, false, NULL);
|
ShutDown(mf, false, NULL);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
@ -14,10 +14,8 @@ int CloseLoop(Connection *connection);
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
#ifdef __url_h__
|
#ifdef __url_h__
|
||||||
int do_gopher(const char *url, URLComponents *components, FILE *file);
|
int do_gopher(const char *url, URLComponents *components);
|
||||||
int do_http_0_9(const char *url, URLComponents *components, FILE *file);
|
int do_http(const char *url, URLComponents *components);
|
||||||
int do_http_1_0(const char *url, URLComponents *components, FILE *file);
|
|
||||||
int do_http_1_1(const char *url, URLComponents *components, FILE *file);
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
Loading…
x
Reference in New Issue
Block a user