new options

This commit is contained in:
Kelvin Sherlock 2013-08-11 12:51:58 -04:00
parent 86f0933ec7
commit 58df66cc71
3 changed files with 146 additions and 0 deletions

102
options.c Normal file
View File

@ -0,0 +1,102 @@
#ifdef __ORCAC__
#pragma optimize 79
#pragma noroot
#endif
#include <stdio.h>
#include <stdlib.h>
#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;
}

17
options.h Normal file
View File

@ -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

27
options.text Normal file
View File

@ -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;