From 58df66cc71c0ebe122a92296112338c0fc00d8f3 Mon Sep 17 00:00:00 2001 From: Kelvin Sherlock Date: Sun, 11 Aug 2013 12:51:58 -0400 Subject: [PATCH] new options --- options.c | 102 +++++++++++++++++++++++++++++++++++++++++++++++++++ options.h | 17 +++++++++ options.text | 27 ++++++++++++++ 3 files changed, 146 insertions(+) create mode 100644 options.c create mode 100644 options.h create mode 100644 options.text diff --git a/options.c b/options.c new file mode 100644 index 0000000..a202258 --- /dev/null +++ b/options.c @@ -0,0 +1,102 @@ +#ifdef __ORCAC__ +#pragma optimize 79 +#pragma noroot +#endif + +#include +#include + +#include "options.h" + + extern void help(void); + +int GetOptions(int argc, char **argv, + struct Options *options) +{ + int i, j; + int eof = 0; + int mindex = 1; /* mutation index */ + + for (i = 1; i < argc; ++i) + { + char *cp = argv[i]; + char c = cp[0]; + + + + if (eof || c != '-') + { + if (mindex != i) argv[mindex] = argv[i]; + ++mindex; + continue; + } + + // long opt check would go here... + if (cp[1] == '-' && cp[2] == 0) + { + eof = 1; + continue; + } + + // special case for '-' + j = 0; + if (cp[1] != 0) j = 1; + + for (; ; ++j) + { + char *optarg = 0; + + c = cp[j]; + if (!c) break; + switch(c) + { + case '0': + options->_0 = 1; + options->_1 = 0; + break; + case '1': + options->_1 = 1; + options->_0 = 0; + break; + case 'I': + options->_I = 1; + break; + case 'O': + options->_O = 1; + break; + case 'h': + help(); + exit(0); + break; + case 'i': + options->_i = 1; + break; + case 'o': + ++j; + if (cp[j]) { + optarg = cp + j; + } else { + ++i; + if (i < argc) optarg = argv[i]; + } + if (!optarg) { + fputs("-o requires an argument\n", stderr); + exit(1); + } + options->_o = optarg; + options->_O = 0; + break; + case 'v': + options->_v = 1; + break; + default: + fprintf(stderr, "-%c : invalid option\n", c); + exit(1); + break; + } + // could optimize out if no options have flags. + if (optarg) break; + } + } + return mindex; +} diff --git a/options.h b/options.h new file mode 100644 index 0000000..18fd5c4 --- /dev/null +++ b/options.h @@ -0,0 +1,17 @@ +#ifndef __Options__ +#define __Options__ + +typedef struct Options +{ + unsigned _0:1; + unsigned _1:1; + unsigned _I:1; + unsigned _O:1; + unsigned _i:1; + char *_o; + unsigned _v:1; +} Options; + +int GetOptions(int argc, char **argv, + Options *options); +#endif diff --git a/options.text b/options.text new file mode 100644 index 0000000..8420541 --- /dev/null +++ b/options.text @@ -0,0 +1,27 @@ +# +# gopher command line options. +# + +%extra_includes -> + extern void help(void); + +-h [virtual] -> + help(); + 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; + +-1 -> + options->_0 = 0; \ No newline at end of file