From f30b52bba397d6d6cb8d1ffa2eb72f267bd2dcfc Mon Sep 17 00:00:00 2001 From: Dirk Zimoch Date: Thu, 18 Aug 2016 16:24:24 +0200 Subject: [PATCH] improve usability of afterInit command --- runScript.c | 94 +++++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 70 insertions(+), 24 deletions(-) diff --git a/runScript.c b/runScript.c index 8080aba..e73b459 100644 --- a/runScript.c +++ b/runScript.c @@ -2,6 +2,7 @@ #include #include #include +#include #include #include #include @@ -9,7 +10,7 @@ #include #ifdef vxWorks -#include "asprintf.h" +#include #ifdef _WRS_VXWORKS_MAJOR /* vxWorks 6+ */ #include @@ -18,6 +19,7 @@ #include #include "strdup.h" #endif +#include #endif #ifdef BASE_VERSION @@ -360,7 +362,11 @@ end: struct cmditem { struct cmditem* next; - char cmd[1]; + int type; + union { + char* a[12]; + char cmd[256]; + } x; } *cmdlist, **cmdlast=&cmdlist; void afterInitHook(initHookState state) @@ -378,43 +384,72 @@ void afterInitHook(initHookState state) ) return; for (item = cmdlist; item != NULL; item = item->next) { -#ifdef _WRS_VXWORKS_MAJOR - SHELL_EVAL_VALUE result; - shellInterpEvaluate(line_exp, "C", &result); -#elif defined(vxWorks) - execute(item->cmd); -#else - iocshCmd(item->cmd); +#ifndef EPICS_3_13 + if (item->type == 1) + { + printf("%s\n", item->x.cmd); + iocshCmd(item->x.cmd); + } + else #endif + ((void (*)())item->x.a[0])(item->x.a[1], item->x.a[2], item->x.a[3], item->x.a[4], item->x.a[5], + item->x.a[6], item->x.a[7], item->x.a[8], item->x.a[9], item->x.a[10], item->x.a[11]); } } -int afterInit(const char* command) +extern volatile int interruptAccept; + +static int first_time = 1; + +static struct cmditem *newItem(char* cmd, int type) { struct cmditem *item; - static int first_time = 1; - - if (!command) + if (!cmd) { - fprintf(stderr, "usage: afterInit \"command line\"\n"); - return -1; + fprintf(stderr, "usage: afterInit command, args...\n"); + return NULL; } - + if (interruptAccept) + { + fprintf(stderr, "afterInit can only be used before iocInit\n"); + return NULL; + } if (first_time) { first_time = 0; initHookRegister(afterInitHook); } - item = malloc(sizeof(struct cmditem) + strlen(command)); + item = malloc(sizeof(struct cmditem)); if (item == NULL) { perror("afterInit"); - return -1; + return NULL; } - item->next = 0; - strcpy(item -> cmd, command); + item->type = type; + item->next = NULL; *cmdlast = item; cmdlast = &item->next; + return item; +} + +int afterInit(char* cmd, char* a1, char* a2, char* a3, char* a4, char* a5, char* a6, char* a7, char* a8, char* a9, char* a10, char* a11) +{ + struct cmditem *item = newItem(cmd, 0); + if (!item) return -1; + + item->x.a[0] = cmd; + item->x.a[1] = a1; + item->x.a[2] = a2; + item->x.a[3] = a3; + item->x.a[4] = a4; + item->x.a[5] = a5; + item->x.a[6] = a6; + item->x.a[7] = a7; + item->x.a[8] = a8; + item->x.a[9] = a9; + item->x.a[10] = a10; + item->x.a[11] = a11; + return 0; } @@ -427,19 +462,30 @@ static const iocshFuncDef runScriptDef = { &(iocshArg) { "substitutions", iocshArgString }, }}; -static void runScriptFunc (const iocshArgBuf *args) +static void runScriptFunc(const iocshArgBuf *args) { runScript(args[0].sval, args[1].sval); } static const iocshFuncDef afterInitDef = { "afterInit", 1, (const iocshArg *[]) { - &(iocshArg) { "commandline", iocshArgString }, + &(iocshArg) { "commandline", iocshArgArgv }, }}; -static void afterInitFunc (const iocshArgBuf *args) +static void afterInitFunc(const iocshArgBuf *args) { - afterInit(args[0].sval); + int i, n; + struct cmditem *item = newItem(args[0].aval.av[1], 1); + if (!item) return; + + n = sprintf(item->x.cmd, "%.255s", args[0].aval.av[1]); + for (i = 2; i < args[0].aval.ac; i++) + { + if (strpbrk(args[0].aval.av[i], " ,\"\\")) + n += sprintf(item->x.cmd+n, " '%.*s'", 255-3-n, args[0].aval.av[i]); + else + n += sprintf(item->x.cmd+n, " %.*s", 255-1-n, args[0].aval.av[i]); + } } static void runScriptRegister(void)