Make delimiter of multiple attribute value from immlist configurable Update option --pretty-print will not support print with option --atribute --- src/imm/tools/imm_list.c | 113 +++++++++++++++++++++++++++++++---------------- 1 file changed, 74 insertions(+), 39 deletions(-) mode change 100644 => 100755 src/imm/tools/imm_list.c
diff --git a/src/imm/tools/imm_list.c b/src/imm/tools/imm_list.c old mode 100644 new mode 100755 index d1dc422..4326396 --- a/src/imm/tools/imm_list.c +++ b/src/imm/tools/imm_list.c @@ -72,13 +72,18 @@ static void usage(const char *progname) printf("\t-h, --help - display this help and exit\n"); printf( "\t-p, --pretty-print=<yes|no> - select pretty print, default yes\n"); + printf( + "\tdo not support pretty print with option -a, --attribute=NAME\n"); + printf( + "\t-d, --delimiter=<STRING> - format multiple attribute value\n"); printf("\t-t, --timeout <sec>\n"); printf("\t\tutility timeout in seconds\n"); printf("\nEXAMPLE\n"); printf("\timmlist -a saAmfApplicationAdminState safApp=OpenSAF\n"); printf("\timmlist safApp=myApp1 safApp=myApp2\n"); - printf("\timmlist --pretty-print=no -a saAmfAppType safApp=OpenSAF\n"); + printf("\timmlist --pretty-print=no saAmfAppType safApp=OpenSAF\n"); + printf("\timmlist -d 'abc' safApp=OpenSAF\n"); } static void print_attr_value_raw(SaImmValueTypeT attrValueType, @@ -108,6 +113,7 @@ static void print_attr_value_raw(SaImmValueTypeT attrValueType, break; case SA_IMM_ATTR_SANAMET: { SaNameT *myNameT = (SaNameT *)attrValue; + printf("%s", saAisNameBorrow(myNameT)); break; } @@ -117,14 +123,14 @@ static void print_attr_value_raw(SaImmValueTypeT attrValueType, case SA_IMM_ATTR_SAANYT: { SaAnyT *anyp = (SaAnyT *)attrValue; unsigned int i = 0; + if (anyp->bufferSize == 0) { printf("-empty-"); } else { printf("0x"); for (; i < anyp->bufferSize; i++) { - if (((int)anyp->bufferAddr[i]) < 0x10) { + if (((int)anyp->bufferAddr[i]) < 0x10) printf("0"); - } printf("%x", (int)anyp->bufferAddr[i]); } } @@ -141,19 +147,19 @@ static void print_attr_value(SaImmValueTypeT attrValueType, { switch (attrValueType) { case SA_IMM_ATTR_SAINT32T: - printf("%d (0x%x) ", *((SaInt32T *)attrValue), + printf("%d (0x%x)", *((SaInt32T *)attrValue), *((SaInt32T *)attrValue)); break; case SA_IMM_ATTR_SAUINT32T: - printf("%u (0x%x) ", *((SaUint32T *)attrValue), + printf("%u (0x%x)", *((SaUint32T *)attrValue), *((SaUint32T *)attrValue)); break; case SA_IMM_ATTR_SAINT64T: - printf("%lld (0x%llx) ", *((SaInt64T *)attrValue), + printf("%lld (0x%llx)", *((SaInt64T *)attrValue), *((SaInt64T *)attrValue)); break; case SA_IMM_ATTR_SAUINT64T: - printf("%llu (0x%llx) ", *((SaUint64T *)attrValue), + printf("%llu (0x%llx)", *((SaUint64T *)attrValue), *((SaUint64T *)attrValue)); break; case SA_IMM_ATTR_SATIMET: { @@ -163,34 +169,35 @@ static void print_attr_value(SaImmValueTypeT attrValueType, ctime_r(&time, buf); buf[strlen(buf) - 1] = '\0'; /* Remove new line */ - printf("%llu (0x%llx, %s) ", *((SaTimeT *)attrValue), + printf("%llu (0x%llx, %s)", *((SaTimeT *)attrValue), *((SaTimeT *)attrValue), buf); break; } case SA_IMM_ATTR_SAFLOATT: - printf("%.8g ", *((SaFloatT *)attrValue)); + printf("%.8g", *((SaFloatT *)attrValue)); break; case SA_IMM_ATTR_SADOUBLET: - printf("%.17g ", *((SaDoubleT *)attrValue)); + printf("%.17g", *((SaDoubleT *)attrValue)); break; case SA_IMM_ATTR_SANAMET: { SaNameT *myNameT = (SaNameT *)attrValue; - printf("%s (%zu) ", saAisNameBorrow(myNameT), + + printf("%s (%zu)", saAisNameBorrow(myNameT), strlen(saAisNameBorrow(myNameT))); break; } case SA_IMM_ATTR_SASTRINGT: - printf("%s ", *((char **)attrValue)); + printf("%s", *((char **)attrValue)); break; case SA_IMM_ATTR_SAANYT: { SaAnyT *anyp = (SaAnyT *)attrValue; unsigned int i = 0; + if (anyp->bufferSize) { printf("0x"); for (; i < anyp->bufferSize; i++) { - if (((int)anyp->bufferAddr[i]) < 0x10) { + if (((int)anyp->bufferAddr[i]) < 0x10) printf("0"); - } printf("%x", (int)anyp->bufferAddr[i]); } } @@ -209,37 +216,26 @@ static char *get_attr_type_name(SaImmValueTypeT attrValueType) switch (attrValueType) { case SA_IMM_ATTR_SAINT32T: return "SA_INT32_T"; - break; case SA_IMM_ATTR_SAUINT32T: return "SA_UINT32_T"; - break; case SA_IMM_ATTR_SAINT64T: return "SA_INT64_T"; - break; case SA_IMM_ATTR_SAUINT64T: return "SA_UINT64_T"; - break; case SA_IMM_ATTR_SATIMET: return "SA_TIME_T"; - break; case SA_IMM_ATTR_SANAMET: return "SA_NAME_T"; - break; case SA_IMM_ATTR_SAFLOATT: return "SA_FLOAT_T"; - break; case SA_IMM_ATTR_SADOUBLET: return "SA_DOUBLE_T"; - break; case SA_IMM_ATTR_SASTRINGT: return "SA_STRING_T"; - break; case SA_IMM_ATTR_SAANYT: return "SA_ANY_T"; - break; default: return "Unknown"; - break; } } @@ -253,6 +249,7 @@ static void display_class_definition(const SaImmClassNameT className, { SaImmClassCategoryT classCategory; SaAisErrorT error; + SaImmAttrDefinitionT_2 **attrDefinitions; SaImmAttrDefinitionT_2 *attrDefinition; int i; @@ -260,7 +257,7 @@ static void display_class_definition(const SaImmClassNameT className, error = immutil_saImmOmClassDescriptionGet_2( immHandle, className, &classCategory, &attrDefinitions); - if (SA_AIS_OK != error) { + if (error != SA_AIS_OK) { fprintf(stderr, "error - saImmOmClassDescriptionGet_2 FAILED: %s\n", saf_error(error)); @@ -292,14 +289,14 @@ static void display_class_definition(const SaImmClassNameT className, get_attr_type_name(attrDefinition->attrValueType)); // Print multiplicity - if (attrDefinition->attrFlags & SA_IMM_ATTR_INITIALIZED) { + if (attrDefinition->attrFlags & SA_IMM_ATTR_INITIALIZED) printf("[1"); - } else + else printf("[0"); - if (attrDefinition->attrFlags & SA_IMM_ATTR_MULTI_VALUE) { + if (attrDefinition->attrFlags & SA_IMM_ATTR_MULTI_VALUE) printf("..*]"); - } else + else printf("]"); if (attrDefinition->attrFlags & SA_IMM_ATTR_CONFIG) { @@ -404,6 +401,8 @@ static void display_class_definition(const SaImmClassNameT className, static void display_object(const char *name, SaImmAccessorHandleT accessorHandle, int pretty_print, + int delimiter_print, + char *character, const SaImmAttrNameT *attributeNames) { int i = 0, j; @@ -411,12 +410,13 @@ static void display_object(const char *name, SaNameT objectName; SaAisErrorT error; SaImmAttrValuesT_2 **attributes; + char *seperate_character = ""; saAisNameLend(name, &objectName); error = immutil_saImmOmAccessorGet_2(accessorHandle, &objectName, attributeNames, &attributes); - if (SA_AIS_OK != error) { + if (error != SA_AIS_OK) { if (error == SA_AIS_ERR_NOT_EXIST) fprintf(stderr, "error - object or attribute does not exist\n"); @@ -428,6 +428,15 @@ static void display_object(const char *name, exit(EXIT_FAILURE); } + if (delimiter_print) + seperate_character = character; + + if (pretty_print && !delimiter_print) + seperate_character = " "; + + if (!pretty_print && !delimiter_print) + seperate_character = ":"; + if (pretty_print) { printf("%-50s %-12s Value(s)\n", "Name", "Type"); printf( @@ -436,9 +445,13 @@ static void display_object(const char *name, printf("\n%-50s %-12s ", attr->attrName, get_attr_type_name(attr->attrValueType)); if (attr->attrValuesNumber > 0) { - for (j = 0; j < attr->attrValuesNumber; j++) + for (j = 0; j < attr->attrValuesNumber; j++) { print_attr_value(attr->attrValueType, attr->attrValues[j]); + if ((j + 1) < attr->attrValuesNumber) + printf("%s", + seperate_character); + } } else printf("<Empty>"); } @@ -452,7 +465,8 @@ static void display_object(const char *name, attr->attrValueType, attr->attrValues[j]); if ((j + 1) < attr->attrValuesNumber) - printf(":"); + printf("%s", + seperate_character); } printf("\n"); } else @@ -470,6 +484,7 @@ int main(int argc, char *argv[]) {"timeout", required_argument, 0, 't'}, {"help", no_argument, 0, 'h'}, {"pretty-print", required_argument, 0, 'p'}, + {"delimiter", required_argument, 0, 'd'}, {0, 0, 0, 0}}; SaAisErrorT error; SaImmHandleT immHandle; @@ -481,6 +496,9 @@ int main(int argc, char *argv[]) int class_desc_print = 0; int rc = EXIT_SUCCESS; unsigned long timeoutVal = 60; + int delimiter_print = 0; + char *character = ""; + int atribute_set = 0; /* Support for long DN */ setenv("SA_ENABLE_EXTENDED_NAMES", "1", 1); @@ -490,7 +508,7 @@ int main(int argc, char *argv[]) osaf_extended_name_init(); while (1) { - c = getopt_long(argc, argv, "a:p:t:ch", long_options, NULL); + c = getopt_long(argc, argv, "a:d:p:t:ch", long_options, NULL); if (c == -1) /* have all command-line options have been parsed? */ @@ -502,6 +520,7 @@ int main(int argc, char *argv[]) attributeNames, ++len * sizeof(SaImmAttrNameT)); attributeNames[len - 2] = strdup(optarg); attributeNames[len - 1] = NULL; + atribute_set = 1; pretty_print = 0; break; case 'c': @@ -517,6 +536,12 @@ int main(int argc, char *argv[]) case 'p': if (!strcasecmp(optarg, "no")) pretty_print = 0; + else + pretty_print = 1; + break; + case 'd': + character = optarg; + delimiter_print = 1; break; default: fprintf(stderr, @@ -537,6 +562,14 @@ int main(int argc, char *argv[]) goto done; } + if (atribute_set && pretty_print) { + fprintf( + stderr, + "error - disallow --pretty-print=yes with option -a\n"); + rc = EXIT_FAILURE; + goto done; + } + immutilWrapperProfile.errorsAreFatal = 0; immutilWrapperProfile.nTries = timeoutVal; immutilWrapperProfile.retryInterval = 1000; @@ -557,7 +590,7 @@ int main(int argc, char *argv[]) } else { error = immutil_saImmOmAccessorInitialize(immHandle, &accessorHandle); - if (SA_AIS_OK != error) { + if (error != SA_AIS_OK) { fprintf( stderr, "error - saImmOmAccessorInitialize FAILED: %s\n", @@ -567,15 +600,17 @@ int main(int argc, char *argv[]) } /* Remaining arguments should be object names to print - * attributes for. */ + * attributes for. + */ while (optind < argc) { display_object(argv[optind], accessorHandle, - pretty_print, attributeNames); + pretty_print, delimiter_print, + character, attributeNames); optind++; } error = immutil_saImmOmAccessorFinalize(accessorHandle); - if (SA_AIS_OK != error) { + if (error != SA_AIS_OK) { fprintf(stderr, "error - saImmOmAccessorFinalize FAILED: %s\n", saf_error(error)); @@ -586,7 +621,7 @@ int main(int argc, char *argv[]) done_finalize: error = immutil_saImmOmFinalize(immHandle); - if (SA_AIS_OK != error) { + if (error != SA_AIS_OK) { fprintf(stderr, "error - saImmOmFinalize FAILED: %s\n", saf_error(error)); rc = EXIT_FAILURE; -- 2.7.4 _______________________________________________ Opensaf-devel mailing list Opensaf-devel@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/opensaf-devel