| 1 |
/* |
| 2 |
* Copyright (c) 2002-2005, Network Appliance, Inc. All rights reserved. |
| 3 |
* |
| 4 |
* This Software is licensed under one of the following licenses: |
| 5 |
* |
| 6 |
* 1) under the terms of the "Common Public License 1.0" a copy of which is |
| 7 |
* in the file LICENSE.txt in the root directory. The license is also |
| 8 |
* available from the Open Source Initiative, see |
| 9 |
* http://www.opensource.org/licenses/cpl.php. |
| 10 |
* |
| 11 |
* 2) under the terms of the "The BSD License" a copy of which is in the file |
| 12 |
* LICENSE2.txt in the root directory. The license is also available from |
| 13 |
* the Open Source Initiative, see |
| 14 |
* http://www.opensource.org/licenses/bsd-license.php. |
| 15 |
* |
| 16 |
* 3) under the terms of the "GNU General Public License (GPL) Version 2" a |
| 17 |
* copy of which is in the file LICENSE3.txt in the root directory. The |
| 18 |
* license is also available from the Open Source Initiative, see |
| 19 |
* http://www.opensource.org/licenses/gpl-license.php. |
| 20 |
* |
| 21 |
* Licensee has the right to choose one of the above licenses. |
| 22 |
* |
| 23 |
* Redistributions of source code must retain the above copyright |
| 24 |
* notice and one of the license notices. |
| 25 |
* |
| 26 |
* Redistributions in binary form must reproduce both the above copyright |
| 27 |
* notice, one of the license notices in the documentation |
| 28 |
* and/or other materials provided with the distribution. |
| 29 |
*/ |
| 30 |
|
| 31 |
#include "dapl_proto.h" |
| 32 |
|
| 33 |
#define MAX_ARGC 500 |
| 34 |
#define MAX_ARG_LEN 100 |
| 35 |
|
| 36 |
/* Parse command line arguments */ |
| 37 |
bool DT_Params_Parse(int argc, char *argv[], Params_t * params_ptr) |
| 38 |
{ |
| 39 |
Server_Cmd_t *Server_Cmd; |
| 40 |
Transaction_Cmd_t *Transaction_Cmd; |
| 41 |
Quit_Cmd_t *Quit_Cmd; |
| 42 |
Limit_Cmd_t *Limit_Cmd; |
| 43 |
Performance_Cmd_t *Performance_Cmd; |
| 44 |
FFT_Cmd_t *FFT_Cmd; |
| 45 |
|
| 46 |
char *filename; |
| 47 |
FILE *fd; |
| 48 |
mygetopt_t opts; |
| 49 |
int c; |
| 50 |
char *cp; |
| 51 |
char *sp; |
| 52 |
char line[256]; |
| 53 |
char *my_argv[MAX_ARGC]; |
| 54 |
int my_argc; |
| 55 |
int i; |
| 56 |
DT_mygetopt_init(&opts); |
| 57 |
opts.opterr = 0; /* turn off automatical error handler */ |
| 58 |
|
| 59 |
fd = 0; |
| 60 |
my_argc = 0; |
| 61 |
for (i = 0; i < MAX_ARGC; i++) { |
| 62 |
my_argv[i] = NULL; |
| 63 |
} |
| 64 |
|
| 65 |
/* dapltest with no arguments means run as a server with default values */ |
| 66 |
if (argc == 1) { |
| 67 |
params_ptr->test_type = SERVER_TEST; |
| 68 |
params_ptr->ReliabilityLevel = DAT_QOS_BEST_EFFORT; |
| 69 |
Server_Cmd = ¶ms_ptr->u.Server_Cmd; |
| 70 |
DT_Server_Cmd_Init(Server_Cmd); |
| 71 |
if (!DT_Mdep_GetDefaultDeviceName(Server_Cmd->dapl_name)) { |
| 72 |
DT_Mdep_printf("can't get default device name\n"); |
| 73 |
return false; |
| 74 |
} |
| 75 |
params_ptr->server_port = Server_Cmd->port; |
| 76 |
return true; |
| 77 |
} |
| 78 |
/* check for a script file */ |
| 79 |
if (strncmp(argv[1], "-f", 2) == 0) { |
| 80 |
if (argc == 2) { /* dapltest -fdata */ |
| 81 |
filename = argv[1] + 2; |
| 82 |
} else { |
| 83 |
if (argc == 3 && strcmp(argv[1], "-f") == 0) { /* dapltest -f data */ |
| 84 |
filename = argv[2]; |
| 85 |
} else { |
| 86 |
DT_Mdep_printf |
| 87 |
("-f <script_file> allows no additional options\n"); |
| 88 |
goto main_usage; |
| 89 |
} |
| 90 |
} |
| 91 |
|
| 92 |
if (!filename || strlen(filename) == 0) { |
| 93 |
DT_Mdep_printf |
| 94 |
("Missing <script_file> with -f option\n"); |
| 95 |
goto main_usage; |
| 96 |
} |
| 97 |
/* read the script file and create a fake argc, argv */ |
| 98 |
fd = fopen(filename, "r"); |
| 99 |
if (fd == 0) { |
| 100 |
DT_Mdep_printf("Cannot open script file: %s\n", |
| 101 |
filename); |
| 102 |
goto main_usage; |
| 103 |
} |
| 104 |
my_argc = 1; |
| 105 |
my_argv[0] = DT_Mdep_Malloc(MAX_ARG_LEN); |
| 106 |
if (!my_argv[0]) { |
| 107 |
DT_Mdep_printf("No Memory\n"); |
| 108 |
goto error_return; |
| 109 |
} |
| 110 |
strcpy(my_argv[0], argv[0]); |
| 111 |
while (fgets(&line[0], 256, fd)) { |
| 112 |
sp = &line[0]; |
| 113 |
for (;;) { |
| 114 |
cp = strtok(sp, " \t\n"); |
| 115 |
sp = 0; /* so can continue to parse this string */ |
| 116 |
if (!cp) { /* no more token found */ |
| 117 |
break; |
| 118 |
} |
| 119 |
if (*cp == '#') { /* Comment; go to next line. */ |
| 120 |
break; |
| 121 |
} |
| 122 |
my_argv[my_argc] = DT_Mdep_Malloc(MAX_ARG_LEN); |
| 123 |
if (!my_argv[my_argc]) { |
| 124 |
DT_Mdep_printf("No Memory\n"); |
| 125 |
goto error_return; |
| 126 |
} |
| 127 |
strcpy(my_argv[my_argc], cp); |
| 128 |
my_argc++; |
| 129 |
} |
| 130 |
} |
| 131 |
} else { |
| 132 |
my_argc = argc; |
| 133 |
for (i = 0; i < argc; i++) { |
| 134 |
my_argv[i] = argv[i]; |
| 135 |
} |
| 136 |
} |
| 137 |
|
| 138 |
#if 0 |
| 139 |
for (i = 0; i < my_argc; i++) { |
| 140 |
DT_Mdep_printf("ARG %s\n", my_argv[i]); |
| 141 |
} |
| 142 |
exit(1); |
| 143 |
#endif |
| 144 |
|
| 145 |
/* get test type - which must be the first arg */ |
| 146 |
c = DT_mygetopt_r(my_argc, my_argv, "T:", &opts); |
| 147 |
if (c != 'T') { |
| 148 |
DT_Mdep_printf("Must Specify Test (-T) option first\n"); |
| 149 |
goto main_usage; |
| 150 |
} |
| 151 |
if ((opts.optarg == 0) || strlen(opts.optarg) == 0 |
| 152 |
|| *opts.optarg == '-') { |
| 153 |
DT_Mdep_printf("Must specify test type\n"); |
| 154 |
goto main_usage; |
| 155 |
} |
| 156 |
switch (*opts.optarg) { |
| 157 |
case 'S': /* Server Test */ |
| 158 |
{ |
| 159 |
params_ptr->test_type = SERVER_TEST; |
| 160 |
Server_Cmd = ¶ms_ptr->u.Server_Cmd; |
| 161 |
DT_Server_Cmd_Init(Server_Cmd); |
| 162 |
if (!DT_Server_Cmd_Parse(Server_Cmd, |
| 163 |
my_argc, my_argv, &opts)) { |
| 164 |
goto error_return; |
| 165 |
} |
| 166 |
params_ptr->ReliabilityLevel = |
| 167 |
Server_Cmd->ReliabilityLevel; |
| 168 |
params_ptr->debug = Server_Cmd->debug; |
| 169 |
params_ptr->server_port = Server_Cmd->port; |
| 170 |
break; |
| 171 |
} |
| 172 |
case 'T': /* Transaction Test */ |
| 173 |
{ |
| 174 |
params_ptr->test_type = TRANSACTION_TEST; |
| 175 |
Transaction_Cmd = ¶ms_ptr->u.Transaction_Cmd; |
| 176 |
DT_Transaction_Cmd_Init(Transaction_Cmd); |
| 177 |
if (!DT_Transaction_Cmd_Parse(Transaction_Cmd, |
| 178 |
my_argc, my_argv, &opts)) |
| 179 |
{ |
| 180 |
goto error_return; |
| 181 |
} |
| 182 |
params_ptr->ReliabilityLevel = |
| 183 |
Transaction_Cmd->ReliabilityLevel; |
| 184 |
params_ptr->debug = Transaction_Cmd->debug; |
| 185 |
params_ptr->server_port = Transaction_Cmd->port; |
| 186 |
DT_NetAddrLookupHostAddress(¶ms_ptr->server_netaddr, |
| 187 |
Transaction_Cmd-> |
| 188 |
server_name); |
| 189 |
break; |
| 190 |
} |
| 191 |
case 'Q': /* Quit server Test */ |
| 192 |
{ |
| 193 |
params_ptr->test_type = QUIT_TEST; |
| 194 |
Quit_Cmd = ¶ms_ptr->u.Quit_Cmd; |
| 195 |
DT_Quit_Cmd_Init(Quit_Cmd); |
| 196 |
if (!DT_Quit_Cmd_Parse(Quit_Cmd, |
| 197 |
my_argc, my_argv, &opts)) { |
| 198 |
goto error_return; |
| 199 |
} |
| 200 |
params_ptr->ReliabilityLevel = |
| 201 |
Quit_Cmd->ReliabilityLevel; |
| 202 |
params_ptr->debug = Quit_Cmd->debug; |
| 203 |
params_ptr->server_port = Quit_Cmd->port; |
| 204 |
DT_NetAddrLookupHostAddress(¶ms_ptr->server_netaddr, |
| 205 |
Quit_Cmd->server_name); |
| 206 |
break; |
| 207 |
} |
| 208 |
case 'L': /* Limit Test */ |
| 209 |
{ |
| 210 |
params_ptr->test_type = LIMIT_TEST; |
| 211 |
Limit_Cmd = ¶ms_ptr->u.Limit_Cmd; |
| 212 |
DT_Limit_Cmd_Init(Limit_Cmd); |
| 213 |
if (!DT_Limit_Cmd_Parse |
| 214 |
(Limit_Cmd, my_argc, my_argv, &opts)) { |
| 215 |
goto error_return; |
| 216 |
} |
| 217 |
params_ptr->ReliabilityLevel = |
| 218 |
Limit_Cmd->ReliabilityLevel; |
| 219 |
params_ptr->debug = Limit_Cmd->debug; |
| 220 |
params_ptr->server_port = Limit_Cmd->port; |
| 221 |
break; |
| 222 |
} |
| 223 |
case 'P': /* Performance Test */ |
| 224 |
{ |
| 225 |
params_ptr->test_type = PERFORMANCE_TEST; |
| 226 |
Performance_Cmd = ¶ms_ptr->u.Performance_Cmd; |
| 227 |
|
| 228 |
if (!DT_Performance_Cmd_Init(Performance_Cmd)) { |
| 229 |
goto error_return; |
| 230 |
} |
| 231 |
|
| 232 |
if (!DT_Performance_Cmd_Parse(Performance_Cmd, |
| 233 |
my_argc, my_argv, &opts)) |
| 234 |
{ |
| 235 |
goto error_return; |
| 236 |
} |
| 237 |
|
| 238 |
params_ptr->ReliabilityLevel = Performance_Cmd->qos; |
| 239 |
params_ptr->debug = Performance_Cmd->debug; |
| 240 |
params_ptr->server_port = Performance_Cmd->port; |
| 241 |
DT_NetAddrLookupHostAddress(¶ms_ptr->server_netaddr, |
| 242 |
Performance_Cmd-> |
| 243 |
server_name); |
| 244 |
break; |
| 245 |
} |
| 246 |
case 'F': |
| 247 |
{ |
| 248 |
params_ptr->test_type = FFT_TEST; |
| 249 |
FFT_Cmd = ¶ms_ptr->u.FFT_Cmd; |
| 250 |
DT_FFT_Cmd_Init(FFT_Cmd); |
| 251 |
if (!DT_FFT_Cmd_Parse(FFT_Cmd, my_argc, my_argv, &opts)) { |
| 252 |
goto error_return; |
| 253 |
} |
| 254 |
params_ptr->ReliabilityLevel = |
| 255 |
FFT_Cmd->ReliabilityLevel; |
| 256 |
DT_NetAddrLookupHostAddress(¶ms_ptr->server_netaddr, |
| 257 |
FFT_Cmd->server_name); |
| 258 |
params_ptr->debug = false; |
| 259 |
params_ptr->server_port = FFT_Cmd->port; |
| 260 |
break; |
| 261 |
} |
| 262 |
|
| 263 |
default: |
| 264 |
{ |
| 265 |
DT_Mdep_printf("Invalid Test Type\n"); |
| 266 |
goto main_usage; |
| 267 |
} |
| 268 |
} |
| 269 |
|
| 270 |
if (fd) { |
| 271 |
for (i = 0; i < my_argc; i++) { |
| 272 |
DT_Mdep_Free(my_argv[i]); |
| 273 |
} |
| 274 |
fclose(fd); |
| 275 |
} |
| 276 |
return true; |
| 277 |
|
| 278 |
main_usage: |
| 279 |
Dapltest_Main_Usage(); |
| 280 |
error_return: |
| 281 |
if (fd) { |
| 282 |
for (i = 0; i < my_argc; i++) { |
| 283 |
DT_Mdep_Free(my_argv[i]); |
| 284 |
} |
| 285 |
fclose(fd); |
| 286 |
} |
| 287 |
return false; |
| 288 |
} |