//-------------------------------------------------------------------------------------------------- static void DeleteXAttr ( void ) { const char* namePtr = le_arg_GetArg(1); if (namePtr == NULL) { fprintf(stderr, "Please specify an extended attribute name.\n"); PrintHelp(); exit(EXIT_FAILURE); } const char* pathPtr = le_arg_GetArg(2); if (pathPtr == NULL) { fprintf(stderr, "Please specify a file or directory.\n"); PrintHelp(); exit(EXIT_FAILURE); } if (removexattr(pathPtr, namePtr) == -1) { fprintf(stderr, "Could not delete extended attribute. %m\n"); exit(EXIT_FAILURE); } }
// ------------------------------------------------------------------------------------------------- static int HandleCommand ( int numArgs ) // ------------------------------------------------------------------------------------------------- { const char* command = le_arg_GetArg(0); if (command == NULL) { fprintf(stderr, "Internal Error: Argument processor failed to return argument.\n"); return EXIT_FAILURE; } // If the user is asking for help, give it to them now. if ( (strcmp(command, "help") == 0) || (strcmp(command, "--help") == 0) || (strcmp(command, "-h") == 0)) { PrintHelp(); return EXIT_SUCCESS; } // From here on in, we only have commands that need two args, so if we don't have that, it's a // problem. if (numArgs != 2) { fprintf(stderr, "Wrong number of arguments.\n"); PrintHelp(); return EXIT_FAILURE; } // Get the name of the kernel module we're working with. const char* kmodName = le_arg_GetArg(1); if (kmodName == NULL) { fprintf(stderr, "Internal Error: Argument processor failed to return argument.\n"); return EXIT_FAILURE; } // Are we handling a load? if (strcmp(command, "load") == 0) { return CallApi(command, kmodName, &le_kernelModule_Load); } // How about an unload? if (strcmp(command, "unload") == 0) { return CallApi(command, kmodName, &le_kernelModule_Unload); } // The command wasn't handled, so report this, print the help and exit. fprintf(stderr, "Unrecognized command, '%s'.\n", command); PrintHelp(); return EXIT_FAILURE; }
//-------------------------------------------------------------------------------------------------- static const char* GetExecPath ( size_t* indexPtr ///< [OUT] Index of the exec path argument on the command line. ) { // The executable path is the first argument after the list of options. Search for the exec // path starting from the second argument. size_t i = 1; while (1) { const char* argPtr = le_arg_GetArg(i); if (argPtr == NULL) { fprintf(stderr, "Please specify an executable.\n"); exit(EXIT_FAILURE); } if (argPtr[0] != '-') { // This is the executable path. *indexPtr = i; return argPtr; } i++; } }
//-------------------------------------------------------------------------------------------------- static const char* GetAppName ( void ) { // The app name should be the first argument on the command-line. const char* appNamePtr = le_arg_GetArg(0); if (appNamePtr == NULL) { fprintf(stderr, "Please specify an application.\n"); exit(EXIT_FAILURE); } if ( (strcmp(appNamePtr, "--help") == 0) || (strcmp(appNamePtr, "-h") == 0) ) { PrintHelp(); } if (appNamePtr[0] == '-') { fprintf(stderr, "Please specify an application. Application name cannot start with '-'.\n"); exit(EXIT_FAILURE); } return appNamePtr; }
static void TestArgs(void) { const char* arg; LE_ASSERT(NULL != (arg = le_arg_GetArg(0))); LE_DEBUG("First arg is '%s'", arg); LE_ASSERT(strcmp(arg, "param1") == 0); LE_ASSERT(NULL != (arg = le_arg_GetArg(1))); LE_DEBUG("Second arg is '%s'", arg); LE_ASSERT(strcmp(arg, "param 2") == 0); LE_ASSERT(NULL != (arg = le_arg_GetArg(2))); LE_DEBUG("Third arg is '%s'", arg); LE_ASSERT(strcmp(arg, "param") == 0); LE_ASSERT(NULL != (arg = le_arg_GetArg(3))); LE_DEBUG("Fourth arg is '%s'", arg); LE_ASSERT(strcmp(arg, "3") == 0); LE_ASSERT(NULL == (arg = le_arg_GetArg(4))); }
//-------------------------------------------------------------------------------------------------- static void SetXAttr ( void ) { const char* namePtr = le_arg_GetArg(1); if (namePtr == NULL) { fprintf(stderr, "Please specify an extended attribute name.\n"); PrintHelp(); exit(EXIT_FAILURE); } const char* valuePtr = le_arg_GetArg(2); if (valuePtr == NULL) { fprintf(stderr, "Please specify an extended attribute value.\n"); PrintHelp(); exit(EXIT_FAILURE); } const char* pathPtr = le_arg_GetArg(3); if (pathPtr == NULL) { fprintf(stderr, "Please specify a file or directory.\n"); PrintHelp(); exit(EXIT_FAILURE); } if (setxattr(pathPtr, namePtr, valuePtr, strlen(valuePtr), 0) == -1) { fprintf(stderr, "Could not set extended attribute. %m\n"); exit(EXIT_FAILURE); } }
//-------------------------------------------------------------------------------------------------- static void PrintXAttrs ( void ) { const char* pathPtr = le_arg_GetArg(1); if (pathPtr == NULL) { fprintf(stderr, "Please specify a file or directory.\n"); PrintHelp(); exit(EXIT_FAILURE); } // Get the list of extended attributes for the object. char nameBuf[MAX_XATTR_LEN]; ssize_t listLen = listxattr(pathPtr, nameBuf, sizeof(nameBuf)); if (listLen < 0) { fprintf(stderr, "Could not read list of extended attributes. %m.\n"); exit(EXIT_FAILURE); } // Iterate through the list of attributes. int i; for (i = 0; i < listLen; i += strlen(&(nameBuf[i])) + 1) { char* namePtr = &(nameBuf[i]); // Get the value. char value[LIMIT_MAX_PATH_BYTES]; ssize_t valueLen = getxattr(pathPtr, namePtr, value, sizeof(value)); if (valueLen < 0) { fprintf(stderr, "Could not read extended attribute value for '%s'. %m.\n", namePtr); exit(EXIT_FAILURE); } // Print both the name and value. printf(" name=%s; value=%.*s\n", namePtr, (int)valueLen, value); } }
//-------------------------------------------------------------------------------------------------- void cm_adc_ProcessAdcCommand ( const char * command, ///< [IN] Command size_t numArgs ///< [IN] Number of arguments ) { if (strcmp(command, "help") == 0) { cm_adc_PrintAdcHelp(); } else if (strcmp(command, "list") == 0) { cm_adc_List(); } else if (strcmp(command, "read") == 0) { const char* channelName; if(numArgs < 3) { printf("adc read requires a channel name\n"); exit(EXIT_FAILURE); } else if (numArgs > 3) { printf("adc read extra arguments will be ignored\n"); } channelName = le_arg_GetArg(2); if(LE_OK != cm_adc_ReadAndPrintValue(channelName)) { printf("Read %s failed.\n", channelName); exit(EXIT_FAILURE); } } else { printf("Invalid command for adc service.\n"); exit(EXIT_FAILURE); } exit(EXIT_SUCCESS); }
//-------------------------------------------------------------------------------------------------- static const char* GetOptionValue ( const char* option, ///< [IN] The option. size_t startIndex, ///< [IN] Index of the first argument to search from. size_t endIndex ///< [IN] Index of the last argument to search to. ) { // Make the string to search for. char searchStr[LIMIT_MAX_ARGS_STR_BYTES]; int n = snprintf(searchStr, sizeof(searchStr), "%s=", option); if ( (n < 0) || (n >= sizeof(searchStr)) ) { INTERNAL_ERR("Option string is too long."); } // Search the list of command line arguments for the specified option. size_t i = startIndex; for (; i <= endIndex; i++) { const char* argPtr = le_arg_GetArg(i); INTERNAL_ERR_IF(argPtr == NULL, "Wrong number of arguments."); char* subStr = strstr(argPtr, searchStr); if ( (subStr != NULL) && (subStr == argPtr) ) { // The argPtr begins with the searchStr. The remainder of the argBuf is the value string. const char* valueStr = argPtr + strlen(searchStr); return valueStr; } } return NULL; }
//-------------------------------------------------------------------------------------------------- void _le_test_Init ( void ) { Mutex = le_mutex_CreateNonRecursive("UnitTestMutex"); NumFailures = 0; PassThrough = false; int i = le_arg_NumArgs() - 1; for (; i >= 0; i--) { char buf[sizeof(PassThroughArgLongForm)]; // Check the command line arguments for the PassThroughArgs strings. if ( (le_arg_GetArg(i, buf, sizeof(PassThroughArgLongForm)) == LE_OK) && ((strcmp(buf, PassThroughArg) == 0) || (strcmp(buf, PassThroughArgLongForm) == 0)) ) { PassThrough = true; } } }
//-------------------------------------------------------------------------------------------------- static bool GetFlagArg ( const char* flag, ///< [IN] The flag to find size_t startIndex, ///< [IN] Index of the first argument to search from. size_t endIndex ///< [IN] Index of the last argument to search to. ) { // Search the list of command line arguments for the specified option. size_t i = startIndex; for (; i <= endIndex; i++) { const char* argPtr = le_arg_GetArg(i); INTERNAL_ERR_IF(argPtr == NULL, "Wrong number of arguments."); if (strcmp(argPtr, flag) == 0) { return true; } } return false; }
//-------------------------------------------------------------------------------------------------- void cm_mrc_ProcessRadioCommand ( const char * command, ///< [IN] Radio command size_t numArgs ///< [IN] Number of arguments ) { if (strcmp(command, "help") == 0) { cm_mrc_PrintRadioHelp(); exit(EXIT_SUCCESS); } else if (strcmp(command, "status") == 0) { exit(cm_mrc_GetModemStatus()); } else if (strcmp(command, "on") == 0) { exit(cm_mrc_SetRadioPower(LE_ON)); } else if (strcmp(command, "off") == 0) { exit(cm_mrc_SetRadioPower(LE_OFF)); } else if (strcmp(command, "rat") == 0) { if (cm_cmn_CheckEnoughParams(1, numArgs, "RAT value missing. e.g. cm radio" " rat <[CDMA] [GSM] [UMTS] [LTE]> or <AUTO>")) { le_mrc_RatBitMask_t rat = 0; const char* ratStrPtr; int index; for (index = 2 ; index < numArgs ; index++) { ratStrPtr = le_arg_GetArg(index); LE_DEBUG("Args (%d) => '%s'",index, ratStrPtr); if (strcmp(ratStrPtr, "AUTO") == 0) { if(cm_mrc_SetRat(LE_MRC_BITMASK_RAT_ALL) == LE_OK) { exit(EXIT_SUCCESS); } else { LE_ERROR("Failed to set LE_MRC_BITMASK_RAT_ALL rat value"); printf("Failed to set LE_MRC_BITMASK_RAT_ALL rat value\n"); exit(EXIT_FAILURE); } } else if (strcmp(ratStrPtr, "CDMA") == 0) { rat |= LE_MRC_BITMASK_RAT_CDMA; } else if (strcmp(ratStrPtr, "GSM") == 0) { rat |= LE_MRC_BITMASK_RAT_GSM; } else if (strcmp(ratStrPtr, "LTE") == 0) { rat |= LE_MRC_BITMASK_RAT_LTE; } else if (strcmp(ratStrPtr, "UMTS") == 0) { rat |= LE_MRC_BITMASK_RAT_UMTS; } else { LE_ERROR("INVALID RAT option!!"); printf("INVALID RAT option!!\n"); exit(EXIT_FAILURE); } } if(cm_mrc_SetRat(rat) == LE_OK) { exit(EXIT_SUCCESS); } LE_ERROR("Failed to set rat value"); printf("Failed to set rat value\n"); } exit(EXIT_FAILURE); } else { printf("Invalid command for radio service.\n"); exit(EXIT_FAILURE); } }