1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214
|
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <tcl.h>
#include "misc.h"
#include "io_utils.h" /* io_handle() */
/*
typedef int GapIO;
GapIO *io_handle(int *x) {}
*/
#include "gap_cli_arg.h"
static void parse_args_set(cli_args *a, char *store, char *val) {
if (a->type == ARG_STR) {
*((char **)&store[a->offset]) = val;
} else if (a->type == ARG_ARR) {
strncpy((char *)&store[a->offset], val, a->value-1);
} else if (a->type == ARG_IO) {
int handle = atoi(val);
GapIO *io = io_handle(&handle);
if (io)
*((GapIO **)&store[a->offset]) = io;
else
return; /* Doesn't set a->def */
} else if (a->type == ARG_INT) {
*((int *)&store[a->offset]) = atoi(val);
} else if (a->type == ARG_OBJ) {
/* Cannot initialise objs */
*((Tcl_Obj **)&store[a->offset]) = NULL;
} else if (a->type == ARG_FLOAT) {
*((float *)&store[a->offset]) = atof(val);
} else if (a->type == ARG_DOUBLE) {
*((double *)&store[a->offset]) = atof(val);
} else {
fprintf(stderr, "Unknown argument type %d\n", a->type);
}
a->def = ""; /* mark as used */
}
/*
* Parses command line arguments.
* 'args' specifies an array cli_args. Each cli_arg contains a default. Each
* argument with a default of NULL are non optional. This function will return
* -1 if any of these arguments aren't specified, or if unknown arguments
* are specified. Returns 0 otherwise.
*/
int gap_parse_args(cli_args *args, void *store, int argc, char **argv) {
cli_args *a;
int ret;
/* Initialise defaults */
for (a = args; a->command; a++)
if (a->def)
parse_args_set(a, store, a->def);
else if (a->type == ARG_ARR)
memset((char *)&((char *)store)[a->offset], 0, a->value); /* YUK */
ret = gap_parse_config(args, store, argc, argv);
/* Check all required options were set */
for (a = args; a->command; a++)
if (!a->def) {
verror(ERR_WARN, "parse_args",
"No argument given for option '%s'\n",
a->command);
return -1;
}
return ret;
}
int gap_parse_config(cli_args *args, void *store, int argc, char **argv) {
int i, ret = 0;
cli_args *a;
char *aname;
for (i = 1; i < argc; i++) {
aname = argv[i];
for (a = args; a->command; a++) {
if (strcmp(a->command, aname)== 0) {
if (a->value) {
if (i == argc - 1) {
verror(ERR_WARN, "parse_args",
"No argument given for option '%s'\n",
aname);
ret = -1;
break;
}
parse_args_set(a, store, argv[++i]);
} else
parse_args_set(a, store, "1");
break;
}
}
if (!a->command) {
verror(ERR_WARN,"parse_args", "Unknown option '%s'\n", aname);
ret = -1;
}
}
return ret;
}
static void parse_args_obj_set(cli_args *a, char *store, Tcl_Obj *val) {
if (a->type == ARG_OBJ) {
*((Tcl_Obj **)&store[a->offset]) = val;
} else if (a->type == ARG_STR) {
*((char **)&store[a->offset]) = Tcl_GetStringFromObj(val, NULL);
} else if (a->type == ARG_IO) {
int handle;
GapIO *io;
Tcl_GetIntFromObj(NULL, val, &handle);
io = io_handle(&handle);
if (io)
*((GapIO **)&store[a->offset]) = io;
else
return; /* Doesn't set a->def */
} else if (a->type == ARG_INT) {
int i;
Tcl_GetIntFromObj(NULL, val, &i);
*((int *)&store[a->offset]) = i;
} else if (a->type == ARG_FLOAT) {
double d;
Tcl_GetDoubleFromObj(NULL, val, &d);
*((float *)&store[a->offset]) = d;
} else if (a->type == ARG_DOUBLE) {
double d;
Tcl_GetDoubleFromObj(NULL, val, &d);
*((double *)&store[a->offset]) = d;
} else {
fprintf(stderr, "Unknown argument type %d\n", a->type);
}
a->def = ""; /* mark as used */
}
int gap_parse_obj_args(cli_args *args, void *store, int objc,
Tcl_Obj * const objv[]) {
cli_args *a;
int ret;
/* Initialise defaults */
for (a = args; a->command; a++)
if (a->def)
parse_args_set(a, store, a->def);
else if (a->type == ARG_ARR)
memset((char *)&((char *)store)[a->offset], 0, a->value); /* YUK */
ret = gap_parse_obj_config(args, store, objc, objv);
/* Check all required options were set */
for (a = args; a->command; a++)
if (!a->def)
return -1;
return ret;
}
int gap_parse_obj_config(cli_args *args, void *store, int objc,
Tcl_Obj * const objv[]) {
int i, ret = 0;
cli_args *a;
Tcl_Obj *one_str;
one_str = Tcl_NewStringObj("1", -1);
for (i = 1; i < objc; i++) {
char *aname = Tcl_GetStringFromObj(objv[i], NULL);
for (a = args; a->command; a++) {
if (strcmp(a->command, aname) == 0) {
if (a->value) {
if (i == objc - 1) {
verror(ERR_WARN, "parse_args",
"No argument given for option '%s'\n",
aname);
ret = -1;
break;
}
parse_args_obj_set(a, store, objv[++i]);
} else
parse_args_obj_set(a, store, one_str);
break;
}
}
if (!a->command) {
verror(ERR_WARN,"parse_args", "Unknown option '%s'\n", aname);
ret = -1;
}
}
Tcl_DecrRefCount(one_str);
return ret;
}
|