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 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347
|
/* -*- Mode: C; c-basic-offset:4 ; indent-tabs-mode:nil -*- */
/*
* Copyright (c) 2004-2005 The Trustees of Indiana University and Indiana
* University Research and Technology
* Corporation. All rights reserved.
* Copyright (c) 2004-2013 The University of Tennessee and The University
* of Tennessee Research Foundation. All rights
* reserved.
* Copyright (c) 2004-2005 High Performance Computing Center Stuttgart,
* University of Stuttgart. All rights reserved.
* Copyright (c) 2004-2005 The Regents of the University of California.
* All rights reserved.
* Copyright (c) 2012-2017 Los Alamos National Security, LLC. All rights
* reserved.
* Copyright (c) 2012-2022 Cisco Systems, Inc. All rights reserved
* Copyright (c) 2015-2017 Research Organization for Information Science
* and Technology (RIST). All rights reserved.
* Copyright (c) 2016-2017 Intel, Inc. All rights reserved.
* Copyright (c) 2017 IBM Corporation. All rights reserved.
* $COPYRIGHT$
*
* Additional copyrights may follow
*
* $HEADER$
*/
#include "opal_config.h"
#include <ctype.h>
#include <stdio.h>
#include <string.h>
#include "opal/class/opal_list.h"
#include "opal/class/opal_object.h"
#include "opal/mca/threads/mutex.h"
#include "opal/util/argv.h"
#include "opal/util/cmd_line.h"
#include "opal/util/opal_environ.h"
#include "opal/util/output.h"
#include "opal/constants.h"
#include "opal/mca/base/mca_base_var.h"
/*
* Some usage message constants
*
* Max width for param listings before the description will be listed
* on the next line
*/
#define PARAM_WIDTH 25
/*
* Max length of any line in the usage message
*/
#define MAX_WIDTH 76
/*
* Description of a command line option
*/
struct ompi_cmd_line_option_t {
opal_list_item_t super;
char clo_short_name;
char *clo_single_dash_name;
char *clo_long_name;
int clo_num_params;
char *clo_description;
opal_cmd_line_type_t clo_type;
char *clo_mca_param_env_var;
void *clo_variable_dest;
bool clo_variable_set;
opal_cmd_line_otype_t clo_otype;
};
typedef struct ompi_cmd_line_option_t ompi_cmd_line_option_t;
static void option_constructor(ompi_cmd_line_option_t *cmd);
static void option_destructor(ompi_cmd_line_option_t *cmd);
OBJ_CLASS_INSTANCE(ompi_cmd_line_option_t, opal_list_item_t, option_constructor, option_destructor);
/*
* An option that was used in the argv that was parsed
*/
struct ompi_cmd_line_param_t {
opal_list_item_t super;
/* Note that clp_arg points to storage "owned" by someone else; it
has the original option string by reference, not by value.
Hence, it should not be free()'ed. */
char *clp_arg;
/* Pointer to the existing option. This is also by reference; it
should not be free()ed. */
ompi_cmd_line_option_t *clp_option;
/* This argv array is a list of all the parameters of this option.
It is owned by this parameter, and should be freed when this
param_t is freed. */
int clp_argc;
char **clp_argv;
};
typedef struct ompi_cmd_line_param_t ompi_cmd_line_param_t;
static void param_constructor(ompi_cmd_line_param_t *cmd);
static void param_destructor(ompi_cmd_line_param_t *cmd);
OBJ_CLASS_INSTANCE(ompi_cmd_line_param_t, opal_list_item_t, param_constructor, param_destructor);
/*
* Instantiate the opal_cmd_line_t class
*/
static void cmd_line_constructor(opal_cmd_line_t *cmd);
static void cmd_line_destructor(opal_cmd_line_t *cmd);
OBJ_CLASS_INSTANCE(opal_cmd_line_t, opal_object_t, cmd_line_constructor, cmd_line_destructor);
/*
* Private variables
*/
static char special_empty_token[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, '\0'};
/*
* Private functions
*/
static int make_opt(opal_cmd_line_t *cmd, opal_cmd_line_init_t *e);
static void free_parse_results(opal_cmd_line_t *cmd);
static int split_shorts(opal_cmd_line_t *cmd, char *token, char **args, int *output_argc,
char ***output_argv, int *num_args_used, bool ignore_unknown);
static ompi_cmd_line_option_t *find_option(opal_cmd_line_t *cmd, const char *option_name)
__opal_attribute_nonnull__(1) __opal_attribute_nonnull__(2);
static int set_dest(ompi_cmd_line_option_t *option, char *sval);
static void fill(const ompi_cmd_line_option_t *a, char result[3][BUFSIZ]);
static int qsort_callback(const void *a, const void *b);
static opal_cmd_line_otype_t get_help_otype(opal_cmd_line_t *cmd);
static char *build_parsable(ompi_cmd_line_option_t *option);
/*
* Create an entire command line handle from a table
*/
int opal_cmd_line_create(opal_cmd_line_t *cmd, opal_cmd_line_init_t *table)
{
int ret = OPAL_SUCCESS;
/* Check bozo case */
if (NULL == cmd) {
return OPAL_ERR_BAD_PARAM;
}
OBJ_CONSTRUCT(cmd, opal_cmd_line_t);
if (NULL != table) {
ret = opal_cmd_line_add(cmd, table);
}
return ret;
}
/* Add a table to an existing cmd line object */
int opal_cmd_line_add(opal_cmd_line_t *cmd, opal_cmd_line_init_t *table)
{
int i, ret;
/* Ensure we got a table */
if (NULL == table) {
return OPAL_SUCCESS;
}
/* Loop through the table */
for (i = 0;; ++i) {
/* Is this the end? */
if ('\0' == table[i].ocl_cmd_short_name && NULL == table[i].ocl_cmd_single_dash_name
&& NULL == table[i].ocl_cmd_long_name) {
break;
}
/* Nope -- it's an entry. Process it. */
ret = make_opt(cmd, &table[i]);
if (OPAL_SUCCESS != ret) {
return ret;
}
}
return OPAL_SUCCESS;
}
/*
* Append a command line entry to the previously constructed command line
*/
int opal_cmd_line_make_opt_mca(opal_cmd_line_t *cmd, opal_cmd_line_init_t entry)
{
/* Ensure we got an entry */
if ('\0' == entry.ocl_cmd_short_name && NULL == entry.ocl_cmd_single_dash_name
&& NULL == entry.ocl_cmd_long_name) {
return OPAL_SUCCESS;
}
return make_opt(cmd, &entry);
}
/*
* Create a command line option, --long-name and/or -s (short name).
*/
int opal_cmd_line_make_opt3(opal_cmd_line_t *cmd, char short_name, const char *sd_name,
const char *long_name, int num_params, const char *desc)
{
opal_cmd_line_init_t e;
e.ocl_mca_param_name = NULL;
e.ocl_cmd_short_name = short_name;
e.ocl_cmd_single_dash_name = sd_name;
e.ocl_cmd_long_name = long_name;
e.ocl_num_params = num_params;
e.ocl_variable_dest = NULL;
e.ocl_variable_type = OPAL_CMD_LINE_TYPE_NULL;
e.ocl_description = desc;
e.ocl_otype = OPAL_CMD_LINE_OTYPE_GENERAL;
return make_opt(cmd, &e);
}
/*
* Parse a command line according to a pre-built OPAL command line
* handle.
*/
int opal_cmd_line_parse(opal_cmd_line_t *cmd, bool ignore_unknown, bool ignore_unknown_option,
int argc, char **argv)
{
int i, j, orig, ret;
ompi_cmd_line_option_t *option;
ompi_cmd_line_param_t *param;
bool is_unknown_option;
bool is_unknown_token;
bool is_option;
char **shortsv;
int shortsc;
int num_args_used;
bool have_help_option = false;
bool printed_error = false;
bool help_without_arg = false;
/* Bozo check */
if (0 == argc || NULL == argv) {
return OPAL_SUCCESS;
}
/* Thread serialization */
opal_mutex_lock(&cmd->lcl_mutex);
/* Free any parsed results that are already on this handle */
free_parse_results(cmd);
/* Analyze each token */
cmd->lcl_argc = argc;
cmd->lcl_argv = opal_argv_copy(argv);
/* Check up front: do we have a --help option? */
option = find_option(cmd, "help");
if (NULL != option) {
have_help_option = true;
}
/* Now traverse the easy-to-parse sequence of tokens. Note that
incrementing i must happen elsewhere; it can't be the third
clause in the "if" statement. */
param = NULL;
option = NULL;
for (i = 1; i < cmd->lcl_argc;) {
is_unknown_option = false;
is_unknown_token = false;
is_option = false;
/* Are we done? i.e., did we find the special "--" token? If
so, copy everying beyond it into the tail (i.e., don't
bother copying the "--" into the tail). */
if (0 == strcmp(cmd->lcl_argv[i], "--")) {
++i;
while (i < cmd->lcl_argc) {
opal_argv_append(&cmd->lcl_tail_argc, &cmd->lcl_tail_argv, cmd->lcl_argv[i]);
++i;
}
break;
}
/* If it's not an option, then this is an error. Note that
this is different than an unrecognized token; an
unrecognized option is *always* an error. */
else if ('-' != cmd->lcl_argv[i][0]) {
is_unknown_token = true;
}
/* Nope, this is supposedly an option. Is it a long name? */
else if (0 == strncmp(cmd->lcl_argv[i], "--", 2)) {
is_option = true;
option = find_option(cmd, cmd->lcl_argv[i] + 2);
}
/* It could be a short name. Is it? */
else {
option = find_option(cmd, cmd->lcl_argv[i] + 1);
/* If we didn't find it, try to split it into shorts. If
we find the short option, replace lcl_argv[i] and
insert the rest into lcl_argv starting after position
i. If we don't find the short option, don't do
anything to lcl_argv so that it can fall through to the
error condition, below. */
if (NULL == option) {
shortsv = NULL;
shortsc = 0;
ret = split_shorts(cmd, cmd->lcl_argv[i] + 1, &(cmd->lcl_argv[i + 1]), &shortsc,
&shortsv, &num_args_used, ignore_unknown);
if (OPAL_SUCCESS == ret) {
option = find_option(cmd, shortsv[0] + 1);
if (NULL != option) {
opal_argv_delete(&cmd->lcl_argc, &cmd->lcl_argv, i, 1 + num_args_used);
opal_argv_insert(&cmd->lcl_argv, i, shortsv);
cmd->lcl_argc = opal_argv_count(cmd->lcl_argv);
} else {
is_unknown_option = true;
}
opal_argv_free(shortsv);
} else {
is_unknown_option = true;
}
}
if (NULL != option) {
is_option = true;
}
}
/* If we figured out above that this is an option, handle it */
if (is_option) {
if (NULL == option) {
is_unknown_option = true;
} else {
is_unknown_option = false;
orig = i;
++i;
/* Suck down the following parameters that belong to
this option. If we run out of parameters, or find
that any of them are the special_empty_param
(inserted by split_shorts()), then print an error
and return. */
param = OBJ_NEW(ompi_cmd_line_param_t);
if (NULL == param) {
opal_mutex_unlock(&cmd->lcl_mutex);
return OPAL_ERR_OUT_OF_RESOURCE;
}
param->clp_arg = cmd->lcl_argv[i];
param->clp_option = option;
/* If we have any parameters to this option, suck down
tokens starting one beyond the token that we just
recognized */
for (j = 0; j < option->clo_num_params; ++j, ++i) {
/* If we run out of parameters, error, unless its a help request
which can have 0 or 1 arguments */
if (i >= cmd->lcl_argc) {
/* If this is a help request, can have no arguments */
if ((NULL != option->clo_single_dash_name
&& 0 == strcmp(option->clo_single_dash_name, "h"))
|| (NULL != option->clo_long_name
&& 0 == strcmp(option->clo_long_name, "help"))) {
help_without_arg = true;
continue;
}
fprintf(stderr,
"%s: Error: option \"%s\" did not "
"have enough parameters (%d)\n",
cmd->lcl_argv[0], cmd->lcl_argv[orig], option->clo_num_params);
if (have_help_option) {
fprintf(stderr, "Type '%s --help' for usage.\n", cmd->lcl_argv[0]);
}
OBJ_RELEASE(param);
printed_error = true;
goto error;
} else {
if (0 == strcmp(cmd->lcl_argv[i], special_empty_token)) {
fprintf(stderr,
"%s: Error: option \"%s\" did not "
"have enough parameters (%d)\n",
cmd->lcl_argv[0], cmd->lcl_argv[orig], option->clo_num_params);
if (have_help_option) {
fprintf(stderr, "Type '%s --help' for usage.\n", cmd->lcl_argv[0]);
}
if (NULL != param->clp_argv) {
opal_argv_free(param->clp_argv);
}
OBJ_RELEASE(param);
printed_error = true;
goto error;
}
/* Otherwise, save this parameter */
else {
/* Save in the argv on the param entry */
opal_argv_append(¶m->clp_argc, ¶m->clp_argv, cmd->lcl_argv[i]);
/* If it's the first, save it in the
variable dest and/or MCA parameter */
if (0 == j
&& (NULL != option->clo_mca_param_env_var
|| NULL != option->clo_variable_dest)) {
if (OPAL_SUCCESS != (ret = set_dest(option, cmd->lcl_argv[i]))) {
opal_mutex_unlock(&cmd->lcl_mutex);
return ret;
}
}
}
}
}
/* If there are no options to this command or it is
a help request with no argument, see if we need to
set a boolean value to "true". */
if (0 == option->clo_num_params || help_without_arg) {
if (OPAL_SUCCESS != (ret = set_dest(option, "1"))) {
opal_mutex_unlock(&cmd->lcl_mutex);
return ret;
}
}
/* If we succeeded in all that, save the param to the
list on the opal_cmd_line_t handle */
if (NULL != param) {
opal_list_append(&cmd->lcl_params, ¶m->super);
}
}
}
/* If we figured out above that this was an unknown option,
handle it. Copy everything (including the current token)
into the tail. If we're not ignoring unknowns, then print
an error and return. */
if (is_unknown_option || is_unknown_token) {
if (!ignore_unknown || (is_unknown_option && !ignore_unknown_option)) {
fprintf(stderr, "%s: Error: unknown option \"%s\"\n", cmd->lcl_argv[0],
cmd->lcl_argv[i]);
printed_error = true;
if (have_help_option) {
fprintf(stderr, "Type '%s --help' for usage.\n", cmd->lcl_argv[0]);
}
}
error:
while (i < cmd->lcl_argc) {
opal_argv_append(&cmd->lcl_tail_argc, &cmd->lcl_tail_argv, cmd->lcl_argv[i]);
++i;
}
/* Because i has advanced, we'll fall out of the loop */
}
}
/* Thread serialization */
opal_mutex_unlock(&cmd->lcl_mutex);
/* All done */
if (printed_error) {
return OPAL_ERR_SILENT;
}
return OPAL_SUCCESS;
}
/*
* Return a consolidated "usage" message for a OPAL command line handle.
*/
char *opal_cmd_line_get_usage_msg(opal_cmd_line_t *cmd)
{
size_t i, len;
int argc;
size_t j;
char **argv;
char *ret, line[(MAX_WIDTH * 2) + 1];
char *start, *desc, *ptr;
opal_list_item_t *item;
ompi_cmd_line_option_t *option, **sorted;
opal_cmd_line_otype_t otype;
/* Thread serialization */
opal_mutex_lock(&cmd->lcl_mutex);
/* Make an argv of all the usage strings */
argc = 0;
argv = NULL;
ret = NULL;
/* First, take the original list and sort it */
sorted = (ompi_cmd_line_option_t **) malloc(sizeof(ompi_cmd_line_option_t *)
* opal_list_get_size(&cmd->lcl_options));
if (NULL == sorted) {
opal_mutex_unlock(&cmd->lcl_mutex);
return NULL;
}
i = 0;
OPAL_LIST_FOREACH (item, &cmd->lcl_options, opal_list_item_t) {
sorted[i++] = (ompi_cmd_line_option_t *) item;
}
qsort(sorted, i, sizeof(ompi_cmd_line_option_t *), qsort_callback);
/* Find if a help argument was passed, and return its type if it was. */
otype = get_help_otype(cmd);
/* Now go through the sorted array and make the strings */
for (j = 0; j < opal_list_get_size(&cmd->lcl_options); ++j) {
option = sorted[j];
if (otype == OPAL_CMD_LINE_OTYPE_PARSABLE) {
ret = build_parsable(option);
opal_argv_append(&argc, &argv, ret);
free(ret);
ret = NULL;
} else if (otype == OPAL_CMD_LINE_OTYPE_NULL || option->clo_otype == otype) {
if (NULL != option->clo_description) {
bool filled = false;
/* Build up the output line */
memset(line, 0, sizeof(line));
if ('\0' != option->clo_short_name) {
line[0] = '-';
line[1] = option->clo_short_name;
filled = true;
} else {
line[0] = ' ';
line[1] = ' ';
}
if (NULL != option->clo_single_dash_name) {
line[2] = (filled) ? '|' : ' ';
strncat(line, "-", sizeof(line) - strlen(line) - 1);
strncat(line, option->clo_single_dash_name, sizeof(line) - strlen(line) - 1);
filled = true;
}
if (NULL != option->clo_long_name) {
if (filled) {
strncat(line, "|", sizeof(line) - strlen(line) - 1);
} else {
strncat(line, " ", sizeof(line) - strlen(line) - 1);
}
strncat(line, "--", sizeof(line) - strlen(line) - 1);
strncat(line, option->clo_long_name, sizeof(line) - strlen(line) - 1);
}
strncat(line, " ", sizeof(line) - strlen(line) - 1);
for (i = 0; (int) i < option->clo_num_params; ++i) {
char temp[MAX_WIDTH * 2];
snprintf(temp, MAX_WIDTH * 2, "<arg%d> ", (int) i);
strncat(line, temp, sizeof(line) - strlen(line) - 1);
}
if (option->clo_num_params > 0) {
strncat(line, " ", sizeof(line) - strlen(line) - 1);
}
/* If we're less than param width, then start adding the
description to this line. Otherwise, finish this line
and start adding the description on the next line. */
if (strlen(line) > PARAM_WIDTH) {
opal_argv_append(&argc, &argv, line);
/* Now reset the line to be all blanks up to
PARAM_WIDTH so that we can start adding the
description */
memset(line, ' ', PARAM_WIDTH);
line[PARAM_WIDTH] = '\0';
} else {
/* Add enough blanks to the end of the line so that we
can start adding the description */
for (i = strlen(line); i < PARAM_WIDTH; ++i) {
line[i] = ' ';
}
line[i] = '\0';
}
/* Loop over adding the description to the array, breaking
the string at most at MAX_WIDTH characters. We need a
modifiable description (for simplicity), so strdup the
clo_description (because it's likely a compiler
constant, and may barf if we write temporary \0's in
the middle). */
desc = strdup(option->clo_description);
if (NULL == desc) {
free(sorted);
opal_mutex_unlock(&cmd->lcl_mutex);
return strdup("");
}
start = desc;
len = strlen(desc);
do {
/* Trim off leading whitespace */
while (isspace(*start) && start < desc + len) {
++start;
}
if (start >= desc + len) {
break;
}
/* Last line */
if (strlen(start) < (MAX_WIDTH - PARAM_WIDTH)) {
strncat(line, start, sizeof(line) - strlen(line) - 1);
opal_argv_append(&argc, &argv, line);
break;
}
/* We have more than 1 line's worth left -- find this
line's worth and add it to the array. Then reset
and loop around to get the next line's worth. */
for (ptr = start + (MAX_WIDTH - PARAM_WIDTH); ptr > start; --ptr) {
if (isspace(*ptr)) {
*ptr = '\0';
strncat(line, start, sizeof(line) - strlen(line) - 1);
opal_argv_append(&argc, &argv, line);
start = ptr + 1;
memset(line, ' ', PARAM_WIDTH);
line[PARAM_WIDTH] = '\0';
break;
}
}
/* If we got all the way back to the beginning of the
string, then go forward looking for a whitespace
and break there. */
if (ptr == start) {
for (ptr = start + (MAX_WIDTH - PARAM_WIDTH); ptr < start + len; ++ptr) {
if (isspace(*ptr)) {
*ptr = '\0';
strncat(line, start, sizeof(line) - strlen(line) - 1);
opal_argv_append(&argc, &argv, line);
start = ptr + 1;
memset(line, ' ', PARAM_WIDTH);
line[PARAM_WIDTH] = '\0';
break;
}
}
/* If we reached the end of the string with no
whitespace, then just add it on and be done */
if (ptr >= start + len) {
strncat(line, start, sizeof(line) - strlen(line) - 1);
opal_argv_append(&argc, &argv, line);
start = desc + len + 1;
}
}
} while (start < desc + len);
free(desc);
}
}
}
if (NULL != argv) {
ret = opal_argv_join(argv, '\n');
opal_argv_free(argv);
} else {
ret = strdup("");
}
free(sorted);
/* Thread serialization */
opal_mutex_unlock(&cmd->lcl_mutex);
/* All done */
return ret;
}
/*
* Test if a given option was taken on the parsed command line.
*/
bool opal_cmd_line_is_taken(opal_cmd_line_t *cmd, const char *opt)
{
return (opal_cmd_line_get_ninsts(cmd, opt) > 0);
}
/*
* Return the number of instances of an option found during parsing.
*/
int opal_cmd_line_get_ninsts(opal_cmd_line_t *cmd, const char *opt)
{
int ret;
ompi_cmd_line_param_t *param;
ompi_cmd_line_option_t *option;
/* Thread serialization */
opal_mutex_lock(&cmd->lcl_mutex);
/* Find the corresponding option. If we find it, look through all
the parsed params and see if we have any matches. */
ret = 0;
option = find_option(cmd, opt);
if (NULL != option) {
OPAL_LIST_FOREACH (param, &cmd->lcl_params, ompi_cmd_line_param_t) {
if (param->clp_option == option) {
++ret;
}
}
}
/* Thread serialization */
opal_mutex_unlock(&cmd->lcl_mutex);
/* All done */
return ret;
}
/*
* Return a specific parameter for a specific instance of a option
* from the parsed command line.
*/
char *opal_cmd_line_get_param(opal_cmd_line_t *cmd, const char *opt, int inst, int idx)
{
int num_found;
ompi_cmd_line_param_t *param;
ompi_cmd_line_option_t *option;
/* Thread serialization */
opal_mutex_lock(&cmd->lcl_mutex);
/* Find the corresponding option. If we find it, look through all
the parsed params and see if we have any matches. */
num_found = 0;
option = find_option(cmd, opt);
if (NULL != option) {
/* Ensure to check for the case where the user has asked for a
parameter index greater than we will have */
if (idx < option->clo_num_params) {
OPAL_LIST_FOREACH (param, &cmd->lcl_params, ompi_cmd_line_param_t) {
if (param->clp_argc > 0 && param->clp_option == option) {
if (num_found == inst) {
opal_mutex_unlock(&cmd->lcl_mutex);
return param->clp_argv[idx];
}
++num_found;
}
}
}
}
/* Thread serialization */
opal_mutex_unlock(&cmd->lcl_mutex);
/* All done */
return NULL;
}
/*
* Return the number of arguments parsed on a OPAL command line handle.
*/
int opal_cmd_line_get_argc(opal_cmd_line_t *cmd)
{
return (NULL != cmd) ? cmd->lcl_argc : OPAL_ERROR;
}
/*
* Return a string argument parsed on a OPAL command line handle.
*/
char *opal_cmd_line_get_argv(opal_cmd_line_t *cmd, int index)
{
return (NULL == cmd) ? NULL
: (index >= cmd->lcl_argc || index < 0) ? NULL
: cmd->lcl_argv[index];
}
/*
* Return the entire "tail" of unprocessed argv from a OPAL command
* line handle.
*/
int opal_cmd_line_get_tail(opal_cmd_line_t *cmd, int *tailc, char ***tailv)
{
if (NULL != cmd) {
opal_mutex_lock(&cmd->lcl_mutex);
*tailc = cmd->lcl_tail_argc;
*tailv = opal_argv_copy(cmd->lcl_tail_argv);
opal_mutex_unlock(&cmd->lcl_mutex);
return OPAL_SUCCESS;
} else {
return OPAL_ERROR;
}
}
/**************************************************************************
* Static functions
**************************************************************************/
static void option_constructor(ompi_cmd_line_option_t *o)
{
o->clo_short_name = '\0';
o->clo_single_dash_name = NULL;
o->clo_long_name = NULL;
o->clo_num_params = 0;
o->clo_description = NULL;
o->clo_type = OPAL_CMD_LINE_TYPE_NULL;
o->clo_mca_param_env_var = NULL;
o->clo_variable_dest = NULL;
o->clo_variable_set = false;
o->clo_otype = OPAL_CMD_LINE_OTYPE_NULL;
}
static void option_destructor(ompi_cmd_line_option_t *o)
{
if (NULL != o->clo_single_dash_name) {
free(o->clo_single_dash_name);
}
if (NULL != o->clo_long_name) {
free(o->clo_long_name);
}
if (NULL != o->clo_description) {
free(o->clo_description);
}
if (NULL != o->clo_mca_param_env_var) {
free(o->clo_mca_param_env_var);
}
}
static void param_constructor(ompi_cmd_line_param_t *p)
{
p->clp_arg = NULL;
p->clp_option = NULL;
p->clp_argc = 0;
p->clp_argv = NULL;
}
static void param_destructor(ompi_cmd_line_param_t *p)
{
if (NULL != p->clp_argv) {
opal_argv_free(p->clp_argv);
}
}
static void cmd_line_constructor(opal_cmd_line_t *cmd)
{
/* Initialize the mutex. Since we're creating (and therefore the
only thread that has this instance), there's no need to lock it
right now. */
OBJ_CONSTRUCT(&cmd->lcl_mutex, opal_recursive_mutex_t);
/* Initialize the lists */
OBJ_CONSTRUCT(&cmd->lcl_options, opal_list_t);
OBJ_CONSTRUCT(&cmd->lcl_params, opal_list_t);
/* Initialize the argc/argv pairs */
cmd->lcl_argc = 0;
cmd->lcl_argv = NULL;
cmd->lcl_tail_argc = 0;
cmd->lcl_tail_argv = NULL;
}
static void cmd_line_destructor(opal_cmd_line_t *cmd)
{
opal_list_item_t *item;
/* Free the contents of the options list (do not free the list
itself; it was not allocated from the heap) */
for (item = opal_list_remove_first(&cmd->lcl_options); NULL != item;
item = opal_list_remove_first(&cmd->lcl_options)) {
OBJ_RELEASE(item);
}
/* Free any parsed results */
free_parse_results(cmd);
/* Destroy the lists */
OBJ_DESTRUCT(&cmd->lcl_options);
OBJ_DESTRUCT(&cmd->lcl_params);
/* Destroy the mutex */
OBJ_DESTRUCT(&cmd->lcl_mutex);
}
static int make_opt(opal_cmd_line_t *cmd, opal_cmd_line_init_t *e)
{
ompi_cmd_line_option_t *option;
/* Bozo checks */
if (NULL == cmd) {
return OPAL_ERR_BAD_PARAM;
} else if ('\0' == e->ocl_cmd_short_name && NULL == e->ocl_cmd_single_dash_name
&& NULL == e->ocl_cmd_long_name) {
return OPAL_ERR_BAD_PARAM;
} else if (e->ocl_num_params < 0) {
return OPAL_ERR_BAD_PARAM;
}
/* see if the option already exists */
if (NULL != e->ocl_cmd_single_dash_name
&& NULL != find_option(cmd, e->ocl_cmd_single_dash_name)) {
opal_output(0, "Duplicate cmd line entry %s", e->ocl_cmd_single_dash_name);
return OPAL_ERR_BAD_PARAM;
}
if (NULL != e->ocl_cmd_long_name && NULL != find_option(cmd, e->ocl_cmd_long_name)) {
opal_output(0, "Duplicate cmd line entry %s", e->ocl_cmd_long_name);
return OPAL_ERR_BAD_PARAM;
}
/* Allocate and fill an option item */
option = OBJ_NEW(ompi_cmd_line_option_t);
if (NULL == option) {
return OPAL_ERR_OUT_OF_RESOURCE;
}
option->clo_short_name = e->ocl_cmd_short_name;
if (NULL != e->ocl_cmd_single_dash_name) {
option->clo_single_dash_name = strdup(e->ocl_cmd_single_dash_name);
}
if (NULL != e->ocl_cmd_long_name) {
option->clo_long_name = strdup(e->ocl_cmd_long_name);
}
option->clo_num_params = e->ocl_num_params;
if (NULL != e->ocl_description) {
option->clo_description = strdup(e->ocl_description);
}
option->clo_type = e->ocl_variable_type;
option->clo_variable_dest = e->ocl_variable_dest;
if (NULL != e->ocl_mca_param_name) {
(void) mca_base_var_env_name(e->ocl_mca_param_name, &option->clo_mca_param_env_var);
}
option->clo_otype = e->ocl_otype;
/* Append the item, serializing thread access */
opal_mutex_lock(&cmd->lcl_mutex);
opal_list_append(&cmd->lcl_options, &option->super);
opal_mutex_unlock(&cmd->lcl_mutex);
/* All done */
return OPAL_SUCCESS;
}
static void free_parse_results(opal_cmd_line_t *cmd)
{
opal_list_item_t *item;
/* Free the contents of the params list (do not free the list
itself; it was not allocated from the heap) */
for (item = opal_list_remove_first(&cmd->lcl_params); NULL != item;
item = opal_list_remove_first(&cmd->lcl_params)) {
OBJ_RELEASE(item);
}
/* Free the argv's */
if (NULL != cmd->lcl_argv) {
opal_argv_free(cmd->lcl_argv);
}
cmd->lcl_argv = NULL;
cmd->lcl_argc = 0;
if (NULL != cmd->lcl_tail_argv) {
opal_argv_free(cmd->lcl_tail_argv);
}
cmd->lcl_tail_argv = NULL;
cmd->lcl_tail_argc = 0;
}
/*
* Traverse a token and split it into individual letter options (the
* token has already been certified to not be a long name and not be a
* short name). Ensure to differentiate the resulting options from
* "single dash" names.
*/
static int split_shorts(opal_cmd_line_t *cmd, char *token, char **args, int *output_argc,
char ***output_argv, int *num_args_used, bool ignore_unknown)
{
int i, j, len;
ompi_cmd_line_option_t *option;
char fake_token[3];
int num_args;
/* Setup that we didn't use any of the args */
num_args = opal_argv_count(args);
*num_args_used = 0;
/* Traverse the token. If it's empty (e.g., if someone passes a
"-" token, which, since the upper level calls this function as
(argv[i] + 1), will be empty by the time it gets down here),
just return that we didn't find a short option. */
len = (int) strlen(token);
if (0 == len) {
return OPAL_ERR_BAD_PARAM;
}
fake_token[0] = '-';
fake_token[2] = '\0';
for (i = 0; i < len; ++i) {
fake_token[1] = token[i];
option = find_option(cmd, fake_token + 1);
/* If we don't find the option, either return an error or pass
it through unmodified to the new argv */
if (NULL == option) {
if (!ignore_unknown) {
return OPAL_ERR_BAD_PARAM;
} else {
opal_argv_append(output_argc, output_argv, fake_token);
}
}
/* If we do find the option, copy it and all of its parameters
to the output args. If we run out of parameters (i.e., no
more tokens in the original argv), that error will be
handled at a higher level) */
else {
opal_argv_append(output_argc, output_argv, fake_token);
for (j = 0; j < option->clo_num_params; ++j) {
if (*num_args_used < num_args) {
opal_argv_append(output_argc, output_argv, args[*num_args_used]);
++(*num_args_used);
} else {
opal_argv_append(output_argc, output_argv, special_empty_token);
}
}
}
}
/* All done */
return OPAL_SUCCESS;
}
static ompi_cmd_line_option_t *find_option(opal_cmd_line_t *cmd, const char *option_name)
{
ompi_cmd_line_option_t *option;
/* Iterate through the list of options hanging off the
opal_cmd_line_t and see if we find a match in either the short
or long names */
OPAL_LIST_FOREACH (option, &cmd->lcl_options, ompi_cmd_line_option_t) {
if ((NULL != option->clo_long_name && 0 == strcmp(option_name, option->clo_long_name))
|| (NULL != option->clo_single_dash_name
&& 0 == strcmp(option_name, option->clo_single_dash_name))
|| (strlen(option_name) == 1 && option_name[0] == option->clo_short_name)) {
return option;
}
}
/* Not found */
return NULL;
}
static int set_dest(ompi_cmd_line_option_t *option, char *sval)
{
int ival = atol(sval);
long lval = strtoul(sval, NULL, 10);
size_t i;
/* Set MCA param. We do this in the environment because the MCA
parameter may not have been registered yet -- and if it isn't
registered, we don't really want to register a dummy one
because we don't know what it's type and default value should
be. These are solvable problems (e.g., make a re-registration
overwrite everything), but it's far simpler to just leave the
registered table alone and set an environment variable with the
desired value. The environment variable will get picked up
during a nromal parameter lookup, and all will be well. */
if (NULL != option->clo_mca_param_env_var) {
switch (option->clo_type) {
case OPAL_CMD_LINE_TYPE_STRING:
case OPAL_CMD_LINE_TYPE_INT:
case OPAL_CMD_LINE_TYPE_SIZE_T:
opal_setenv(option->clo_mca_param_env_var, sval, true, &environ);
break;
case OPAL_CMD_LINE_TYPE_BOOL:
opal_setenv(option->clo_mca_param_env_var, "1", true, &environ);
break;
default:
break;
}
}
/* Set variable */
if (NULL != option->clo_variable_dest) {
switch (option->clo_type) {
case OPAL_CMD_LINE_TYPE_STRING:
*((char **) option->clo_variable_dest) = strdup(sval);
break;
case OPAL_CMD_LINE_TYPE_INT:
/* check to see that the value given to us truly is an int */
for (i = 0; i < strlen(sval); i++) {
if (!isdigit(sval[i]) && '-' != sval[i]) {
/* show help isn't going to be available yet, so just
* print the msg
*/
fprintf(stderr, "--------------------------------------------------------------"
"--------------\n");
fprintf(stderr,
"Open MPI has detected that a parameter given to a command line\n");
fprintf(stderr, "option does not match the expected format:\n\n");
if (NULL != option->clo_long_name) {
fprintf(stderr, " Option: %s\n", option->clo_long_name);
} else if ('\0' != option->clo_short_name) {
fprintf(stderr, " Option: %c\n", option->clo_short_name);
} else {
fprintf(stderr, " Option: <unknown>\n");
}
fprintf(stderr, " Param: %s\n\n", sval);
fprintf(stderr,
"This is frequently caused by omitting to provide the parameter\n");
fprintf(stderr, "to an option that requires one. Please check the command line "
"and try again.\n");
fprintf(stderr, "--------------------------------------------------------------"
"--------------\n");
return OPAL_ERR_SILENT;
}
}
*((int *) option->clo_variable_dest) = ival;
break;
case OPAL_CMD_LINE_TYPE_SIZE_T:
/* check to see that the value given to us truly is a size_t */
for (i = 0; i < strlen(sval); i++) {
if (!isdigit(sval[i]) && '-' != sval[i]) {
/* show help isn't going to be available yet, so just
* print the msg
*/
fprintf(stderr, "--------------------------------------------------------------"
"--------------\n");
fprintf(stderr,
"Open MPI has detected that a parameter given to a command line\n");
fprintf(stderr, "option does not match the expected format:\n\n");
if (NULL != option->clo_long_name) {
fprintf(stderr, " Option: %s\n", option->clo_long_name);
} else if ('\0' != option->clo_short_name) {
fprintf(stderr, " Option: %c\n", option->clo_short_name);
} else {
fprintf(stderr, " Option: <unknown>\n");
}
fprintf(stderr, " Param: %s\n\n", sval);
fprintf(stderr,
"This is frequently caused by omitting to provide the parameter\n");
fprintf(stderr, "to an option that requires one. Please check the command line "
"and try again.\n");
fprintf(stderr, "--------------------------------------------------------------"
"--------------\n");
return OPAL_ERR_SILENT;
}
}
*((size_t *) option->clo_variable_dest) = lval;
break;
case OPAL_CMD_LINE_TYPE_BOOL:
*((bool *) option->clo_variable_dest) = 1;
break;
default:
break;
}
}
return OPAL_SUCCESS;
}
/*
* Helper function to qsort_callback
*/
static void fill(const ompi_cmd_line_option_t *a, char result[3][BUFSIZ])
{
int i = 0;
result[0][0] = '\0';
result[1][0] = '\0';
result[2][0] = '\0';
if ('\0' != a->clo_short_name) {
snprintf(&result[i][0], BUFSIZ, "%c", a->clo_short_name);
++i;
}
if (NULL != a->clo_single_dash_name) {
snprintf(&result[i][0], BUFSIZ, "%s", a->clo_single_dash_name);
++i;
}
if (NULL != a->clo_long_name) {
snprintf(&result[i][0], BUFSIZ, "%s", a->clo_long_name);
++i;
}
}
static int qsort_callback(const void *aa, const void *bb)
{
int ret, i;
char str1[3][BUFSIZ], str2[3][BUFSIZ];
const ompi_cmd_line_option_t *a = *((const ompi_cmd_line_option_t **) aa);
const ompi_cmd_line_option_t *b = *((const ompi_cmd_line_option_t **) bb);
/* Icky comparison of command line options. There are multiple
forms of each command line option, so we first have to check
which forms each option has. Compare, in order: short name,
single-dash name, long name. */
fill(a, str1);
fill(b, str2);
for (i = 0; i < 3; ++i) {
if (0 != (ret = strcasecmp(str1[i], str2[i]))) {
return ret;
}
}
/* Shrug -- they must be equal */
return 0;
}
/*
* Helper function to find the option type specified in the help
* command.
*/
static opal_cmd_line_otype_t get_help_otype(opal_cmd_line_t *cmd)
{
/* Initialize to NULL, if it remains so, the user asked for
"full" help output */
opal_cmd_line_otype_t otype = OPAL_CMD_LINE_OTYPE_NULL;
char *arg;
arg = opal_cmd_line_get_param(cmd, "help", 0, 0);
/* If not "help", check for "h" */
if (NULL == arg) {
arg = opal_cmd_line_get_param(cmd, "h", 0, 0);
}
/* If arg is still NULL, give them the General info by default */
if (NULL == arg) {
arg = "general";
}
if (0 == strcmp(arg, "debug")) {
otype = OPAL_CMD_LINE_OTYPE_DEBUG;
} else if (0 == strcmp(arg, "output")) {
otype = OPAL_CMD_LINE_OTYPE_OUTPUT;
} else if (0 == strcmp(arg, "input")) {
otype = OPAL_CMD_LINE_OTYPE_INPUT;
} else if (0 == strcmp(arg, "mapping")) {
otype = OPAL_CMD_LINE_OTYPE_MAPPING;
} else if (0 == strcmp(arg, "ranking")) {
otype = OPAL_CMD_LINE_OTYPE_RANKING;
} else if (0 == strcmp(arg, "binding")) {
otype = OPAL_CMD_LINE_OTYPE_BINDING;
} else if (0 == strcmp(arg, "devel")) {
otype = OPAL_CMD_LINE_OTYPE_DEVEL;
} else if (0 == strcmp(arg, "compatibility")) {
otype = OPAL_CMD_LINE_OTYPE_COMPAT;
} else if (0 == strcmp(arg, "launch")) {
otype = OPAL_CMD_LINE_OTYPE_LAUNCH;
} else if (0 == strcmp(arg, "dvm")) {
otype = OPAL_CMD_LINE_OTYPE_DVM;
} else if (0 == strcmp(arg, "general")) {
otype = OPAL_CMD_LINE_OTYPE_GENERAL;
} else if (0 == strcmp(arg, "parsable")) {
otype = OPAL_CMD_LINE_OTYPE_PARSABLE;
}
return otype;
}
/*
* Helper function to build a parsable string for the help
* output.
*/
static char *build_parsable(ompi_cmd_line_option_t *option)
{
char *line;
int length;
length = snprintf(NULL, 0, "%c:%s:%s:%d:%s\n", option->clo_short_name,
option->clo_single_dash_name, option->clo_long_name, option->clo_num_params,
option->clo_description);
line = (char *) malloc(length * sizeof(char));
if ('\0' == option->clo_short_name) {
snprintf(line, length, "0:%s:%s:%d:%s\n", option->clo_single_dash_name,
option->clo_long_name, option->clo_num_params, option->clo_description);
} else {
snprintf(line, length, "%c:%s:%s:%d:%s\n", option->clo_short_name,
option->clo_single_dash_name, option->clo_long_name, option->clo_num_params,
option->clo_description);
}
return line;
}
|