mirror of
https://github.com/ksherlock/gopher.git
synced 2025-02-08 17:30:23 +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 "readline2.h"
|
||||
#include "prototypes.h"
|
||||
#include "flags.h"
|
||||
|
||||
#include "s16debug.h"
|
||||
|
||||
@ -249,28 +250,97 @@ static int gopher_dir(Word ipid, FILE *file)
|
||||
return eof ? 0 : -1;
|
||||
}
|
||||
|
||||
int do_gopher(const char *url, URLComponents *components, FILE *file)
|
||||
int do_gopher(const char *url, URLComponents *components)
|
||||
{
|
||||
Connection connection;
|
||||
char *host;
|
||||
char *path;
|
||||
char type;
|
||||
int ok;
|
||||
|
||||
FILE *file;
|
||||
char *filename;
|
||||
|
||||
file = stdout;
|
||||
|
||||
|
||||
if (!components->portNumber) components->portNumber = 70;
|
||||
|
||||
host = URLComponentGetCMalloc(url, components, URLComponentHost);
|
||||
path = URLComponentGetCMalloc(url, components, URLComponentPath);
|
||||
if (!host)
|
||||
{
|
||||
fprintf(stderr, "URL `%s': no host.", url);
|
||||
free(path);
|
||||
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);
|
||||
|
||||
if (!ok)
|
||||
{
|
||||
free(host);
|
||||
free(path);
|
||||
if (file != stdout) fclose(file);
|
||||
return -1;
|
||||
}
|
||||
|
||||
@ -280,29 +350,26 @@ int do_gopher(const char *url, URLComponents *components, FILE *file)
|
||||
// path is /[type][resource]
|
||||
// where [type] is 1 char and the leading / is ignored.
|
||||
|
||||
if (components->path.length <= 1)
|
||||
if (path)
|
||||
{
|
||||
// / or blank
|
||||
type = '1'; // directory
|
||||
}
|
||||
else if (components->path.length == 2)
|
||||
{
|
||||
// / type
|
||||
// invalid -- treat as /
|
||||
type = '1';
|
||||
// path[0] = '/'
|
||||
// path[1] = type
|
||||
type = path[1];
|
||||
TCPIPWriteTCP(
|
||||
connection.ipid,
|
||||
path + 2,
|
||||
components->path.length - 2,
|
||||
false,
|
||||
false);
|
||||
}
|
||||
else
|
||||
{
|
||||
type = url[components->path.location+1];
|
||||
TCPIPWriteTCP(
|
||||
connection.ipid,
|
||||
url + components->path.location + 2,
|
||||
components->path.length - 2,
|
||||
0,
|
||||
0);
|
||||
type = 1;
|
||||
}
|
||||
//
|
||||
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.
|
||||
|
||||
@ -322,10 +389,12 @@ int do_gopher(const char *url, URLComponents *components, FILE *file)
|
||||
}
|
||||
|
||||
fflush(file);
|
||||
if (file != stdout) fclose(file);
|
||||
|
||||
CloseLoop(&connection);
|
||||
free(host);
|
||||
|
||||
free(path);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
96
main.c
96
main.c
@ -9,11 +9,10 @@
|
||||
|
||||
#include "url.h"
|
||||
#include "connection.h"
|
||||
#include "prototypes.h"
|
||||
#include "flags.h"
|
||||
|
||||
|
||||
extern int setfiletype(const char *filename);
|
||||
extern void do_gopher(const char *url, URLComponents *components, FILE *file);
|
||||
|
||||
struct Flags flags;
|
||||
|
||||
// startup/shutdown flags.
|
||||
enum {
|
||||
@ -91,9 +90,11 @@ void help(void)
|
||||
|
||||
fputs("gopher [options] url\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 <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);
|
||||
|
||||
exit(0);
|
||||
@ -144,34 +145,50 @@ char *get_url_filename(const char *cp, URLComponents *components)
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
int i;
|
||||
Word flags;
|
||||
Word mf;
|
||||
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");
|
||||
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)
|
||||
{
|
||||
case 'v':
|
||||
fputs("gopher v 0.1\n", stdout);
|
||||
case 'V':
|
||||
fputs("gopher v 0.2\n", stdout);
|
||||
exit(0);
|
||||
break;
|
||||
|
||||
case 'v':
|
||||
flags._v = 1;
|
||||
break;
|
||||
|
||||
case 'o':
|
||||
filename = optarg;
|
||||
flags._O = 0;
|
||||
flags._o = optarg;
|
||||
break;
|
||||
|
||||
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;
|
||||
|
||||
case 'h':
|
||||
@ -213,48 +230,11 @@ int main(int argc, char **argv)
|
||||
|
||||
if (components.schemeType == SCHEME_GOPHER)
|
||||
{
|
||||
FILE *file = NULL;
|
||||
|
||||
if (filename)
|
||||
{
|
||||
file = fopen(filename, "w");
|
||||
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);
|
||||
do_gopher(url, &components);
|
||||
}
|
||||
else if (components.schemeType == SCHEME_HTTP)
|
||||
{
|
||||
do_http(url, &components);
|
||||
}
|
||||
else
|
||||
{
|
||||
@ -263,7 +243,7 @@ int main(int argc, char **argv)
|
||||
}
|
||||
}
|
||||
|
||||
ShutDown(flags, false, NULL);
|
||||
ShutDown(mf, false, NULL);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
@ -14,10 +14,8 @@ int CloseLoop(Connection *connection);
|
||||
#endif
|
||||
|
||||
#ifdef __url_h__
|
||||
int do_gopher(const char *url, URLComponents *components, FILE *file);
|
||||
int do_http_0_9(const char *url, URLComponents *components, FILE *file);
|
||||
int do_http_1_0(const char *url, URLComponents *components, FILE *file);
|
||||
int do_http_1_1(const char *url, URLComponents *components, FILE *file);
|
||||
int do_gopher(const char *url, URLComponents *components);
|
||||
int do_http(const char *url, URLComponents *components);
|
||||
#endif
|
||||
|
||||
#endif
|
||||
|
Loading…
x
Reference in New Issue
Block a user