/* * ok = createStringmaps(); * * Create the string-maps to assist in parsing the --keys and * --counters switches. */ static int createStringmaps( void) { sk_stringmap_t *map; sk_stringmap_status_t sm_err; sk_stringmap_entry_t sm_entry; sk_aggbag_type_iter_t iter; sk_aggbag_type_t type; const char *names[] = {"key", "counter"}; unsigned int i; memset(&sm_entry, 0, sizeof(sm_entry)); for (i = 0; i < 2; ++i) { /* create the string-map of field identifiers */ if (0 == i) { sm_err = skStringMapCreate(&key_name_map); map = key_name_map; } else { sm_err = skStringMapCreate(&counter_name_map); map = counter_name_map; } if (sm_err) { skAppPrintErr("Unable to create string map for %ss", names[i]); return -1; } skAggBagFieldTypeIteratorBind( &iter, ((0 == i) ? SK_AGGBAG_KEY : SK_AGGBAG_COUNTER)); while ((sm_entry.name = skAggBagFieldTypeIteratorNext(&iter, &type)) != NULL) { sm_entry.id = type; sm_err = skStringMapAddEntries(map, 1, &sm_entry); if (sm_err) { skAppPrintErr("Unable to add %s field named '%s': %s", names[i], sm_entry.name, skStringMapStrerror(sm_err)); return -1; } if (SKAGGBAG_FIELD_NHIPv6 == type || SKAGGBAG_FIELD_SUM_ELAPSED == type) { break; } } } return 0; }
/* * status = timestampFormatParse(format_string, out_flags); * * Parse the comma-separated list of timestamp format strings * contained in 'format_string' and set 'out_flags' to the result * of parsing the string. Return 0 on success, or -1 if parsing of * the values fails or conflicting values are given. */ static int timestampFormatParse( const char *format, uint32_t *out_flags) { char buf[256]; char *errmsg; sk_stringmap_t *str_map = NULL; sk_stringmap_iter_t *iter = NULL; sk_stringmap_entry_t *found_entry; const sk_stringmap_entry_t *entry; int name_seen = 0; int zone_seen = 0; int rv = -1; /* create a stringmap of the available timestamp formats */ if (SKSTRINGMAP_OK != skStringMapCreate(&str_map)) { skAppPrintOutOfMemory(NULL); goto END; } if (skStringMapAddEntries(str_map, -1, timestamp_names) != SKSTRINGMAP_OK){ skAppPrintOutOfMemory(NULL); goto END; } if (skStringMapAddEntries(str_map, -1, timestamp_zones) != SKSTRINGMAP_OK){ skAppPrintOutOfMemory(NULL); goto END; } /* attempt to match */ if (skStringMapParse(str_map, format, SKSTRINGMAP_DUPES_ERROR, &iter, &errmsg)) { skAppPrintErr("Invalid %s: %s", appOptions[OPT_TIMESTAMP_FORMAT].name, errmsg); goto END; } *out_flags = 0; while (skStringMapIterNext(iter, &found_entry, NULL) == SK_ITERATOR_OK) { *out_flags |= found_entry->id; switch (found_entry->id) { #if 0 case SKTIMESTAMP_NOMSEC: break; #endif case 0: case SKTIMESTAMP_EPOCH: case SKTIMESTAMP_ISO: case SKTIMESTAMP_MMDDYYYY: if (name_seen) { entry = timestamp_names; strncpy(buf, entry->name, sizeof(buf)); for (++entry; entry->name; ++entry) { strncat(buf, ",", sizeof(buf)-strlen(buf)-1); strncat(buf, entry->name, sizeof(buf)-strlen(buf)-1); } skAppPrintErr("Invalid %s: May only specify one of %s", appOptions[OPT_TIMESTAMP_FORMAT].name, buf); goto END; } name_seen = 1; break; case SKTIMESTAMP_UTC: case SKTIMESTAMP_LOCAL: if (zone_seen) { entry = timestamp_zones; strncpy(buf, entry->name, sizeof(buf)); for (++entry; entry->name; ++entry) { strncat(buf, ",", sizeof(buf)-strlen(buf)-1); strncat(buf, entry->name, sizeof(buf)-strlen(buf)-1); } skAppPrintErr("Invalid %s: May only specify one of %s", appOptions[OPT_TIMESTAMP_FORMAT].name, buf); goto END; } zone_seen = 1; break; default: skAbortBadCase(found_entry->id); } } rv = 0; END: if (str_map) { skStringMapDestroy(str_map); } if (iter) { skStringMapIterDestroy(iter); } return rv; }
/* * status = loadschemeParse(scheme_name, load_scheme); * * Parse the load-scheme name in 'scheme_name' and set * 'load_scheme' to the result of parsing the string. Return 0 on * success, or -1 if parsing of the value fails. */ static int loadschemeParse( const char *scheme_name, bin_load_scheme_enum_t *load_scheme) { char buf[128]; sk_stringmap_entry_t new_entry; sk_stringmap_t *str_map = NULL; sk_stringmap_status_t sm_err; sk_stringmap_entry_t *sm_entry; const sk_stringmap_entry_t *e; int rv = -1; memset(&new_entry, 0, sizeof(new_entry)); new_entry.name = buf; /* create a stringmap of the available load-scheme names */ if (SKSTRINGMAP_OK != skStringMapCreate(&str_map)) { skAppPrintOutOfMemory(NULL); goto END; } if (skStringMapAddEntries(str_map, -1, load_schemes) != SKSTRINGMAP_OK){ skAppPrintOutOfMemory(NULL); goto END; } /* allow the integer ID of each load-scheme to work */ for (e = load_schemes; e->name; ++e) { new_entry.id = e->id; snprintf(buf, sizeof(buf), "%u", e->id); if (skStringMapAddEntries(str_map, 1, &new_entry) != SKSTRINGMAP_OK) { skAppPrintOutOfMemory(NULL); goto END; } } /* attempt to match */ sm_err = skStringMapGetByName(str_map, scheme_name, &sm_entry); switch (sm_err) { case SKSTRINGMAP_OK: *load_scheme = (bin_load_scheme_enum_t)sm_entry->id; rv = 0; break; case SKSTRINGMAP_PARSE_AMBIGUOUS: skAppPrintErr("Invalid %s: '%s' is ambiguous", appOptions[OPT_LOAD_SCHEME].name, scheme_name); break; case SKSTRINGMAP_PARSE_NO_MATCH: skAppPrintErr("Invalid %s: '%s' is not recognized", appOptions[OPT_LOAD_SCHEME].name, scheme_name); break; default: skAppPrintErr("Unexpected return value from string-map parser (%d)", sm_err); break; } END: if (str_map) { skStringMapDestroy(str_map); } return rv; }