-
Notifications
You must be signed in to change notification settings - Fork 1
/
afterInit.c
283 lines (258 loc) · 8.7 KB
/
afterInit.c
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
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <dbScan.h>
#include <dbAccess.h>
#include <initHooks.h>
#include <epicsVersion.h>
#ifdef BASE_VERSION
#define EPICS_3_13
#else
#include <iocsh.h>
#include <epicsExport.h>
#include <epicsString.h>
#endif
#ifdef INCinitHooksh
/* old: without iocPause etc */
#define initHookAfterIocRunning initHookAfterInterruptAccept
#endif
struct cmditem
{
struct cmditem* next;
int when;
int useIocshCmd;
union {
const char* a[12];
char cmd[256];
} x;
} *cmdlist, **cmdlast=&cmdlist;
static void afterInitHook(initHookState state)
{
struct cmditem *item;
for (item = cmdlist; item != NULL; item = item->next)
{
if (item->when != state) continue;
#ifndef EPICS_3_13
if (item->useIocshCmd)
{
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]);
}
}
static struct cmditem *newItem(const char* cmd, int useIocshCmd, int when)
{
static int first_time = 1;
struct cmditem *item;
if (interruptAccept)
{
fprintf(stderr, "This function can only be used before iocInit\n");
return NULL;
}
if (first_time)
{
first_time = 0;
initHookRegister(afterInitHook);
}
item = malloc(sizeof(struct cmditem));
if (item == NULL)
{
perror("afterInit");
return NULL;
}
item->useIocshCmd = useIocshCmd;
item->when = when;
if (when >= initHookAtIocPause && when < initHookAfterInterruptAccept)
{
/* Reverse order for "shutdown hooks" */
if (!cmdlist) cmdlast = &item->next;
item->next = cmdlist;
cmdlist = item;
}
else
{
item->next = NULL;
*cmdlast = item;
cmdlast = &item->next;
}
return item;
}
static int hookNameToNumber(const char* hook)
{
static const struct { char* name; int hook; } hookMap[] = {
{ "IocBuild", initHookAtIocBuild },
{ "Beginning", initHookAtBeginning },
{ "AfterCallbackInit", initHookAfterCallbackInit },
{ "AfterCaLinkInit", initHookAfterCaLinkInit },
{ "AfterInitDrvSup", initHookAfterInitDrvSup },
{ "AfterInitRecSup", initHookAfterInitRecSup },
{ "AfterInitDevSup", initHookAfterInitDevSup },
{ "AfterInitDatabase", initHookAfterInitDatabase },
{ "AfterFinishDevSup", initHookAfterFinishDevSup },
{ "AfterScanInit", initHookAfterScanInit },
{ "AfterInitialProcess", initHookAfterInitialProcess },
{ "AfterCaServerInit", initHookAfterCaServerInit },
{ "AfterIocBuilt", initHookAfterIocBuilt },
{ "IocRun", initHookAtIocRun },
{ "AfterDatabaseRunning", initHookAfterDatabaseRunning },
{ "AfterCaServerRunning", initHookAfterCaServerRunning },
{ "AfterIocRunning", initHookAfterIocRunning },
{ "IocPause", initHookAtIocPause },
{ "AfterCaServerPaused", initHookAfterCaServerPaused },
{ "AfterDatabasePaused", initHookAfterDatabasePaused },
{ "AfterIocPaused", initHookAfterIocPaused },
#if defined(EPICS_VERSION_INT)
#if EPICS_VERSION_INT >= VERSION_INT(7, 0, 3, 1)
{ "Shutdown", initHookAtShutdown },
{ "AfterCloseLinks", initHookAfterCloseLinks },
{ "AfterStopScan", initHookAfterStopScan },
{ "AfterStopCallback", initHookAfterStopCallback },
{ "AfterStopLinks", initHookAfterStopLinks },
{ "BeforeFree", initHookBeforeFree },
{ "AfterShutdown", initHookAfterShutdown },
#endif
#endif
{ "InterruptAccept", initHookAfterInterruptAccept },
{ "End", initHookAtEnd },
{ NULL, -1 }
};
int i;
if (!hook)
{
fprintf(stderr, "usage: atInitStage hook, command, args...\n");
return -1;
}
if (epicsStrnCaseCmp("initHook", hook, 8) == 0) hook+=8;
if (epicsStrnCaseCmp("At", hook, 2) == 0) hook+=2;
for (i = 0; hookMap[i].name; i++) {
if (epicsStrCaseCmp(hook, hookMap[i].name) == 0
|| (hookMap[i].name[0] == 'A' && epicsStrCaseCmp(hook, hookMap[i].name+5) == 0))
{
return hookMap[i].hook;
}
}
fprintf(stderr, "atInitStage error: Unknown hook name '%s'\n", hook);
return -1;
}
#ifdef vxWorks
int atInitStageVx(int when, const char* cmd, const char* a1, const char* a2, const char* a3, const char* a4, const char* a5, const char* a6, const char* a7, const char* a8, const char* a9, const char* a10, const char* a11)
{
struct cmditem *item = newItem(cmd, 0, when);
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;
}
int atInitStage(const char* hook, const char* cmd, const char* a1, const char* a2, const char* a3, const char* a4, const char* a5, const char* a6, const char* a7, const char* a8, const char* a9, const char* a10)
{
int when = hookNameToNumber(hook);
if (when < 0) return -1;
if (!cmd)
{
fprintf(stderr, "usage: atInitStage hook, command, args...\n");
return -1;
}
return atInitStageVx(when, cmd, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, NULL);
}
int afterInit(const char* cmd, const char* a1, const char* a2, const char* a3, const char* a4, const char* a5, const char* a6, const char* a7, const char* a8, const char* a9, const char* a10, const char* a11)
{
if (!cmd)
{
fprintf(stderr, "usage: afterInit command, args...\n");
return -1;
}
return atInitStageVx(initHookAfterIocRunning, cmd, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11);
}
int atInit(const char* cmd, const char* a1, const char* a2, const char* a3, const char* a4, const char* a5, const char* a6, const char* a7, const char* a8, const char* a9, const char* a10, const char* a11)
{
if (!cmd)
{
fprintf(stderr, "usage: atInit command, args...\n");
return -1;
}
return atInitStageVx(initHookAtBeginning, cmd, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11);
}
#endif
#ifndef EPICS_3_13
static void atInitStageIocsh(int when, int wordcount, char* cmdword[])
{
int i, n;
struct cmditem *item = newItem(cmdword[1], 1, when);
if (!item) return;
n = sprintf(item->x.cmd, "%.*s", (int)sizeof(item->x.cmd)-1, cmdword[1]);
for (i = 2; i < wordcount; i++)
{
if (strpbrk(cmdword[i], " ,\"\\"))
n += sprintf(item->x.cmd+n, " '%.*s'",(int)sizeof(item->x.cmd)-4-n, cmdword[i]);
else
n += sprintf(item->x.cmd+n, " %.*s", (int)sizeof(item->x.cmd)-2-n, cmdword[i]);
}
}
static const iocshFuncDef afterInitDef = {
"afterInit", 1, (const iocshArg *[]) {
&(iocshArg) { "commandline", iocshArgArgv },
}};
static void afterInitFunc(const iocshArgBuf *args)
{
if (!args[0].aval.av[0])
{
fprintf(stderr, "usage: afterInit command, args...\n");
return;
}
atInitStageIocsh(initHookAfterIocRunning, args[0].aval.ac, args[0].aval.av);
}
static const iocshFuncDef atInitDef = {
"atInit", 1, (const iocshArg *[]) {
&(iocshArg) { "commandline", iocshArgArgv },
}};
static void atInitFunc(const iocshArgBuf *args)
{
if (!args[0].aval.av[0])
{
fprintf(stderr, "usage: atInit command, args...\n");
return;
}
atInitStageIocsh(initHookAtBeginning, args[0].aval.ac, args[0].aval.av);
}
static const iocshFuncDef atInitStageDef = {
"atInitStage", 2, (const iocshArg *[]) {
&(iocshArg) { "hook", iocshArgString },
&(iocshArg) { "commandline", iocshArgArgv },
}};
static void atInitStageFunc(const iocshArgBuf *args)
{
int when = hookNameToNumber(args[0].sval);
if (when < 0) return;
if (!args[1].aval.av[0])
{
fprintf(stderr, "usage: atInitStage hook, command, args...\n");
return;
}
atInitStageIocsh(when, args[1].aval.ac, args[1].aval.av);
}
static void afterInitRegister(void)
{
static int firstTime = 1;
if (firstTime) {
firstTime = 0;
iocshRegister (&afterInitDef, afterInitFunc);
iocshRegister (&atInitDef, atInitFunc);
iocshRegister (&atInitStageDef, atInitStageFunc);
}
}
epicsExportRegistrar(afterInitRegister);
#endif