Skip to content

Commit

Permalink
replace flags with options
Browse files Browse the repository at this point in the history
  • Loading branch information
ksherlock committed Aug 11, 2013
1 parent 58df66c commit cd55a2d
Show file tree
Hide file tree
Showing 8 changed files with 78 additions and 19 deletions.
6 changes: 3 additions & 3 deletions gopher.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@
#include "url.h"
#include "connection.h"
#include "readline2.h"
#include "prototypes.h"
#include "flags.h"

#include "options.h"
#include "s16debug.h"

#include "prototypes.h"

static FileInfoRecGS FileInfo;
static Word FileAttr;

Expand Down
5 changes: 3 additions & 2 deletions http.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,14 @@

#include "url.h"
#include "connection.h"
#include "prototypes.h"
#include "dictionary.h"
#include "flags.h"
#include "options.h"
#include "readline2.h"
#include "http.utils.h"
#include "s16debug.h"

#include "prototypes.h"


static FileInfoRecGS FileInfo;
static Word FileAttr;
Expand Down
40 changes: 31 additions & 9 deletions main.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@

#include "url.h"
#include "connection.h"
#include "options.h"

#include "prototypes.h"
#include "flags.h"

// startup/shutdown flags.
enum {
Expand Down Expand Up @@ -171,6 +172,30 @@ char *get_url_filename(const char *cp, URLComponents *components)
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)
{
Expand All @@ -180,14 +205,11 @@ int main(int argc, char **argv)
int x;


x = ParseFlags(argc, argv);
if (x < 0) return 1;

argv += x;
argc -= x;
memset(&flags, 0, sizeof(flags));
argc = GetOptions(argc, argv, &flags);


if (argc != 1)
if (argc != 2)
{
help();
return 1;
Expand Down Expand Up @@ -216,12 +238,12 @@ int main(int argc, char **argv)



if (argc == 1)
if (argc == 2)
{
const char *url;
URLComponents components;

url = *argv;
url = argv[1];

if (!ParseURL(url, strlen(url), &components))
{
Expand Down
9 changes: 5 additions & 4 deletions makefile.mk
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
CFLAGS += $(DEFINES) -v -w
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 \
dictionary.o flags.o time.o
dictionary.o options.o time.o

gopher: $(OBJS)
$(CC) $(LDFLAGS) $(OBJS) $(LDLIBS) -o $@
Expand All @@ -22,10 +22,11 @@ connection.o: connection.c connection.h
readline2.o: readline2.c readline2.h
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

data.o: data.c data.h
Expand Down
14 changes: 14 additions & 0 deletions options.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
#include "options.h"

extern void help(void);
extern void version(void);
/* global */
struct Options flags;

int GetOptions(int argc, char **argv,
struct Options *options)
Expand Down Expand Up @@ -53,17 +56,28 @@ int GetOptions(int argc, char **argv,
case '0':
options->_0 = 1;
options->_1 = 0;
options->_9 = 0;
break;
case '1':
options->_1 = 1;
options->_0 = 0;
options->_9 = 0;
break;
case '9':
options->_9 = 1;
options->_0 = 0;
options->_1 = 0;
break;
case 'I':
options->_I = 1;
break;
case 'O':
options->_O = 1;
break;
case 'V':
version();
exit(0);
break;
case 'h':
help();
exit(0);
Expand Down
1 change: 1 addition & 0 deletions options.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ typedef struct Options
{
unsigned _0:1;
unsigned _1:1;
unsigned _9:1;
unsigned _I:1;
unsigned _O:1;
unsigned _i:1;
Expand Down
19 changes: 18 additions & 1 deletion options.text
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,41 @@

%extra_includes ->
extern void help(void);
extern void version(void);
/* global */
struct Options flags;



-h [virtual] ->
help();
exit(0);

-V [virtual] ->
version();
exit(0);

# -o specifies the output name
# -O gets the name from the URL.
-o: ->
options->_O = 0;

-O

-i
-I
-v

# -[0|1] -- set HTTP version.

-0 ->
options->_1 = 0;
options->_9 = 0;

-1 ->
options->_0 = 0;
options->_0 = 0;
options->_9 = 0;

-9 ->
options->_0 = 0;
options->_1 = 0;
3 changes: 3 additions & 0 deletions prototypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,5 +59,8 @@ void tiTimeRec2ISO8601(const TimeRecPtr t, char *str);
void tiTimeRec2GMTString(const TimeRecPtr t, char *str);
#endif

#ifdef __Options__
extern struct Options flags;
#endif

#endif

0 comments on commit cd55a2d

Please sign in to comment.