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
|
/*
* SNOOPY COMMAND LOGGER
*
* Copyright (c) 2015 Bostjan Skufca Jese <bostjan@a2o.si>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
/*
* Includes order: from local to global
*/
#include "action-run-datasource.h"
#include "action-common.h"
#include "snoopy.h"
#include "entrypoint/test-cli.h"
#include "configuration.h"
#include "error.h"
#include "datasourceregistry.h"
#include "inputdatastorage.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void snoopyTestCli_action_run_datasource_showList ()
{
printf("Available datasources:\n");
int dCount = snoopy_datasourceregistry_getCount();
for (int i=0 ; i<dCount ; i++) {
printf(" %s\n", snoopy_datasourceregistry_getName(i));
}
}
void snoopyTestCli_action_run_datasource_showHelp ()
{
char * helpContent =
"Snoopy TEST SUITE CLI utility :: Action `run` :: Subsystem `datasource`\n"
"\n"
"Usage:\n"
" snoopy-test run datasource DATASOURCE [ARGS]\n"
" snoopy-test run datasource --all # Runs all datasources\n"
" snoopy-test run datasource --help # Shows this help message\n"
" snoopy-test run datasource --list # Lists all available datasources\n"
"\n";
printf("%s", helpContent);
snoopyTestCli_action_run_datasource_showList();
}
int snoopyTestCli_action_run_datasource (int argc, char ** argv)
{
const char *arg1;
const char *datasourceName;
const char *datasourceArg;
char * datasourceResult;
int retVal;
const snoopy_configuration_t *CFG;
/* Initialize Snoopy */
snoopy_entrypoint_test_cli_init((char const *)g_argv[0], g_argv, NULL);
/* Get config pointer */
CFG = snoopy_configuration_get();
/* Check if there is a data source name passed as an argument */
if (argc < 1) {
snoopyTestCli_action_run_datasource_showHelp();
fatalError("Missing argument: `datasource name` or `--list`");
}
arg1 = argv[0];
/* Is second argument --list? */
if (0 == strcmp(arg1, "--all")) {
snoopyTestCli_action_run_datasource_all(CFG->datasource_message_max_length);
return 0;
}
if (0 == strcmp(arg1, "--help")) {
snoopyTestCli_action_run_datasource_showHelp();
return 0;
}
if (0 == strcmp(arg1, "--list")) {
snoopyTestCli_action_run_datasource_showList();
return 0;
}
datasourceName = arg1;
/* Check if what we got is a valid datasource name */
if (SNOOPY_FALSE == snoopy_datasourceregistry_doesNameExist(datasourceName)) {
snoopyTestCli_action_run_datasource_showHelp();
fatalError("Invalid datasource name given");
}
/* Is there an argument for this data source */
if (argc >= 2) {
datasourceArg = argv[1];
} else {
datasourceArg = "";
}
/* Call the datasource */
datasourceResult = malloc(CFG->datasource_message_max_length+1);
retVal = snoopy_datasourceregistry_callByName(datasourceName, datasourceResult, CFG->datasource_message_max_length+1, datasourceArg);
if (SNOOPY_DATASOURCE_FAILED(retVal)) {
fatalErrorValueFree("Datasource failed", datasourceResult);
}
/* Display */
printf("%s\n", datasourceResult);
/* Housekeeping and return */
free(datasourceResult);
snoopy_entrypoint_test_cli_exit();
return 0;
}
void snoopyTestCli_action_run_datasource_all (size_t datasource_message_max_length)
{
char *itemName = NULL;
const char *itemArgs = NULL;
char *itemResult = NULL;
size_t itemResultBufSize;
size_t itemResultSize;
int dCount;
/* Initialize variables and spaces */
itemResultBufSize = datasource_message_max_length + 1;
itemResult = malloc(itemResultBufSize);
/* Loop through all datasources and just send to output */
dCount = snoopy_datasourceregistry_getCount();
for (int i=0 ; i<dCount ; i++) {
itemName = snoopy_datasourceregistry_getName(i);
printf("Datasource %15s: ", itemName);
/* Which arguments to pass to data source */
if (strcmp(itemName, "env") == 0) {
itemArgs = "HOME";
} else if (strcmp(itemName, "snoopy_literal") == 0) {
itemArgs = "somestring";
} else {
itemArgs = "";
}
/* Execute the data source function */
itemResultSize = snoopy_datasourceregistry_callById(i, itemResult, itemResultBufSize, itemArgs);
if (itemResultSize > itemResultBufSize) {
snoopy_error_handler("Maximum data source message size exceeded");
}
/* Copy content, append */
printf("%s\n", itemResult);
}
/* Memory housekeeping */
free(itemResult);
}
|