feat: Refining the afterInit.

* Better error handling
* help message added
* version added
* afterInit=YES build var required to enable
This commit is contained in:
Jerzy Jamroz
2024-10-21 12:41:40 +02:00
committed by Andrew Johnson
parent 77140abe70
commit 326c8d588f
3 changed files with 84 additions and 43 deletions

View File

@@ -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

View File

@@ -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 <dbAccess.h>
#include <epicsExport.h>
@@ -12,23 +12,36 @@
#include <stdlib.h>
#include <string.h>
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 <PV> <VAL>\"\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);

View File

@@ -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