Skip to content

Commit 02d926a

Browse files
committed
Combine default_envs and extra_envs into one linked list for addReccasterEnvVars
1 parent e93ca6e commit 02d926a

File tree

7 files changed

+113
-79
lines changed

7 files changed

+113
-79
lines changed

client/castApp/src/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ TESTS += testtcp
4545

4646
TESTPROD_HOST += testAddEnvVars
4747
testAddEnvVars_SRCS += testAddEnvVars.c
48+
testAddEnvVars_SRCS += dbcb.c
4849
testAddEnvVars_SYS_LIBS_WIN32 = ws2_32
4950
TESTS += testAddEnvVars
5051

client/castApp/src/caster.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include <epicsAssert.h>
88
#include <epicsThread.h>
99
#include <epicsStdio.h>
10+
#include <cantProceed.h>
1011

1112
#define epicsExportSharedSymbols
1213

@@ -114,9 +115,12 @@ void casterInit(caster_t *self)
114115
self->onmsg = &casterShowMsgDefault;
115116
self->current = casterStateInit;
116117
self->timeout = reccastTimeout;
117-
ellInit(&self->extra_envs);
118+
ellInit(&self->envs);
118119
ellInit(&self->exclude_patterns);
119120

121+
/* add default_envs to envs list which can be expanded by the user with addReccasterEnvVars iocsh function */
122+
addToReccasterLinkedList(self, default_envs_count, default_envs, &self->envs, "casterInit", "Default environment variable");
123+
120124
if(shSocketPair(self->wakeup))
121125
errlogPrintf("Error: casterInit failed to create shutdown socket: %d\n", SOCKERRNO);
122126
}
@@ -137,7 +141,7 @@ void casterShutdown(caster_t *self)
137141
epicsEventMustWait(self->shutdownEvent);
138142

139143
epicsMutexMustLock(self->lock);
140-
ellFree(&self->extra_envs);
144+
ellFree(&self->envs);
141145
ellFree(&self->exclude_patterns);
142146
epicsMutexUnlock(self->lock);
143147

client/castApp/src/caster.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ epicsShareExtern double reccastTimeout;
2222
epicsShareExtern double reccastMaxHoldoff;
2323

2424
extern const char* default_envs[];
25+
extern const size_t default_envs_count;
2526

2627
typedef enum {
2728
casterUDPSetup,
@@ -74,8 +75,7 @@ typedef struct _caster_t {
7475
int shutdown;
7576
char lastmsg[MAX_STRING_SIZE];
7677

77-
ELLLIST extra_envs;
78-
78+
ELLLIST envs;
7979
ELLLIST exclude_patterns;
8080

8181
} caster_t;
@@ -105,6 +105,9 @@ int casterSendInfo(caster_t *c, ssize_t rid, const char* name, const char* val);
105105
epicsShareFunc
106106
int casterPushPDB(void *junk, caster_t *caster);
107107

108+
epicsShareFunc
109+
void addToReccasterLinkedList(caster_t* self, size_t itemCount, const char **items, ELLLIST* reccastList, const char* funcName, const char* itemDesc);
110+
108111
epicsShareFunc
109112
void addReccasterEnvVars(caster_t* self, int argc, char **argv);
110113

client/castApp/src/castinit.c

Lines changed: 28 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -76,20 +76,19 @@ static void casthook(initHookState state)
7676
}
7777

7878
/* Helper function to add items from iocsh calls to internal linked lists
79+
* self is the caster instance
80+
* itemCount is the number of items in the items array
81+
* items is the array of strings to add to the list
82+
* reccastList is the linked list to add the items to
7983
* funcName is the name of the IOC shell function being called (for error messages)
8084
* itemDesc is string to describe what is being added (for error messages)
81-
* defaultArray is an optional arg for a default list to also check for duplicates
8285
*/
83-
static void addToReccasterLinkedList(caster_t* self, int argc, char **argv, ELLLIST* list, const char* funcName, const char* itemDesc, const char** defaultArray)
86+
void addToReccasterLinkedList(caster_t* self, size_t itemCount, const char **items, ELLLIST* reccastList, const char* funcName, const char* itemDesc)
8487
{
85-
size_t i, j;
88+
size_t i;
8689
int dup;
8790
ELLNODE *cur;
88-
argv++; argc--; /* skip function arg */
89-
if(argc < 1) {
90-
errlogSevPrintf(errlogMinor, "At least one argument expected for %s\n", funcName);
91-
return;
92-
}
91+
9392
epicsMutexMustLock(self->lock);
9493
if(self->shutdown) {
9594
/* shutdown in progress, silent no-op */
@@ -105,61 +104,58 @@ static void addToReccasterLinkedList(caster_t* self, int argc, char **argv, ELLL
105104
}
106105

107106
/* sanitize input - check for dups and empty args */
108-
for (i = 0; i < argc; i++) {
109-
const size_t arg_len = strlen(argv[i]) + 1;
110-
if(argv[i][0] == '\0') {
107+
for (i = 0; i < itemCount; i++) {
108+
const size_t arg_len = strlen(items[i]) + 1;
109+
if(items[i][0] == '\0') {
111110
errlogSevPrintf(errlogMinor, "Arg is empty for %s\n", funcName);
112111
continue;
113112
}
114113
dup = 0;
115-
/* if the defaultArray arg is used, check if this is a duplicate */
116-
if (defaultArray) {
117-
for(j = 0; defaultArray[j]; j++) {
118-
if(strcmp(argv[i], defaultArray[j]) == 0) {
119-
dup = 1;
120-
break;
121-
}
122-
}
123-
if(dup) {
124-
errlogSevPrintf(errlogMinor, "Item %s is already in list sent by reccaster by default\n", argv[i]);
125-
continue;
126-
}
127-
}
128114
/* check if dup in existing linked list */
129-
for(cur = ellFirst(list); cur; cur = ellNext(cur)) {
115+
for(cur = ellFirst(reccastList); cur; cur = ellNext(cur)) {
130116
string_list_t *pitem = CONTAINER(cur, string_list_t, node);
131-
if (strcmp(argv[i], pitem->item_str) == 0) {
117+
if (strcmp(items[i], pitem->item_str) == 0) {
132118
dup = 1;
133119
break;
134120
}
135121
}
136122
if(dup) {
137-
errlogSevPrintf(errlogMinor, "%s %s already in list for %s\n", itemDesc, argv[i], funcName);
123+
errlogSevPrintf(errlogMinor, "%s %s already in list for %s\n", itemDesc, items[i], funcName);
138124
continue;
139125
}
140126
string_list_t *new_node = mallocMustSucceed(sizeof(string_list_t) + arg_len, funcName);
141127
new_node->item_str = (char *)(new_node + 1);
142-
memcpy(new_node->item_str, argv[i], arg_len);
128+
memcpy(new_node->item_str, items[i], arg_len);
143129

144-
ellAdd(list, &new_node->node);
130+
ellAdd(reccastList, &new_node->node);
145131
}
146132
epicsMutexUnlock(self->lock);
147133
}
148134

149135
/* Example call: addReccasterEnvVars("SECTOR") or addReccasterEnvVars("SECTOR", "BUILDING")
150-
* Appends the given env variables to the extra_envs list to be sent in addition to the default_envs array
136+
* Appends the given env variables to the envs list to be sent. This includes some hard-coded env vars sent by default
151137
*/
152138
void addReccasterEnvVars(caster_t* self, int argc, char **argv)
153139
{
154-
addToReccasterLinkedList(self, argc, argv, &self->extra_envs, "addReccasterEnvVars", "Environment variable", (const char**)default_envs);
140+
argv++; argc--; /* skip function arg */
141+
if(argc < 1) {
142+
errlogSevPrintf(errlogMinor, "At least one argument expected for addReccasterEnvVars\n");
143+
return;
144+
}
145+
addToReccasterLinkedList(self, argc, (const char **)argv, &self->envs, "addReccasterEnvVars", "Environment variable");
155146
}
156147

157148
/* Example call: addReccasterExcludePattern("TEST:*") or addReccasterExcludePattern("TEST:*", "*_")
158149
* Appends the given patterns to the exclude_patterns list so those PVs and their meta-data are not sent
159150
*/
160151
void addReccasterExcludePattern(caster_t* self, int argc, char **argv)
161152
{
162-
addToReccasterLinkedList(self, argc, argv, &self->exclude_patterns, "addReccasterExcludePattern", "Exclude pattern", NULL);
153+
argv++; argc--; /* skip function arg */
154+
if(argc < 1) {
155+
errlogSevPrintf(errlogMinor, "At least one argument expected for addReccasterExcludePattern\n");
156+
return;
157+
}
158+
addToReccasterLinkedList(self, argc, (const char **)argv, &self->exclude_patterns, "addReccasterExcludePattern", "Exclude pattern");
163159
}
164160

165161
static const iocshArg addReccasterEnvVarsArg0 = { "environmentVar", iocshArgArgv };

client/castApp/src/dbcb.c

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -35,18 +35,17 @@ const char* default_envs[] =
3535
"PWD",
3636
"EPICS_HOST_ARCH",
3737
"IOCNAME",
38-
"HOSTNAME",
3938

4039
/* iocStats */
4140
"ENGINEER",
4241
"LOCATION",
4342

4443
NULL
4544
};
45+
const size_t default_envs_count = NELEMENTS(default_envs) - 1; /* exclude NULL */
4646

4747
static int pushEnv(caster_t *caster)
4848
{
49-
size_t i;
5049
ELLNODE *cur;
5150
int ret = 0;
5251

@@ -64,16 +63,8 @@ static int pushEnv(caster_t *caster)
6463
if(ret)
6564
ERRRET(ret, caster, "Failed to send epics version");
6665

67-
for(i=0; !ret && default_envs[i]; i++) {
68-
const char *val = getenv(default_envs[i]);
69-
if(val && val[0]!='\0')
70-
ret = casterSendInfo(caster, 0, default_envs[i], val);
71-
if(ret)
72-
casterMsg(caster, "Error sending env %s", default_envs[i]);
73-
}
74-
7566
epicsMutexMustLock(caster->lock);
76-
for(cur = ellFirst(&caster->extra_envs); !ret && cur; cur = ellNext(cur)) {
67+
for(cur = ellFirst(&caster->envs); !ret && cur; cur = ellNext(cur)) {
7768
const string_list_t *penvvar = CONTAINER(cur, string_list_t, node);
7869
const char *val = getenv(penvvar->item_str);
7970
if (val && val[0] != '\0')

0 commit comments

Comments
 (0)