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 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377
|
/*
acsadmin.c: Aggregate Custody Signal adminstration interface.
Authors: Andrew Jenkins, Sebastian Kuzminsky,
University of Colorado at Boulder
Based on bpadmin.c, by Scott Burleigh/JPL
Copyright (c) 2008-2011, Regents of the University of Colorado.
This work was supported by NASA contracts NNJ05HE10G, NNC06CB40C, and
NNC07CB47C.
*/
#include "acs.h"
static int echo = 0;
static void printText(const char *text)
{
char *textScratch = strdup(text);
if (echo && textScratch != NULL)
{
writeMemo(textScratch);
}
free(textScratch);
puts(text);
}
static void handleQuit()
{
printText("Please enter command 'q' to stop the program.");
}
static void printSyntaxError(int lineNbr)
{
char buffer[80];
isprintf(buffer, sizeof buffer, "Syntax error at line %d of bpadmin.c", lineNbr);
printText(buffer);
}
#define SYNTAX_ERROR { printSyntaxError(__LINE__); return; }
static void printUsage()
{
puts("Valid commands are:");
puts("\tq\tQuit");
puts("\th\tHelp");
puts("\t?\tHelp");
puts("\tv\tPrint version of ION.");
puts("\t1\tInitialize");
puts("\t 1 <logLevel> [<heapWords>]");
puts("\ta\tAdd custodian information");
puts("\t a <custodianEid> <acsSize> [<acsDelay>]");
puts("\ts\tSet minimum custody ID");
puts("\t s <minimumCustodyId>");
puts("\te\tEnable or disable echo of printed output to log file");
puts("\t e { 0 | 1 }");
puts("\t#\tComment");
puts("\t # <comment text, ignored by the program>");
}
static void initializeAcs(int tokenCount, char **tokens)
{
long heapWordsRequested;
int logLevelRequested;
if (tokenCount < 2)
{
SYNTAX_ERROR;
}
logLevelRequested = atoi(tokens[1]);
if (tokenCount == 2)
{
heapWordsRequested = 0; /* means "use default" */
} else {
heapWordsRequested = atol(tokens[2]);
}
if (acsInitialize(heapWordsRequested, logLevelRequested) < 0)
{
printText("Couldn't initialize ACS");
return;
}
}
static void executeList(int tokenCount, char **tokens)
{
if (acsAttach() < 0) return;
listCustodianInfo(printText);
}
static void executeAdd(int tokenCount, char **tokens)
{
long acsSize;
long acsDelay;
char text[200];
if (acsAttach() < 0) return;
if (tokenCount < 2)
{
printText("Add what?");
return;
}
if (tokenCount < 3)
{
printText("Does custodian support ACS?");
return;
}
acsSize = atol(tokens[2]);
if (acsSize < 0)
{
printText("acsSize must be >=0");
return;
}
updateCustodianAcsSize(tokens[1], (unsigned long)(acsSize));
if (tokenCount > 3)
{
acsDelay = atol(tokens[3]);
if(acsDelay < 0) {
printText("acsDelay must be non-negative, or not specified.");
return;
}
updateCustodianAcsDelay(tokens[1], (unsigned long)(acsDelay));
isprintf( text, sizeof text, "added acs rule; %s %ld %ld", tokens[1], acsSize, acsDelay );
}
else
isprintf( text, sizeof text, "added acs rule; %s %ld (no delay)", tokens[1], acsSize );
writeMemo( text );
}
static void executeSetMinimumCustodyId(int tokenCount, char **tokens)
{
long minimumCustodyId;
if (acsAttach() < 0) return;
if (tokenCount < 2)
{
printText("Set minimum custody ID to what?");
return;
}
minimumCustodyId = atol(tokens[1]);
if (minimumCustodyId < 0)
{
printText("minimumCustodyId must be positive");
return;
}
updateMinimumCustodyId(minimumCustodyId);
}
static void switchEcho(int tokenCount, char **tokens)
{
if (tokenCount < 2)
{
printText("Echo on or off?");
return;
}
switch (*(tokens[1]))
{
case '0':
echo = 0;
break;
case '1':
echo = 1;
break;
default:
printText("Echo on or off?");
}
}
static int acsadmin_processLine(char *line)
{
int lineLength;
int tokenCount;
char *cursor;
int i;
char *tokens[9];
char buffer[80];
if (line == NULL) return 0;
lineLength = strlen(line);
if (lineLength <= 0) return 0;
if (line[lineLength - 1] == 0x0a) /* LF (newline) */
{
line[lineLength - 1] = '\0'; /* lose it */
lineLength--;
if (lineLength <= 0) return 0;
}
if (line[lineLength - 1] == 0x0d) /* CR (DOS text) */
{
line[lineLength - 1] = '\0'; /* lose it */
lineLength--;
if (lineLength <= 0) return 0;
}
tokenCount = 0;
for (cursor = line, i = 0; i < 9; i++)
{
if (*cursor == '\0')
{
tokens[i] = NULL;
}
else
{
findToken(&cursor, &(tokens[i]));
tokenCount++;
}
}
if (tokenCount == 0)
{
return 0;
}
/* Skip over any trailing whitespace. */
while (isspace((int) *cursor))
{
cursor++;
}
/* Make sure we've parsed everything. */
if (*cursor != '\0')
{
printText("Too many tokens.");
return 0;
}
/* Have parsed the command. Now execute it. */
switch (*(tokens[0])) /* Command code. */
{
case 0: /* Empty line. */
case '#': /* Comment. */
return 0;
case '?':
case 'h':
printUsage();
return 0;
case 'v':
isprintf(buffer, sizeof buffer, "%s",
IONVERSIONNUMBER);
printText(buffer);
return 0;
case '1':
initializeAcs(tokenCount, tokens);
return 0;
case 'l':
executeList(tokenCount, tokens);
return 0;
case 'a':
executeAdd(tokenCount, tokens);
return 0;
case 's':
executeSetMinimumCustodyId(tokenCount, tokens);
return 0;
case 'e':
switchEcho(tokenCount, tokens);
return 0;
case 'q':
return -1; /* End program. */
default:
printText("Invalid command. Enter '?' for help.");
return 0;
}
}
#if defined (VXWORKS) || defined (RTEMS)
int acsadmin(int a1, int a2, int a3, int a4, int a5,
int a6, int a7, int a8, int a9, int a10)
{
char *cmdFileName = (char *) a1;
#else
int main(int argc, char **argv)
{
char *cmdFileName = (argc > 1 ? argv[1] : NULL);
#endif
FILE *cmdFile;
char line[256];
if (cmdFileName == NULL) /* Interactive. */
{
signal(SIGINT, handleQuit);
while (1)
{
printf(": ");
if (fgets(line, sizeof line, stdin) == NULL)
{
if (feof(stdin))
{
break;
}
perror("acsadmin fgets failed");
break; /* Out of loop. */
}
if (acsadmin_processLine(line))
{
break; /* Out of loop. */
}
}
}
else if (strcmp(cmdFileName, ".") == 0) /* Shutdown. */
{
}
else /* Scripted. */
{
cmdFile = fopen(cmdFileName, "r");
if (cmdFile == NULL)
{
perror("Can't open command file");
}
else
{
while (1)
{
if (fgets(line, sizeof line, cmdFile) == NULL)
{
if (feof(cmdFile))
{
break; /* Loop. */
}
perror("acsadmin fgets failed");
break; /* Loop. */
}
if (line[0] == '#') /* Comment.*/
{
continue;
}
if (acsadmin_processLine(line))
{
break; /* Out of loop. */
}
}
fclose(cmdFile);
}
}
writeErrmsgMemos();
acsDetach();
bp_detach();
printText("Stopping acsadmin.");
return 0;
}
|