replace flags with options

This commit is contained in:
Kelvin Sherlock 2013-08-11 13:55:41 -04:00
parent 58df66cc71
commit cd55a2de3b
8 changed files with 78 additions and 19 deletions

View File

@ -17,11 +17,11 @@
#include "url.h" #include "url.h"
#include "connection.h" #include "connection.h"
#include "readline2.h" #include "readline2.h"
#include "prototypes.h" #include "options.h"
#include "flags.h"
#include "s16debug.h" #include "s16debug.h"
#include "prototypes.h"
static FileInfoRecGS FileInfo; static FileInfoRecGS FileInfo;
static Word FileAttr; static Word FileAttr;

5
http.c
View File

@ -32,13 +32,14 @@
#include "url.h" #include "url.h"
#include "connection.h" #include "connection.h"
#include "prototypes.h"
#include "dictionary.h" #include "dictionary.h"
#include "flags.h" #include "options.h"
#include "readline2.h" #include "readline2.h"
#include "http.utils.h" #include "http.utils.h"
#include "s16debug.h" #include "s16debug.h"
#include "prototypes.h"
static FileInfoRecGS FileInfo; static FileInfoRecGS FileInfo;
static Word FileAttr; static Word FileAttr;

40
main.c
View File

@ -11,8 +11,9 @@
#include "url.h" #include "url.h"
#include "connection.h" #include "connection.h"
#include "options.h"
#include "prototypes.h" #include "prototypes.h"
#include "flags.h"
// startup/shutdown flags. // startup/shutdown flags.
enum { enum {
@ -171,6 +172,30 @@ char *get_url_filename(const char *cp, URLComponents *components)
return out; return out;
} }
#define GOPHER_VERSION "0.3"
void version(void)
{
puts("gopher version " GOPHER_VERSION);
}
void help(void)
{
puts("gopher version " GOPHER_VERSION);
puts("usage: gopher [options] url");
puts("");
puts("-h show help");
puts("-V show version");
puts("-o file write output to file");
puts("-O write output to file based on URL");
puts("");
puts("HTTP options:");
puts("-9 use HTTP version 0.9");
puts("-0 use HTTP version 1.0");
puts("-1 use HTTP version 1.1");
puts("-i print headers");
puts("-I print only headers (HEAD)");
}
int main(int argc, char **argv) int main(int argc, char **argv)
{ {
@ -180,14 +205,11 @@ int main(int argc, char **argv)
int x; int x;
x = ParseFlags(argc, argv); memset(&flags, 0, sizeof(flags));
if (x < 0) return 1; argc = GetOptions(argc, argv, &flags);
argv += x;
argc -= x;
if (argc != 1) if (argc != 2)
{ {
help(); help();
return 1; return 1;
@ -216,12 +238,12 @@ int main(int argc, char **argv)
if (argc == 1) if (argc == 2)
{ {
const char *url; const char *url;
URLComponents components; URLComponents components;
url = *argv; url = argv[1];
if (!ParseURL(url, strlen(url), &components)) if (!ParseURL(url, strlen(url), &components))
{ {

View File

@ -1,7 +1,7 @@
CFLAGS += $(DEFINES) -v -w CFLAGS += $(DEFINES) -v -w
OBJS = main.o gopher.o url.o connection.o readline2.o scheme.o ftype.o \ OBJS = main.o gopher.o url.o connection.o readline2.o scheme.o ftype.o \
mime.o setftype.o s16debug.o common.o http.o http.utils.o \ mime.o setftype.o s16debug.o common.o http.o http.utils.o \
dictionary.o flags.o time.o dictionary.o options.o time.o
gopher: $(OBJS) gopher: $(OBJS)
$(CC) $(LDFLAGS) $(OBJS) $(LDLIBS) -o $@ $(CC) $(LDFLAGS) $(OBJS) $(LDLIBS) -o $@
@ -22,10 +22,11 @@ connection.o: connection.c connection.h
readline2.o: readline2.c readline2.h readline2.o: readline2.c readline2.h
common.o: common.c common.o: common.c
flags.o: flags.c flags.h options.o: options.c options.h
gopher.o: gopher.c url.h connection.h flags.h
http.o: http.c url.h connection.h flags.h gopher.o: gopher.c url.h connection.h options.h
http.o: http.c url.h connection.h options.h
http.utils.o: http.utils.c http.utils.o: http.utils.c
data.o: data.c data.h data.o: data.c data.h

View File

@ -9,6 +9,9 @@
#include "options.h" #include "options.h"
extern void help(void); extern void help(void);
extern void version(void);
/* global */
struct Options flags;
int GetOptions(int argc, char **argv, int GetOptions(int argc, char **argv,
struct Options *options) struct Options *options)
@ -53,10 +56,17 @@ int GetOptions(int argc, char **argv,
case '0': case '0':
options->_0 = 1; options->_0 = 1;
options->_1 = 0; options->_1 = 0;
options->_9 = 0;
break; break;
case '1': case '1':
options->_1 = 1; options->_1 = 1;
options->_0 = 0; options->_0 = 0;
options->_9 = 0;
break;
case '9':
options->_9 = 1;
options->_0 = 0;
options->_1 = 0;
break; break;
case 'I': case 'I':
options->_I = 1; options->_I = 1;
@ -64,6 +74,10 @@ int GetOptions(int argc, char **argv,
case 'O': case 'O':
options->_O = 1; options->_O = 1;
break; break;
case 'V':
version();
exit(0);
break;
case 'h': case 'h':
help(); help();
exit(0); exit(0);

View File

@ -5,6 +5,7 @@ typedef struct Options
{ {
unsigned _0:1; unsigned _0:1;
unsigned _1:1; unsigned _1:1;
unsigned _9:1;
unsigned _I:1; unsigned _I:1;
unsigned _O:1; unsigned _O:1;
unsigned _i:1; unsigned _i:1;

View File

@ -4,15 +4,25 @@
%extra_includes -> %extra_includes ->
extern void help(void); extern void help(void);
extern void version(void);
/* global */
struct Options flags;
-h [virtual] -> -h [virtual] ->
help(); help();
exit(0); exit(0);
-V [virtual] ->
version();
exit(0);
# -o specifies the output name # -o specifies the output name
# -O gets the name from the URL. # -O gets the name from the URL.
-o: -> -o: ->
options->_O = 0; options->_O = 0;
-O -O
-i -i
@ -20,8 +30,15 @@
-v -v
# -[0|1] -- set HTTP version. # -[0|1] -- set HTTP version.
-0 -> -0 ->
options->_1 = 0; options->_1 = 0;
options->_9 = 0;
-1 -> -1 ->
options->_0 = 0; options->_0 = 0;
options->_9 = 0;
-9 ->
options->_0 = 0;
options->_1 = 0;

View File

@ -59,5 +59,8 @@ void tiTimeRec2ISO8601(const TimeRecPtr t, char *str);
void tiTimeRec2GMTString(const TimeRecPtr t, char *str); void tiTimeRec2GMTString(const TimeRecPtr t, char *str);
#endif #endif
#ifdef __Options__
extern struct Options flags;
#endif
#endif #endif