/* ** Copyright (C) 2001-2007 by Carnegie Mellon University. ** ** @OPENSOURCE_HEADER_START@ ** ** Use of the SILK system and related source code is subject to the terms ** of the following licenses: ** ** GNU Public License (GPL) Rights pursuant to Version 2, June 1991 ** Government Purpose License Rights (GPLR) pursuant to DFARS 252.225-7013 ** ** NO WARRANTY ** ** ANY INFORMATION, MATERIALS, SERVICES, INTELLECTUAL PROPERTY OR OTHER ** PROPERTY OR RIGHTS GRANTED OR PROVIDED BY CARNEGIE MELLON UNIVERSITY ** PURSUANT TO THIS LICENSE (HEREINAFTER THE "DELIVERABLES") ARE ON AN ** "AS-IS" BASIS. CARNEGIE MELLON UNIVERSITY MAKES NO WARRANTIES OF ANY ** KIND, EITHER EXPRESS OR IMPLIED AS TO ANY MATTER INCLUDING, BUT NOT ** LIMITED TO, WARRANTY OF FITNESS FOR A PARTICULAR PURPOSE, ** MERCHANTABILITY, INFORMATIONAL CONTENT, NONINFRINGEMENT, OR ERROR-FREE ** OPERATION. CARNEGIE MELLON UNIVERSITY SHALL NOT BE LIABLE FOR INDIRECT, ** SPECIAL OR CONSEQUENTIAL DAMAGES, SUCH AS LOSS OF PROFITS OR INABILITY ** TO USE SAID INTELLECTUAL PROPERTY, UNDER THIS LICENSE, REGARDLESS OF ** WHETHER SUCH PARTY WAS AWARE OF THE POSSIBILITY OF SUCH DAMAGES. ** LICENSEE AGREES THAT IT WILL NOT MAKE ANY WARRANTY ON BEHALF OF ** CARNEGIE MELLON UNIVERSITY, EXPRESS OR IMPLIED, TO ANY PERSON ** CONCERNING THE APPLICATION OF OR THE RESULTS TO BE OBTAINED WITH THE ** DELIVERABLES UNDER THIS LICENSE. ** ** Licensee hereby agrees to defend, indemnify, and hold harmless Carnegie ** Mellon University, its trustees, officers, employees, and agents from ** all claims or demands made against them (and any related losses, ** expenses, or attorney's fees) arising out of, or relating to Licensee's ** and/or its sub licensees' negligent use or willful misuse of or ** negligent conduct or willful misconduct regarding the Software, ** facilities, or other rights or assistance granted by Carnegie Mellon ** University under this License, including, but not limited to, any ** claims of product liability, personal injury, death, damage to ** property, or violation of any laws or regulations. ** ** Carnegie Mellon University Software Engineering Institute authored ** documents are sponsored by the U.S. Department of Defense under ** Contract F19628-00-C-0003. Carnegie Mellon University retains ** copyrights in all material produced under this contract. The U.S. ** Government retains a non-exclusive, royalty-free license to publish or ** reproduce these documents, or allow others to do so, for U.S. ** Government purposes only pursuant to the copyright license under the ** contract clause at 252.227.7013. ** ** @OPENSOURCE_HEADER_END@ */ /* ** mapsid.c ** dump a map of sid integers to names from silk_site.h */ #include "silk.h" RCSIDENT("$SiLK: mapsid.c 6947 2007-04-17 21:12:48Z mthomas $"); #include "utils.h" #include "sksite.h" /* LOCAL DEFINES AND TYPEDEFS */ /* where to write --help output */ #define USAGE_FH stdout /* where to send output */ #define OUT_FH stdout /* LOCAL FUNCTIONS */ static void appUsageLong(void); static int appOptionsHandler(clientData cData, int opt_index, char *opt_arg); /* LOCAL VARIABLES */ /* whether to print the classes for each sensor */ static int print_classes = 0; /* OPTIONS SETUP */ typedef enum { OPT_PRINT_CLASSES } appOptionsEnum; static struct option appOptions[] = { {"print-classes", NO_ARG, 0, OPT_PRINT_CLASSES}, {0,0,0,0} /* sentinel entry */ }; static const char *appHelp[] = { ("Print the name of the class(es) that each sensor\n" "\tcollects data for. Def. No"), (char *)NULL }; /* FUNCTION DEFINITIONS */ /* * appUsageLong(); * * Print complete usage information to USAGE_FH. Pass this * function to skOptionsSetUsageCallback(); optionsParse() will * call this funciton and then exit the program when the --help * option is given. */ static void appUsageLong(void) { #define USAGE_MSG \ ("[SWITCHES] [SENSORS]\n" \ "\tMaps between sensor names and sensor IDs. Prints a list of\n" \ "\tall sensors when no command line arguments are given.\n") FILE *fh = USAGE_FH; skAppStandardUsage(fh, USAGE_MSG, appOptions, appHelp); sksiteOptionsUsage(fh); } /* * status = appOptionsHandler(cData, opt_index, opt_arg); * * This function is passed to optionsRegister(); it will be called * by optionsParse() for each user-specified switch that the * application has registered; it should handle the switch as * required---typically by setting global variables---and return 1 * if the switch processing failed or 0 if it succeeded. Returning * a non-zero from from the handler causes optionsParse() to return * a negative value. * * The clientData in 'cData' is typically ignored; 'opt_index' is * the index number that was specified as the last value for each * struct option in appOptions[]; 'opt_arg' is the user's argument * to the switch for options that have a REQUIRED_ARG or an * OPTIONAL_ARG. */ static int appOptionsHandler( clientData UNUSED(cData), int opt_index, char UNUSED(*opt_arg)) { switch ((appOptionsEnum)opt_index) { case OPT_PRINT_CLASSES: print_classes = 1; break; } return 0; /* OK */ } /* * printSensorClasses(sid); * * Print as a comma separated list the names of the classes that * the sensor 'sid' collects data for. */ static void printSensorClasses(sensorID_t sid) { char class_name[SK_MAX_STRLEN_FLOWTYPE+1]; int class_count = 0; class_iter_t ci; classID_t clid; sksiteSensorClassIterator(sid, &ci); while (sksiteClassIteratorNext(&ci, &clid)) { sksiteClassGetName(class_name, sizeof(class_name), clid); if (class_count == 0) { fprintf(OUT_FH, " [%s", class_name); } else { fprintf(OUT_FH, ",%s", class_name); } ++class_count; } fprintf(OUT_FH, "]"); } /* * printByNameOrNumber(sensor); * * Look up the sensor that has the name or the ID specified in the * string 'sensor' and print it to OUT_FH. Also print its * class(es) if requested. */ static void printByNameOrNumber(const char *sensor) { int err; uint32_t temp; sensorID_t sid; int count; sensor_iter_t si; char sensor_name[SK_MAX_STRLEN_SENSOR+1]; /* try to parse as a number */ err = skStringParseUint32(&temp, sensor, 0, SK_INVALID_SENSOR-1); if (err == -1 || err == -2) { skAppPrintErr("No name or number was provided."); return; } if (err == -4) { skAppPrintErr(("number '%s' is not a valid sensor number:" " too large to parse"), sensor); return; } if (err == -12) { skAppPrintErr("number '%s' is not a valid sensor number: out of range", sensor); return; } if (err == 0) { /* got a clean parse */ sid = (sensorID_t)temp; if ( !sksiteSensorExists(sid) ) { skAppPrintErr("number '%s' is not a valid sensor number", sensor); return; } sksiteSensorGetName(sensor_name, sizeof(sensor_name), sid); fprintf(OUT_FH, "%5u -> %s", sid, sensor_name); if (print_classes) { printSensorClasses(sid); } fprintf(OUT_FH, "\n"); return; } /* didn't get a clean parse. try to treat as a name */ sid = sksiteSensorLookup(sensor); if (sid != SK_INVALID_SENSOR) { sksiteSensorGetName(sensor_name, sizeof(sensor_name), sid); fprintf(OUT_FH, "%s -> %5u", sensor_name, sid); if (print_classes) { printSensorClasses(sid); } fprintf(OUT_FH, "\n"); return; } /* try a case-insensitive search, manually iterativing over all * the sensors */ count = 0; sksiteSensorIterator(&si); while (sksiteSensorIteratorNext(&si, &sid)) { sksiteSensorGetName(sensor_name, sizeof(sensor_name), sid); if (0 == strcasecmp(sensor_name, sensor)) { ++count; fprintf(OUT_FH, "%s -> %5u", sensor_name, sid); if (print_classes) { printSensorClasses(sid); } fprintf(OUT_FH, "\n"); } } if (count == 0) { skAppPrintErr("name '%s' is not a valid sensor name", sensor); } } /* * printAllSensors(); * * Print all sensor IDs and Names to the OUT_FH. Also, print class * list if requested. */ static void printAllSensors(void) { sensor_iter_t si; sensorID_t sid; int sensor_count = 0; char sensor_name[SK_MAX_STRLEN_SENSOR+1]; int sensor_maxlen = -1 * sksiteSensorGetMaxNameStrLen(); sksiteSensorIterator(&si); while ( sksiteSensorIteratorNext(&si, &sid) ) { sksiteSensorGetName(sensor_name, sizeof(sensor_name), sid); fprintf(OUT_FH, "%5u -> %*s", sid, sensor_maxlen, sensor_name); if (print_classes) { printSensorClasses(sid); } fprintf(OUT_FH, "\n"); sensor_count++; } fprintf(OUT_FH, "Total sensors %u\n", sensor_count); } int main(int argc, char **argv) { int arg_index; /* verify same number of options and help strings */ assert((sizeof(appHelp)/sizeof(char *)) == (sizeof(appOptions)/sizeof(struct option))); /* register the application */ skAppRegister(argv[0]); skOptionsSetUsageCallback(&appUsageLong); /* register the options */ if (optionsRegister(appOptions, (optHandler)appOptionsHandler, NULL) || sksiteOptionsRegister(SK_SITE_FLAG_CONFIG_FILE)) { skAppPrintErr("unable to register options"); exit(EXIT_FAILURE); } /* parse the options */ arg_index = optionsParse(argc, argv); if (arg_index < 0) { /* options parsing should print error */ skAppUsage(); /* never returns */ } /* ensure the site config is available */ if (sksiteConfigure(1)) { exit(EXIT_FAILURE); } if (argc > arg_index) { for ( ; arg_index < argc; ++arg_index) { printByNameOrNumber(argv[arg_index]); } } else { /* no args. dump all */ printAllSensors(); } skAppUnregister(); return 0; } /* ** Local Variables: ** mode:c ** indent-tabs-mode:nil ** c-basic-offset:4 ** End: */