From 326c8d588f62a6ae3718e72a7ee75b435fc4083c Mon Sep 17 00:00:00 2001 From: Jerzy Jamroz Date: Mon, 21 Oct 2024 12:41:40 +0200 Subject: [PATCH] feat: Refining the afterInit. * Better error handling * help message added * version added * afterInit=YES build var required to enable --- modules/database/src/ioc/misc/Makefile | 8 +- modules/database/src/ioc/misc/afterInit.c | 115 ++++++++++++++-------- modules/database/src/std/softIoc/Makefile | 4 +- 3 files changed, 84 insertions(+), 43 deletions(-) diff --git a/modules/database/src/ioc/misc/Makefile b/modules/database/src/ioc/misc/Makefile index 2affde029..7949e3807 100644 --- a/modules/database/src/ioc/misc/Makefile +++ b/modules/database/src/ioc/misc/Makefile @@ -11,9 +11,14 @@ SRC_DIRS += $(IOCDIR)/misc +# Include afterInit module with afterInit=YES +ifeq ($(afterInit),YES) +DBD += afterInit.dbd +dbCore_SRCS += afterInit.c +endif + DBD += system.dbd DBD += dlload.dbd -DBD += afterInit.dbd DBD += dbCore.dbd INC += epicsRelease.h @@ -26,7 +31,6 @@ dbCore_SRCS += iocInit.c dbCore_SRCS += miscIocRegister.c dbCore_SRCS += dlload.c dbCore_SRCS += iocshRegisterCommon.c -dbCore_SRCS += afterInit.c dbCore_SRCS += registerAllRecordDeviceDrivers.cpp miscIocRegister_CFLAGS_iOS = -DSYSTEM_UNAVAILABLE diff --git a/modules/database/src/ioc/misc/afterInit.c b/modules/database/src/ioc/misc/afterInit.c index cb634bfda..298939322 100644 --- a/modules/database/src/ioc/misc/afterInit.c +++ b/modules/database/src/ioc/misc/afterInit.c @@ -1,5 +1,5 @@ /* Copyright (C) 2020 Dirk Zimoch */ -/* Copyright (C) 2020-2023 European Spallation Source, ERIC */ +/* Copyright (C) 2020-2024 European Spallation Source, ERIC */ #include #include @@ -12,23 +12,36 @@ #include #include -struct cmditem { +// Version within the message +static const char helpMessage[] = + "afterInit version 1.0.1\n" + "Allows you to define commands to be run after the iocInit\n" + "Example commands:\n" + " afterInit \"dbpf \"\n" + " afterInit \"date\"\n"; + +struct cmditem +{ struct cmditem *next; char *cmd; }; -struct cmditem *cmdlist, **cmdlast = &cmdlist; +static struct cmditem *cmdlist, **cmdlast = &cmdlist; -void afterInitHook(initHookState state) { - if (state != initHookAfterIocRunning) return; +static void afterInitHook(initHookState state) +{ + if(state != initHookAfterIocRunning) + return; struct cmditem *item = cmdlist; struct cmditem *next = NULL; - while (item) { - printf("%s\n", item->cmd); - if (iocshCmd(item->cmd)) { - errlogPrintf("afterInit: Command '%s' failed to run\n", item->cmd); - }; + while(item) + { + epicsStdoutPrintf("%s\n", item->cmd); + + if(iocshCmd(item->cmd)) + errlogPrintf("ERROR afterInit command '%s' failed to run\n", item->cmd); + next = item->next; free(item->cmd); free(item); @@ -36,56 +49,78 @@ void afterInitHook(initHookState state) { } } -static struct cmditem *newItem(char *cmd) { - struct cmditem *item; - item = malloc(sizeof(struct cmditem)); - if (item == NULL) { +static struct cmditem *newItem(char *cmd) +{ + struct cmditem *item = malloc(sizeof(struct cmditem)); + + if(item == NULL) + { + errno = ENOMEM; return NULL; } - item->cmd = epicsStrDup(cmd); - item->next = NULL; + item->cmd = epicsStrDup(cmd); + + if(item->cmd == NULL) + { + free(item); + errno = ENOMEM; + return NULL; + } + + item->next = NULL; *cmdlast = item; cmdlast = &item->next; return item; } static const iocshFuncDef afterInitDef = { - "afterInit", 1, - (const iocshArg *[]){ - &(iocshArg){"commandline", iocshArgString}, - }}; + "afterInit", + 1, + (const iocshArg *[]){&(iocshArg){"command (before iocInit)", iocshArgString}}, + helpMessage}; -static void afterInitFunc(const iocshArgBuf *args) { +static void afterInitFunc(const iocshArgBuf *args) +{ static int first_time = 1; - char *cmd; + char *cmd = args[0].sval; - if (first_time) { + if(interruptAccept) + { + errlogPrintf("WARNING afterInit can only be used before iocInit (check help)\n"); + return; + } + + if(!cmd || !cmd[0]) + { + errlogPrintf("WARNING afterInit received an empty argument (check help)\n"); + return; + } + + if(first_time) + { first_time = 0; - initHookRegister(afterInitHook); - } - if (interruptAccept) { - errlogPrintf("afterInit can only be used before iocInit\n"); - return; + if(initHookRegister(afterInitHook) < 0) + { + errno = ENOMEM; + errlogPrintf("ERROR initHookRegister memory allocation failure %s\n", strerror(errno)); + } } - cmd = args[0].sval; - if (!cmd || !cmd[0]) { - errlogPrintf("Usage: afterInit \"command\"\n"); - return; - } struct cmditem *item = newItem(cmd); - if (!item) - errlogPrintf("afterInit: error adding command %s; %s", cmd, - strerror(errno)); + if(!item) + errlogPrintf("ERROR afterInit failed to add the command '%s' %s\n", cmd, strerror(errno)); } -static void afterInitRegister(void) { - static int firstTime = 1; - if (firstTime) { - firstTime = 0; +static void afterInitRegister(void) +{ + static int first_time = 1; + if(first_time) + { + first_time = 0; iocshRegister(&afterInitDef, afterInitFunc); } } + epicsExportRegistrar(afterInitRegister); diff --git a/modules/database/src/std/softIoc/Makefile b/modules/database/src/std/softIoc/Makefile index b30a6d602..e8827b22c 100644 --- a/modules/database/src/std/softIoc/Makefile +++ b/modules/database/src/std/softIoc/Makefile @@ -18,8 +18,10 @@ DBD += softIoc.dbd softIoc_DBD += base.dbd softIoc_DBD += dlload.dbd -softIoc_DBD += afterInit.dbd softIoc_DBD += system.dbd +ifeq ($(afterInit),YES) +softIoc_DBD += afterInit.dbd +endif softIoc_SRCS += softIoc_registerRecordDeviceDriver.cpp softIoc_SRCS_DEFAULT += softMain.cpp